La connessione USB e FLEX board

La connessione USB e FLEX board

La connessione USB e FLEX board: Questa demo dimostra come impostare la connessione USB tra il PC e la scheda FLEX . L’obiettivo e’ di controllare i LED di sistema del dsPIC e del PIC18 dal PC.

Il dsPIC LED e’ controllato tramite comunicazione SPI con PIC18. L’applicazione e’ divisa in tre parti:
1. Nella prima parte e’ prevista una breve descrizione della USB e l’obiettivo è quello di fornire una visione concisa del protocollo USB.
2. La seconda parte riguarda il dsPIC con l’uso della connessione SPI con PIC18.
3. La terza parte riguarda l’applicazione del PC. E’ possibile scaricare una semplice applicazione in Windows (XP o Vista) per comunicare con FLEX per accendere o spegnere i LED del dsPIC e PIC18.

La connessione USB e FLEX board – codice

/** 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 inbuffer[i]=0;
// outbuffer[i]=0;
}

ADCON1=0x0F;
SSPSTAT= 0b00000000;
SSPCON1= 0b00100001;
TRISBbits.TRISB1=0;
TRISBbits.TRISB0=1;

TRISCbits.TRISC7=0;
TRISBbits.TRISB5=0;
TRISBbits.TRISB4=1;
while(1) {

USBTasks(); // USB Tasks

ProcessIO();
// ReadSPI();

} // end while

} // end main

/******************************************************************************
* Function: static void InitializeSystem(void)
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: InitializeSystem is a centralize initialization routine.
* All required USB initialization routines are called from
* here.
*
* User application initialization routine should also be
* called from here.
*
* Note: None
*****************************************************************************/
static void InitializeSystem(void)
{

#if defined(USE_USB_BUS_SENSE_IO) // see usbcfg.h
tris_usb_bus_sense = INPUT_PIN; // See io_cfg.h
#endif

#if defined(USE_SELF_POWER_SENSE_IO) // see usbcfg.h
tris_self_power = INPUT_PIN;
#endif

mInitializeUSBDriver(); // See usbdrv.h

SSPSTAT= 0b00000000;
SSPCON1= 0b00100010;
TRISBbits.TRISB1=0;
TRISBbits.TRISB0=1;

TRISCbits.TRISC7=0;
TRISBbits.TRISB5=0;

INTCONbits.TMR0IE=1;
INTCONbits.PEIE = 1; // enable Peripheral Interrupt Enable
INTCONbits.GIE = 1; // enable global interrupts

// PICkitInit(); // See pickit.c and pickit.h

} // end InitializeSystem

/******************************************************************************
* Function: void USBTasks(void)
*
* PreCondition: InitializeSystem has been called.
*
* Input: None
*
* Output: None
*
* Side Effects: None
*
* Overview: Service loop for USB tasks.
*
* Note: None
*****************************************************************************/
void USBTasks(void)
{
/*
* Servicing Hardware
*/
USBCheckBusStatus(); // Must use polling method
// if(UCFGbits.UTEYE!=1)
USBDriverService(); // Interrupt or polling method

// #if defined(USB_USE_CDC)
// CDCTxService();
// #endif

} // end USBTasks

/*****************************************************************************
* The following is a value stored in upper program memory to let the boot-
* loader know if an application program is loaded. If the bootloader does
* not read this code, it jumps to bootloader mode.
****************************************************************************/

//#pragma romdata _bootcode = 0x7FFE
//rom word bootcode = 0x5555;

/** EOF main.c ***************************************************************/

Per maggiori informazioni clicca qui.

Repost: 1 Lug 2008

STAMPA     Tags:, ,

One Response

  1. lucagiuliodori 13 giugno 2011

Leave a Reply