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.

code not working

Status
Not open for further replies.

jijita

New Member
it shuld take data from dip switch and write it on eeprom i2c then read it and display it on leds
it's allways displaying on the leds 11111111
all the leds are on and this is only when i test portc 0
before i even test portc1 it shows all leds on
LIST P=pic16F877A
#INCLUDE<P16F877A.INC>


addr equ 0x20
eepal equ 0x21
eepah equ 0x22
term equ 0x23
cmpt equ 0x24

ORG 0X00 ;the start address in memory is 0
GOTO Main ;goto Main
ORG 0X04 ;the start address is 0X04
GOTO INTRTN ;goto INTRTN
INTRTN BTFSC term,1
GOTO lire
INCF cmpt,f ;add 1 to cmpt
movf cmpt,w
ADDWF PCL,1 ;add W to PCL
NOP
GOTO I2C_sendadress
GOTO I2C_eepaddrhigh
GOTO I2C_eepaddrlow
GOTO I2C_sendByte
GOTO I22C_STOP ;Yes
GOTO TESTING
I2C_sendByte BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF PORTB,w
MOVWF SSPBUF ;put W in SSPBUF
RETFIE
lire INCF cmpt,1
movf cmpt,w
addwf PCL,1
NOP
GOTO I2C_sendadress
GOTO I2C_eepaddrhigh
GOTO I2C_eepaddrlow
GOTO RSTART
GOTO I2C_sendadress
GOTO read
GOTO take
GOTO stopread
I2C_sendadress BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVLW addr ;put addr in W
MOVWF SSPBUF ;put addr in SSPBUF
RETFIE
RSTART BCF PIR1,SSPIF
BSF STATUS,RP0
BSF SSPCON2,RSEN
BCF STATUS,RP0
INCF addr,F
RETFIE
read BCF PIR1,SSPIF
BSF STATUS,RP0
BSF SSPCON2,RCEN ; lancer la r馗eption
BCF STATUS,RP0
RETFIE
take BCF PIR1,SSPIF
MOVF SSPBUF,W
MOVWF PORTD
BSF STATUS,RP0
BSF SSPCON2,PEN ; lancer le stop-condition
BCF STATUS,RP0
RETFIE
stopread BCF PIR1,SSPIF
BCF term,1
CLRF cmpt
BSF PORTC,5
RETFIE
I2C_eepaddrhigh BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF eepah,W ;put eepa in W
MOVWF SSPBUF ;put eepa in SSPBUF
RETFIE
I2C_eepaddrlow BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF eepal,W ;put eepa+1 in W
MOVWF SSPBUF ;put eepa+1 in SSPBUF
RETFIE
I22C_STOP BCF PIR1,SSPIF ;must be cleared before returning
BSF STATUS,RP0
BSF SSPCON2,PEN ;Initiate STOP condition on SDA and SCL pins
BCF STATUS,RP0
RETFIE
TESTING CLRF cmpt
BSF term,0
RETFIE


Main BCF STATUS,RP1 ;turns to Bank 1
BSF STATUS,RP0
MOVLW B'00000111' ;Prescaler is /256
OPTION
BSF INTCON,7 ;Enables all unmasked interrupts
BSF INTCON,6 ;Enables all unmasked peripheral interrupts
MOVLW B'00001000'
MOVWF PIE1
MOVLW B'11111111'
MOVWF TRISB
MOVLW B'00011011'
MOVWF TRISC
CLRF TRISD
BCF STATUS,RP0
CLRF eepah
CLRF eepal
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF term
CLRF cmpt

;Initialisation I2C

BSF STATUS,RP0 ;turns to Bank 1
CLRF SSPSTAT ;Clears SSPSTAT
MOVLW D'2' ;2 is the recharge value of the Baud Rate Generator
MOVWF SSPADD ;Set SSPADD=2
BCF STATUS,RP0 ;turns to Bank 0
MOVLW B'00101000' ;Enable MSSP module in mode I2C master
MOVWF SSPCON ;

TEST MOVLW B'10100000' ;put 10100000 in W
MOVWF addr ;Set addr=10100000
BTFSC PORTC,0
GOTO $-1
CALL DEBOUNCER
BTFSC PORTC,0
GOTO TEST
BTFSS PORTC,0
GOTO $-1
BSF STATUS,RP0
BSF SSPCON2,SEN ;Initiate START condition on SDA and SCL pins
BCF STATUS,RP0
BTFSS term,0
GOTO $-1
BCF term,0
BSF term,1
BCF STATUS,RP0
BTFSC PORTC,1
GOTO $-1
CALL DEBOUNCER
BTFSC PORTC,1
GOTO TEST
BTFSS PORTC,1
GOTO $-1
BSF STATUS,RP0
BSF SSPCON2,SEN
BCF STATUS,RP0
btfsc term,1
GOTO $-1
INCF eepal,F
GOTO TEST
DEBOUNCER MOVLW D'222'
MOVWF TMR0
BCF INTCON,2
BTFSS INTCON,2
GOTO $-1
RETURN
end
 
