Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C pic to pic 16F767 16F876A

Status
Not open for further replies.

alejandrodaniel

Newbie level 3
Joined
Aug 1, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,305
as the title.
If I link 16f767 an external EEPROM 24C64 works fine.
in the SCL/SDA line pull-up resistors 2K2.
the code:
Code:
''''''''''''''' SUBPROCEDURAS ''''''''''''''''''''''''''''''''''''''''''''''''''
SUB PROCEDURE Escribe_Eeprom(DIM wAddress AS WORD,DIM Dato AS WORD)
I2C_Start()                      'Envia señal de inicio I2C
I2C_Wr($A6)                     'Envia byte a la EEPROM externa
I2C_Wr(HI(wAddress))       'Envia el valor alto address de la EEPROM)
I2C_Wr(LO(wAddress))       'Envia el valor bajo address de la EEPROM)
I2C_Wr(Dato)                    'Envia el valor del dato
I2C_Stop()                        'Envia señal de parada I2C
END SUB
''''''''''''''' INICIA '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Main:
     ADCON1=%10001111         'PORTA.0/1/2/3 analogicas,resto digital.PORTB digital,justificado a derecha + Vref
     TRISA=%000000                'PORTA como output
     TRISB=%00000000             'PORTB como output
     TRISC=%00011000             'Puertas PORTC.3 y PORTC.4 son input
     PORTA=%000000                'Setaje PORTA
     PORTB=%00000000            'Setaje PORTB
     PORTC=%00000000            'Setaje PORTC
     Address_mem=0                'Inicializo la variable del Address
     vValor=0                            'Inicializo la variable del dato
     I2C_Init(312500)                'SSPADD=((FOSC/CLOCK_I2C)/4)-1
     Envia_Slave:
        Escribe_Eeprom(Address_mem,vValor)
           INC(vValor)
           INC(Address_mem)
           GOTO Envia_Slave
        ELSE
           GOTO Envia_Slave
        END IF
END.

But if I want to send a data to pic_slave and it should write in it's internal EEPROM it do incorrect write.
This is the code:
Code:
''''''''''''''' SUBPROCEDURAS ''''''''''''''''''''''''''''''''''''''''''''''''''
SUB PROCEDURE Escribe_Slave(DIM wID_Add AS WORD,DIM Dato AS WORD)
I2C_Start()                  'Envia señal de inicio I2C
I2C_Wr(wID_Add)         'Envia byte ID_Address al slave
I2C_Wr(Dato)                'Envia el valor del dato
I2C_Stop()                     'Envia señal de parada I2C
END SUB
''''''''''''''' INICIA '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Main:
     ADCON1=%10001111   'PORTA.0/1/2/3 analogicas,resto digital.PORTB digital,justificado a derecha + Vref
     TRISA=%000000            'PORTA como output
     TRISB=%00000000         'PORTB como output
     TRISC=%00011000         'Puertas PORTC.3 y PORTC.4 son input
     PORTA=%000000            'Setaje PORTA
     PORTB=%00000000          'Setaje PORTB
     PORTC=%00000000          'Setaje PORTC
     Address_mem=0               'Inicializo la variable del Address
     vValor=0                          'Inicializo la variable del dato
     I2C_Init(312500)               'SSPADD=((FOSC/CLOCK_I2C)/4)-1
     Envia_Slave:
        IF I2C_Is_Idle THEN
           Escribe_Slave($A4,vValor)
           INC(vValor)
           Delay_ms(25)            'Tiempo espera Slave escriba en memoria
           GOTO Envia_Slave
        ELSE
           GOTO Envia_Slave
        END IF
END.
This is the code of the slave 16F876A:
Code:
PROGRAM Control_mot_16F876A
'***************************** VARIABLES ***************************************
DIM Address_mem,Clear_buffer,Rx_Buffer,Tx_Data AS BYTE
'**************************** SUB PROCEDURAS ***********************************
SUB PROCEDURE Interrupt()
IF TESTBIT(PIR1,SSPIF)=1 THEN                  'I2C slave subroutine
   IF TESTBIT(SSPSTAT,R_W)=1 THEN              'Lee el dato pedido del master
      SSPBUF=Tx_Data                           'Agregar codigo
      SETBIT(SSPCON,CKP)                       'Libera el clock
      GOTO EXIT_I2C
   END IF
   IF TESTBIT(SSPSTAT,BF)=0 THEN               'Todo hecho
      GOTO EXIT_I2C
   END IF
   IF TESTBIT(SSPSTAT,D_A)=1 THEN              'Dato,no ID_Address
      Rx_Buffer=SSPBUF                         'Lee el dato del master
      Eeprom_Write(Address_mem,Rx_Buffer)      'Lo escribe en memoria
      Delay_ms(10)                             'Espera que termine de escribir en memoria
      INC(Address_mem)                         'Incrementa el valor del address
      GOTO EXIT_I2C
   END IF
END IF
EXIT_I2C:
   CLEARBIT(PIR1,SSPIF)                        'Limpia el flag del interrupt
   Clear_buffer=SSPBUF                         'Limpio el buffer
END SUB
'***************************** INICIA ******************************************
Main:
  ADCON1=%10000010                             'Cinco puertas analogicas
  TRISA=%000000                                'PORTA como output
  TRISB=%00000000                              'PORTB como output
  TRISC=%00011000                              'Puertas PORTC.3 y PORTC.4 son input
  PORTA=%000000                                'Setaje PORTA
  PORTB=%00000000                              'Setaje PORTB
  PORTC=%00000000                              'Setaje PORTC
  SSPADD=$A4                                   'Setaje ID_Address
  SSPCON=%00110110                             'Setaje SSPCON --- ALTERNATIVA %00111110
  SSPCON2=%00000000                            'Setaje SSPCON2 --- Habilita GCEN y RCEN %10001000
  PIE1=%00001000                               'Habilita SSP interrupts
  INTCON=%11000000                             'Habilita GIE y PEIE
  SSPSTAT=%00000000                            'Desabilito SSPSTAT
  PIR1.SSPIF=0                                 'Limpia el flag del interrupt
  Address_mem=0
  WHILE true
     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  WEND
END.
the end of trasmizione data i find myself with the internal eeprom of 16F876A written randomly (both data and position) :cry:

Excuse my English
 

alejandrodaniel

Newbie level 3
Joined
Aug 1, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,305
Since I can not get to grips with this problem, I now come to USART between master and slave.thanks.
 

alejandrodaniel

Newbie level 3
Joined
Aug 1, 2009
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,305
I take the discussion.
the code works fine with the external EEPROM.
so I think it is a problem of setting the SSPCON,SSPCON2,SSPSTAT,PIR1,etc..................

code master 16F767
Code:
''''''''''''''' SUBPROCEDURAS ''''''''''''''''''''''''''''''''''''''''''''''''''
SUB PROCEDURE Escribe_Slave(DIM wAddress AS WORD,DIM Dato AS WORD)
I2C_Start()                      'Envia señal de inicio I2C
I2C_Wr($A4)                     'Envia byte al slave
I2C_Wr(Dato)                    'Envia el valor del dato
I2C_Stop()                        'Envia señal de parada I2C
END SUB
''''''''''''''' INICIA '''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Main:
     ADCON1=%10001111         'PORTA.0/1/2/3 analogicas,resto digital.PORTB digital,justificado a derecha + Vref
     TRISA=%000000                'PORTA como output
     TRISB=%00000000             'PORTB como output
     TRISC=%00011000             'Puertas PORTC.3 y PORTC.4 son input
     PORTA=%000000                'Setaje PORTA
     PORTB=%00000000            'Setaje PORTB
     PORTC=%00000000            'Setaje PORTC
     Address_mem=0                'Inicializo la variable del Address
     vValor=0                            'Inicializo la variable del dato
     I2C_Init(312500)                'SSPADD=((FOSC/CLOCK_I2C)/4)-1
     Envia_Slave:
        Escribe_Slave(vValor,vValor)
           INC(vValor)
           GOTO Envia_Slave
        ELSE
           GOTO Envia_Slave
        END IF
END.

This is the modified code for the slave.
simpler, but it does not work.
regardless of what comes to the SSPBUF should write a value in the internal EEPROM 16F876A so sequencial.
but instead scribe values at random locations at random.
Code:
'*************** VARIABLES *********************************
DIM Address_mem,Clear_buffer AS BYTE
'*************** SUB PROCEDURAS ***************************
SUB PROCEDURE Interrupt()
IF PIR1.SSPIF=1 THEN                           'I2C slave subroutine
   PIR1.SSPIF=0                                'Limpia el flag del interrupt
   IF SSPSTAT.D_A=1 THEN                       'Dato,no ID_Address
      Eeprom_Write(Address_mem,Address_mem)    'Lo escribe en memoria
      Delay_ms(25)                             'Espera que termine de escribir en memoria
      Address_mem=Address_mem+1                'Incrementa el valor del address
      Clear_buffer=SSPBUF                      'Limpio el buffer
   END IF
END IF
END SUB
'*************** INICIA *************************************
Main:
  ADCON1=%10000010                             'Cinco puertas analogicas
  TRISA=%000000                                'PORTA como output
  TRISB=%00000000                              'PORTB como output
  TRISC=%00011000                              'Puertas PORTC.3 y PORTC.4 son input
  PORTA=%000000                                'Setaje PORTA
  PORTB=%00000000                              'Setaje PORTB
  PORTC=%00000000                             'Setaje PORTC
  SSPADD=$A4                                   'Setaje ID_Address
  INTCON=%00000000                             '
  SSPCON=%00110110                             'Setaje SSPCON --- ALTERNATIVA %00111110=62
  PIE1=%00001000                               'Habilita SSP interrupts
  PIR1=%00000000                               'Setaje PIR1
  INTCON=%11000000                             'Habilita GIE y PEIE
  SSPSTAT=%00000000                            'Desabilito SSPSTAT
  Address_mem=0
  WHILE TRUE
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  WEND
END.


Thanks for your patience
 

Aeddon

Newbie level 4
Joined
Nov 5, 2009
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Singapore
Activity points
1,319
Need Help here with RF transmitting/Recieving PIC tp PIC

hi guys,
I am Newbie to Micro controller, currently doing Final year project, need help on RF module transmitting/Recieving assembly code From PIC to PIC.
Best if there is an schematics diagram and asm code.
thanks
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top