4
Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, the need for converting continuously variable input to a computer is critical to interfacing with the world of humans. The problem is that computers think only with two states, On or Off. These pieces of information are referred to as “bits”. By grouping eight bits as a single entity a “byte” is created. The memory of PicAxe is organized to remember bytes as a means of representing numbers that we normally use. Here is an example. Consider “0” to be off and “1” to be on and imagine a set of LEDs that are either on or off. 0 is represented by 00000000 as a single byte, all off. 1 is represented by 00000001 2 is represented by 00000010 3 is represented by 00000011 (or 2+1) 4 is represented by 00000100 5 is represented by 00000101 (or 4+1) 6 is represented by 00000110 (or 4+2) 7 is represented by 00000111 (or 4+2+1) 8 is represented by 00001000 Hopefully you see the pattern developing. I stead of adding a lot of lights each representing a single digit, we can think of the numerical value of the light doubling as the count moves from right to left. The numeric value for the eight lights is: 128-64-32-16-8-4-2-1 and the next digit would be 256, so (including zero) there are 256 possible values using a single byte. But what would we do about larger numbers? Easy! Just continue a byte at a time and the number rises RAPIDLY. If you try 2 to the 8 th power on your calculator you get 256. No surprise there. Try 2 to the 16 th power, or do 2x2x2 16 times to see that two bytes would be 65,536. For scary fun see how many bytes you would need to express our national debt. It surprised me! WOW. But I digress. With a single byte the PicAxe is able to change an analog input to a number between 0 and 255. Here’s a circuit that uses a simple potentiometer (or pot in the lingo). The jagged line represents a resistor and the arrow as a wiper that moves along the resistor, dividing the voltage as a value between, in this case, zero and 5 volts. PicAxe then converts the analog voltage to a digital value using:

Analog Inputs - pandprr.com Inputs.pdf · Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, ... The memory of

Embed Size (px)

Citation preview

Page 1: Analog Inputs - pandprr.com Inputs.pdf · Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, ... The memory of

Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, the need for converting continuously variable input to a computer is critical to interfacing with the world of humans. The problem is that computers think only with two states, On or Off. These pieces of information are referred to as “bits”. By grouping eight bits as a single entity a “byte” is created. The memory of PicAxe is organized to remember bytes as a means of representing numbers that we normally use. Here is an example. Consider “0” to be off and “1” to be on and imagine a set of LEDs that are either on or off. 0 is represented by 00000000 as a single byte, all off. 1 is represented by 00000001 2 is represented by 00000010 3 is represented by 00000011 (or 2+1) 4 is represented by 00000100 5 is represented by 00000101 (or 4+1) 6 is represented by 00000110 (or 4+2) 7 is represented by 00000111 (or 4+2+1) 8 is represented by 00001000 Hopefully you see the pattern developing. I stead of adding a lot of lights each representing a single digit, we can think of the numerical value of the light doubling as the count moves from right to left. The numeric value for the eight lights is: 128-64-32-16-8-4-2-1 and the next digit would be 256, so (including zero) there are 256 possible values using a single byte. But what would we do about larger numbers? Easy! Just continue a byte at a time and the number rises RAPIDLY. If you try 2 to the 8th power on your calculator you get 256. No surprise there. Try 2 to the 16th power, or do 2x2x2 16 times to see that two bytes would be 65,536. For scary fun see how many bytes you would need to express our national debt. It surprised me! WOW. But I digress. With a single byte the PicAxe is able to change an analog input to a number between 0 and 255. Here’s a circuit that uses a simple potentiometer (or pot in the lingo).

The jagged line represents a resistor and the arrow as a wiper that moves along the resistor, dividing the voltage as a value between, in this case, zero and 5 volts. PicAxe then converts the analog voltage to a digital value using:

Page 2: Analog Inputs - pandprr.com Inputs.pdf · Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, ... The memory of

readadc 4, b1 in the above example the PicAxe pin to be evaluated is “4” and the result will be stored in a memory location called “b1”. The value stored in b1 can be used in the program. Using an RC servo connected to pin 1 and a pot connected to pin 4. Main: readadc 4, b1 servo 1, 250, b1 goto main This looks like an interesting program. The servo should “follow” the turning of the pot. Of course we saw that a servo likes numbers in a 50 to 240 range, so expect bizarre stuff as the pot nears either end of its travel. But what about “words”? Microprocessors have memory limits when it comes to chaining bytes together and the 08M2 chip has a ten bit limit. Sometimes this is called a byte and a “nibble”, but I didn’t say that! That would give this chip a range of 0 to 1023, or 1024 different values. Not too bad, but we must store the result in a “word” such as w0. Since these words are made up of two bytes some of the byte storage is consumer as words are needed. It looks like this: w0 is made up of b1 and b0 w1 is made up of b3 and b2 w2 is made up of b5 and b4 and so on until we run out of bytes on 08M2 at 28 and for the older 08M it is 14. If asked to store numbers in excess of 1024 the chip behavior is erratic and unpredictable, so don’t do that! readadc10 4,w0 will convert the voltage on pin 4 to a 10 bit binary number and store it as a word. In this case it is in w0 and uses the memory locations b1 and b0. These memory locations cannot be used for storage in other parts of the program, but can be read and used as data for some other operation. Of course, if an operation changes b0 or b1 then the value that was read in originally will be altered.

Page 3: Analog Inputs - pandprr.com Inputs.pdf · Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, ... The memory of

PWM is a good Word example: The pwm command looks like this: pwmout pin, period, duty The 08M2 chip uses pin #2 for pwm. A good period number is 250 And duty could be from 0 to 1023 Using a word variable will allow a control over a large range, but remember that ta stalled motor is going to need a fair amount of voltage to get it started, so an initial duty might be about 400. A potentiometer will serve as a variable voltage divider and its center pin can be connected directly to the PicAxe if the outer pins of the pot are connected to 0 volts and +5 volts. Higher voltage will damage the chip! Build this circuit for a test of how to control a DC motor speed using a potentiometer

Page 4: Analog Inputs - pandprr.com Inputs.pdf · Analog Inputs Switches or push buttons provide a yes or no kind of input to a computer. While this is extensively used, ... The memory of