symphony888
Newbie level 5
- Joined
- Nov 28, 2011
- Messages
- 9
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,467
Hi guys,
I have some problems here.
Firstly, I'm using PICDEM2 Plus and the chip connected to it is PIC18F4520.
Also there is an EEPROM that is attached to the board - 24LC256
In my program, I've set my TRISCbits.RC3 and TRISCbits.RC4 as '0' for my Master side and TRISCbits.RC3 and TRISCbits.RC4 as '1' for my Slave Side.
I've connected physically by wiring up the Master's Board RC3 and RC4 to the Slave's Board RC3 and RC4. I'm using the exact same board for Master and Slave.
I've used the Watch Window to see if I get any Acknowledgement from the Slave. The result was '0' meaning I got an ACK from the Slave. The real problems lies when I want to try to send the Data over to the Slave, it does not work. Also, I include a command where I would blink the LED as the data, but Slave did not receive hence LED was not blinking at all.
Anyone care to help me out? Thanks in advance.
Master.c
Code:
#include <p18f4520.h>
#include <delays.h>
#include "myHeader.h"
#include <stdlib.h>
#include <i2c.h>
void main(void)
{
unsigned char sync_mode = 0, slew = 0, data[3], slave_address = 0x88;
ADCON1=0x0f; // Set Port A & E as digital I/O
TRISA = 0b11010111;
TRISB = 0b11000000; // all input
TRISC = 0b10000000;
TRISE = 0b11101100; // bit 1, 2 & 3 of Port A as output
TRISD = 0b00001111; // Port D as output
PORTA = 0b11010000;
PORTB = 0b00000001;
PORTC = 0b11111111;
PORTD = 0b00000000;
PORTE = 0;
Init_LCD();
OpenI2C (MASTER, SLEW_OFF);
data[2]='A';
while(1)
{
if(!PORTAbits.RA4)
{
IdleI2C(); // Wait until the bus is idle
StartI2C(); // Send START condition
IdleI2C(); // Wait for the end of the START condition
WriteI2C( slave_address & 0xfe ); // Send address with R/W cleared for write
IdleI2C(); // Wait for ACK
WriteI2C(0x11); // Write first byte of data
IdleI2C(); // Wait for ACK
StopI2C(); // Hang up, send STOP condition
}
else
{
PORTBbits.RB1 = ~PORTBbits.RB1;
Delay10KTCYx(50);
}
}
}
Slave.c
Code:
#include <p18f4520.h>
#include <delays.h>
#include "myHeader.h"
#include <stdlib.h>
#include <i2c.h>
void highPriorityISR(void);
void SSPISR(void);
unsigned char data=0x22, temp,i=0, data2;
#pragma code high_vector=0x08
void interrupt_at_high_vector(void)
{
_asm GOTO highPriorityISR _endasm
}
#pragma code
#pragma interrupt highPriorityISR
void highPriorityISR() {
if (PIR1bits.SSPIF) {
AckI2C();
SSPISR();
PIR1bits.SSPIF = 0; // Clear the interrupt flag
}
return;
}
void SSPISR(void) {
PORTBbits.RB1=~PORTBbits.RB1;
Delay10KTCYx(255);
}
void main(void)
{
unsigned char sync_mode = 0, slew = 0,slave_address = 0x88,i;
ADCON1=0x0F;
INTCONbits.GIEH = 0;
RCONbits.IPEN = 1; // Enable interrupt priorities
INTCON &= 0x3F; // Globally enable interrupts
PIE1bits.SSPIE = 1; // Enable SSP interrupt
IPR1bits.SSPIP = 1; // Set SSP interrupt priority to high
PIR1bits.SSPIF = 0;
SSPCON2bits.SEN = 1;
SSPCON1bits.CKP = 1;
INTCONbits.GIEH = 1;
ADCON1=0x0f; // Set Port A & E as digital I/O
TRISA = 0b11010111;
TRISB = 0b11000000; // all input
TRISC = 0b10011000;
TRISE = 0b11101100; // bit 1, 2 & 3 of Port A as output
TRISD = 0b00001111; // Port D as output
PORTA = 0b11010000;
PORTB = 0b00000001;
PORTC = 0b11111111;
PORTD = 0b00000000;
PORTE = 0;
Init_LCD();
OpenI2C (SLAVE_7, SLEW_OFF);
SSPADD=0x88;
while(DataRdyI2C()==0);
ReadI2C();
data2=SSPBUF;
while(DataRdyI2C()==0);
ReadI2C();
data2=SSPBUF;
}