There is a few problems that I found on quick glance.

Why don't you use the internal EEPROM, it is much easer to use...(Less Programming)

I also see that you use a interrupt for the I2C, this can cause problems if timming is not perfect

What other devices are on the I2C Bus?

Have you looked a Nigel's tutorials?

If you have to use I2C memory I have a working I2C code, let me know

jijita said:
it shuld take data from dip switch and write it on eeprom i2c then read it and display it on leds
it's allways displaying on the leds 11111111
all the leds are on and this is only when i test portc 0
before i even test portc1 it shows all leds on
LIST P=pic16F877A
#INCLUDE<P16F877A.INC>


addr equ 0x20
eepal equ 0x21
eepah equ 0x22
term equ 0x23
cmpt equ 0x24

ORG 0X00 ;the start address in memory is 0
GOTO Main ;goto Main
ORG 0X04 ;the start address is 0X04
GOTO INTRTN ;goto INTRTN
This line is not needed, unless located later in program
INTRTN BTFSC term,1
GOTO lire
INCF cmpt,f ;add 1 to cmpt
movf cmpt,w
ADDWF PCL,1 ;add W to PCL
NOP
GOTO I2C_sendadress
GOTO I2C_eepaddrhigh
GOTO I2C_eepaddrlow
GOTO I2C_sendByte
GOTO I22C_STOP ;Yes
GOTO TESTING
I2C_sendByte BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF PORTB,w
MOVWF SSPBUF ;put W in SSPBUF
RETFIE
lire INCF cmpt,1
movf cmpt,w
addwf PCL,1
NOP
GOTO I2C_sendadress
GOTO I2C_eepaddrhigh
GOTO I2C_eepaddrlow
GOTO RSTART
GOTO I2C_sendadress
GOTO read
GOTO take
GOTO stopread
I2C_sendadress BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVLW addr ;put addr in W
MOVWF SSPBUF ;put addr in SSPBUF
RETFIE
RSTART BCF PIR1,SSPIF
BSF STATUS,RP0
BSF SSPCON2,RSEN
BCF STATUS,RP0
INCF addr,F
RETFIE
read BCF PIR1,SSPIF
BSF STATUS,RP0
BSF SSPCON2,RCEN ; lancer la rÈception
BCF STATUS,RP0
RETFIE
take BCF PIR1,SSPIF
MOVF SSPBUF,W
MOVWF PORTD
BSF STATUS,RP0
BSF SSPCON2,PEN ; lancer le stop-condition
BCF STATUS,RP0
RETFIE
stopread BCF PIR1,SSPIF
BCF term,1
CLRF cmpt
BSF PORTC,5
RETFIE
I2C_eepaddrhigh BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF eepah,W ;put eepa in W
MOVWF SSPBUF ;put eepa in SSPBUF
RETFIE
I2C_eepaddrlow BCF PIR1,SSPIF ;must be cleared before returning
BCF STATUS,RP0 ;turns to Bank 0
MOVF eepal,W ;put eepa+1 in W
MOVWF SSPBUF ;put eepa+1 in SSPBUF
RETFIE
I22C_STOP BCF PIR1,SSPIF ;must be cleared before returning
BSF STATUS,RP0
BSF SSPCON2,PEN ;Initiate STOP condition on SDA and SCL pins
BCF STATUS,RP0
RETFIE
TESTING CLRF cmpt
BSF term,0
RETFIE


Main BCF STATUS,RP1 ;turns to Bank 1
BSF STATUS,RP0
MOVLW B'00000111' ;Prescaler is /256
OPTION
this line should be OPTION_REG
In the data sheet 30292c it states on page 137 to not use this instruction
BSF INTCON,7 ;Enables all unmasked interrupts
BSF INTCON,6 ;Enables all unmasked peripheral interrupts
MOVLW B'00001000'
MOVWF PIE1
MOVLW B'11111111'
MOVWF TRISB
MOVLW B'00011011'
MOVWF TRISC
CLRF TRISD
BCF STATUS,RP0
Do a clrf status, to make sure that it is in bank 0
CLRF eepah
CLRF eepal
CLRF PORTB
CLRF PORTC
CLRF PORTD
CLRF term
CLRF cmpt

;Initialisation I2C

BSF STATUS,RP0 ;turns to Bank 1
CLRF SSPSTAT ;Clears SSPSTAT
MOVLW D'2' ;2 is the recharge value of the Baud Rate Generator
MOVWF SSPADD ;Set SSPADD=2
BCF STATUS,RP0 ;turns to Bank 0
MOVLW B'00101000' ;Enable MSSP module in mode I2C master
MOVWF SSPCON ;

