Getting Started with Freescale's FSLBOT Using CodeWarrior v7.2 and Stick OS

Created by Tony Storey, last modified by Robert Nelson on Sep 08, 2017

Getting Started with Freescale’s FSLBOT Using CodeWarrior v7.2 and Stick OS

Features

The Freescale FSLBOT is a mechatronics development platform used to demonstrate the capabilities of the ColdFire TWR-MECH dev board. The TWR-MECH board can be programmed in C/C++ or through one of two higher-level, BASIC-style coded operating systems, Stick OS or the Robot Vision Toolkit. The TWR_MECH board features:

• Sensors interface via I2C, SPI, A/D, or through GPIO
• On-board accelerometer, magnetometer, and touch sensing
• Touch sensing boards with 12 touch pads and 10 LEDs
• Four PWM controlled RC servo motors
• Programmable in C/C++ through CodeWarrior or a BASIC style language through Stick OS or the Robot Vision Tool Kit

Introduction

This getting started guide goes over what the Freescale getting started documentation doesn’t cover such as setting up the Flash Programmer within CodeWarrior v7.2, programming the MCF52259 with the Stick OS s-record file, and using a communication terminal to load the correct libraries and program the robot to walk. This getting started guide assumes the developer has already assembled the FSLBOT, installed batteries, and double checked the proper jumper configuration as outlined in the Freescale FSLBOT documentation found at http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=FSLBOT&fpsp=1&tab=Documentation_Tab.

Application

Setting up the CodeWarrior Flash Programmer

  1. The First Step is to download and install CodeWarrior for ColdFire v7.2.

  2. Next download and install Stick OS (www.freescale.com/mechbot).

  3. Connect the USB cable from the TWR-MECH board OSBDM (j17) to an open USB port on your PC.
    • Ensure the TWR-MECH jumper settings are in the default configuration, as noted in the Quick Start Guide
    • Follow the prompts to install the OSBDM driver the first time the board is connected.
    • Now CodeWarrior can be opened
    • Close the Startup pop-up window or click Start Using CodeWarrior
    • Close the Tip of the Day Window
    • Click on Tools in the upper toolbar and click Flash Programmer as seen below in figure 1
    • In the “Connection” box, select ColdFire v2-v4 JM60 OSBDM (this is found circled in red in figure 1)


    Figure 1 – Opening the Flash Programmer Utility

• In the box found under “Use Target Initialization” Browse within the ColdFire V7.2\ColdFire_Support\Initialization_Files and select MCF52259.cfg or click the Load Settings button at the bottom of the window and select the same MCF52259.cfg file. This can be seen below in figure 2 and 3 respectively.


Figure 2 – Selecting Target Initialization File


Figure 3 – Clicking the Load Settings Button to Configure Flash Programmer

• The Flash can be erased by clicking on the Erase / Blank Check within the Flash Programmer window. Simply click the Erase button as seen in figure 4.


Figure 4 – Erasing Flash Memory

• Next click Program \ Verify within the Flash Programmer window on the left.
• In the top window, Browse to the location of the downloaded, and unzipped, Stick OS file. Here you will find an s-record file (StickOS.TWR-MECH.v1.90g.elf.S19). Click on this file to point the programmer to it. See figure 5 below.


Figure 5 – Pointing the Flash Programmer to the Downloaded S-Record File

• Finally click the Program button and the Status box should read “Program Command Succeeded”.
• Next we can continue to connect the USB to the other port labeled USB OTG (j50)

  1. The fourth step in getting the FSLBOT to walk is to follow the Freescale FSLBOT Quick Start Guide to ensure the Stick OS USB driver is installed.
    • If the computer does not install the driver automatically, after connecting the USB OTG port to the PC, you will need to “Browse” to the previously downloaded Stick OS folder.

  2. Once the Stick OS driver has been installed:
    • Open a Communication Terminal, such as Hyper Terminal, and set it up as follows:
    a. Bits per second: 9600
    b. Data bits: 8
    c. Parity: None
    d. Stop bits: 1
    e. Flow control: None

Loading the Stick OS Library and Programs Through the Terminal Window

  1. Three text files have been included, one (StickOS_Library.txt) is the libraries with the Stick OS subroutines, and the other two are variations on robot walking programs. To use these files, after connecting to a Terminal program, follow the instructions at the top and bottom of each text file.
    • The command “new” will be typed into the Terminal window followed by “auto”.
    • The library file can then be cut and pasted into the Terminal window.
    • Then type “renumber10000” and “save” to save the subroutine library.
    • To load a program, type “auto”.
    • Cut and paste the program into the Terminal window.
    • Type “save” and then “run”.
    • One can type “list” or “subs” to show the current program loaded and the library subroutines. This can be seen below in figure 6, and 7 respectively.
    • Make sure the battery switch is in the on position to provide power to the servo rails.
    • Depending on the program loaded the Terminal window will provide feedback with respect to the touch-sense pads. See figure 8 for Terminal window feedback examples.

Note: After cutting and pasting libraries and programs in the Terminal window, be sure to delete the auto-generated line number at the command prompt before entering in commands. This shows up when pasting in libraries with no line numbers, the line numbers are auto-generated


Figure 6 – The “list” Command in the Terminal Window


Figure 7 – The “subs” Command in the Terminal Window


Figure 8 – Touch-Sense Feedback in the Terminal Window

Capabilities, Limitations, and Alterations

The FLSBOT and TWR-MECH board can also be programmed exclusively using CodeWarrior and C/C++ in a bare metal implementation, or using MQX, the Freescale real-time operating system.

Additional Information

Further information and documentation can be found on the Freescale website at www.freescale.com/mechbot and the Tower community at www.towergeeks.org.
The OSBDM USB drivers can be found at www.pemicro.com/osbdm.

Appendix:

The complete, Getting Started with Freescale’s FSLBOT Using CodeWarrior v7.2 and Stick OS, and Stick OS program and library files can be downloaded.

FSL_Bot.doc (638 KB)
StickOS_Library.txt

////
////	in terminal select 9600 Baud rate 8, 1, none
////	
////	type "new", type "auto",  cut and paste below library into terminal window
////
////	Taken and adapted from cpustick.com



/////////////////////// Cut below this line //////////////////////////////////////////


rem **************************************************
rem *** at25df641, 64-Mbit SPI Serial Flash Memory ***
rem **************************************************
//
sub at25df641_init
  rem call this to initialize an at25df641 SPI Serial Flash Memory
  rem you must provide a sub at25df641_cs as a callback that will
  rem manipulate the at25df641 cs* pin
  dim cmd as byte, id
  cmd = 0x9f  // Read Manufacturer and Device ID
  gosub at25df641_cs 0
  qspi cmd, id
  gosub at25df641_cs 1
  assert id!=0&&id!=-1
endsub
//
sub at25df641_status status
  rem call this to read the status register of an at25df641 SPI
  rem Serial Flash Memory
  dim cmd as byte, statusb as byte
  cmd = 0x05  // Read Status Register
  gosub at25df641_cs 0
  qspi cmd, statusb
  gosub at25df641_cs 1
  status = statusb
endsub
//
sub at25df641_chip_erase
  rem call this to erase the entire at25df641 SPI Serial Flash Memory
  dim cmd as byte, status
  cmd = 0x06  // Write Enable
  gosub at25df641_cs 0
  qspi cmd
  gosub at25df641_cs 1
  gosub at25df641_status status
  assert (status&3)==2
  cmd = 0x60  // Chip Erase
  gosub at25df641_cs 0
  qspi cmd
  gosub at25df641_cs 1
  do
    gosub at25df641_status status
  until !(status&1)
  assert !status
endsub
//
sub at25df641_4k_erase addr
  rem call this to erase a 4k page of an at25df641 SPI Serial Flash
  rem Memory starting at addr
  dim cmd as byte, a1 as byte, a2 as byte, a3 as byte, status
  cmd = 0x06  // Write Enable
  gosub at25df641_cs 0
  qspi cmd
  gosub at25df641_cs 1
  gosub at25df641_status status
  assert (status&3)==2
  cmd = 0x20  // Block Erase (4-KBytes)
  a1 = addr>>16, a2 = addr>>8, a3 = addr
  gosub at25df641_cs 0
  qspi cmd, a1, a2, a3
  gosub at25df641_cs 1
  do
    gosub at25df641_status status
  until !(status&1)
  assert !status
