Chapter 2 – The revelations and first successful execution


I wish to die of embarrassment now.

From my man CPW1992, from the Proton forums:

"Check yo'self before you wreck yo'self"

OK, he didn't really quote gangster rap at me, but he did suggest that it looked like I had a Goto before my LED code that was preventing the USBBuffer data from ever being used. I replied that I thought it was a mistake in my code posting, and I thought I had caught that error already. Uh, no. CPW1992 was right, my LED code was never executing because I forgot to remove the Goto command from the spot the EasyHID framework placed it. You can see this was my intent, since I placed a Goto _after_ my LED code… I just forgot to remove the first one, so my LED code never ran, so no amount of poking this board could possibly yield good results.

Well, guess what happened? (Hint: Duh)

It works.

My co-worker's kickass Macintosh USB diddler tool is just killer for this, and works without me understanding a damn thing about how it works… exactly as I had hoped. :) He has it so hard coded to my test board that all I have to do is plug it in, wait a couple seconds for it to show up, put a value from 0-255 in a little box, and hit send. After doing nothing more than commenting out the first annoying Goto I tested the board and the LED I chose lit quite satisfyingly by my imperious command. Sweeeeeeeeeet. And CPW1992 is da man of da moment in my eyes. It may have been an easy error to find, but he found it and I didn't. YOU DA MAN, CPW1992!!!! :)

Wow. I can actually move on. I didn't expect this so soon. Heh.

OK, how about the rest of the blinky code? I need to be able to figure out what a given byte is intended for, and I haven't used conditionals and comparisons in PICBASIC much at all so far, so I will enable and expand the tutorial code to turn LEDs on and off depending on byte value. As expected, I failed already. :)

In the section of code that was commented out I attempted to use some IF…THEN…ELSE logic to compare the byte I received over USB to the intended function, the idea being if I got a d16 then the 3rd LED on the PICDEM board would light. For reasons unknown to me I would get compiler errors when the other lines were uncommented, and it would claim that there was a "misplaced or incorrect ELSEIF" and was so helpful as to suggest I check its corresponding IF command. It seemed to me like this was not possible, as my syntax seemed to be correct for a single IF…THEN comparison. Just not multiple comparisons. Fortunately, I stumbled into the answer by perusing the command reference for IF…THEN.

The trick is that while this statement is valid BY ITSELF:

If USBBuffer[0] = 2 Then LED_1 = 1

This set of statements are NOT valid:

If USBBuffer[0] = 2 Then LED_1 = 1
ElseIf USBBuffer[0] = 4 Then LED_1 = 0

After pondering the way I saw example command syntax in the manual, and piddling around a bit (tweak, recompile, flash, test, lather, rinse, repeat) I finally figured it out. This is the way to represent the two command lines above:

If USBBuffer[0] = 2 Then
LED_1 = 1
ElseIf USBBuffer[0] = 4 Then
LED_1 = 0

UNBELIEVABLE!! This was not obvious to me, and not referred to in the manual in any way that I noticed. When I drop the instruction down to the next line, it works. If I put the expression and instruction on one line as part of a group of conditional tests, it fails. I don't care why. Lesson learned, move on!

I am not sure where to go from here with this tutorial. I may go back and eliminate all of the EasyHID framework and try to get the PICBASIC USB commands to work directly, as I have no doubt that this is a more versatile and powerful way of doing things rather than let EasyHID do everything for me. When I feel I have concluded this tutorial I will post a final summary.





The latest source code implements all 4 LEDs on the PICDEM FS USB board, and allows for turning any discreet LED on or off at any time.

Device = 18F4550
XTAL = 48   ;not 20 as I thought AND tested, 20 likely means CORE speed only while 48 is USB
USB_DESCRIPTOR = "USBProjectDESC.inc"
ALL_DIGITAL = true
TRISD =%00000000    'set PORTD to outputs
PORTD = 0

Symbol LED_1 PORTD.0
Symbol LED_2 PORTD.1
Symbol LED_3 PORTD.2
Symbol LED_4 PORTD.3

' USB Buffer...
Symbol USBBufferSizeMax = 8
Symbol USBBufferSizeTX  = 8
Symbol USBBufferSizeRX  = 8
Dim    USBBuffer[USBBufferSizeMax] As Byte

' some useful flags...
Dim PP0 As Byte SYSTEM        ' USBPOLL status return
Symbol CARRY_FLAG = STATUS.0  ' high if microcontroller does not have control over the buffer
Symbol ATTACHED_STATE = 6     ' is USB attached

' flash some LEDs to indicate the board works and is about to enter main USB loop
LED_1 = 0 : DelayMS 500 : LED_1 = 1 : DelayMS 500 : LED_1 = 0
LED_2 = 0 : DelayMS 500 : LED_2 = 1 : DelayMS 500 : LED_2 = 0
LED_3 = 0 : DelayMS 500 : LED_3 = 1 : DelayMS 500 : LED_3 = 0
LED_4 = 0 : DelayMS 500 : LED_4 = 1 : DelayMS 500 : LED_4 = 0
     
' ************************************************************
' * main program loop - remember, you must keep the USB      *
' * connection alive with a call to USBPoll, USBIn or USBOut *
' * every couple of milliseconds or so                       *
' ************************************************************
GoSub AttachToUSB

ProgramLoop:
   GoSub DoUSBIn
   GoSub DoUSBOut

If USBBuffer[0] = 2 Then 
LED_1 = 1
ElseIf USBBuffer[0] = 4 Then 
LED_1 = 0
ElseIf USBBuffer[0] = 8 Then 
LED_2 = 1
ElseIf USBBuffer[0] = 16 Then 
LED_2 = 0
ElseIf USBBuffer[0] = 32 Then 
LED_3 = 1
ElseIf USBBuffer[0] = 64 Then 
LED_3 = 0
ElseIf USBBuffer[0] = 128 Then 
LED_4 = 1
ElseIf USBBuffer[0] = 255 Then 
LED_4 = 0
EndIf

GoTo ProgramLoop   
   
' ************************************************************
' * receive data from the USB bus                            *
' ************************************************************
DoUSBIn:
   USBIn 1, USBBuffer, USBBufferSizeRX, DoUSBIn
   Return
' ************************************************************
' * transmit data                                            *
' ************************************************************
DoUSBOut:   
   USBOut 1, USBBuffer, USBBufferSizeTX, DoUSBOut
   Return
' ************************************************************
' * wait for USB interface to attach                         *
' ************************************************************
AttachToUSB:
   Repeat								
      USBPoll		
   Until PP0 = ATTACHED_STATE
   Return	






Back to the PICBASIC USB tutorial TOC

 
L10 Web Stats Reporter 3.15 LevelTen Hit Counter - Free PHP Web Analytics Script
LevelTen dallas web development firm - website design, flash, graphics & marketing