Docstoc

Using PICAXE with Inputs Using PICAXE with Analog Inputs Using PICAXE

Document Sample
Using PICAXE with Inputs Using PICAXE with Analog Inputs Using PICAXE Powered By Docstoc
					Using PICAXE with Inputs
You can also use inputs with a PICAXE. Here we use a simple top switch block. The
switch is in a potential divider circuit so that the output of the block is normally held low
(zero), but when the switch is pressed the block’s output goes high (one). You can test
this with the PICAXE. The switch is on input pin 1.
    main:  if input1=1 then light                       ‘   if switch pressed jump
           low 4                                        ‘   make sure LED is off
           goto main                                    ‘   do it again
    light: high 4                                       ‘   new label – turn LED on
           goto main                                    ‘   check switch again

Here is another program which flashes a different LED depending on whether the switch
is pressed.
    main:   if input1=1 then flash1                     ‘ jump if input is high
            high 7                                      ‘ output 7 on
            goto flash7
    flash1: high 1                                      ‘   output 1 on
    flash7: pause 200                                   ‘   wait 0.2 seconds
            let pins=%00000000                          ‘   turn all outputs off
            pause 200                                   ‘   wait 0.2 seconds
            goto main                                   ‘   jump back to start

Using the 7 segment display can you create a circuit which counts the number of times
the switch is pressed? Here is the first part:
    init:       symbol count = b0                ‘ set up an alias
                count = 0                        ‘ start count at zero
    main:       if input1 = 1 then add1          ‘ jump if key pressed
                pause 50                         ‘ give key time to settle
                ...                              ‘ you do the rest!

Using Analog Inputs

You can use standard sensor blocks with any
PICAXE input, perhaps using a comparator to set
the switching level. However a PICAXE 18 has
analog inputs on pins 0, 1 and 2. Connecting directly
to the core block will use pin 1. If you put the
sensing block before one of the 4 key units then the
input will be on pin 2.

main:     readadc 1,b0   'read input 1 into b0
          debug b0       'send value to computer
          b1=b0/20       'change into 8 levels
          if b1<8 then skip
          b1=7
skip:     let pins=0     'turn current LED off
          high b1        'show value on 8 LEDs
          goto main



ab6a4fa0-1914-41f7-bc55-cbf1d1bdae6f.docwww.school-electronics.co.uk                    page 1
The following code reads the light sensor on pin 2 and displays the result on the
LCD display. Analog input is low resolution for PICAXE18 and high resolution for
18A.

init: pause 500
main: readadc 2,b1
      serout 4,N2400,(254,128,"Input 2 is ",#b1,"                      ") 'show number
      pause 200
      goto main

This code takes an analog input and displays it as 8 levels on LEDs.

main: readadc 1,b0                       'read input 1 into variable b0
      debug b0                           'check value on computer
      b1=b0/20                           'change into 8 levels
      if b1<8 then skip
      b1=7
skip: let pins=0                         'turn current LED off
      high b1                            'show value on 8 LEDs
      goto main

This program sets two LEDs according to the analog input.

main: readadc 1,b0                       'read channel 2 into variable b0
      debug b0
      if b0 > 90 then top                'if b0 > 90 then do top
      if b0 < 50 then bot                'if b0 < 50 then do bot
      low 1                              'else switch off 1
      low 2                              'and switch off 2
      goto main                          'jump back to the start

top:    high 1                           'switch on 1
        low 2                            'switch off 2
        goto main

bot:    high 2                           'switch on 2
        low 1                            'switch off 1
        goto main

Instead of an LDR you could use a thermistor or potentiometer input.

Using the DS18B20 Temperature Sensor

This Dallas One-Wire sensor provides the
temperature in degrees centigrade when
interrogated by the PICAXE ReadTemp instruction.
Here is a simple example displaying the result on an
LCD screen. The pcb passes 4 inputs through to a
preceding block.

loop: pause 500



ab6a4fa0-1914-41f7-bc55-cbf1d1bdae6f.docwww.school-electronics.co.uk                     page 2
        readtemp 1,b1
        if b1>127 then neg
        serout 4,N2400,(254,128,"Temp is ",#b1," ")
        goto loop
                                                                              DS18B20
neg:    let b1=b1-128
        serout 4,N2400,(254,128,"Temp is ","-",#b1,"                    ")
        goto loop

Using an Infra-red Remote Control

You can use a TSOP1836 device to receive signals
from an ordinary remote control. As long as it uses the
standard RC5 coding system then the Picaxe can
understand it using Infrain and Infra commands. The
first program turns LEDs on and off depending on
which keys on the remote are pressed. The pcb looks
more complicated than it is really because it passes
through 4 of the inputs.

loop:     infrain       ‘always     uses pin 0
          if infra =    1 then     swon1
          if infra =    2 then     swon2
          if infra =    3 then     swon3                               330R               4k7
          if infra =    4 then     swoff1                                        TSOP
                                                                                 1840
          if infra =    5 then     swoff2
          if infra =    6 then     swoff3
          goto loop

swon1: high 1
        goto loop
swon2: high 2                                                                    Key     Value
        goto loop                                                                1       1
swon3: high 3
                                                                                 2       2
        goto loop
swoff1: low 1                                                                    3       3
        goto loop                                                                4       4
swoff2: low 2                                                                    5       5
        goto loop                                                                6       6
swoff3: low 3                                                                    7       7
        goto loop                                                                8       8
                                                                                 9       9
This program takes the key press and displays it on an LCD                       P+      10
display. It would need extending to display more keys.                           0       11
                                                                                 V+      12
init: pause 500                                                                  P-      13
loop: let pins=0              ‘clear outputs
                                                                                 10+     14
      infrain                                                                    V-      15
      if infra = 1 then swon1                                                    Mute    16
      if infra = 2 then swon2                                                    Power   17
      if infra = 3 then swon3
      goto loop




ab6a4fa0-1914-41f7-bc55-cbf1d1bdae6f.docwww.school-electronics.co.uk                     page 3
swon1:       high 1
       serout 4,N2400,(254,128,"Key pressed ",#1,"                     ")
       goto loop
swon2:       high 2
       serout 4,N2400,(254,128,"Key pressed ",#2,"                     ") ‘show number
       goto loop
swon3:       high 3
       serout 4,N2400,(254,128,"Key pressed ",#3,"                     ")
       goto loop

The next program uses the P+ and P- keys (along with V+ and V-) to set the level
of two variables and display them on the LCD.
         symbol P = b0
         symbol V = b1

init: pause 500                                  'allow LCD to initialise
      serout 4,N2400,("Ready")
      P=50                                       'initial value set half way
      V=50

main: infrain
      if infra=10       then   increaseP         'P+
      if infra=13       then   decreaseP         'P-
      if infra=12       then   increaseV         'V+
      if infra=15       then   decreaseV         'V-
      goto main

increaseP:      if P=100 then display            '100 is max possible value
                P=P+1
                goto display
decreaseP:      if P=1 then display              '1 is minimum value
                P=P-1
                goto display
increaseV:      if V=100 then display
                V=V+1
                goto display
decreaseV:      if V=1 then display
                V=V-1

display:       serout 4,N2400,(254,128,"P is set at ",#b0," ")
               serout 4,N2400,(254,192,"V is set at ",#b1," ")
         goto main




ab6a4fa0-1914-41f7-bc55-cbf1d1bdae6f.docwww.school-electronics.co.uk                     page 4

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:24
posted:9/9/2010
language:English
pages:4
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!