endsub
//
sub at25df641_read addr, data
  rem call this to read an arbitrary amount of data from an
  rem at25df641 SPI Serial Flash Memory starting at addr
  dim cmd as byte, a1 as byte, a2 as byte, a3 as byte
  cmd = 0x03  // Read Array
  a1 = addr>>16, a2 = addr>>8, a3 = addr
  gosub at25df641_cs 0
  qspi cmd, a1, a2, a3, data
  gosub at25df641_cs 1
endsub
//
sub at25df641_write addr, data
  rem call this to write an arbitrary amount of data to an
  rem at25df641 SPI Serial Flash Memory starting at addr
  dim cmd as byte, a1 as byte, a2 as byte, a3 as byte, status
  cmd = 0x06  // Write Enable
  gosub at25df641_cs 0
  qspi cmd
  gosub at25df641_cs 1
  gosub at25df641_status status
  assert (status&3)==2
  cmd = 0x02  // Byte/Page Program (1 to 256 Bytes)
  a1 = addr>>16, a2 = addr>>8, a3 = addr
  gosub at25df641_cs 0
  qspi cmd, a1, a2, a3, data
  gosub at25df641_cs 1
  do
    gosub at25df641_status status
  until !(status&1)
  assert !status
endsub
//
// ********************************************
// *** fslbot, Freescale Mechatronics Robot ***
// ********************************************
//
sub fslbot_init
  rem call this to initialize the facial LEDs of the fslbot
  gosub pca9554_init 0x38, 0xfe, 0
endsub
//
sub fslbot_right_step
  rem call this to step the right foot
  lfoot = 0  // relax left foot
  for rfoot = rfoot to 1500+flower step 10  // lean left
    sleep delay ms
  next
  for lfoot = 1500+fstart to 1500+fraise step 10  // stand left
    sleep delay ms
  next
  do   // take right step
    if rfoot>1500+ftip then
      rfoot = rfoot-10
    endif
    if rhip>1500-hshift then
      rhip = rhip-10
      lhip = rhip
    endif
    sleep delay ms
  until rfoot<=1500+fstart&&rhip<=1500-hshift
  for lfoot = lfoot to 1500 step -10  // fall back right
    sleep delay ms
  next
endsub
//
sub fslbot_left_step
  rem call this to step the left foot
  rfoot = 0  // relax right foot
  for lfoot = lfoot to 1500-flower step -10  // lean right
    sleep delay ms
  next
  for rfoot = 1500-fstart to 1500-fraise step -10  // stand right
    sleep delay ms
  next
  do   // take left step
    if lfoot<1500-ftip then
      lfoot = lfoot+10
    endif
    if lhip<1500+hshift then
      lhip = lhip+10
      rhip = lhip
    endif
    sleep delay ms
  until lfoot>=1500-fstart&&lhip>=1500+hshift
  for rfoot = rfoot to 1500 step 10  // fall back left
    sleep delay ms
  next
endsub
//
sub fslbot_stand_square
  rem call this to stand square
  rfoot = 1500, rhip = 1500, lhip = 1500, lfoot = 1500
endsub
//
sub fslbot_mouth leds
  rem call this to update the facial LEDs of the fslbot
  gosub pca9554_write 0x38, 0xfe, leds
endsub
//
rem *************************************************
rem *** mag3110, Three-Axis, Digital Magnetometer ***
rem *************************************************
//
sub mag3110_init addr
  rem initialize the magnetometer to take 10Hz measurements
  dim reg as byte, data as byte
  // make sure we're talking to the 3110
  reg = 7  // who_am_i
  i2c start addr
  i2c write reg
  i2c read data
  i2c stop
  assert data==0xc4
  // initialize the 3110
  restore mag3110_init_data
  while 1 do
    read reg, data
    if reg==255 then
      break
    endif
    i2c start addr
    i2c write reg, data
    i2c stop
  endwhile
  label mag3110_init_data
  data 0x11, 0x80  // enable automatic resets
  data 0x10, 0x69  // 10Hz
  data 255, 255
