I2C-Master write to Slave

Discussion in 'Embedded Systems and Microcontrollers' started by snowbell, Feb 7, 2010.

  1. snowbell

    Thread Starter New Member

    Sep 14, 2009
    3
    0
    Hi, I am trying to use I2C to send 0x00 or 0x01 from master to slave and the slave will light an LED if it receives 0x01, and not light the LED if it receives 0x00. I tested the code and the LED doesn't light up at all. I'm using PIC 18F4620 and CCS compiler. Anyone knows the reason why? The master and slave codes are as follows:

    Master:
    Code ( (Unknown Language)):
    1.  
    2. #define SLAVE1_WRT_ADDR   0x12 //LSB=0
    3. #define SLAVE1_READ_ADDR  0x13 //LSB=1
    4.  
    5. //====================================
    6. void main()
    7. {
    8. int8 cmd;
    9.  
    10. while(1)
    11.   {
    12.    if(input(PIN_C0))
    13.       cmd = 0x01;
    14.     else
    15.       cmd = 0x00;
    16.    
    17.    i2c_start();
    18.    i2c_write(SLAVE1_WRT_ADDR);
    19.    i2c_write(cmd);
    20.    i2c_stop();
    21.  
    22.    //printf("read %X \n\r", data);
    23.  
    24.   }
    25.  
    26. }  
    27.  
    Slave:
    Code ( (Unknown Language)):
    1.  
    2. int8 data;
    3.  
    4.  
    5. #INT_SSP
    6. void ssp_interrupt()
    7. {
    8.    int8 incoming, state;
    9.    
    10.    state = i2c_isr_state();
    11.        
    12.    if(state < 0x80)     // Master is sending data
    13.    {
    14.       incoming = i2c_read();
    15.       data = incoming;
    16.    }
    17.    
    18.    if(state >= 0x80)   // Master is requesting data from slave
    19.    {
    20.       i2c_write(data);
    21.    }
    22.  
    23. }
    24.  
    25.  
    26. //======================================
    27. void main ()
    28. {
    29.  
    30.    enable_interrupts(INT_SSP);
    31.    enable_interrupts(GLOBAL);
    32.    //set_tris_c(0x01);
    33.        
    34.    while(1)
    35.    {
    36.       if (data == 0x01)
    37.          output_high(PIN_C0);
    38.       if (data == 0x00)
    39.          output_low(PIN_C0);
    40.    }
    41.  
    42. }
    43.  
     
  2. sebgus

    New Member

    Feb 5, 2010
    6
    0
    In the master you have defined the write-adress as 0x12, but you haven't defined anything in the slave. Haven't worked anything with the CCS compiler but in the SSPADD-register you define the slave-adress.

    I programmed two microcontrollers a while a go where the first would send a value from the internal A/D-converter over I2C to the slave which would use the PWM as a D/A-converter to show the voltage on MCU #1s A/D-converter.

    Here is the source code for those (poorly commented, but I hope you understand the flow):

    Master:

    Code ( (Unknown Language)):
    1. #include <pic18.h>
    2. #include <delay.c>
    3.  
    4. void Init_I2C(void);
    5. void I2C_Write(char SlaveAdress, char Data);
    6. unsigned char ad8bit(unsigned char kanal);
    7. unsigned char i,j,data;
    8.  
    9. void main(){
    10.     TRISA=0x01;
    11.     TRISB=0x00;
    12.     Init_I2C();
    13.     while(1)
    14.     {
    15.         RB4=1;  // Turn on a LED on RB4
    16.         data = ad8bit(0);
    17.         I2C_Write(0x40,data); // Send to 0x40 and data as value
    18.         DelayMs(100);
    19.         RB4=0;  // Turn off a LED on RB4 (just to indicate that it passes this loop)
    20.         DelayMs(100);
    21.     }
    22. }
    23.    
    24. unsigned char ad8bit(unsigned char kanal){ // A/D-converter
    25.     kanal<<=3;
    26.     ADCON0=kanal | 0xC1;
    27.     DelayUs(10);
    28.     GODONE=1;
    29.     while(GODONE);
    30.     return ADRESH;
    31. }
    32.  
    33. void Init_I2C(void){ // Initialize I2C
    34.         TRISC=0xFF;
    35.         SSPCON1=0x28;
    36.         SSPADD=0x28;
    37. }
    38.  
    39. void I2C_Write(char SlaveAdress, char Data){ //Write-sequence
    40.     SEN=1;
    41.     for(j=0;j<20;j++);  //Short delay
    42.     SSPBUF=SlaveAdress;
    43.     for(j=0;j<100;j++);     //Short delay
    44.     SSPBUF=Data;
    45.     for(j=0;j<100;j++);     //Short delay
    46.     PEN=1;
    47.     for(j=0;j<50;j++);  //Short delay
    48. }
    Slave:
    Code ( (Unknown Language)):
    1. #include <pic18.h>
    2. #include <delay.c>
    3.  
    4. void Init_I2C(void);
    5. unsigned char I2C_Read2(void);
    6.  
    7. unsigned char data;
    8.  
    9. void main(){
    10.     Init_I2C();
    11.     PR2=0xFF;
    12.     TMR2ON=1;
    13.     CCP1CON=0x0C;
    14.     while(1)
    15.     {
    16.         data=SSPBUF; // Get the value from the buffer
    17.         CCPR1L=data; // PWM it out!
    18.     }
    19. }      
    20.  
    21. void Init_I2C(void){ // Initialize I2C
    22.         TRISC=0xFB;
    23.         SSPCON1=0x36;
    24.         SSPADD=0x40;
    25. }
    This code was used on two PIC18F458 with the HI-Tech PICC18 compiler.

    Hope you can solve it!
     
    Last edited: Feb 7, 2010
Loading...