////////////////////////////////////////////////////////////////////////////////
//********************* http://www.technologuepro.com/ ***********************//
// ISET Nabeul Le 18/07/2011  --------+-------                                //
// Carte interface USB                                                        /
////////////////////////////////////////////////////////////////////////////////
program test_usb; 
uses USBdsc;
var i : byte;
const nom_carte : array[16] of byte =('U','S','B',' ','I','N','T','E','R','F','A','C','E',' ',' ',' ');
const nom_Propr : array[16] of byte =('T','E','C','H','N','O','L','O','G','U','E',' ','P','r','o',' ');
var readbuff : array[64] of char; absolute 0x500;   // Buffers should be in USB RAM, please consult datasheet
var writebuff : array[64] of char; absolute 0x540;
// entrees sorties :
var LED_1  : sbit at RC0_bit;
var LED_2  : sbit at RC1_bit;
var LED_3  : sbit at RC2_bit;
var LED_4  : sbit at RC6_bit;
var Bouton_1  : sbit at RA1_bit;
var Bouton_2  : sbit at RA2_bit;
var Bouton_3  : sbit at RA3_bit;
var Bouton_4  : sbit at RA4_bit;
// Lcd module connections
var LCD_RS : sbit at RB0_bit;
var LCD_EN : sbit at RB1_bit;
var LCD_D4 : sbit at RB5_bit;
var LCD_D5 : sbit at RB4_bit;
var LCD_D6 : sbit at RB3_bit;
var LCD_D7 : sbit at RB2_bit;
var LCD_RS_Direction : sbit at TRISB0_bit;
var LCD_EN_Direction : sbit at TRISB1_bit;
var LCD_D4_Direction : sbit at TRISB5_bit;
var LCD_D5_Direction : sbit at TRISB4_bit;
var LCD_D6_Direction : sbit at TRISB3_bit;
var LCD_D7_Direction : sbit at TRISB2_bit;
procedure Interrupt();
begin
   USB_Interrupt_Proc();
end;
begin
  ADCON1 := 0x0E;
  CMCON  :=  7;
  trisa:=0xFF;
  trisc:=0x00;
  portc:=0;
  trisb:=0;
  portb:=0;
  LED_1:=1;
  HID_Enable(@readbuff,@writebuff);
  delay_ms(1000);
  Lcd_Init();delay_ms(100);
  lcd_cmd(_LCD_CURSOR_OFF);delay_ms(100);
  delay_ms(100);
  LED_1:=0;
  while TRUE do
    begin
      while(HID_Read() = 0) do ;
      if  readbuff[0] = 'A' then LED_1:=1;
      if  readbuff[0] = 'B' then LED_1:=0;
      if  readbuff[0] = 'C' then LED_2:=1;
      if  readbuff[0] = 'D' then LED_2:=0;
      if  readbuff[0] = 'E' then LED_3:=1;
      if  readbuff[0] = 'F' then LED_3:=0;
      if  readbuff[0] = 'G' then LED_4:=1;
      if  readbuff[0] = 'H' then LED_4:=0;
      if  readbuff[0] = 'M' then
                             begin
                             lcd_cmd(_LCD_CLEAR);
                             for i:=1 to 16 do
                                   begin
                                   lcd_chr(1,i,readbuff[i])
                                   end;
                             for i:=17 to 31 do
                                   begin
                                   lcd_chr(2,i-16,readbuff[i])
                                   end;
                             end;
      if  readbuff[0] = 'Z' then
                             BEGIN
                             writebuff[0]:= (adc_read(0) shr 2);
                             writebuff[1]:= Bouton_1 + 48;
                             writebuff[2]:= Bouton_2 + 48;
                             writebuff[3]:= Bouton_3 + 48;
                             writebuff[4]:= Bouton_4 + 48;
                             while(HID_Write(@writebuff,64) = 0) do ;
                             END;
    end;
end.