endsub
//
sub mag3110_poll addr, xa, ya, deg
  rem poll the magnetometer x and y values and return the heading
  dim reg as byte, data as byte, x as short, y as short
  reg = 1  // out_x_msb, out_x_lsb, out_y_msb, out_y_lsb
  i2c start addr
  i2c write reg
  i2c read x, y
  i2c stop
  // sign extend the x and y values
  xa = x, ya = y
  if x&0x8000 then
    xa = xa|0xffff0000
  endif
  if y&0x8000 then
    ya = ya|0xffff0000
  endif
  // calibrate the x and y values based on mix/max
  gosub mag3110_cal xa, ya
  // compute and print the heading
  gosub mag3110_heading xa, ya, deg
endsub
//
sub mag3110_heading x, y, deg
  rem compute the heading in degrees from x and y values
  dim q, xq, yq, i, s100, rad100, deg100
  // normalize x and y to the first quadrant
  if x>=0&&y>=0 then
    q = 0, xq = x, yq = y
  elseif x<0&&y<0 then
    q = 2, xq = -x, yq = -y
  elseif x<0 then
    q = 3, xq = y, yq = -x
  else
    assert y<0
    q = 1, xq = -y, yq = x
  endif
  assert xq>=0&&yq>=0
  // N.B. we use 100x scale for integers in this routine
  // our arctangent approximation only works for y<=x
  if yq>xq then
    i = 1, s100 = xq*100/yq
  else
    if xq then
      i = 0, s100 = yq*100/xq
    else
      i = 0, s100 = 0  // we must not be calibrated
    endif
  endif
  // arctangent(s) = s/(1+0.28*s^2)
  rad100 = s100*100/(100+28*s100*s100/100/100)
  if i then
    // arctangent(s) = pi/2 - arctangent(1/s)
    rad100 = 314/2-rad100
  endif
  // convert back to degrees and denormalize the quadrant
  deg100 = 9000*rad100/(314/2)
  if q==1 then
    deg100 = deg100+27000
  elseif q==2 then
    deg100 = deg100+18000
  elseif q==3 then
    deg100 = deg100+9000
  endif
  // N.B. transform magnetic north for mag orientation
  deg = (deg100/100+90)%360
endsub
//
sub mag3110_cal x, y
  rem calibrate the magnetometer min and max x and y values
  dim xmin, xmax, ymin, ymax
  // get the initial calibration values
  gosub mag3110_getcal xmin, xmax, ymin, ymax
  if x<xmin||!xmin then
    xmin = x
  endif
  if x>xmax||!xmax then
    xmax = x
  endif
  if y<ymin||!ymin then
    ymin = y
  endif
  if y>ymax||!ymax then
    ymax = y
  endif
  // set the updated calibration values
  gosub mag3110_setcal xmin, xmax, ymin, ymax
  // calibrate the caller's values
  x = x-(xmax+xmin)/2, y = y-(ymax+ymin)/2
endsub
//
rem ********************************************************
rem *** mma7455, Three Axis Digital Output Accelerometer ***
rem ********************************************************
//
sub mma7455_init addr
  rem initialize the mma7455 at i2c addr
  dim cmd as byte, data as byte
  cmd = 0x16, data = 0x01  // Mode control
  i2c start addr
  i2c write cmd, data
  i2c stop
endsub
//
sub mma7455_poll addr, x, y, z
  rem poll the mma7455 at i2c addr for the x, y, and z values
  dim cmd as byte, xb as byte, yb as byte, zb as byte
  cmd = 0x6  // 8 bits output value X
  i2c start addr
  i2c write cmd
  i2c read xb, yb, zb
  i2c stop
  x = xb, y = yb, z = zb
