Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F4431 Preoblem with I2C communication

Status
Not open for further replies.

vojke

Junior Member level 1
Joined
Sep 9, 2010
Messages
19
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Belgrade, Serbia
Activity points
1,457
Line Follower Robot Project. Problem with I2C communication

Greetings!

I have an PIC16F887 and he plays a master role in communication with the PIC18F4431 which plays a slave role. He will drive motors and measure distance via IR sensors. Later, the role of a master will play another device (BeagleBoard in combination with USB web camera) but that's not important now. I just want that my communication works, but for some reason it doesn't work.

MASTER: main function

//////////////////////////////////
static char condition = 0;
void main()
{
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
TRISC.RB3 = 0; // RC3 - SCL Output
TRISC.RB4 = 0; // RC4 - SDA Output
I2C1_Init(100000);
delay_ms(500);

while(1) {
while (condition < 1) {
I2C1_Start();
I2C1_Wr(0xA0); // address
I2C1_Wr(0x00); // 3 byte packet
I2C1_Wr(0xFF);
I2C1_Wr(0xFF);
I2C1_Stop();
uslov++;
}
}
}
///////////////////////////////////////

SLAVE: main function

//////////////////////////////////////
void main()
{

// Disable all interrupts
INTCON = 0;
INTCON2 = 0;
INTCON3 = 0;

// Set I/O registers
TRISA = 0xCF; // Inputs: A0-IRZ, A1-IRL, A2-IRD, A3-12V, A4-uu, A5-uu, A6-OSC2, A7-OSC1
TRISB = 0x00; // Outputs: B0-PWM0-E1, B1-PWM1-E2, B2-uu, B3-uu, B4-uu, B5-uu, B6-prog, B7-prog
TRISC = 0x40; // Outputs: C0-LEDZ, C1-LEDC, C2-I3, C3-I4, C4-I1, C5-I2, C7-uu; Inputs: C6 - 12V
TRISD = 0x0C; // Outputs: D0-uu, D1-uu, D4-uu, D5-uu, D6-uu, D7-uu; Inputs: , D2-SDA, D3-SCL;
TRISE = 0x00; // Outputs: E0-uu, E2-uu, E3-uu

PORTA = 0;
PORTB = 0;
PORTC = 0;
PORTD = 0;
PORTE = 0;

// Set A/D registers

ANSEL0 = 0x0F; // Analog Inputs: A0-AN0-IRZ, A1-AN1-IRL, A2-AN2-IRD, A3-AN3-12V
ANSEL1 = 0x00; //
ADCHS = 0x00; // Channel Select Register: AN0, AN1, AN2, AN3

// Set PWM registers
PTPERL = 0x3F; // 8-bit resolution PWM
PTPERH = 0; // 8-bit resolution PWM
//DTCON = 0b11000101; // death-time 2 us
PTCON0 = 0b00001010; // prescaler = 1:16 PWM = 5 kHz
PTCON1 = 0b10000000; // enable PWM clk
PWMCON0 = 0b00111100; // enable PWM !!!

PDC0L = 128;
PDC1L = 128;


// EEPROM I2C Address
SSPADD = 0xA0;

// Set the mode, enable the clock and SSP interface
SSPCON = SPEN | SSPCKP | I2CMODE; // SPEN = 0b00100000, SSPCKP = 00010000 , I2CMODE = 0b00000110

// Enable SSP interrupts
PIE1 = SSPIE; // SSPIE = 00001000
PIR1 = 0;
PIR2 = 0;
PIR3 = 0;

// Enable peripheral and global interrupts
INTCON |= PEIE | GIE; // GIE = 0b10000000, PEIE = 0b01000000


PORTB.RB7 = 1;
while(1) {}
}
//////////////////////////////////////////////

SLAVE: fragment of a interrupt function

/////////////////////////////////////////////

void interrupt() {
PORTB.RB6 = 1; // Turn on LED on RB6 so that I know that there was an interrupt
PDC0L = 0;
PDC0L = 255;
sleep_mode=0;
// Disable interrupts
INTCON &= ~(PEIE | GIE); // GIE = 0b10000000, PEIE = 0b01000000
// SSP transmission/reception is complete
.......
.......
Here is what should be done in iterrupt (irrelevant to this problem)
.......
.......
// Enable peripheral and global interrupts
INTCON |= PEIE | GIE; // GIE = 0b10000000, PEIE = 0b01000000
}

I can detect, with my oscilloscope, that there are information coming on slave PIC18F4431 pins PORTD.RB2(SDA) and PORTD.RB3(SCL).
But there is nothing happening. Program never enters the interrupt routine. LED on RB6 (which when ON signals that program has entered interrupt routine) is never ON.

Has someone any idea, what is wrong?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top