|
|
View previous topic :: View next topic |
Author |
Message |
hadeelqasaimeh
Joined: 05 Jan 2006 Posts: 105
|
i2c problem |
Posted: Wed Sep 27, 2006 2:15 am |
|
|
hi all
I write a simple code to communicate between two pics. It don't work. I don't know why:
Master code:
Code: |
#include <16f877a.h>
#include "lcd_kbd1.c"
#fuses XT,NOWDT,NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use I2C(master, sda=PIN_B0, scl=PIN_B1)
#byte tris_b =0xff
#byte tris_d =0xff
int8 value;
void main()
{ LCD_Init ( );
while(1)
{
value = input_d();
LCD_PutCmd ( CLEAR_DISP );
LCD_SetPosition ( 0x80);//80-8f
printf(LCD_PutChar,"value:");
LCD_SetPosition ( 0x88);//c0-cf
printf(LCD_PutChar,"%u",value);
delay_ms(1000);
i2c_start(); // Start condition
i2c_write(0xa0); // Device address/Read
i2c_write(value);
i2c_stop(); // Stop condition
}
}
|
and slave code
Code: |
#include <16f877a.h>
#include "lcd_kbd1.c"
//#include <string.h>
#fuses XT,NOWDT,NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0xa0,FORCE_HW)
#byte tris_b =0x00 //rb0-rb3 are o/p
#byte tris_c =0x80
int8 value;
#INT_SSP //SPI or I2C activity
void ssp_isr()
{
if (i2c_poll())
{
value=i2c_read();
// LCD_PutCmd ( CLEAR_DISP );
// LCD_SetPosition ( 0x80);//80-8f
// / printf(LCD_PutChar,"value:");
// LCD_SetPosition ( 0x88);//c0-cf
// printf(LCD_PutChar,"%u",value);
output_high (PIN_B1);
delay_ms(500);
output_low (PIN_B1);
}
}
void main()
{
LCD_Init ( );
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
LCD_PutCmd ( CLEAR_DISP );
LCD_SetPosition ( 0x80);//80-8f
printf(LCD_PutChar,"welcome");
while(1)
{
output_high (PIN_B6);
delay_ms(1000);
output_low (PIN_B6);
}
}
|
I want just to send from master the value of port d, and receive them on slave then give indication or print on lcd |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 27, 2006 1:15 pm |
|
|
Quote: |
I write a simple code to communicate between two pics,its dont work
,i dont know why. |
Use the CCS example file for an i2c slave. It's called EX_SLAVE.C and
it's in this folder: c:\Program Files\Picc\Examples
Put the EX_SLAVE.C program in one PIC. Then put the program in
the following link on another PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9
Remember to add the pull-up resistors to the SCL and SDA signals.
(You can 2.2K or 4.7K resistors) |
|
|
bsodmike
Joined: 05 Aug 2006 Posts: 52
|
|
Posted: Wed Sep 27, 2006 2:18 pm |
|
|
An obvious question, why are you using hardware I2C on one, and software I2C on the other?
Why not stick to hardware on both? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Sep 27, 2006 3:10 pm |
|
|
Quote: | #use I2C(slave,sda=PIN_C4,scl=PIN_C3,address=0xa0,FORCE_HW) |
In the i2c Slave statement above, the FORCE_HW is not needed.
The compiler will always generate code to use the PIC's hardware SSP
module. Note: This is only true for the slave.
Try it. Compile the EX_SLAVE.C example file, with and without
FORCE_HW. Then use the 'fc' command in a DOS window to compare
the two .LST files. |
|
|
|
|
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
|