endsub
//
// ************************************************************
// *** mpr121, Proximity Capacitive Touch Sensor Controller ***
// ************************************************************
//
sub mpr121_init addr
  rem just follow AN3944: MPR121 Quick Start Guide
  dim i
  dim r as byte, d as byte
  i2c start addr
  for i = 1 to 0x17 step 2
    r = 0x40+i, d = 0xf
    i2c write r, d
    r = 0x41+i, d = 0xa
    i2c write r, d
  next
  restore mpr121_init_data
  do
    read r, d
    i2c write r, d
  until r==0x5e
  i2c stop
  label mpr121_init_data
  data 0x2b, 0x1, 0x2c, 0x1, 0x2d, 0x0, 0x2e, 0x0
  data 0x2f, 0x1, 0x30, 0x1, 0x31, 0xff, 0x32, 0x2
  data 0x5d, 0x4, 0x7b, 0xb, 0x7d, 0x9c, 0x7e, 0x65
  data 0x7f, 0x8c, 0x5e, 0x8c
endsub
//
sub mpr121_poll addr, bits
  rem read and return both bytes of the touch register
  dim r as byte, r0 as byte, r1 as byte
  i2c start addr
  r = 0
  i2c write r
  i2c read r0, r1
  i2c stop
  bits = r1<<8|r0
endsub
//
// ***************************************
// *** PCA9554, 8-bit I2C-bus I/O port ***
// ***************************************
//
sub pca9554_init addr, mask, input
  rem configure mask bits of the port for input
  dim cmd as byte, data as byte
  cmd = 3
  i2c start addr
  i2c write cmd
  i2c read data
  i2c stop
  cmd = 3, data = (data&~mask)|(input&mask)
  i2c start addr
  i2c write cmd, data
  i2c stop
endsub
//
sub pca9554_write addr, mask, value
  rem update mask bits of the output port to value
  dim cmd as byte, data as byte
  cmd = 1
  i2c start addr
  i2c write cmd
  i2c read data
  i2c stop
  cmd = 1, data = (data&~mask)|(value&mask)
  i2c start addr
  i2c write cmd, data
  i2c stop
endsub
//
sub pca9554_read addr, value
  rem read the input port
  dim cmd as byte, data as byte
  cmd = 0
  i2c start addr
  i2c write cmd
  i2c read data
  i2c stop
  value = data
endsub

/////////////////////// Cut above this line ///////////////////////////////////



////
////	Next type "renumber10000", type "save library"  
////	You can type "subs" to see all the subruitines in library
////	You can type "list subname" to list the BASIC lines of individual subs without
////	having to load then into the current progam

Walking_Prog.txt

////
////	This program makes fslbot walk and flash mouth leds if
////	touch sense is activated
////	The terminal feeds back the touch input
////	Type "auto" to enter program
////	Cut and paste the below program into terminal window
////
////	Taken and adapted from cpustick.com
////
////	Type "auto" and then cut and paste below code


/////////////////////// Cut below this line //////////////////////////////


