Sunday, 19 February 2012

Code for capacitive sensing

Following on from an earlier post, introducing capacitive (touch) sensing, here's the Oshonsoft PIC Basic Code to read three pads, connected to PORTB.2, PORTB.3, PORTB.4

Define CLOCK_FREQUENCY = 20
Define CONFIG1L = 0x24
Define CONFIG1H = 0x0c
Define CONFIG2L = 0x3e
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x01 '81=use MCLR, 01=pin1 is input
Define CONFIG4L = 0x80
Define CONFIG4H = 0x00
Define CONFIG5L = 0x0f
Define CONFIG5H = 0xc0
Define CONFIG6L = 0x0f
Define CONFIG6H = 0xe0
Define CONFIG7L = 0x0f
Define CONFIG7H = 0x40

Dim tmpinput1 As Byte
Dim wd As Byte

AllDigital
Config PORTA = Output
Config PORTB = Input
tmpinput1 = 0
wd = 0

init:
       WaitMs 200
     
       UsbSetVendorId 0x1099
       UsbSetProductId 0x1099
       UsbSetVersionNumber 0x1001
       UsbSetManufacturerString "Nerd Club"
       UsbSetProductString "Capacitive board game sensor test"
       UsbSetSerialNumberString "1234567890"
       UsbOnIoInGosub usbonioin
       UsbOnIoOutGosub usbonioout
       UsbOnFtInGosub usbonftin
       UsbOnFtOutGosub usbonftout
       UsbStart
            

loop:
       UsbService
     
       'turn portB into outputs
       Config PORTB = Output
       PORTB = 0xff
     
       'wait a bit to settle
       WaitUs 1
     
       'turn portb to inputs
       Config PORTB = Input
     
       'wait a bit to settle
       WaitUs 1
     
       'read which pins are still high
       '(these should be the ones with coins over them)
       tmpinput1 = PORTB
       If tmpinput1.3 = 1 Then High PORTA.4 Else Low PORTA.4
       If tmpinput1.4 = 1 Then High PORTA.3 Else Low PORTA.3
       If tmpinput1.5 = 1 Then High PORTA.2 Else Low PORTA.2
     
       Goto loop
End
     
usbonioout:
       'received data from PC
       Select Case UsbIoBuffer(7)
     
              Case 1
                          
              '---------------------------------------------
              Case 252 'write a value to onboard PIC eeprom
              '---------------------------------------------
              Write UsbIoBuffer(0), UsbIoBuffer(1)
            
       EndSelect
Return

usbonioin:
       'send data back to PC
       UsbIoBuffer(0) = tmpinput1
       UsbIoBuffer(7) = wd
       wd = wd + 1
Return

usbonftin:

Return

usbonftout:

Return


This code reads whether a touch is detected over each of the pads and lights up a corresponding LED on PORTA.2, PORTA.3, PORTA.4. It also sends the value of the input pins on PORTB to a USB client (so the board game can be connected to a PC and data read in that way too, for future development).

(For our trials, we concentrate on just the LED outputs).

Here's a video showing how the LEDs light as a playing piece (metallic disc) is picked up and put down on the playing board. Note that the LEDs flash in exactly the same way as when the playing piece is stationary and momentarily touched by the player:

1 comment:

  1. hi

    What is your compiler name?
    Is this code running on pic16f777 or ...?

    please guide me !

    thank you
    Mohammad Alizadeh

    ReplyDelete