Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Code truyen so 16 bit co dau i2c 2 pic 4431

master
#include <18F4431.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP

#use delay(clock= 20000000)

#use i2c(master, sda=PIN_c4, scl=PIN_c5)

//#use fast_io(c)

//#use fast_io(D)

const int8 slave_addr = 0x10;

int16 v1,v2,v3;

int8 data_1,data_2;

void ghi_i2c(int16 a)

data_1=make8(a,1);

data_2=make8(a,0);

i2c_start();

i2c_write( slave_addr);

i2c_write( data_1);

delay_ms(1);

i2c_write( data_2);

i2c_stop();

void main()

v1=-400;

v2=900;
v3=400;

while(true)

ghi_i2c(v1);

delay_ms(900);

ghi_i2c(v2);

delay_ms(900);

ghi_i2c(v3);

delay_ms(900);

Code slave
//chuong trinh cho slave

#include <18F4431.H>

#fuses HS,NOWDT,NOPROTECT,NOLVP,STVREN, FCMEN , SSP_RD

#use delay(clock= 20000000)

#use i2c(SLAVE, FORCE_HW, SDA=PIN_C4, SCL=PIN_C5, address=0x10)

#include <lcd_4bit_ks.c>

#use fast_io(a)

#use fast_io(b)

#use fast_io(c)

#use fast_io(d)
#use fast_io(e)

int8 dc=0,data2,data3,state;

int flag;

int16 data;

#int_ssp

void ngat_ssp()

state= i2c_isr_state();

if( state< 0x80)

if (state==0)

dc= i2c_read(0);

if (state==1)

data2= i2c_read(1);

if (state==2)

data3= i2c_read(1);

data=make16(data2,data3);

flag=1;

if( state>= 0x80)

clear_interrupt(INT_SSP);

void main()
{

LCD_init();

enable_interrupts( global);

enable_interrupts( int_ssp);

while(true)

if(flag==1)

lcd_putcmd(0x85);

lcd_putcmd(0x01);//xoa man hinh

Printf(lcd_putchar,"%ld-%d-%d",data,data2,data3);

flag=0;

You might also like