Well, usually, “The Beginning” tends to come after some trilogy, but, eh!, who cares !
I’ve been playing with Microchip PICs for quite a number of years, mostly with small-and-somehow-useless blink-a-led projects. Now, I’ve got a project I want to develop! I’ll explain the ultimate goal in the next tutorials, but today, I’ll focus on quite simple things, but which took me quite long because of the lack of doc, my low understanding of electronics, lack of time… etc…
The PIC DEM 2 board, also called DB-DP113, is a fully featured board, with a PIC 18F4520, an H44780 compatible 16×2 LCD and an USB port. Carefully reading the datasheet of the 4520, one will notice it doesn’t feature an USB connection. The board has a USB-UART converter on board. Sure provides some example code, but only in microC, so I started from scatch in JAL. JAL is a C-like language, close to Python or PHP. It all starts with some includes and configuration bits, then the actual code is very simple, thanks to the extensive JAL library. There is a lib for the LCD, for the DS18B20 (1-wire) and for the serial connection, isn’t it GREAT ?!
One tricky part is the configuration of the speed, the quartz (Xtal) is oscillating at 20MHz but for some reason, I had to declare the speed at 48MHz, and set the OSC parameter to HS_PLL. There must issues, but the whole works now (it seems that the LED is blinking too fast, while it should stay on for ~0.7 seconds).
const byte str1 = "Hello serial world" -- define a string print_string(serial_hw_data, str1) -- output string to serial lcd_cursor_position(0,0) print_string(lcd, str1) -- output string to serial -- inform user PIC is ready ! serial_hw_write("!") -- let's build our loop var byte char -- will store received char var word counter = 10 var byte str2 = " " var byte cnt = 0 var sword temperature var bit r forever loop led = on ds18b20_convert() -- tell all ds18b20 devices to start conversion delay_1ms(700) led = off r = ds18b20_s_temp(temperature) -- read conversion result if (r) then -- success, so print temperature --format_sword_dec(serial_hw_data, temperature, 5, 1) -- print temperature in xx.x format lcd_cursor_position(1,0) format_sword_dec(lcd, temperature, 5, 1) -- print temperature in xx.x format format_sword_dec(serial_hw_data, temperature, 5, 1) -- print temperature in xx.x format print_string(serial_hw_data, "\n") end if end loop
The current montage looks like that:
Note the presence of the PIC KIT 2 programming tool (the PIC DEM2 board has an ICSP connector)! The temperature is sent over the serial-usb cable and visible in the console open on my PC. It is also shown on the LCD.
- move this working code to a breadboarded PIC 18F4550 (adapt 4520 -> 4550 (USB, OSC))
- add more DS18B20 sensors on the 1-wire (handle unique IDs) and test cable lengths (>10m)
- connect an XBee to the PIC, stream over the XBee
- optimize the code/PIC sleep modes/idle to minimize power consumption
Stay tuned !