// *** fslbot demo ***
  // touch mouth to blink leds
  // adjust potentiometer for walk speed
  // touch left/right cheek to take 6 steps leading from left/right
  // touch forehead to stop march
  //
  dim blink, lit  // state of the mouth LEDs
  dim right, left  // counts of steps to take
  //
 // *** initialize our modules ***
 gosub mpr121_init 0x5a
 gosub fslbot_init
 //
 // *** configure the mpr121_isr ***
 dim isr as pin irq7* for digital input inverted
 on isr do gosub mpr121_isr
 //
 // *** configure the mouth_timer ***
 configure timer 1 for 300 ms
 on timer 1 do gosub mouth_timer
 //
 // *** configure the servo pins ***
 dim rfoot as pin dtin0 for servo output
 dim rhip as pin dtin1 for servo output
 dim lhip as pin dtin2 for servo output
 dim lfoot as pin dtin3 for servo output
 //
 // *** configure the walk speed potentiometer
 dim pot as pin an6 for analog input
 //
 // *** set the walk stride variables ***
 dim delay
 dim flower, fstart, fraise, ftip, hshift
 delay = 15
 flower = 600, fstart = 150, fraise = 230, ftip = 50, hshift = 250
 //
 // *** stand square ***
 gosub fslbot_stand_square
 //
 // *** main loop -- just walk ***
 //
 while 1 do
 if left>right&&left>0 then
 // lead with the left foot
 gosub fslbot_left_step
 left = left-2
 elseif right>left&&right>0 then
 // lead with the right foot
 gosub fslbot_right_step
 right = right-2
 else
 gosub fslbot_stand_square
 endif
 delay = pot*15/1650
 endwhile
 end
 //
 sub mpr121_isr
 dim bits
 // get the touch value and respond appropriately
 gosub mpr121_poll 0x5a, bits
 if bits&128 then
 print "mouth"
 blink = !blink
 endif
 if bits&64 then
 print "forehead"
 left = 0, right = 0
 endif
 if bits&32 then
 print "left cheek"
 left = 6, right = 5
 endif
 if bits&16 then
 print "right cheek"
 right = 6, left = 5
 endif
 endsub
 //
 sub mouth_timer
 // blink all bits if we're supposed to
 if blink then
 lit = ~lit
 gosub fslbot_mouth lit
 endif
 endsub


///////////////////// Cut above this line ////////////////////////////

////
////	Type "save" to save the program
////	Type "run" to run the saved program
////

Robot_Demo.txt

//// 	*** fslbot demo ***
//// 	touch mouth to blink leds
//// 	adjust potentiometer for walk speed
//// 	touch left/right cheek to take 6 steps leading from left/right
////
////
////	Taken and adapted from cpustick.com
////
////
////	Type "auto" and then cut and paste below code

/////////////////////// Cut below this line //////////////////////////////


 10 //
 20 dim mpr121
 30  mpr121 = 0x5a // i2c address
 40 dim ioex
 50 ioex = 0x38 // i2c address
 60 //
 70 dim blink, lit // state of the mouth LEDs
 80 dim right, left // counts of steps to take
 90 //