TEST MOVLW B'10100000' ;put 10100000 in W
MOVWF addr ;Set addr=10100000
BTFSC PORTC,0
GOTO $-1
CALL DEBOUNCER
BTFSC PORTC,0
GOTO TEST
BTFSS PORTC,0
GOTO $-1
BSF STATUS,RP0
BSF SSPCON2,SEN ;Initiate START condition on SDA and SCL pins
BCF STATUS,RP0
BTFSS term,0
GOTO $-1
BCF term,0
BSF term,1
BCF STATUS,RP0
BTFSC PORTC,1
GOTO $-1
CALL DEBOUNCER
BTFSC PORTC,1
GOTO TEST
BTFSS PORTC,1
GOTO $-1
BSF STATUS,RP0
BSF SSPCON2,SEN
BCF STATUS,RP0
btfsc term,1
GOTO $-1
INCF eepal,F
GOTO TEST
DEBOUNCER MOVLW D'222'
MOVWF TMR0
BCF INTCON,2
BTFSS INTCON,2
GOTO $-1
RETURN
end
 
reply

actually i would like to see your code
thank you for replying to my problem
the problem is that i have to use i2c and i have to use interrupts
because this is a small test code for a bigger one
there's nothing else on the i2c bus
i checked nigel's tutorial but he's not using interrupts and i definitely need to use interrupts
is there a way to know where is the code hanging
where are the bugs?
thank you for your help
 
Mine don't use interrupts either.
It has one part for sending, one for recieving, etc.

It was used for interfacing with a RTC.

First, why do you need Interrupts?
What is the final project? (details)

There might be other options you might not have thought of.

Have you used the simulator in Mplab?

I will have another look at the code.
Please give me more info on the project.

Kent
 
kentken said:
First, why do you need Interrupts?
What is the final project? (details)

I'm also puzzled why you need interrupts?.

Also, your existing code doesn't appear to save and restore any registers in the interrupt routines?.

I would suggest you have a look on the PICList, there's been quite a lot about using the I2C hardware over the years, the generally opinion is that it's difficult to get working, and that the MicroChip application note routines don't actually work.

I've never bothered looking into using the I2C hardware, there doesn't seem much point - unless you are wanting to use the PIC as an I2C slave.
 
I was going through the code last night.
I found that only one pin is read at a time, then stored.
You should be able to save the whole port in one shot.

I found some errors that would cause it not to work:
In the I2C_sendadress, there is a MOVLW addr, this will put the location of addr into W not its contents.
In Debouncer, TMR0 Interrupts are cleared, but TMR0 isn't turned on.
Also the use of a computed jump for the goto's don't always work right.

From what I can gather, the Interrupts arn't even needed.

Take a look at page 80 of data sheet 30292, it explains the I2C master mode.

Nigel is right about the microchip ap notes.

Please let me know more about what you need this to do in your final project.

What type of memory are you using?

I am redoing your whole program, please send more info, so it will work right.

Kent
 
my project

my project is:
i'm receiving data via infrared to the pic from another pic
the data are images actually sent from pc to first pic then to second pic via infrared .while receiving data via infrared the data has to be stored on an externel eeprom then read from eeprom and displayed on a screen
since infrared is very fast i have to use interrupts to be able to take the data and store them on eeprom
 
Re: my project

jijita said:
my project is:
i'm receiving data via infrared to the pic from another pic
the data are images actually sent from pc to first pic then to second pic via infrared .while receiving data via infrared the data has to be stored on an externel eeprom then read from eeprom and displayed on a screen
since infrared is very fast i have to use interrupts to be able to take the data and store them on eeprom

Wow, that is a hand full
First try the program as writen, it might work for you.
It should work, but it is untested. If you want, I will help to fill in the cracks.

What is the end result for?
 

Attachments

  • i2cproblemedcode.asm.txt
    10.4 KB · Views: 378
i will test the code and tell you what happened

i will test the code later this evening
i have a question about timer 0
if i have a 4 mhz osc and prescalor 256 and this code what would be the calculated time or delay
MOVLW D'202'
MOVWF TMR0
BCF INTCON,2
BTFSS INTCON,2
GOTO $-1
RETURN
as i think it's 202*256=51,7ms
is this true?
please i really need to know if i'm right
 
No TMR0 will count to 255 and then overflow back to 0
So, if you load it with d'202' then it will only have to count 256 - 202 = 54 times before it overflows...

you'll need 256 prescaler tics for 1 TMR0 tick, so 54 * 256 = 13824 ticks
 
Using the stopwatch in Mplab is one of the easiest ways to check.

Please let me know how things work.
I would also like to know more about your project, it sounds like fun and complex.

Have fun
Kent
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top