Olufola
Joined: 19 May 2009 Posts: 21
|
One of the Slave codes and the Master code |
Posted: Tue May 19, 2009 2:42 pm |
|
|
SLAVE CODE
Code: |
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#elif defined(__PCH__)
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=194,force_hw)
BYTE address, buffer[0x10];
BYTE incoming, state;
#INT_SSP
void SSP_isr(void)
{
output_high(pin_d6);
clear_interrupt(int_SSP);
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
address = incoming;
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state == 0x80) //Master is requesting data
{
i2c_write(79);
}
}
void main ()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE)
{
set_tris_c(225);
output_high(pin_d4);
delay_ms(120);
output_low(pin_d4);
delay_ms(120);
}
} |
MASTER CODE
Code: |
/#include <E:\My Files\My_custom_i2c_library.h>
#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#elif defined(__PCH__)
#include <18F4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,force_hw)
/*
BOOLEAN slave_ready(BYTE );
void write_to_slave(BYTE, BYTE);
BYTE read_from_slave(BYTE);
void ssp_interrupt ();
*/
byte address1 = 194;
/*
byte address2 = 196;
byte address2 = 198;
byte address2 = 200;
byte address2 = 202;
byte address2 = 204;
byte address2 = 206;
byte address2 = 208;
byte address2 = 210;
byte address2 = 212;
*/
byte data1 =0;
/*
byte data2 =0;
byte data3 =0;
byte data4 =0;
byte data5 =0;
byte data6 =0;
byte data7 =0;
byte data8 =0;
byte data9 =0;
byte data10 =0;
*/
BOOLEAN slave_ready(BYTE );
void write_to_slave(BYTE, BYTE);
BYTE read_from_slave(BYTE);
void ssp_interrupt ();
VOID MAIn (void)
{
while (true)
{
data1 = read_from_slave(address1);
printf("data read from sensor 1 is %4u",(int32) data1);
delay_ms(120);
}
/*
do
{
while(!ext_eeprom_ready());
i2c_start();
state = i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
if(state == 0)
{
state = i2c_write(address);
if(state == 0)
{
i2c_start();
state = i2c_write((0xa0|(BYTE)(address>>7))|1);
if(state == 0)
{
data=i2c_read(0);
i2c_stop();
printf("data received is %4u", data);
}
}
}
if(state == 1)
{
i2c_stop();
}
}while(state != 0);
*/
}
BOOLEAN slave_ready(BYTE address)
{
int ack;
i2c_start(); // If the write command is acknowledged,
ack = i2c_write(address); // then the device is ready.
i2c_stop();
return !ack;
}
void write_to_slave(BYTE address, BYTE data)
{
while(!slave_ready(address))
i2c_start();
i2c_write(address&0xfe);
i2c_write(data);
i2c_stop();
}
BYTE read_from_slave(BYTE address)
{
BYTE data;
// while(!slave_ready(address))
i2c_start();
i2c_write(address|1);
data=i2c_read(0);
i2c_stop();
return(data);
}
void ssp_interrupt ()
{
BYTE incoming, state,buffer[16],address;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
address = incoming;
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state == 0x80) //Master is requesting data
{
i2c_write(buffer[address]);
}
} |
|
|