|
|
View previous topic :: View next topic |
Author |
Message |
PIT! Guest
|
Help with interruptions please. |
Posted: Thu Oct 04, 2007 2:55 pm |
|
|
Hello,
I guess I posted before, but I can´t find the post sorry! =/
First thing, I use MPLAB 7.61 with CCS 3.184.
I´m using this 2 programs:
Master:
Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=pin_c6, rcv=pin_c7, parity=n, bits=8)
#use i2c(Master, sda=PIN_C4, scl=PIN_C3)
...
void main(void)
{
int c;
glcd_gotoxy(1,1,1);
glcd_putc("Pic:");
// Write the letter 'B' to the slave board.
i2c_start();
i2c_write(0x70);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
i2c_start();
i2c_write(0x70);
i2c_write(0x01);
i2c_write('C');
i2c_stop();
i2c_start();
i2c_write(0x70);
i2c_write(0x02);
i2c_write('D');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0x70);
i2c_write(0x00);
i2c_start();
i2c_write(0x71);
dado_com[0] = i2c_read(0);
i2c_stop();
posx=1;
posy=2;
for (i=0; i<8; i++)
{
if (bit_test(dado_com[0],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[0],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
posx=1;
posy=3;
i2c_start();
i2c_write(0x70);
i2c_write(0x01);
i2c_start();
i2c_write(0x71);
dado_com[1] = i2c_read(0);
i2c_stop();
for (i=0; i<8; i++)
{
if (bit_test(dado_com[1],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[1],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
posx=1;
posy=4;
i2c_start();
i2c_write(0x70);
i2c_write(0x02);
i2c_start();
i2c_write(0x71);
dado_com[2] = i2c_read(0);
i2c_stop();
for (i=0; i<8; i++)
{
if (bit_test(dado_com[2],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[2],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
glcd_gotoxy(1,6,1);
glcd_putc("Memoria:");
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
dado_com[0] = i2c_read(0);
i2c_stop();
posx=1;
posy=7;
for (i=0; i<8; i++)
{
if (bit_test(dado_com[0],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[0],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
posx=1;
posy=8;
i2c_start();
i2c_write(0xA0);
i2c_write(0x01);
i2c_start();
i2c_write(0xA1);
dado_com[1] = i2c_read(0);
i2c_stop();
for (i=0; i<8; i++)
{
if (bit_test(dado_com[1],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[1],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
posx=1;
posy=9;
i2c_start();
i2c_write(0xA0);
i2c_write(0x02);
i2c_start();
i2c_write(0xA1);
dado_com[2] = i2c_read(0);
i2c_stop();
for (i=0; i<8; i++)
{
if (bit_test(dado_com[2],7-i)==1)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("1");
}
else if (bit_test(dado_com[2],7-i)==0)
{
glcd_gotoxy(posx,posy,1);
glcd_putc("0");
}
posx++;
}
|
Slave:
Code: |
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) // Jumpers: 8 to 11, 7 to 12
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x70)
typedef enum {NOTHING, CONTROL_READ,
ADDRESS_READ, READ_COMMAND_READ} I2C_STATE;
I2C_STATE fState;
BYTE address, buffer[0x10];
#INT_SSP
void ssp_interupt ()
{
BYTE incoming;
if (i2c_poll() == FALSE) {
if (fState == ADDRESS_READ) { //i2c_poll() returns false on the
i2c_write (buffer[address]);//interupt receiving the second
fState = NOTHING; //command byte for random read operation
}
}
else {
output_b(0x55);
incoming = i2c_read();
if (fState == NOTHING){
fState = CONTROL_READ;
}
else if (fState == CONTROL_READ) {
address = incoming;
fState = ADDRESS_READ;
}
else if (fState == ADDRESS_READ) {
buffer[address] = incoming;
fState = NOTHING;
}
}
}
void main ()
{
int i;
fState = NOTHING;
address = 0x00;
for (i=0;i<0x10;i++)
buffer[i] = 0x00;
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
output_b(0xAA);
while (TRUE) {
}
}
|
My problem is that every time I use interruptions, it reloads all the main!
Exemple:
With the output_b(0xAA) my led´s stay green, with the output_b(0x55) they stay red.
So when I get a interrupt, it stays like 1 second red, then goes back to green. It should never go back to green.
I have a 24c08 and the pic on the i2c line. The read memory part works fine, but the pic part gets only 11111111 ( because of the pull-up resistors ).
I hope you understand it!
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Thu Oct 04, 2007 3:06 pm |
|
|
There is a problem in your thinking.
Don't use I2C_POLL for this. It says a byte has been received. You need to load the byte to be read back, for your 'read' operation on the master,_when the address has been received, with the low bit set_. I2C_POLL, will still be true at this point. This is why there is I2C_ISR_STATE, which allows you to detect the read/write bit.
Best Wishes |
|
|
PIT! Guest
|
|
Posted: Fri Oct 05, 2007 5:04 am |
|
|
But that is the ex_slave.c
Just added the output_b lines, to see where my error was!
And my version of ccs does not have i2c_isr_state.
Here is the notice from the CCS versions page of when it was added:
3.231 New function added: I2C_ISR_STATE()
Mine is CCS 3.184. =/
Thanks |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 05, 2007 5:13 am |
|
|
Do a search on the forum. There has been a replacement I2C_ISR_STATE function published in the past for use in the older compilers. I'm afraid I don't think CCS, actually tried what they posted, and when they did, they realised that they needed to check the R/W bit, and added I2C_ISR_STATE....
Best Wishes |
|
|
PIT! Guest
|
|
Posted: Fri Oct 05, 2007 5:16 am |
|
|
Yeh i found it, thanks.
But i don´t think the problem is the actual function, is the fact that is reload the main.
Because every interruption i make, it reloads the constants of the initialize program. So it is impossible to work with any program.
Thanks. |
|
|
Ttelmah Guest
|
|
Posted: Fri Oct 05, 2007 6:57 am |
|
|
The only thing wrong with the master main, is that it'll drop off the end (need something to stop this...). However this won't cause a restart (since WDT is disabled).
There are some 'nasties' in the slave (for instance, what happens if the master accidentally sends bore than 0x10 bytes?....
Best Wishes |
|
|
|
|
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
|