khanh my
Junior Member level 2
Hello,
I' using CCS C example of code for I2C communication between one Master and one Slave. It's work well.
However, i'm trying now with two Slaves, One Master and i have problem. 2 slave data transmission on the master was wrong (LCD of Master 255)
code master
code slave1:
Code Slave 2:
tks 4all
- - - Updated - - -
Here is the simulation file and code master, slave1,2
https://www.mediafire.com/?l6m73e5v4uny4bn
I' using CCS C example of code for I2C communication between one Master and one Slave. It's work well.
However, i'm trying now with two Slaves, One Master and i have problem. 2 slave data transmission on the master was wrong (LCD of Master 255)
code master
Code:
#include <16f887.h>
#fuses hs,nolvp,nowdt
#use delay(clock=20000000)
#include <lcd_lib_4bit.c>
#use i2c(master,sda=pin_c4,scl=pin_c3 )
#define slave_add 0x10
#define slave_add2 0x40
unsigned int doc,doc2;
void hienthi_lcd(void);
unsigned int read_slave(void);
unsigned int read_slave2(void);
void main() {
set_tris_d(0x00);
lcd_init();
while(true){
doc=read_slave();
doc2=read_slave2();
hienthi_lcd();
}
}
unsigned int read_slave(void) {
unsigned int dulieu;
i2c_start(1);
i2c_write(0x10);
dulieu=i2c_read();
i2c_stop();
return dulieu;
}
unsigned int read_slave2(void) {
unsigned int dulieu2;
i2c_start(1);
i2c_write(0x40);
dulieu2=i2c_read();
i2c_stop();
return dulieu2;
}
void hienthi_lcd() {
lcd_putcmd(line_1);
lcd_putchar("MASTER");
lcd_putcmd(line_2);
printf(lcd_putchar,"H0=%3u H1=%3u",doc,doc2);
}
code slave1:
Code:
#include <16f887.h>
#fuses hs,nolvp,nowdt
#device adc=10
#use delay(clock=20000000)
#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x10)
#include <lcd_lib_4bit.c>
float doc;
unsigned int16 a;
void doc_adc(void);
void xuly_adc(void);
void hienthi_lcd(void);
#int_ssp
void ssp_interupt () {
if(i2c_isr_state()==0x80) {
i2c_write((unsigned int)doc);
}
}
void main (void) {
set_tris_a(0xff);
set_tris_d(0x00);
lcd_init();
setup_adc_ports(san0);
setup_adc(adc_clock_internal);
enable_interrupts(int_ssp);
clear_interrupt(int_ssp);
enable_interrupts(global);
while (true) {
doc_adc();
xuly_adc();
hienthi_lcd ();
}
}
void doc_adc () {
set_adc_channel(0);
delay_us(10);
a=read_adc ();
}
void xuly_adc () {
doc=((float)a*4.88)/10;
}
void hienthi_lcd () {
lcd_putcmd(line_1);
lcd_putchar("SLAVE 1");
lcd_putcmd(line_2);
printf(lcd_putchar,"H0=%3.0f",doc);
}
Code Slave 2:
Code:
#include <16f887.h>
#fuses hs,nolvp,nowdt
#device adc=10
#use delay(clock=20000000)
#use i2c(slave,sda=pin_c4,scl=pin_c3,address=0x40)
#include <lcd_lib_4bit.c>
float doc2;
unsigned int16 a2;
void doc_adc(void);
void xuly_adc(void);
void hienthi_lcd(void);
#int_ssp
void ssp_interupt () {
if(i2c_isr_state()==0x80) {
i2c_write((unsigned int)doc2);
}
}
void main (void) {
set_tris_a(0xff);
set_tris_d(0x00);
lcd_init();
setup_adc_ports(san0);
setup_adc(adc_clock_internal);
enable_interrupts(int_ssp);
clear_interrupt(int_ssp);
enable_interrupts(global);
while (true) {
doc_adc();
xuly_adc();
hienthi_lcd ();
}
}
void doc_adc () {
set_adc_channel(0);
delay_us(10);
a2=read_adc ();
}
void xuly_adc () {
doc2=((float)a2*4.88)/10;
}
void hienthi_lcd () {
lcd_putcmd(line_1);
lcd_putchar("SLAVE 2");
lcd_putcmd(line_2);
printf(lcd_putchar,"H1=%3.0f",doc2);
}
tks 4all
- - - Updated - - -
Here is the simulation file and code master, slave1,2
https://www.mediafire.com/?l6m73e5v4uny4bn