|
|
View previous topic :: View next topic |
Author |
Message |
vsmguy
Joined: 13 Jan 2007 Posts: 91
|
SPI Read returns only 0 in slave when I hook up SS to INT |
Posted: Thu Apr 18, 2013 10:03 am |
|
|
I am coding a SPI slave using a PIC16F877A and need to be aware when the SS line goes from high (idle) to low (active) as information from the master is framed as a stream of bytes with the leading byte (first byte after SS goes active) telling me what the remaining bytes are for.
I have shorted SS (from master) to INT and written this:
Code: | #include <16F877A.h>
#device adc=16
#device *=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=20000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7, ERRORS)
// TODO: add a 100ohm resistor between the SS pin and the chip select output of the master microcontroller // Clock base Sample on
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H) // high trailing edge
volatile int1 slave_cs_enabled = false; //has the salve STB been enabled?
//#PRIORITY EXT, SSP
#PRIORITY INT_EXT, INT_SSP
#INT_EXT
void slave_cs_isr()
{
slave_cs_enabled = true;
}
//#INT_SSP
void main()
{
unsigned int8 data = 0;
enable_interrupts(INT_EXT);
ext_int_edge( H_TO_L );
clear_interrupt(INT_EXT); //if you read the data sheet, you will
enable_interrupts(GLOBAL);
//Now the code will call the interrupt in the rising edge.
setup_spi(SPI_SLAVE | SPI_MODE_3);
while(1) {
if(slave_cs_enabled)
{
slave_cs_enabled = false;
printf("STB\n\r");
}
if(spi_data_is_in())
{
data = spi_read();
printf("%X\n\r", data);
}
}
}
|
All I get after STB are "00". When I take out the STB logic, I get stream of bytes but that is not useful to me as I now don't know where a stream starts and ends.
Help! |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Thu Apr 18, 2013 10:45 am |
|
|
You have to read the pin to clear the interrupt, otherwise it keeps on interrupting. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Thu Apr 18, 2013 3:20 pm |
|
|
Not for int_ext. Only for the interrupt on change.
Obvious question is how fast the data is being sent, and whether there is any delay between operating the select, and the data being sent. Given the way the code is written, there is a huge delay before you check if data has arrived, so if multiple bytes are sent, something will be lost. Time to get into and out of the interrupt, and then the time to print the message. A total of about 600uSec, before the 'data is in' flag is tested....
Best Wishes |
|
|
vsmguy
Joined: 13 Jan 2007 Posts: 91
|
|
Posted: Thu Apr 18, 2013 3:29 pm |
|
|
Ttelmah wrote: |
Obvious question is how fast the data is being sent, and whether there is any delay between operating the select, and the data being sent. Given the way the code is written, there is a huge delay before you check if data has arrived, so if multiple bytes are sent, something will be lost.
|
Sure, but given that the master sends out mostly non-zero values, I find it difficult to believe this PIC slave does not see at-least one non zero value.
Ttelmah wrote: | Time to get into and out of the interrupt, and then the time to print the message. A total of about 600uSec, before the 'data is in' flag is tested....
|
I will put in a #int_ssp ISR and see what I get.
In the meantime, could the issue be that I defined the priority:
Code: | #PRIORITY INT_EXT, INT_SSP
|
but never defined a INT_SSP ISR in reality? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Fri Apr 19, 2013 12:42 am |
|
|
#priority does basically nothing....
All it does is specify the _order_ the interrupt flags will be checked in the global ISR. If you don't have a priority entry, they are checked in the order they are defined in the code.
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
|