![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
freshdesing
Joined: 14 Jul 2010 Posts: 2
|
I2C communication between two 16F877A |
Posted: Wed Jul 14, 2010 6:01 am |
|
|
Hi, I connected two pics 16F877A using I2C. The master has four LEDs, when lit the first, sent as data "1" when lit the second led, sent as data "2" and so since four leds. The slave has an LCD screen that initially displays "waiting for data, and when it arrives (should) represents the data that has come on the LCD screen. The problem is that it works, it lights the first LED of the master and the slave not receive any data.. Both pic's has led to "status" that flashes every half second, and both work well, so do not stay in any loop or freeze. ... I would appreciate some extra help. The codes are:
MASTER´s code:
Code: |
// RB7 --> ENTRADA ON/OFF
// RB6 --> ENTRADA ON/OFF
// RB5 --> ENTRADA ON/OFF
// RB4 --> ENTRADA ON/OFF
// RB0/INT -->
// RA0 --> LED STATUS
// RC7 -->
// RC6 -->
// RC5 -->
// RC4 --> SDA
// RC3 --> SCL
// RC2 -->
// RC1 -->
// RD0 --> OUT E
// RD1 --> OUT RS
// RD2 --> OUT RW
// RD3 -->
// RD4 --> OUT D4
// RD5 --> OUT D5
// RD6 --> OUT D6
// RD7 --> OUT D7
//
//
//
////////////////////////////////////////////////////////////////////////////////
#include <16f877A.h> //pic a utilizar
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT //ordenes para el programador
#use delay (clock=4000000) //Fosc=4Mhz
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3,FAST,FORCE_HW)
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#byte port_a = 0x05
#byte port_b = 0x06
#byte port_c = 0x07
#byte port_d = 0x08
#byte port_e = 0x09
int dato=0;
void i2c(int data)
{
i2c_start(); //condicion de inicio
i2c_write(0xa0); //direccion del esclavo con el que haremos la comunicacion
//A6 A5 A4 A2 A1 A0 R/W -> R/W = 0 escritura; 1 lectura
i2c_write(data); //enviamos dato
i2c_stop(); //finalizacion de la comunicacion
delay_ms(1000); //introducimos retardo para que no este constantemte escribiendo
}
#INT_TIMER1
void control_timer1(void)
{
output_bit(pin_a0,!input(pin_a0));
set_timer1(3036);
}
void main(void)
{
disable_interrupts(GLOBAL);
set_tris_a(0x00); // RA0 --> LED STATUS
// RA1 -->
// RA2 -->
// RA3 -->
// RA4 -->
// RA5 -->
port_b_pullups(false); // Resistencias de polarización del puerto B
set_tris_b(0xF0); // RB7 -->
// RB6 -->
// RB5 -->
// RB4 -->
// RB3 -->
// RB2 -->
// RB1 -->
// RB0 -->
set_tris_c(0x00); // RC0 -->
// RC1 -->
// RC2 -->
// RC3 --> SCL
// RC4 --> SDA
// RC5 -->
// RC6 -->
// RC7 -->
set_tris_d(0x00); // RD0 --> OUT E
output_d (0x00); // RD1 --> OUT RS
// RD2 --> OUT RW
// RD3 -->
// RD4 --> OUT D4
// RD5 --> OUT D5
// RD6 --> OUT D6
// RD7 --> OUT D7
setup_adc_ports(NO_ANALOGS);
setup_comparator(NC_NC_NC_NC);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
while(1)
{
delay_ms(2000);
output_bit(pin_d0,1);
dato=0;
i2c(dato);
delay_ms(500);
output_bit(pin_d0,0);
delay_ms(2000);
output_bit(pin_d1,1);
dato=1;
i2c (dato);
delay_ms(500);
output_bit(pin_d1,0);
delay_ms(2000);
output_bit(pin_d2,1);
dato=2;
i2c(dato);
delay_ms(500);
output_bit(pin_d2,0);
delay_ms(2000);
output_bit(pin_d3,1);
dato=3;
i2c(dato);
delay_ms(500);
output_bit(pin_d3,0);
}
}
|
SLAVE´s code
Code: |
// RD0 --> OUT LED
// RD1 --> OUT LED
// RD2 --> OUT LED
// RD3 --> OUT LED
// RA0 --> LED STATUS
// RC4 --> SDA
// RC3 --> SCL
//
//
//
////////////////////////////////////////////////////////////////////////////////
#include <16f877A.h> //pic a utilizar
#fuses XT,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT //ordenes para el programador
#use delay (clock=4000000) //Fosc=4Mhz
#define use_portd_lcd TRUE
#include <lcd.c>
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)
#byte port_a = 0x05
#byte port_b = 0x06
#byte port_c = 0x07
#byte port_d = 0x08
#byte port_e = 0x09
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3,ADDRESS=0xA0,FAST,FORCE_HW)
int dato=0;
BYTE incoming, state; // I2C vars
BYTE address, buffer[0x10]; // Address and Array of Bytes
#INT_SSP
void i2c_isr()
{
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
if(state == 0)
{
incoming = i2c_read();
printf(LCD_PUTC, "\fEl dato recibido es:\n %u",incoming);
}
if(state == 1) //First received byte is address
{
incoming = i2c_read();
address = incoming;
}
if(state == 2) //Second received byte is data
{
incoming = i2c_read();
printf(LCD_PUTC, "\fEl dato recibido es:\n %u",incoming);
buffer[address] = incoming;
}
}
if(state == 0x80) //Master is requesting data
{
i2c_write (buffer[address]);
}
}
#INT_TIMER1
void control_timer1(void)
{
output_bit(pin_a0,!input(pin_a0));
set_timer1(3036);
}
void main(void)
{
delay_ms(200); // power up delay
disable_interrupts(GLOBAL);
set_tris_a(0x00); // RA0 -->
// RA1 -->
// RA2 -->
// RA3 -->
// RA4 -->
// RA5 -->
port_b_pullups(false); // Resistencias de polarización del puerto B
set_tris_b(0xF1);
set_tris_c(0xFF); // RC0 -->
// RC1 -->
// RC2 -->
// RC3 --> SCL
// RC4 --> SDA
// RC5 -->
// RC6 -->
// RC7 -->
//set_tris_d(0x00); // RD0 --> OUT LED
// RD1 --> OUT LED
// RD2 --> OUT LED
// RD3 --> OUT LED
// RD4 -->
// RD5 -->
// RD6 -->
// RD7 -->
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
enable_interrupts(INT_TIMER1);
setup_psp(PSP_DISABLED);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
lcd_init(); //inicializa lcd
printf(LCD_PUTC, "\fEsperando dato..");
while(1)
{
}
}
|
Thanks in advance.
Last edited by freshdesing on Wed Jul 14, 2010 7:25 am; edited 1 time in total |
|
![](templates/subSilver/images/spacer.gif) |
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Jul 14, 2010 7:12 am |
|
|
First issue:-
Code: |
int dato=0;
void i2c(int dato)
|
Do not do this in CCS or any compiler for that matter. CCS does not issue a warning to say you are using the same name for a local as you are for a global BUT there could and may (in my case, was) be problems.
Use different names for local vars.
Secondly, in your master code you are using standard_io so all those set_tris statements are irrelivant. |
|
![](templates/subSilver/images/spacer.gif) |
freshdesing
Joined: 14 Jul 2010 Posts: 2
|
|
Posted: Wed Jul 14, 2010 7:29 am |
|
|
Thanks, I am trying to use the standard_io, and I didn't remember to change to fast_io. I changed the local variable to "data", but the comunication don't works.
Anybody know if the code of comunication is correctly?
Thnaks. |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|