PDA

View Full Version : [C/MikroC] comunicazione I2C 1Master +Slave


{Red}Lyon
14-03-2010, 13:34
ciao a tutti, vorrei instaurare una comunicazione tramite I2C tra un master 16f877 e + slave 16f887. Per la programmazione utilizzo MIKROC 8.2.0.0

Per lo slave uso una EasyPic6 con impostazione di PullUP e switch resistenze off(ho provato anche ON)...

per il master ho sviluppato una scheda molto simile alla EasyPic e l'ho impostato allo stesso modo.

RC3 master sta collegato a RC3 slave
RC4 master sta collegato a RC4 slave
ai 2 fili c'è collegata una resistenza da 2,2KOhm collegata a sua volta a VCC( +5v )

Ho sviluppato i programmi seguendo alcune discussioni su forum internazionali e anche su questo ma ho alcuni problemi...

Lo slave non riceve il dato.

Ho inserito delle attivazioni di alcuni led nella routine interrupt per vedere fin dove arriva quando dal master invio il dato, e di solito entra in "if (SSPSTAT.R_W == 1)" e non riceve il dato quindi.

Confido nel vostro aiuto.

Posto il codice sorgente del master e dello slave.

MASTER:

/* Nokia 3310 LCD Interfacing
BOTSKOOL
SHOBHITKUKRETI */

#include "nokia3310botskool.h"
#include "all.h"

// Keypad Port and lines
#define KEYPAD_PORT PORTA // Select keypad port
#define KEYPAD_MENU F4 // Menu/Exit
#define KEYPAD_RIGHT F1 // RIGHT
#define KEYPAD_MA F5 // Manuale/Automatico
#define KEYPAD_INC F0 // Inc number
#define KEYPAD_DEC F3 // Dec number

unsigned short manauto;
unsigned int i, datain;
unsigned char test[10];

const unsigned char icon[5] = {27,61,54,94,108};
const Slave_Addy = 0x69;

void attiva()
{
for(i=0; i<5; i++)
nokia_write_data(icon[i]);
}
void spegni()
{
nokia_erase_y(4);
nokia_erase_x(0);
}
void inviadato()
{
I2C_Init(100000);
I2C_Start();
I2C_Wr(Slave_Addy);
I2C_Wr(1);
I2C_Stop();
}

unsigned short leggidato()
{
unsigned short read_data;
I2C_Init(100000);
I2C_Start();
I2C_Wr(Slave_Addy + 1);
read_data = I2C_Rd(0);
delay_ms(100);
I2C_Stop();

if (read_data == 2)
{
nokia_gotoxy(0,5);
printf("dato ricevuto");
}
return read_data;
}

void main()
{
ADCON1 = 7; // Configure AN pins as digital I/O
TRISA = 0xFF;
PORTA = 0;
TRISC = 0b00011000; // Configure PORTC as output
PORTC = 0x00;
TRISB = 0; // PORT C as output
PORTB = 0; // Intial Value at PORTC is Zero
nokia_init(); // Initialising the Nokia LCD
delay_ms(10);
nokia_clear_screen(); // Screen should always be cleared intially
delay_ms(10);
nokia_clean_ddram();
manauto = 0;

do
{
if ((KEYPAD_PORT.KEYPAD_RIGHT == 1)&&(manauto == 0))
{
delay_ms(250);
attiva();
manauto=1;
inviadato();
}
else if ((KEYPAD_PORT.KEYPAD_MENU == 1)&&(manauto == 1))
{
delay_ms(250);
manauto=0;
spegni();
}

}
while(1);
}


SLAVE:

/*
* Project name:
Slave
* Description:
Slave code for PIC 2 PIC I2C Communication
* Configuration:
MCU: PIC16F887
SW: mikroC v8.2.0.0
* NOTES:
*/

const Addy = 0x69; // set I2C device address
unsigned short j; // just dummy for buffer read
unsigned short rxbuffer;

void Init(){
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete
LCD_Cmd(LCD_CLEAR); // Clear display
LCD_Cmd(LCD_CURSOR_OFF); // Turn cursor off
TRISA = 0; // Set PORTA as output
TRISB = 0; // Set PORTB as output
TRISC = 0xff; // Set PORTC as input
SSPADD = Addy; // Get address (7bit). Lsb is read/write flag
SSPCON = 0x3E; // Set to I2C slave with 7-bit address
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.SSPIE = 1; // enable SSP interrupts
PIR1.SSPIF = 0;
SSPSTAT = 0;
}

void interrupt()
{
if (PIR1.SSPIF = 1)
{
PIR1.SSPIF = 0;
PORTA.F0 = 1;

//transmit data to master
if (SSPSTAT.R_W == 1)
{ // Read request from master
PORTA.F2 = 1;
SSPCON.CKP = 1; // Release SCL line
j = SSPBUF;
return;
} // That's it

if (SSPSTAT.BF == 0)
{ // all done,
PORTA.F4 = 1;
j = SSPBUF; // Nothing in buffer so exit
return;
}

//recieve data from master
if (SSPSTAT.D_A == 1)
{ // Data [not address]
PORTA.F6 = 1;
j = SSPBUF; // read buffer to clear flag [address]
return;
}

j = SSPBUF; // read buffer to clear flag [address]

}
}

void main()
{
Init();
lcd_out(1,1,"Slave Attivo");

while(1)
{
if (j == 1)
{
LCD_Out(2,6,"FUNZIONA"); // Print text to LCD, 2nd row, 6th column
}
Delay_ms(350);
}
}

{Red}Lyon
15-03-2010, 13:45
nessuno?