USB-CDC - Using keyboard to switch a LED on/off
I decided to test the USB-Serial (USB-CDC) library but I had nothing special prepared. So I made a small program to blink a LED from PC keyboard using a serial terminal. A more advanced project will have his own PC software host. This tutorial will involve installing a driver for the virtual serial communication port.
Board compatibility
Schematic: Compatible with all FreeJALduino boards.
Firmware: Compatible with FreeJALduino and FreeJALduino5 boards
Set the board
So, let's start by setting the board. Because is about a LED only, we can use USB 5Vcc as power source so set the jumper accordingly. Or you can let it on external power and connect the jack after you finished setting the board. (The last versions of FreeJALduino and FreeJALduino MEGA boards have incorporated a testing LED - didn't made them yet...)
The resistor is 330R. Connect the USB cable.
The Program
Here is the jal program:
include freejalduino4 -- target PICmicro
-- include libraries
include usb_serial
include print
enable_digital_io() -- disable analog I/O (if any)
alias led is pin_A0
pin_A0_direction = output
led = off
const byte str1[] = "Press [o] and [p] buttons \r\n"
const byte str2[] = "to switch the LED on/off \r\n"
-- initialize the USB serial library
usb_serial_init()
-- main loop
forever loop
var byte ch
-- Service USB, call on a regular base to keep communcaiton going
usb_serial_flush()
-- check for input character
if usb_serial_read( ch ) then
if ch == "?" then
print_string( usb_serial_data, str1 )
print_string( usb_serial_data, str2 )
elsif ch == "o" then
led = on
elsif ch == "p" then
led = off
else
usb_serial_data = ch
end if
end if
end loop
--
Compile it and upload the hex file onto board. After launching the program from Pdfsusb program (or automatically started after 10 seconds from board reset), the Windows will detect the new device.
Using Communication Terminal Tool from JalEdit
If you still have the Jaledit running (if not, start it), launch the mini term tool from "Tools/Comm. Terminal..." main menu option. It look like this:
Press "Serial Settings" button from right side and set the port and baudrate as here, and press "OK" button:
Press "Terminal" button from right side and set the rows to 10, as you see here, and press "OK" button:
Now you are ready! Press "Connect" button and you can start playing with the board. You can press "o" letter from your PC keyboard to light the LED and "p" letter to switch off the LED. Also you can press "?" to obtain a help message on your terminal screen as here:
When you get bored, press "Disconnect" button from terminal, close terminal, and prepare the board for another project. Have Fun!
Running the application
Not much of use but this kind of approach is similar to Firmata and UBW Boards... or as here, a Pinguino example. Anyway, we just tested the USB Serial library from Jallib package!