Advertisement
Matthuwee

Untitled

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