tnt888
Joined: 27 Mar 2010 Posts: 2
|
PIC18F26J50 as SPI to USB bridge |
Posted: Sat Mar 27, 2010 10:33 am |
|
|
Hello, I'm trying to use the PIC18F26J50 to collect "long int" from a dsPIC and send it to PC via USB.
The problem is that when the 18F is set to slave mode SPI, the USB interrupts cause problem when communicating with the other PIC. So I tried disabling the interrupts while receiving through SPI and enable it again. Though this works fine with PUTTY terminal, it is not giving me a stable connection with MATLAB on PC.
Code: |
void init_usbspi()
{
setup_spi2 (SPI_SLAVE | SPI_L_TO_H ); //SPI_SS_DISABLED
READY_TRIS = 0; READY = 1; // Reporting For Duty
window_size = spi_read2 (0); // window size per ADC
window_size <<= 8; READY = 0;
window_size |= spi_read2(0);
number_of_adcs = spi_read2 (0); // number of ADCs
number_of_adcs <<= 8;
number_of_adcs |= spi_read2(0);
}
void main()
{
long i, data;
MCU_PLLEN = TRUE;
delay_ms(1000);
usb_cdc_init();
usb_init();
//delay_ms(1000);
init_usbspi();
printf ("%Lu %Lu %Lu\r\n", window_size, number_of_adcs, window_size*number_of_adcs);
//while (!usb_cdc_connected());
while (TRUE)
{
printf (usb_cdc_putc, "Start\n"); //Indicate start of next reading
for (i = 0; i < window_size*number_of_adcs; i++)
{
disable_interrupts (int_usb);
data = rx_reading();
enable_interrupts (int_usb);
output_high (PIN_A0); /////////// FUN /////////
printf (usb_cdc_putc, "%Lu\n", data);
output_low (PIN_A0); /////////// FUN /////////
}
}
}
|
Using the SPI in master mode shouldn't cause any problems with the interrupts, unfortunately the 18F PIC seems to hang once it's set to receive data in this mode.
Code: |
void init_usbspi()
{
setup_spi (SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4);
READY_TRIS = 1; while (!READY); // Waiting for duty
window_size = spi_read (0);
window_size <<= 8;
window_size |= spi_read(0);
number_of_adcs = spi_read (0);
number_of_adcs <<= 8;
number_of_adcs |= spi_read (0);
}
|
Is there any solution to this simple task? |
|