100 // *** initialize our modules ***
110 gosub mpr121_init
120 gosub mouth_init
130 //
140 // *** configure the mpr121_isr ***
150 dim isr as pin irq7* for digital input inverted
160 on isr do gosub mpr121_isr
170 //
180 // *** configure the mouth_timer ***
190 configure timer 1 for 300 ms
200 on timer 1 do gosub mouth_timer
210 //
220 // *** configure the servo pins ***
230 dim rfoot as pin dtin0 for servo output
240 dim rhip as pin dtin1 for servo output
250 dim lhip as pin dtin2 for servo output
260 dim lfoot as pin dtin3 for servo output
270 //
280 // *** configure the walk speed potentiometer
290 dim pot as pin an6 for analog input
300 //
310 // *** set the walk stride variables ***
320 dim delay
330 dim flower, fstart, fraise, ftip, hshift
340 delay = 15
350 flower = 600, fstart = 150, fraise = 230, ftip = 50, hshift = 250
360 //
370 // *** stand square ***
380 gosub stand
390 //
400 // *** main loop -- just walk ***
410 //
420 while 1 do
430 if left>right&&left>0 then
440 // lead with the left foot
450 gosub left_step
460 left = left-2
470 elseif right>left&&right>0 then
480 // lead with the right foot
490 gosub right_step
500 right = right-2
510 else
520 gosub stand
530 endif
540 delay = pot*15/1650
550 endwhile
560 end
570 //
580 // *** walking subroutines ***
590 //
600 sub right_step
610 // step the right foot
620 lfoot = 0 // relax left foot
630 for rfoot = rfoot to 1500+flower step 10 // lean left
640 sleep delay ms
650 next
660 for lfoot = 1500+fstart to 1500+fraise step 10 // stand left
670 sleep delay ms
680 next
690 do // take right step
700 if rfoot>1500+ftip then
710 rfoot = rfoot-10
720 endif
730 if rhip>1500-hshift then
740 rhip = rhip-10
750 lhip = rhip
760 endif
770 sleep delay ms
780 until rfoot<=1500+fstart&&rhip<=1500-hshift
790 for lfoot = lfoot to 1500 step -10 // fall back right
800 sleep delay ms
810 next
820 endsub
830 //
840 sub left_step
850 // step the left foot
860 rfoot = 0 // relax right foot
870 for lfoot = lfoot to 1500-flower step -10 // lean right
880 sleep delay ms
890 next
900 for rfoot = 1500-fstart to 1500-fraise step -10 // stand right
910 sleep delay ms
920 next
930 do // take left step
940 if lfoot<1500-ftip then
950 lfoot = lfoot+10
960 endif
970 if lhip<1500+hshift then
980 lhip = lhip+10
990 rhip = lhip
1000 endif
1010 sleep delay ms
1020 until lfoot>=1500-fstart&&lhip>=1500+hshift
1030 for rfoot = rfoot to 1500 step 10 // fall back left
1040 sleep delay ms
1050 next
1060 endsub
1070 //
1080 sub stand
1090 // stand square
1100 rfoot = 1500, rhip = 1500, lhip = 1500, lfoot = 1500
1110 endsub
1120 //
1130 // *** mouth subroutines ***
1140 //
1150 sub mouth_timer
1160 dim r as byte, d as byte
1170 // blink all bits if we're supposed to
1180 if blink then
1190 lit = ~lit
1200 r = 1, d = lit
1210 i2c start ioex
1220 i2c write r, d
1230 i2c stop
1240 endif
1250 endsub
1260 //
1270 sub mouth_init
1280 dim r as byte, d as byte
1290 // configure bits for output
1300 r = 3, d = 0
1310 i2c start ioex
1320 i2c write r, d
1330 i2c stop
1340 endsub
1350 //
1360 // *** touch subroutines ***
1370 //
1380 sub mpr121_isr
1390 dim bits
1400 // get the touch value and respond appropriately
1410 gosub mpr121_poll bits
1420 if bits&128 then
1430 // print "mouth"
1440 blink = !blink
1450 endif
1460 if bits&64 then
1570 // print "forehead"
1580 left = 0, right = 0
1590 endif
1600 if bits&32 then
1610 // print "left cheek"
1620 left = 6, right = 5
1630 endif
1640 if bits&16 then
1650 // print "right cheek"
1660 right = 6, left = 5
1670 endif
1680 endsub
1690 //
1700 sub mpr121_poll bits
1710 dim r as byte, r0 as byte, r1 as byte
1720 // read and return both bytes of the touch register
1730 i2c start mpr121
1740 r = 0
1750 i2c write r
1760 i2c read r0
1770 r = 1
1780 i2c write r
1790 i2c read r1
1800 i2c stop
1810 bits = r1<<8|r0
1820 endsub
1830 //
1840 sub mpr121_init
1850 dim i
1860 dim r as byte, d as byte
1870 // just follow AN3944: MPR121 Quick Start Guide
1880 i2c start mpr121
1890 for i = 1 to 0x17 step 2
1900 r = 0x40+i, d = 0xf
1910 i2c write r, d
1920 r = 0x41+i, d = 0xa
1930 i2c write r, d
1940 next
1950 restore mpr121
1960 do
1970 read r, d
1980 i2c write r, d
1990 until r==0x5e
2000 i2c stop
2010 label mpr121
2020 data 0x2b, 0x1, 0x2c, 0x1, 0x2d, 0x0, 0x2e, 0x0
2030 data 0x2f, 0x1, 0x30, 0x1, 0x31, 0xff, 0x32, 0x2
2040 data 0x5d, 0x4, 0x7b, 0xb, 0x7d, 0x9c, 0x7e, 0x65
2050 data 0x7f, 0x8c, 0x5e, 0x8c
2060 endsub


///////////////////// Cut above this line ////////////////////////////

////
////	Type "save" to save the program
////	Type "run" to run the saved program
////

quickref.v1.90.pdf (184.2 KB)

##Comments/Questions
For questions or feedback about information on this or any other page, please go to the TechForum: TechForum