Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** INCLUDES *******************************************************/
- #include "system.h"
- #include "system_config.h"
- #include "app_led_usb_status.h"
- #include "usb_config.h"
- #include "usb.h"
- #include "usb_device.h"
- #include "usb_device_cdc.h"
- #include "stdio.h"
- static uint8_t readBuffer[64];
- static uint8_t writeBuffer[64];
- void APP_DeviceCDCBasicDemoTasks();
- void APP_DeviceCDCBasicDemoInitialize();
- void UserInit();
- void interrupt low_priority low_isr(); //low priority interrupt routine
- void Leds(int value);
- int value = 0; //gloable variable
- uint8_t output = 0;
- int bla= 35;
- int b0 = 0;
- int b1 = 0;
- int b2 = 0;
- int b3 = 0;
- int b4 = 0;
- int b5 = 0;
- int b6 = 0;
- int b7 = 0;
- MAIN_RETURN main(void)
- {
- SYSTEM_Initialize(SYSTEM_STATE_USB_START);
- USBDeviceInit();
- USBDeviceAttach();
- UserInit();
- while(1)
- {
- SYSTEM_Tasks();
- //Application specific tasks
- APP_DeviceCDCBasicDemoTasks();
- }//end while
- }//end main
- /*User defined Initialiaze Function*/
- void UserInit()
- {
- PORTA = 0x00;
- TRISA = 0x01; //A0 = input
- PORTB = 0x00;
- TRISB = 0x00; //ouput
- PORTC = 0x00;
- TRISC = 0x00; //ouput
- CMCON = 0x07;
- ADCON0 = 0x01; //7-6 unimplemented
- //5-2 channel
- //1 GO/DONE (in progress "1"/idle "0")
- //0 On bit (enable "1"/disable "0")
- ADCON1 = 0x0E; //7-6 unimplemented
- //5 Voltage ref config bit
- //4 Voltage ref config bit
- //3-0 A/D configure bits (1110 = AN0)
- ADCON2 = 0x3E; //left justified,t clock select Fosc/64 and 20 TAD
- //0011 1110
- RCONbits.IPEN=1; //Enable priority levels on interrupts
- INTCONbits.PEIE=1; //Enables all low priority peripheral interrupts
- PIE1bits.ADIE=1;
- IPR1bits.ADIP =0;
- PIR1bits.ADIF=0;
- ADCON0bits.GO=1; //start ADcon
- INTCONbits.GIE = 1; //Enable global int
- //T0CON = 0xC7;
- //PIE1bits.ADIE = 1;
- //RCONbits.IPEN = 1; //Enable priority levels on interrupts
- //INTCON2bits.TMR0IP = 0; //TMR0 on low prio
- //INTCONbits.TMR0IE = 1; //Enables the TMR0 overflow interrupt
- //INTCONbits.GIEL = 1; //Enable Low Priority Interrupt
- }
- void interrupt low_priority low_isr()
- {
- if(PIR1bits.ADIF == 1)
- {
- value = ADRESH;
- PIR1bits.ADIF = 0; //CLEAR interrupt flag when you are done!!!
- ADCON0bits.GO=1; //start ADcon
- }
- }
- void APP_DeviceCDCBasicDemoTasks()
- {
- /* Check to see if there is a transmission in progress, if there isn't, then
- * we can see about performing an echo response to data received.
- */
- if( USBUSARTIsTxTrfReady() == true)
- {
- uint8_t i;
- uint8_t numBytesRead;
- /*get the data from the computer,and put in an array*/
- numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));
- /* For every byte that was read... */
- for(i=0; i<numBytesRead; i++)
- {
- char abc[50];
- switch(readBuffer[i])
- {
- /* If we receive new line or line feed commands, just echo
- * them direct.
- */
- case 0x0A:
- case 0x0D:
- writeBuffer[i] = readBuffer[i];
- break;
- case 0x5A:
- sprintf(abc," %d",value);
- putrsUSBUSART(abc);
- Leds(value);
- break;
- /* If we receive something else, then echo it plus one
- * so that if we receive 'a', we echo 'b' so that the
- * user knows that it isn't the echo enabled on their
- * terminal program.
- */
- default:
- writeBuffer[i] = readBuffer[i];
- output = writeBuffer[i];
- //Leds(output);
- break;
- }
- }
- if(numBytesRead > 0)
- {
- /* After processing all of the received data, we need to send out
- * the "echo" data now.
- */
- putUSBUSART(writeBuffer,numBytesRead);
- //putrsUSBUSART("" + writeBuffer,numBytesRead);
- //Leds(output);
- }
- }
- //Leds(value);
- CDCTxService();
- }
- void Leds(int getal){
- b7 = 0;
- b6 = 0;
- b5 = 0;
- b4 = 0;
- b3 = 0;
- b2 = 0;
- b1 = 0;
- b0 = 0;
- if(getal>=128){
- b7=1;
- getal=getal-128;
- }
- if(getal>=64){
- b6=1;
- getal=getal-64;
- }
- if(getal>=32){
- b5=1;
- getal=getal-32;
- }
- if(getal>=16){
- b4=1;
- getal=getal-16;
- }
- if(getal>=8){
- b3=1;
- getal=getal-8;
- }
- if(getal>=4){
- b2=1;
- getal=getal-4;
- }
- if(getal>=2){
- b1=1;
- getal=getal-2;
- }
- if(getal>=1){
- b0=1;
- }
- PORTBbits.RB2 = b0;
- PORTBbits.RB3 = b1;
- PORTBbits.RB4 = b2;
- PORTBbits.RB5 = b3;
- PORTBbits.RB6 = b4;
- PORTBbits.RB7 = b5;
- PORTAbits.RA2 = b6;
- PORTAbits.RA3 = b7;
- }
- /*You don't need to care the code below*/
- void APP_DeviceCDCBasicDemoInitialize()
- {
- CDCInitEP();
- line_coding.bCharFormat = 0;
- line_coding.bDataBits = 8;
- line_coding.bParityType = 0;
- line_coding.dwDTERate = 9600;
- }
- bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size)
- {
- switch( (int) event )
- {
- case EVENT_TRANSFER:
- break;
- case EVENT_SOF:
- /* We are using the SOF as a timer to time the LED indicator. Call
- * the LED update function here. */
- APP_LEDUpdateUSBStatus();
- break;
- case EVENT_SUSPEND:
- /* Update the LED status for the suspend event. */
- APP_LEDUpdateUSBStatus();
- break;
- case EVENT_RESUME:
- /* Update the LED status for the resume event. */
- APP_LEDUpdateUSBStatus();
- break;
- case EVENT_CONFIGURED:
- /* When the device is configured, we can (re)initialize the
- * demo code. */
- APP_DeviceCDCBasicDemoInitialize();
- break;
- case EVENT_SET_DESCRIPTOR:
- break;
- case EVENT_EP0_REQUEST:
- /* We have received a non-standard USB request. The HID driver
- * needs to check to see if the request was for it. */
- USBCheckCDCRequest();
- break;
- case EVENT_BUS_ERROR:
- break;
- case EVENT_TRANSFER_TERMINATED:
- break;
- default:
- break;
- }
- return true;
- }
- // /* If the USB device isn't configured yet, we can't really do anything
- // * else since we don't have a host to talk to. So jump back to the
- // * top of the while loop. */
- // if( USBGetDeviceState() < CONFIGURED_STATE )
- // {
- // /* Jump back to the top of the while loop. */
- // continue;
- // }
- // /* If we are currently suspended, then we need to see if we need to
- // * issue a remote wakeup. In either case, we shouldn't process any
- // * keyboard commands since we aren't currently communicating to the host
- // * thus just continue back to the start of the while loop. */
- // if( USBIsDeviceSuspended()== true )
- // {
- // /* Jump back to the top of the while loop. */
- // continue;
- // }
- /*******************************************************************************
- End of File
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement