Hexbug Hack Chapters
(Updated for New HXB27e
PC board)


CH1-Possibilities
  CH2-Whats Inside  
  CH3-Controller Design  
CH4-Assembly
CH5-Control Software
CH6-Testing, Programming
& Final Assembly
CH7-Downloads
CH8-Upgrades
     

Hexbug Hack
Give your Hexbug a Real Brain!
Chapter 5 - Control Software -

As mentioned earlier, the PIC microcontroller chip was chosen for this project because of the excellent support, availability, and low cost from Microchip. Microchip also provides a free software development package for it called MPLAB. (It’s truly FREE, not just for 30 or 60 days as some companies’ evaluation packages are.) Both the program and user’s guide can be downloaded from their site. You will have to buy the programmer though.

Any one of these tools will be suitable for programming the bug. They are a very good value for the money. (There are also third party suppliers of programmers.)

Current as of 3/23/10
Tools-    
From Microchip Direct, Mouser, and Digikey:    
DV164120   PICkit-2 Starter Kit   $49.99
PG164120   PICkit-2 Programmer Only   $34.99
DV164101   PICkit-1 8/14P Flash Starter Kit   $36.00
DV164121   PICkit-2 Debug Express   $49.99
All these packages include the MPLAB IDE software CD.

My personal recommendation is the PICkit-2 Starter Kit. Besides the programmer module and MPLAB software, the kit includes a demo PC board with a PIC16F690 chip, A series of 12 Lessons on assembly programming including source files, and a HI-TECH PICC™ LITE ‘C’ Compiler. With the additional software provided, the programmer module can also be used to analyze serial communications and as a three channel logic analyzer.

More information about MPLAB can be found HERE.
More information about the PICkit-2 can be found HERE.

If you are not familiar with the PIC assembly language and would like to get started, there is a good, free online tutorial, “PIC Assembly Language for the Complete Beginner” by Michael A. Covington at: http://www.covingtoninnovations.com/noppp/picassem2004.pdf

Why write in assembly language? Freedom. Though you have to handle all the details yourself, once they are set up, you can “have it your way”. Basic, C, and other high level languages do take care of a lot of details for you, but in relatively simple projects, there aren’t that many inter-connected details that need it.

Using assembly language with PIC chips is more like ‘writing hardware’. The PIC’s instruction set is made of a few (35) small, fast efficient instructions out of which bigger function can be made. These then can be re-used. It’s a little like creating your own ‘higher level’ project-specific language.

Current as of 3/23/10
  www.Microchipdirect.com
www.Mouser.com
www.Digikey.com

This implementation uses the PICkit-2 programming module. As this is a first/test version, the software for the Bug is very basic, and does not use interrupts so they are disabled in the initialization routine. Future versions may use them- this is just a starting point. The code is broken up into several sections- Initialization, Subroutines, and Behavior.

(If you create a new, or modified program and Bug behavior, please e-mail it and its ‘personality’ description so it can be posted for others to try. If you wish, your contact information will be posted with it)

Initialization Details-

To conserve power, the A/D converter and other analog functions are disabled. If the ANSEL (Analog-Select) register is not cleared, ports GP0,1,2 & 4 will be in analog input mode, and will read digitally as a ‘0’. The watchdog timer is enabled to provide its maximum timeout of 2.3 seconds. By using the internal oscillator two pins are freed up. It runs at 4 mhz. The master clear pin (MCLR) is assigned to be used as a digital input, and here is a tricky detail that can be easily over looked when also using the ICSP function.( See- http://support2.microchip.com/
KBSearch/KB_StdProb.aspx?ID=SQ6UJ9A003LD4
on microchip’s website for more details.)

The MCLR pin is used by the PICkit-2 to start the programming cycle. When the chip is programmed for the first time, the MCLR pin will be in the default master clear mode. On the next programming cycle, the previously installed initialization routine reprograms the pin as a digital input before the PICkit-2 programmer can put it into programming mode. The programming cycle will fail, and it might appear that the chip is bad. The only way out of this problem is to erase the whole chip, then reprogram it. You did remember to read and save internal clock’s factory calibration value didn’t you?

There is another way around this problem. A small delay is inserted into the code before the initialization routines are run. This will allow the PICkit-2 to gain control of the MCLR pin before it can be changed.

Subroutines-

With the initialization details completed, the next elements needed are the subroutines. Some of these are made from lower level ones. Their details can be found in the assembly code listing, so the following is a simplified list of the major ones, and their functionality:

Sound Effect Sub.s; When called, they configure and use the piezo-buzzer-
UhOh1 Sounds like- “Uh-Oh”
OhOhOh   Distress
OhUh1   Sounds like- "OK?"
Trlla   Trill-A
Trllb   Trill-B
Trllup   Sweep Trill-Up
Trlldwn   Sweep Trill-Down
Chips1   Seven, quick, short “Chip” sounds
Chips2   Twenty, quick, short “Chip” sounds
BEEP   256 Cycles at Freq. set in 'TONE'; duration passed in 'W'. Base for all tones. BEEP turns ON needed I/O, then turns it OFF when done.
Motor Sub.s; These configure the ports, and delay the On/Off switch to prevent ‘shoot-thru’-
Fwrd   Motor ON, Forward
Rvrs   Motor ON, Reverse
STOP   Motor OFF, Forward
Utillity Sub.s
BattStat   Returns battery status value in ADRESH, & ADRESL by reading the voltage reference value using the 10 bit A/D converter. The A/D converter's reference voltage is the supply voltage. As battery voltage drops, the voltage reference chip’s value appears to rise. Sets up the ADC using Vcc as reference voltage. Turns on Vref (GP0), and measures AN-1. If Vcc is 2.5VDC, then 1.22 Vref will read as 47.84% of the ADC. With a Max count of 1024, 490 = Vcc = 2.5vdc. At a min battery voltage of 2.2, the ADC would read Vref as 568.
PhotoStat   Read LED used as solar cell, Returns Value in 'W'- operation is similar to BattStat
PwrDwn   Puts PIC into lowest power state before sleep.
Delays; The delay count is passed in 'W' register. It must be set befoer calling these subs-
DLY_S   10uSec per count
DLY_L   2.5mSec per count
DLY_X   100mSec per count
Output-
DeciDig   Convert value passed in variable 'OutByt' to 3 Decimal Digits and output as 3 sets of beeps. If Zero, sound 1 low tone, else sound a high tone beep to equal the count.
This routine can be used to output the BattStat value

