Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

how to flash PIC

Status
Not open for further replies.

ponsakthi

New Member
i am working in mplab ide.
can any one tell me how to flash a word in program memory and read it back in assembly or C language
 
eric
shall i send u the code that i have written for erasing writing and reading a word.can u spot the error
 
If you post your code here I'll have a look and see if I can spot the error.

If it helps at all, here are the routines I use to write to EEPROM in C18.

Code:
unsigned char ReadEEPROM(unsigned char address){
    EECON1=0;                   //ensure CFGS=0 and EEPGD=0 
    EEADR = address;
    EECON1bits.RD = 1;
    return(EEDATA);
}

void WriteEEPROM(unsigned char address,unsigned char data){
char SaveInt;
    SaveInt=INTCON;             //save interrupt status
    EECON1=0;                   //ensure CFGS=0 and EEPGD=0
    EECON1bits.WREN = 1;        //enable write to EEPROM
    EEADR = address;            //setup Address
    EEDATA = data;              //and data
    INTCONbits.GIE=0;           //No interrupts
    EECON2 = 0x55;              //required sequence #1
    EECON2 = 0xaa;              //#2
    EECON1bits.WR = 1;          //#3 = actual write
    INTCON=SaveInt;             //restore interrupts
    while(!PIR2bits.EEIF);      //wait until finished
    EECON1bits.WREN = 0;        //disable write to EEPROM
}

The above is for a 18F1320. To write flash you need to erase a block before writing a block and the write only happens when the last byte of the block is written. You also have to follow the required sequence. The block size varies and so please state which pic you are using.

Mike.
 
eric
shall i send u the code that i have written for erasing writing and reading a word.can u spot the error

hi,
Post your code and we try to help.


Mike,
Saw the frost on that sub Zero LM35?.:D
Dont need freezer aerosol in the UK at the moment..its frosty.
 
code

here goes the code



#include <p18f66j16.inc>
GLOBAL main
ORG 0x13C
main:

;erasing
MOVLW 01h
MOVWF TBLPTRU
MOVLW 7fh
MOVWF TBLPTRH
MOVLW 0f4h
MOVWF TBLPTRL

BSF EECON1, FREE
BCF INTCON, GIE
MOVLW 55h
MOVWF EECON2
MOVLW 0AAh
MOVWF EECON2
BSF EECON1, WR
BCF EECON1, FREE










;writing
clrf TRISB
clrf TRISD
movlw 01h
movwf TBLPTRU
movlw 7fh
movwf TBLPTRH
movlw 0f4h
movwf TBLPTRL

movlw 35h
movwf TABLAT
tblwt*+
movlw 12h
movwf TABLAT
tblwt*

bsf EECON1, WPROG ;
bsf EECON1, WREN ;

movlw 55h
movwf EECON2 ;
movlw 0AAh
movwf EECON2 ;
bsf EECON1, WR ;

bcf EECON1, WPROG ;
bcf EECON1, WREN ;




;reading
MOVLW 01h
MOVWF TBLPTRU
MOVLW 7fh
MOVWF TBLPTRH
MOVLW 0f4h
MOVWF TBLPTRL

TBLRD*+ ;
MOVF TABLAT
movwf PORTB

TBLRD*+ ;
MOVF TABLAT
movwf PORTD

bsf INTCON , GIE
END
 
The only thing I can see wrong is you don't set the WREN bit before doing the erase cycle. I can see why you didn't as the data sheet gives example code with the same error even though the text states it needs setting. Microchip's proof readers seem to have been on an extended holiday for the last year or two.
<edit> You also don't disable interrupts before the required sequence.</edit>

@Eric,
That was no LM35 that was a gaggle of DS18S20s. When I switched a similar circuit on earlier it registered 30°C in my study. The aircon has it now at a comfy 24°C at around midnight.:eek:

Mike.
 
Last edited:
thanks a lot pommie.can u pls tell me how to link and assembly language and c

It's best to write in one or the other, mixing asm and C is normally not necessary. If you know C then a search on here for C18 will turn up a few examples.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top