Advertisement
Matthuwee

Untitled

Jan 7th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.67 KB | None | 0 0
  1. /** INCLUDES *******************************************************/
  2. #include "system.h"
  3. #include "system_config.h"
  4. #include "app_led_usb_status.h"
  5. #include "usb_config.h"
  6. #include "usb.h"
  7. #include "usb_device.h"
  8. #include "usb_device_cdc.h"
  9.  
  10.  
  11. static uint8_t readBuffer[64];
  12. static uint8_t writeBuffer[64];
  13. void APP_DeviceCDCBasicDemoTasks();
  14. void APP_DeviceCDCBasicDemoInitialize();
  15. void UserInit();
  16. void interrupt low_priority low_isr();   //low priority interrupt routine
  17.  
  18. void Leds(int value);
  19. int  value = 0;  //gloable variable
  20. uint8_t output = 0;
  21. int b0 = 0;
  22. int b1 = 0;
  23. int b2 = 0;
  24. int b3 = 0;
  25. int b4 = 0;
  26. int b5 = 0;
  27. int b6 = 0;
  28. int b7 = 0;
  29.  
  30. MAIN_RETURN main(void)
  31. {
  32.     SYSTEM_Initialize(SYSTEM_STATE_USB_START);
  33.  
  34.     USBDeviceInit();
  35.     USBDeviceAttach();
  36.     UserInit();
  37.     while(1)
  38.     {
  39.         SYSTEM_Tasks();
  40.         //Application specific tasks
  41.         APP_DeviceCDCBasicDemoTasks();
  42.  
  43.     }//end while
  44. }//end main
  45.  
  46. /*User defined Initialiaze Function*/
  47. void UserInit()
  48. {
  49.     PORTA = 0x00;
  50.     TRISA = 0x01;       //A0 = input
  51.     PORTB = 0x00;
  52.     TRISB = 0x00;       //ouput
  53.     PORTC = 0x00;
  54.     TRISC = 0x00;       //ouput
  55.     CMCON = 0x07;
  56.  
  57.     ADCON0 = 0x01; //7-6 unimplemented
  58.                    //5-2 channel
  59.                    //1 GO/DONE (in progress "1"/idle "0")
  60.                    //0 On bit (enable "1"/disable "0")
  61.  
  62.     ADCON1 = 0x0E; //7-6 unimplemented
  63.                    //5 Voltage ref config bit
  64.                    //4 Voltage ref config bit
  65.                    //3-0 A/D configure bits (1110 = AN0)
  66.  
  67.     ADCON2 = 0x3E; //left justified,t clock select Fosc/64 and 20 TAD
  68.                    //0011 1110
  69.  
  70.     RCONbits.IPEN=1;        //Enable priority levels on interrupts
  71.     INTCONbits.PEIE=1;      //Enables all low priority peripheral interrupts
  72.     PIE1bits.ADIE=1;
  73.     IPR1bits.ADIP =0;
  74.     PIR1bits.ADIF=0;
  75.     ADCON0bits.GO=1;        //start ADcon
  76.  
  77.     INTCONbits.GIE = 1;     //Enable global int
  78.  
  79.     //T0CON = 0xC7;
  80.     //PIE1bits.ADIE = 1;
  81.     //RCONbits.IPEN = 1;          //Enable priority levels on interrupts
  82.     //INTCON2bits.TMR0IP = 0;     //TMR0 on low prio
  83.     //INTCONbits.TMR0IE = 1;      //Enables the TMR0 overflow interrupt
  84.     //INTCONbits.GIEL = 1;        //Enable Low Priority Interrupt
  85. }
  86.  
  87. void interrupt low_priority low_isr()
  88. {
  89.     if(PIR1bits.ADIF == 1)
  90.     {
  91.         ADCON0bits.GO=1;        //start ADcon
  92.         value = ADRESH;
  93.         PIR1bits.ADIF = 0;      //CLEAR interrupt flag when you are done!!!
  94.     }
  95. }
  96.  
  97. void APP_DeviceCDCBasicDemoTasks()
  98. {
  99.     /* Check to see if there is a transmission in progress, if there isn't, then
  100.      * we can see about performing an echo response to data received.
  101.      */
  102.     if( USBUSARTIsTxTrfReady() == true)
  103.     {
  104.         uint8_t i;
  105.         uint8_t numBytesRead;
  106.         /*get the data from the computer,and put in an array*/
  107.         numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));
  108.  
  109.         /* For every byte that was read... */
  110.         for(i=0; i<numBytesRead; i++)
  111.         {
  112.             switch(readBuffer[i])
  113.             {
  114.                 /* If we receive new line or line feed commands, just echo
  115.                  * them direct.
  116.                  */
  117.                 case 0x0A:
  118.                 case 0x0D:
  119.                     writeBuffer[i] = readBuffer[i];
  120.                     break;
  121.                 case 0x5A:
  122.                     putrsUSBUSART("" + value);
  123.                     Leds(value);
  124.                     break;
  125.                 /* If we receive something else, then echo it plus one
  126.                  * so that if we receive 'a', we echo 'b' so that the
  127.                  * user knows that it isn't the echo enabled on their
  128.                  * terminal program.
  129.                  */
  130.                 default:
  131.                     writeBuffer[i] = readBuffer[i];
  132.                     output = writeBuffer[i];
  133.                     //Leds(output);
  134.                     break;
  135.             }
  136.         }
  137.  
  138.         if(numBytesRead > 0)
  139.         {
  140.             /* After processing all of the received data, we need to send out
  141.              * the "echo" data now.
  142.              */
  143.             putUSBUSART(writeBuffer,numBytesRead);
  144.             //putrsUSBUSART("" + writeBuffer,numBytesRead);
  145.             //Leds(output);
  146.         }
  147.     }
  148.  
  149.     //Leds(value);
  150.  
  151.     CDCTxService();
  152. }
  153.  
  154. void Leds(int getal){
  155.     b7 = 0;
  156.     b6 = 0;
  157.     b5 = 0;
  158.     b4 = 0;
  159.     b3 = 0;
  160.     b2 = 0;
  161.     b1 = 0;
  162.     b0 = 0;
  163.  
  164.     if(getal>=128){
  165.         b7=1;
  166.         getal=getal-128;
  167.     }
  168.     if(getal>=64){
  169.         b6=1;
  170.         getal=getal-64;
  171.     }
  172.     if(getal>=32){
  173.         b5=1;
  174.         getal=getal-32;
  175.     }
  176.     if(getal>=16){
  177.         b4=1;
  178.         getal=getal-16;
  179.     }
  180.     if(getal>=8){
  181.         b3=1;
  182.         getal=getal-8;
  183.     }
  184.     if(getal>=4){
  185.         b2=1;
  186.         getal=getal-4;
  187.     }
  188.     if(getal>=2){
  189.         b1=1;
  190.         getal=getal-2;
  191.     }
  192.     if(getal>=1){
  193.         b0=1;
  194.     }
  195.     PORTBbits.RB2 = b0;
  196.     PORTBbits.RB3 = b1;
  197.     PORTBbits.RB4 = b2;
  198.     PORTBbits.RB5 = b3;
  199.     PORTBbits.RB6 = b4;
  200.     PORTBbits.RB7 = b5;
  201.     PORTAbits.RA2 = b6;
  202.     PORTAbits.RA3 = b7;
  203. }
  204.  
  205. /*You don't need to care the code below*/
  206. void APP_DeviceCDCBasicDemoInitialize()
  207. {
  208.     CDCInitEP();
  209.  
  210.  
  211.     line_coding.bCharFormat = 0;
  212.     line_coding.bDataBits = 8;
  213.     line_coding.bParityType = 0;
  214.     line_coding.dwDTERate = 9600;
  215.  
  216. }
  217.  
  218. bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size)
  219. {
  220.     switch( (int) event )
  221.     {
  222.         case EVENT_TRANSFER:
  223.             break;
  224.  
  225.         case EVENT_SOF:
  226.             /* We are using the SOF as a timer to time the LED indicator.  Call
  227.              * the LED update function here. */
  228.             APP_LEDUpdateUSBStatus();
  229.             break;
  230.  
  231.         case EVENT_SUSPEND:
  232.             /* Update the LED status for the suspend event. */
  233.             APP_LEDUpdateUSBStatus();
  234.             break;
  235.  
  236.         case EVENT_RESUME:
  237.             /* Update the LED status for the resume event. */
  238.             APP_LEDUpdateUSBStatus();
  239.             break;
  240.  
  241.         case EVENT_CONFIGURED:
  242.             /* When the device is configured, we can (re)initialize the
  243.              * demo code. */
  244.            APP_DeviceCDCBasicDemoInitialize();
  245.             break;
  246.  
  247.         case EVENT_SET_DESCRIPTOR:
  248.             break;
  249.  
  250.         case EVENT_EP0_REQUEST:
  251.             /* We have received a non-standard USB request.  The HID driver
  252.              * needs to check to see if the request was for it. */
  253.             USBCheckCDCRequest();
  254.             break;
  255.  
  256.         case EVENT_BUS_ERROR:
  257.             break;
  258.  
  259.         case EVENT_TRANSFER_TERMINATED:
  260.             break;
  261.  
  262.         default:
  263.             break;
  264.     }
  265.     return true;
  266. }
  267. //        /* If the USB device isn't configured yet, we can't really do anything
  268. //         * else since we don't have a host to talk to.  So jump back to the
  269. //         * top of the while loop. */
  270. //        if( USBGetDeviceState() < CONFIGURED_STATE )
  271. //        {
  272. //            /* Jump back to the top of the while loop. */
  273. //            continue;
  274. //        }
  275.  
  276. //        /* If we are currently suspended, then we need to see if we need to
  277. //         * issue a remote wakeup.  In either case, we shouldn't process any
  278. //         * keyboard commands since we aren't currently communicating to the host
  279. //         * thus just continue back to the start of the while loop. */
  280. //        if( USBIsDeviceSuspended()== true )
  281. //        {
  282. //            /* Jump back to the top of the while loop. */
  283. //            continue;
  284. //        }
  285.  
  286. /*******************************************************************************
  287.  End of File
  288. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement