Advertisement
Matthuwee

Untitled

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