Subforum for the Matrix Online Courses, discuss the courses, highlight problems or suggest amendments

Moderator: Benj

Posts: 8
Joined: Fri Sep 14, 2012 5:05 pm


Postby juanlu » Fri Sep 14, 2012 5:13 pm


i am doing course of programming embeded pic, I have problem to write eeprom with assembler.
I have passed course programm to pic and does not work.

unsigned char counter;

movlw 0 // the first memory location
movwf _counter

movff _counter, _eeadr // counter as address and data
movff _counter, _eedata

bcf _eecon1,7 // set access data memory
bcf _eecon1,6 // set EEPROM

bsf _eecon1,2 // enable wren

clrf _intcon // stop interrupts (if any)

movlw 0x55 // mandatory sequence
movwf _eecon2
movlw 0xAA
movwf _eecon2
bsf _eecon1,1

bcf _eecon1,2 // clear wren

btfsc _eecon1,1 // wait for bit 1 is zero
goto wait

incfsz _counter,1 // increment and skip if zero
goto next

movlw 0b11111110 // b0 to output
movwf _trisb

bsf _portb,0 // light LED0

Result light is set but memory does not change. It is allways FF.

Any idea about how to solve it? I have passed this programm to C and memory change according program.


User avatar
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: eeprom

Postby Enamul » Fri Sep 14, 2012 5:30 pm

You can find c code for reading and writing to and from EEPROM in the following post..
University of Nottingham

Posts: 8
Joined: Fri Sep 14, 2012 5:05 pm

Re: eeprom

Postby juanlu » Fri Sep 14, 2012 6:21 pm

thanks Enamul

my program translated to C is OK but not in assembler.
I am learning now and theoretically the assembler code works.

it seems that C variable counter is not known for assembler code.

User avatar
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 64 times
Been thanked: 306 times

Re: eeprom

Postby JonnyW » Fri Sep 14, 2012 8:43 pm

I do not know PIC assembler, but suggest you use 'counter' as an output to port B, not a fixed integer, to ensure this is being updated correctly. If the compiler did not recognise '_counter' it just would not compile and if it was not being incremented then the light would never light or would only loop once. Therefore I'd guess that the problem is not with _counter, but with some other part of the program.

You could try starting _counter at a non-zero value toensure it is looping correctly (and not just remaining at zero).

Try taking the C code equivalent and looking at the ASM generated for this. Add this ASM to your program and check this works. Then repeatedly strip this down until you are left with the program you have below. Starting with a program that works and breaking it into pieces is often an easier way to learn than starting with a blank page and building a new program.


Posts: 8
Joined: Fri Sep 14, 2012 5:05 pm

Re: eeprom

Postby juanlu » Sun Sep 16, 2012 10:59 am

Thanks Jonny,

I have just located the problem and now program runs properly.

It was in chip configuration PIC MICRO CONFIGURATION (expert).
Options were ENABLE and it must be DISABLED according to learningn course. Then I change and everything ok.

Low voltage program must be DISABLED
Dedicated in circuit port must be DISABLED
Extended CPU must be DISABLED

I do not know exactly which one of these three options caused problem. Hope that once I advance in course can understand this issue.