CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question about I2C

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Student16



Joined: 02 Jan 2021
Posts: 5

View user's profile Send private message

Question about I2C
PostPosted: Wed Jan 13, 2021 11:29 am     Reply with quote

Hi I have problems with I2C, I want to send an int value from the master to the slave and display this value on a LCD connnected to the slave.

But I am getting unexpected results (the LCD displays 11, which I think is caused by I2C).

See my code below, am I doing something wrong?

MASTER:
Code:

#include <16F877A.h>
#fuses XT, NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)

#define REEDCONTACT0 PIN_A1
#define REEDCONTACT1 PIN_A2
#define REEDCONTACT2 PIN_A3
#define REEDCONTACT3 PIN_A4
#define DRUKKNOP_FRONT0 PIN_C0
#define DRUKKNOP_FRONT1_OMLAAG PIN_C1
#define DRUKKNOP_FRONT1_OMHOOG PIN_C2
#define DRUKKNOP_FRONT2_OMLAAG PIN_C5
#define DRUKKNOP_FRONT2_OMHOOG PIN_C6
#define DRUKKNOP_FRONT3 PIN_C7
#define DRUKKNOP_FRONT_NOODKNOP PIN_A5
#define DRUKKNOP_LIFT0 PIN_B0
#define DRUKKNOP_LIFT1 PIN_B1
#define DRUKKNOP_LIFT2 PIN_B2
#define DRUKKNOP_LIFT3 PIN_B3
#define DRUKKNOP_LIFT_NOODKNOP PIN_B4
#define TEMP_SENSOR PIN_A0

void main (){
   
   while(1){
      if(input(REEDCONTACT0) == 1){
         i2c_start();
         i2c_write(0x12);
         i2c_write(1); 
         i2c_stop(); 
      }
      if(input(REEDCONTACT1) == 1){
         i2c_start();
         i2c_write(0x12);
         i2c_write(2); 
         i2c_stop(); 
      }
      if(input(REEDCONTACT2) == 1){
         i2c_start();
         i2c_write(0x12);
         i2c_write(3); 
         i2c_stop(); 
      }
      if(input(REEDCONTACT3) == 1){
         i2c_start();
         i2c_write(0x12);
         i2c_write(4); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT0) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(5); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT1_OMLAAG) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(6); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT1_OMHOOG) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(7); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT2_OMLAAG) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(8); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT2_OMHOOG) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(9); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT3) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(10); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_FRONT_NOODKNOP) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(11); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_LIFT0) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(12); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_LIFT1) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(13); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_LIFT2) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(14); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_LIFT3) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(15); 
         i2c_stop(); 
      }
      if(input(DRUKKNOP_LIFT_NOODKNOP) == 0){
         i2c_start();
         i2c_write(0x12);
         i2c_write(16); 
         i2c_stop(); 
      }
   }
}


SLAVE:
Code:

#include <16F877A.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x12)

//LCD module connections
#define LCD_RS_PIN PIN_C0
#define LCD_RW_PIN PIN_C5
#define LCD_ENABLE_PIN PIN_C6
#define LCD_DATA4 PIN_C7
#define LCD_DATA5 PIN_E0
#define LCD_DATA6 PIN_E1
#define LCD_DATA7 PIN_E2
//End LCD module connections

#define GROENE_LED0 PIN_A1
#define GROENE_LED1 PIN_A3
#define GROENE_LED2 PIN_A5
#define GROENE_LED3 PIN_B1
#define RODE_LED0 PIN_A0
#define RODE_LED1 PIN_A2
#define RODE_LED2 PIN_B4
#define RODE_LED3 PIN_B0

#include <lcd.c>

//====================================

int sensor;

#INT_SSP
void ssp_interrupt(){
   sensor = i2c_read(0);
}

void print_op_LCD(int sensorwaarde){
   lcd_putc('\f');
   lcd_putc("Waarde:");
   lcd_gotoxy(1, 2);
   printf(lcd_putc, "%d", sensorwaarde);
   delay_ms(1000);
}

void main(){
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   
   lcd_init();
   lcd_putc("\fReady...\n");
   
   while(1){
      if(sensor == 1){
         output_high(GROENE_LED0);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(GROENE_LED0);
      }
      if(sensor == 2){
         output_high(GROENE_LED1);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(GROENE_LED1);
      }
      if(sensor == 3){
         output_high(GROENE_LED2);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(GROENE_LED2);
      }
      if(sensor == 4){
         output_high(GROENE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(GROENE_LED3);
      }
      if(sensor == 5){
         output_high(RODE_LED0);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED0);
      }
      if(sensor == 6){
         output_high(RODE_LED1);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED1);
      }
      if(sensor == 7){
         output_high(RODE_LED2);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED2);
      }
      if(sensor == 8){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 9){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 10){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 11){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 12){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 13){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 14){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 15){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
      if(sensor == 16){
         output_high(RODE_LED3);
         print_op_LCD(sensor);
         delay_ms(1000);
         output_low(RODE_LED3);
      }
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Jan 13, 2021 11:51 am     Reply with quote

Seriously, look at the I2C slave example (or look here). The first I2C
interrupt is when the address is received, not your data byte....
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 13, 2021 2:46 pm     Reply with quote

hmmmm I was thinking that, for example..
Quote:

if(sensor == 1){
output_high(GROENE_LED0);
print_op_LCD(sensor);
delay_ms(1000);
output_low(GROENE_LED0);

The value for 'sensor' is 1 same as 0b00000001.

This is NOT the same as the printable ASCII '1' character( (49 decimal, 0x31).
You would have to add 48 to the sensor value.

I don't know if the function print_op_LCD() does a conversion to translate the '1' into a printable '1'.
Student16



Joined: 02 Jan 2021
Posts: 5

View user's profile Send private message

Question about I2C
PostPosted: Wed Jan 13, 2021 6:37 pm     Reply with quote

temtronic wrote:
hmmmm I was thinking that, for example..
Quote:

if(sensor == 1){
output_high(GROENE_LED0);
print_op_LCD(sensor);
delay_ms(1000);
output_low(GROENE_LED0);

The value for 'sensor' is 1 same as 0b00000001.

This is NOT the same as the printable ASCII '1' character( (49 decimal, 0x31).
You would have to add 48 to the sensor value.

I don't know if the function print_op_LCD() does a conversion to translate the '1' into a printable '1'.


It does with the following line:
printf(lcd_putc, "%d", sensorwaarde);
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Jan 14, 2021 5:20 am     Reply with quote

Yes, that's because that function 'translates' the binary '1' into a printable character '1'.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group