FoxtrotMike
Joined: 31 May 2007 Posts: 11
|
Question about #INT_SSP |
Posted: Wed May 27, 2009 2:07 pm |
|
|
Hi:
I have to interface a device with a PC. Said device outputs it's data thru a Sync Serial Port, requiring to be provided with a clock signal. I'm using a PIC18 to provide the interface, reading data from the device via SPI and sending it to the PC via UART.
The data reading process starts with a pushbutton, which is read thru the INT_RB interrupt. Currently, I'm using the TIMER1 interrupt to force reading of the device.
This is the code I'm using:
Code: |
#include <18F252.h>
#fuses HS,NOPROTECT,NOWDT,PUT
#use delay(clock=20000000)
#use rs232(baud=2400, parity=N, bits=8, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#use standard_io(A)
#use standard_io(B)
#use standard_io(C)
/////////////
// DEFINES //
/////////////
#define SPI_MODE_0 (SPI_L_TO_H|SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L|SPI_XMIT_L_TO_H)
#define PRESET 63869
/////////////
// GLOBALS //
/////////////
#byte PORTB = 0x0F81
int1 flag = 0;
int data, changes, last_b;
//////////////////////
// PORT_B INTERRUPT //
//////////////////////
#INT_RB
void portb_isr()
{
changes = last_b ^ PORTB;
last_b = PORTB;
//////////////////////////////////////
/////////////// PIN_B7 ///////////////
//////////////////////////////////////
//Detect falling edge on PIN_B7
if (bit_test(changes,7)&& !bit_test(last_b,7))
{
flag = 1;
}
//Detect rising edge on PIN_B7
if (bit_test(changes,7)&& bit_test(last_b,7))
{
flag = 0;
}
}
////////////////////
// TMR1 INTERRUPT //
////////////////////
#INT_TIMER1
void timer1_isr()
{
if(flag)
{
data = spi_read(0xAA);
putc(data);
}
set_timer1(PRESET);
}
//////////////////
// MAIN ROUTINE //
//////////////////
void main(void)
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(PRESET);
setup_timer_2(T2_DIV_BY_16, 129, 1);
setup_spi(SPI_MASTER|SPI_MODE_1|SPI_CLK_T2);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
enable_interrupts(INT_TIMER1);
while(1)
{
}
}
|
So far it works alright, but I'd like to know if there's a way to use the #INT_SSP interrupt in a similar way to the #INT_RDA interrupt. I've used the UART quite a bit, but the SPI is something of a mystery to me. Also, I think that using a timer interrupt for this uses more resources than if I was reading SPI activity directly.
BTW, the code I tried for #INT_SSP was this:
Code: |
#INT_SSP
void spi_isr()
{
if(flag&&spi_data_is_in())
{
data = spi_read(0xAA);
putc(data);
}
}
|
I haven't had much luck with #INT_SSP, so if anyone can give me a pointer on how to implement it, I'd really appreciate it.
Best regards, and thank you in advance!! |
|