CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC18F26J50 as SPI to USB bridge

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tnt888



Joined: 27 Mar 2010
Posts: 2

View user's profile Send private message

PIC18F26J50 as SPI to USB bridge
PostPosted: Sat Mar 27, 2010 10:33 am     Reply with quote

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?
tnt888



Joined: 27 Mar 2010
Posts: 2

View user's profile Send private message

PostPosted: Sat Mar 27, 2010 7:43 pm     Reply with quote

Got it solved. Since SPI wasn't working in master mode (v4.105) used #use_spi in software mode instead.
I think there's quite a few bugs with in spi in the current version.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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