Advertisement
Guest User

USB

a guest
Mar 29th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. unsigned char Read_Buffer[64] absolute 0x500;
  2. unsigned char Write_Buffer[64]absolute 0x510;
  3. unsigned char num,flag;
  4.  
  5. void interrupt()
  6. {
  7. USB_Interrupt_Proc();
  8. TMR0L = 100; //Reload Value
  9. INTCON.TMR0IF = 0; //Re-Enable Timer-0 Interrupt
  10. }
  11.  
  12. void clrUSB()
  13. {
  14. memset(Write_Buffer, 0, sizeof Write_Buffer);
  15. while(!HID_Write(&Write_Buffer, sizeof Write_Buffer));
  16. }
  17.  
  18. //LCD 8-bit Mode Connection
  19. sbit LCD_RS at RC0_bit;
  20. sbit LCD_RW at RC1_bit;
  21. sbit LCD_EN at RC2_bit;
  22. sbit LCD_D7 at RD7_bit;
  23. sbit LCD_D6 at RD6_bit;
  24. sbit LCD_D5 at RD5_bit;
  25. sbit LCD_D4 at RD4_bit;
  26. sbit LCD_D3 at RD3_bit;
  27. sbit LCD_RS_Direction at TRISC0_bit;
  28. sbit LCD_RW_Direction at TRISC1_bit;
  29. sbit LCD_EN_Direction at TRISC2_bit;
  30. sbit LCD_D7_Direction at TRISD7_bit;
  31. sbit LCD_D6_Direction at TRISD6_bit;
  32. sbit LCD_D5_Direction at TRISD5_bit;
  33. sbit LCD_D4_Direction at TRISD4_bit;
  34. sbit LCD_D3_Direction at TRISD3_bit;
  35. // End Lcd module connections
  36.  
  37. char i; // Loop variable
  38.  
  39. void UART1_Write_Text_Newline(unsigned char msg[])
  40. {
  41. UART1_Write_Text(msg);
  42. UART1_Write(10);
  43. UART1_Write(13);
  44. }
  45.  
  46. void clear_buffer(unsigned char buffer[])
  47. {
  48. unsigned int i = 0;
  49. while(buffer[i] != '\0')
  50. {
  51. buffer[i] = '\0';
  52. i++;
  53. }
  54. }
  55.  
  56. void main()
  57. {
  58. UART1_Init(9600);
  59. Delay_ms(100);
  60. UART1_Write_Text_Newline("USB Test Program");
  61.  
  62. ADCON1 |= 0x0F; // Configure AN pins as digital
  63. CMCON |= 7; // Disable comparators
  64. TRISA = 0x00;
  65. TRISB = 0x00;
  66. TRISC = 0x80;
  67. PORTB.B0 = 0;
  68.  
  69. Lcd_Init(); // Initialize Lcd8
  70. Delay_ms(100);
  71. Lcd_Cmd(_LCD_CLEAR); // Clear display
  72. Delay_ms(100);
  73. Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
  74. Delay_ms(100);
  75. Lcd_Out(1,3,"PIC18F4550"); // Write text in first row
  76. Delay_ms(100);
  77. Lcd_Out(2,3,"USB Example!"); // Write text in second row
  78. Delay_ms(2000);
  79.  
  80. INTCON = 0;
  81. INTCON2 = 0xF5;
  82. INTCON3 = 0xC0;
  83. RCON.IPEN = 0;
  84. PIE1 = 0;
  85. PIE2 = 0;
  86. PIR1 = 0;
  87. PIR2 = 0;
  88.  
  89. //
  90. // Configure TIMER 0 for 3.3ms interrupts. Set prescaler to 256
  91. // and load TMR0L to 100 so that the time interval for timer
  92. // interrupts at 48MHz is 256.(256-100).0.083 = 3.3ms
  93. //
  94.  
  95. // The timer is in 8-bit mode by default
  96. T0CON = 0x47; // Prescaler = 256
  97. TMR0L = 100; // Timer count is 256-156 = 100
  98. INTCON.TMR0IE = 1; // Enable T0IE
  99. T0CON.TMR0ON = 1; // Turn Timer 0 ON
  100. INTCON = 0xE0; // Enable interrupts
  101.  
  102. //
  103. // Enable USB port
  104. //
  105.  
  106. if(PORTA.B0 == 1)
  107. {
  108. UART1_Write_Text_Newline("Data is Ready to be Received from the PC");
  109. Hid_Enable(&Read_Buffer,&Write_Buffer);
  110. UART1_Write_Text_Newline("USB connected, waiting for enumeration...");
  111. Delay_ms(2000);
  112. UART1_Write_Text_Newline("OK");
  113. PORTB.B0 = 1;
  114. }
  115.  
  116. // Read from the USB port. Number of bytes read is in num
  117.  
  118. start:
  119.  
  120. while(Hid_Read() == 0); //Stay Here if Data is Not Coming from Serial Port
  121. //If Some Data is Coming then move forward and check whether the keyword start is coming or not
  122. if(strncmp(Read_Buffer,"S",1) == 0)
  123. {
  124. Lcd_Cmd(_LCD_CLEAR);
  125. Lcd_Out(1,2,"Authentication");
  126. Lcd_Out(2,8,"OK");
  127. UART1_Write_Text_Newline("USB Authentication - OK");
  128. UART1_Write_Text_Newline("USB enumaration by PC/HOST");
  129.  
  130. goto loop;
  131. }
  132. else
  133. {
  134. Lcd_Cmd(_LCD_CLEAR);
  135. Lcd_Out(1,2,"Authentication");
  136. Lcd_Out(2,5,"Fails!");
  137. UART1_Write_Text_Newline("USB Authentication - Fails!");
  138.  
  139. goto start;
  140. }
  141.  
  142. loop:
  143.  
  144. //Now Authentication is Successfull Lets Try Something else
  145. //Lets Display the Data Coming from the USB HID Port to the LCD
  146. Delay_ms(1000);
  147. Lcd_Cmd(_LCD_CLEAR);
  148. Lcd_Out(1,1,"Received Data-");
  149. flag = 0;
  150.  
  151. loop_second:
  152.  
  153. clear_buffer(Read_Buffer);
  154. while(Hid_Read() == 0)
  155. {
  156. if(flag == 0)
  157. {
  158. Lcd_Out(2,1,"No Data");
  159. flag = 1;
  160. }
  161. }
  162.  
  163. Lcd_Cmd(_LCD_CLEAR);
  164. Lcd_Out(1,1,"Received Data-");
  165. Lcd_Out(2,1,Read_Buffer);
  166.  
  167. strcpy(Write_Buffer, "Received Data-");
  168. strcat(Write_Buffer, Read_Buffer);
  169. while(!HID_Write(&Write_Buffer, 64)) ;
  170. UART1_Write_Text_Newline(Write_Buffer);
  171. clrUSB();
  172.  
  173. if(strncmp(Read_Buffer,"Disable",1) == 0)
  174. {
  175. Lcd_Cmd(_LCD_CLEAR);
  176. goto end;
  177. }
  178.  
  179. goto loop_second;
  180.  
  181. end:
  182.  
  183. UART1_Write_Text_Newline("USB disconnected, waiting for connection...");
  184. Delay_ms(1000);
  185. Hid_Disable();
  186. Lcd_Out(1,1,"HID DISABLE");
  187. UART1_Write_Text_Newline("OK");
  188. PORTB.B0 = 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement