Spanish Italian
8420 Users    

SUCKERmenu

FLEX Board USB connection

flex usb board

This demo shows how to set up a USB connection between the PC and the FLEX board. The aim is to control the dsPIC LED and the PIC18 LED system from the host PC. The dsPIC LED is controlled by the SPI communication with the PIC18.
The application is divided into three parts:
1. In the first part a brief description of the USB set-up is provided. The aim is to give a brief overview of the USB protocol.
2. The second part concerns the dsPIC side with the use of the SPI connection with the PIC18.
3. The third part concerns the PC side application. You can download a simple application under Windows (XP or Vista) to communicate with FLEX in order to switch on and off both LEDs of dsPIC and PIC18.

/** I N C L U D E S **********************************************************/

#include 
#include 
#include "system\typedefs.h"                        // Required
#include "system\usb\usb.h"                         // Required
#include "io_cfg.h"                                 // Required

#include "system\usb\usb_compile_time_validation.h" // Optional
#include "user\pickit.h"
#include "user\pk_isr.h"


/** V A R I A B L E S ********************************************************/

#pragma udata
char inbuffer[64];

/** P R I V A T E  P R O T O T Y P E S ***************************************/

static void InitializeSystem(void);
void USBTasks(void);


/** V E C T O R  R E M A P P I N G *******************************************/

extern void _startup (void);        // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x002000
void _reset (void)
{
    _asm goto _startup _endasm
}

#pragma code _HIGH_INTERRUPT_VECTOR = 0x002008
void _high_ISR (void)
{
    _asm
        goto InterruptHandler       // jump to interrupt routine
    _endasm
}

#pragma code InterruptVectorLow = 0x002018
void InterruptVectorLow (void)
{
    _asm
        goto InterruptHandler       // jump to interrupt routine
    _endasm
}

/** D E C L A R A T I O N S **************************************************/

#pragma code


/******************************************************************************
 * Function:        void main(void)
 *
 * PreCondition:    None
 *
 * Input:           None
 *
 * Output:          None
 *
 * Side Effects:    None
 *
 * Overview:        Main program entry point.
 *
 * Note:            None
 *****************************************************************************/
unsigned char ReadSPI( void )
{
  SSPBUF = 75;                  // initiate bus cycle
  while ( !SSPSTATbits.BF );      // wait until cycle complete
  return ( SSPBUF );              // return with byte read 
}

void Process_SPI(void){
	if(PORTBbits.RB4 && INTCONbits.RBIF) {
		INTCONbits.RBIF=0;
		//if(ReadSPI()==0xaa) PORTBbits.RB5=!PORTBbits.RB5;
		ReadSPI();
	}
}


void ProcessIO(void)
{
	unsigned char usb_idx = 0;		// index of USB buffer
	

    if (HIDRxReport(inbuffer, 64) > 0)  // USB receive buffer has data
	{
	//PORTBbits.RB5=!PORTBbits.RB5;
	//	do
	//	{
			switch(inbuffer[usb_idx])        // parse buffer for commands			
			{
                case 1:
					//WriteSPI(75);	//switch on led
					PORTBbits.RB5=1;
				break;
				case 0:
					//WriteSPI(80);	//switch off led
					PORTBbits.RB5=0;
				break;
				case 2:
					WriteSPI(80);	//switch off led
				break;
				case 3:
					WriteSPI(75);	//switch on led
				break;
				default: // End of Buffer or unrecognized command
					usb_idx = 64;			// Stop processing.
			} // end switch
	//	} while (usb_idx < 64); // end DO

	} // end if (HIDRxReport(inbuffer, 64) > 0)
    
} // end void ProcessIO(void)

void main(void)
{
	int i;
    InitializeSystem();
	   for (i=0; i

For more informations, click here

Who's new

  • cristipop
  • vivek
  • solohobby
  • raju_tawade
  • sachingehlot

Who's online

There are currently 2 users and 92 guests online.

Online users

  • cristipop
  • Ionela