The details of these subroutines can be found in the assembly code listing.

[Click HERE to down load the basic assembly code .ASM file]
[Click HERE to down load the basic assembly code .HEX file]

Main Code: Test & Personality-

Now for the main ‘personality’ code- For this version, the first thing the Bug should do is test its systems. These should not start until we want them to, so the first part will be to blink the LED and wait for the antennae to be touched. It will then make some sounds, turn the LED On then Off, run the motors forward, reverse and forward again, turn the LED On then Off again, output its battery voltage level as a series of tones, and go back to blinking the LED and waiting for the antennae to be touched. Here is a sample of the code; ( Note that by having created subroutines first, 23 of the 36 instructions are either a subroutine call, or a ‘MOV’e the Literal value into the ‘W’orking register,”MOVLW” )

Test:
Configure I/O
BSF
MOVLW
MOVWF
BCF
STATUS,RP0
b'11001000'
TRISIO
STATUS,RP0
; Set Bank 1 <<--+--###
;Init. I/O- Motor, LED/Vref PWR, Piezo
; Set Port A I/O: 1= Input, 0= Output
; Set Bank 0 <<--+--###
Wait for Antennae bump
TstLp1:        
MOVLW
CALL
BSF
MOVLW
CALL
BCF
BTFSC
  d'10
'DLY_X
GPIO,1
d'2'
DLY_L
GPIO,1
GPIO,3
  ;Wait 1 sec
;100ms/Cycle
;LED On
;Wait 100 msec
;@ 2.5mS per count = 5ms
;LED Off
;Loop until Antennae is touched..
GOTO
  TstLp1
Test Sounds
CALL
MOVLW
CALL
CALL
MOVLW
CALL
  Trlldwn
d'10'
DLY_X
Trllup
d'10'
DLY_X
  ;Down-Sweep Trill
;Wait 1 sec
;100ms/Cycle
; -Sweep Trill
;Wait 1 sec
;100ms/Cycle
Test Battery
;Read Battery Voltage level-    
CALL
MOVF
MOVWF
  BattStat
ADRESH,W
OutByt
  ;Get voltage level
;Save for conversion...
;Convert value passed in ‘OutByt’ to 3 Decimal Digits:
CALL
MOVLW
CALL
  DeciDig
d'10'
DLY_X
  ;Output value as 'Beeps'
;Wait 1 sec
;100ms/Cycle
;Signal end of testing
CALL
CALL
MOVLW
CALL
  Note_G2
Note_E2
d'10'
DLY_X
 

;Wait 1 sec
;100ms/Cycle
Wait for Antennae bump
OprLP:
       
MOVLW
CALL
BSF
MOVLW
CALL
BCF
BTFSC
  d'10'
DLY_X
GPIO,1
d'2'
DLY_L
GPIO,1
GPIO,3
  ;Wait 1 sec
;100ms/Cycle
;LED On
;Wait .1 sec
;@ 2.5mS per count = 5ms
;LED Off
;Loop until Antennae is touched..
GOTO   OprLP
   
Personality Program Goes Here
         

PERSONALITY-1:

The next block of code is the bugs ‘personality’. Look through the code listing for more details. Here is a basic outline:

Main Loop>>
Start walking, start timer
If Antenna bump-
Stop; Count bumps; after 5th bump, complain
Reverse turn;
After 10 bumps, throw a ‘tantrum’-
Move back and forth with sounds;
Sleep for 5 seconds
If no bumps occur for 30sec, celebrate-
Reverse circle with sounds
Sleep for 10 seconds
Wake; reset counters;
Go back to Main Loop-

Some simple additions coming in the near future:
1) Monitor the battery voltage and have the Bug ‘cry’ when its batteries get low.
2) Have the bug respond to light levels- The PIC chip could be put into lowest power ‘Sleep’ mode and would be awakened by the Watch Dog Timer (WDT). If the light level was high enough, the bug would start wandering around. When the light level dropped, the bug could go back into ‘Sleep’ mode.

 


Taking Ideas into Reality
  Edison was very insightful in his definition of genius. There are lots of good ideas and inspirations, but all by themselves they have little value. It takes effort and insight, tools, materials, and lots of time to bring them into solid existence. Applied Inspirations, LLC was founded to assist in bringing new ideas into physical reality by providing:
Examples - Tutorials and projects that demonstrate ideas taken from concept to reality.
Tools - PC boards, software, hardware and kits to explore turning new ideas into products.
Services - For those without the skills, tools or time, we provide services to develop part of, or the whole idea.