Выбрать главу

*************************************************************************/

#include "C:\Program Files\Mikroelektronika\mikroC\Examples\EasyPic4\extra_examples\HIDlibrary\USBdsc.c"

unsigned char Read_buffer[64];

unsigned char Write_buffer[64];

unsigned char num,i;

//

// Timer interrupt service routine

//

void interrupt() {

 HID_InterruptProc(); // Keep alive

 TMR0L = 100;         // Reload TMR0L

 INTCON.TMR0IF = 0;   // Re-enable TMR0 interrupts

}

//

// Start of MAIN program

//

void main() {

 ADCON1 = 0xFF; // Set PORTB to digital I/O

 TRISB = 0;     // Set PORTB to outputs

 PORTB = 0;     // PORTB all 0s to start with

 //

 // Set interrupt registers to power-on defaults

 // Disable all interrupts

 //

 INTCON=0;

 INTCON2=0xF5;

 INTCON3=0xC0;

 RCON.IPEN=0;

 PIE1=0;

 PIE2=0;

 PIR1=0;

 PIR2=0;

 //

 // Configure TIMER 0 for 20ms interrupts. Set prescaler to 256

 // and load TMR0L to 156 so that the time interval for timer

 // interrupts at 8MHz is 256*156*0.5 = 20ms

 //

 // The timer is in 8-bit mode by default

 //

 T0CON = 0x47;      // Prescaler = 256

 TMR0L = 100;       // Timer count is 256-156 = 100

 INTCON.TMR0IE = 1; // Enable T0IE

 T0CON.TMR0ON = 1;  // Turn Timer 0 ON

 INTCON = 0xE0;     // Enable interrupts

 //

 // Enable USB port

 //

 Hid_Enable(&Read_buffer, &Write_buffer);

 Delay_ms(1000);

 Delay_ms(1000);

 //

 // Read from the USB port. Number of bytes read is in num

 //

 for(;;) // do forever

 {

  num=0;

  while (num != 4) {

   num = Hid_Read();

  }

  if (Read_buffer[0] == 'P' && Read_buffer[1] == '=' &&

   Read_buffer[2] == '?' && Read_Buffer[3] == '?') {

   TRISB = 0xFF;

   Write_buffer[0] = 'P';

   Write_buffer[1] = '=';

   Write_buffer[2] = PORTB;

   Write_buffer[3] = 'T';

   Hid_Write(&Write_buffer, 4);

  } else {

   if (Read_buffer[0] == 'P' && Read_buffer[1] == '=' &&

    Read_buffer[3] == 'T') {

    TRISB = 0;

    PORTB = Read_buffer[2];

   }

  }

 }

 Hid_Disable();

}

Figure 8.32: mikroC program listing of the project

The program checks the format of the received command. For P=?? type commands, PORTB is configured as inputs, PORTB data is read into Write_buffer[2], and Write_buffer  is sent to the PC, where Write_buffer[0]=“P,” Write_buffer[1]=“=”, and Write_buffer[3]=“T” as follows:

if (Read_buffer[0] == 'P' && Read_buffer[1] == '=' &&

 Read_buffer[2] == '?' && Read_Buffer[3] == '?') {

 TRISB = 0xFF;

 Write_buffer[0] = 'P';

 Write_buffer[1] = '=';

 Write_buffer[2] = PORTB;

 Write_buffer[3] = 'T';

 Hid_Write(&Write_buffer, 4);

}

For P=nT type commands, PORTB is configured as outputs and Read_buffer[2] is sent to PORTB as follows:

if (Read_buffer[0] == 'P' && Read_buffer[1] == '=' &&

 Read_buffer[3] == 'T') {

 TRISB = 0;

 PORTB = Read_buffer[2];

}

The microcontroller clock should be set as in Project 8.1 (i.e., both the CPU and the USB module should have 48MHz clocks). The other configurations bits should also be set as described in the previous problem.

Testing the Project 

The project can be tested using one of the methods described in the previous project. If you are using the Visual Basic program, send data to the microcontroller and make sure the correct LEDs are turned on. Then connect some of the PORTB pins to logic 0 and click the CLICK TO RECEIVE button. The microcontroller will read its PORTB data and send it to the PC, where it will be displayed on the PC screen.

The project can also be tested using the HID terminal of mikroC IDE. The steps are:

• Start the HID terminal.

• Send a command to the microcontroller to turn on the LEDs (e.g., P=1T) and make sure the correct LEDs are turned on (in this case, LEDs 0, 4, and 5 should turn on, corresponding to the data pattern “0011 0001”).

• Connect bits 2 and 3 of PORTB to logic 1 and the other six bits to ground.

• Send command P=?? to the microcontroller.

• The PC will display the number 12, corresponding to bit pattern “0000 1100”.

The Visual Basic program listing of the project is given in Figure 8.33. Only the main program is given here, as the library declarations are the same as in Figure 8.19. The program jumps to subroutine OnRead when data arrives at the USB bus. The format of this data is checked to be in the format P=nT, and if the format is correct, the received data byte is displayed in the text box.

' vendor and product IDs

Private Const VendorID = 4660

Private Const ProductID = 1

' read and write buffers

Private Const BufferInSize = 8

Private Const BufferOutSize = 8

Dim BufferIn(0 To BufferInSize) As Byte

Dim BufferOut(0 To BufferOutSize) As Byte

Private Sub Command1_Click()

 Form_Unload (0)

 End

End Sub

Private Sub Command2_Click()

 BufferOut(0) = 0          ' first byte is always the report ID

 BufferOut(1) = Asc("P")   ' first data item (“P”)

 BufferOut(2) = Asc("=")   ' second data item (“=”)