• No se han encontrado resultados

I often fix students code simply by setting up the indenting and find things like this

HARD TO SPOT THE ERROR EASY TO SPOT THE ERROR

Do

For Num_flashes = 1 To 10 Set Dontcrosslight

Waitms Flashdelay Reset Dontcrosslight Waitms Flashdelay

Do

For Num_flashes = 1 To 10 Set Dontcrosslight Waitms Flashdelay Reset Dontcrosslight Waitms Flashdelay

11.7 Using variables for data

We have seen ho w a variable can be used to create a stepping pattern in program code now we see how numbers can store information. In a calculator with several memory locations each is given a name such as A,B,C,D,E,F,X,Y.M. etc. The name of the memory location has nothing to do with what you are using it for and it is up to you to remember what was stored in each location. In a

microcontroller each memory location is given a name by the programmer. This means it is much easier for you to remember what is in the memory location and easier to use within your program.

Here are some examples of using variables

Dim Width as Byte DIM is short for dimension and means set aside a part of RAM for our program to use. From now on in the program it will be called Width. It is easier for us to have names for memory locations such as ‘width’ than using the physical address of the RAM, address 1.

Dim Height as Byte Dim V_Position as Byte Dim Speed as Byte Dim X_position as Byte Dim Color as Byte Dim Mass as Byte

Here are some common things you will see in programs

Height = 10 (put 10 into the memory location we dimensioned called height) Incr X_position (increase the value in X_position by 1)

Color = Width / Height (divide the number in Width with Height and put the answer into Color - the values of Width and Height do not change)

Speed = Speed + 12 (get the number from memory location called Speed and increase it by 12 and put it back into the same memory location)

A variable of type Byte can store numbers from 0 to 255 (&B11111111) so it has limited use so often we group bytes together to store bigger numbers.

11.8 Different types of variables

Bigger numbers require more RAM than smaller numbers, also different kinds of numbers require different amounts of RAM (e.g. negative, decimals). Microcontrollers have limited RAM, so to make the best use of RAM we use the best variable type we can. If we dimension a variable as a type that can store huge numbers and only every use numbers up to 10 then we are wasting a precious resource.

Using the Bascom-AVR help file research the following information on the different types of variable you can use.

Number of bytes used to store it

Bit 0 1 1byte for 1 bit however if you

dimension 8 bits they will all be stored in the same byte

Byte 0 255 1

Every microcontroller has different amount of RAM available for storing variables Carry out research on these different AVR microcontrollers

RAM size (bytes) FLASH - program

11.9 Variables and their uses

' ShowComandsV1.bas

$sim

$crystal = 1000000

$regfile = "attiny461.dat"

Config Porta = Output Config Portb = Output Config Pinb.6 = Input

.

'dimension variables Dim Byte1 As Byte Dim Byte2 As Byte Dim Word1 As Word Dim Int1 As Integer Dim Single1 As Single Dim Single2 As Single

Allocating some parts of the RAM, and giving those parts names so that we can refer to it more easily

(dimensioning).

Byte1 = 12

Byte1 = Byte1 + 3 Incr Byte1

Byte2 = byte1

What is the value of the variable byte1 after this?

Byte2 = Byte1 / 10 Division - a byte can only represent whole numbers from 0 to 255 so division truncates (IT DOES NOT ROUND) 16/10 = 1 (whole numbers only!)

Byte2 = Byte1 Mod 10 MOD gives you the remainder of a division (16 mod 10 = 6) Byte2 = Byte1 * 150 This gives the wrong answer because a byte can only hold

a number as big as 255 Word1 = Byte1 * 150 This gives the right answer!

Int1 = 200 Int1 = Int1 – 100 Int1 = Int1 – 100 Int1 = Int1 – 100 Int1 = Int1 – 100

need negative numbers then use integer or long

For Single1 = 0 To 90 Step 5 Single2 = SQR(single1) Next

need DECIMALS use single or double

End Make sure you put an END to your program or it will continue on and potentially cause crashes (if you micro was controlling a car then it might be a car crash- ouch!!)

11.10 Vehicle counter

This program counts different vehicle types everytime a different switch is pressed, if we run it inthe simulator we can see the numbers in decimal, binary and hexadecimal

'VehicleCounterV2.bas

'test our ability to count vehicles,

'as used by someone standing at an intersection monitoring traffic flows

$crystal = 1000000

$regfile = "attiny461.dat"

Config Porta = Output Config Portb = Input

Cars_sw Alias Pinb.0 'RED switch Trucks_sw Alias Pinb.1 'YELLOW switch Bikes_sw Alias Pinb.2 'GREEN Sswitch Peds_sw Alias Pinb.3 'BLUE switch Clear_sw Alias Pinb.4 'WHITE switch

'dimension variables before they are used or you will get a compiler error!

Dim Cars As Byte Dim Trucks As Byte Dim Bikes As Byte Dim Peds As Byte

Const Debouncedelay = 25 Do

Debounce Cars_sw , 0 , Cars_sw_pressed , Sub 'red switch Debounce Trucks_sw , 0 , Trucks_sw_pressed , Sub 'yellow switch Debounce Bikes_sw , 0 , Bikes_sw_pressed , Sub 'green switch

11.11 Rules about variables

Variabes must start with a letter not a digit

e.g. Dim Red_cars As Byte not Dim 1cars As Byte Variabes must not be Bascom reserved(special) words

e.g. Dim band As Byte not Dim And As Byte Variables must contain no spaces

e.g. Dim Red_cars As Byte not Dim Red cars As Byte Variable names should relate to what the variable is used for

e.g. Dim Red_cars As Byte, not Dim hgashg As Byte

Variable names cannot be used for other things such as constants or subroutines

e.g. Dim Red_cars As Byte, means yu cannot have Const Red_cars = 12 as well

11.12 Examples of variables in use

A points table for a competition Dim Blues As Byte

Dim Hurricanes As Byte Dim Waratahs As Byte

as the season progresses the points are added.

Incr Hurricanes (adds one to their score) Blues = Blues + 1 (adds one to their score) Waratahs = Waratahs + 3

Conversions between units Dim Celcius As Integer Dim Fahrenheit As Integer

Fahrenheit = 100

Celcius = 32 - Fahrenheit Celcius = Celcius * 5 Celcius = Celcius / 9

11.13 Byte variable limitations

RAM (the memory inside a computer) is capable of storing 1 byte (or 8 bits) of binary data. This is a finite range of positive, whole numbers from 0 to 255. No negative numbers can be stored, no decimal fractions, and no number greater than 255.

Binary Number Decimal equivalent

00000000 0

00000001 1

00000010 2

--- --- ---

11111101 243

11111110 254

11111111 255

We can see the diference by comparing counting in byte math to counting in the decimal system. In the decimal system the numbers we are used go from –infinity to +infinity, so the numberline goes on forever.

Byte arithmetic because it has a finite set of numbers is like having a number line that goes around on itself.

11.14 Random Numbers

This program generates a random number from 1 to 6 and stores it into a variable in memory

‘ DiceV1.bas

$sim

$crystal = 1000000

$regfile = "attiny461.dat"

Config Porta = Output Config Portb = Input Dim dicethrow As Byte

Do

The line Dim dicethrow As Byte means allocate to the program 1 byte of ram to use and refer to it as dicethrow.

Every variable must be dimensioned before it can be used.

With variables you can do maths

E.g. add 1 to throw. dicethrow=dicethrow+1 literally means get the contents of dicethrow add 1 to it, and then put the answer back into

dicethrow.

Compile the program and then open the simulator (F2), select the variable dicethrow from the

variables list and use F8 (don’t press run) to step through the program to see the numbers generated by the program

11.15 The Bascom-AVR simulator

Press F2 to pen the simulator

Double click in the yellow area under

the word VARIABLE to

select the variables you

want to watch.

Press F8 to step through the program and see what

happens to the value of the variable at

11.16 Electronic dice project 11.17 Programming using variables – dice

A dice can be made using 7 LEDs (why do we need 7? – look closely at the patterns here)

In the above circuit the LEDs have been labelled to match the pin of porta they are connected to.

Note there is a switch connected to Pinb.6

Fill in the table below which shows which LED are on and whichare off to make a particular pattern, remember that even though only 7 LEDs are used we need to control the whole port so need to specify all 8 bits.

A.7 A.6 A.5 A.4 A.3 A.2 A.1 A.0

NO LED

LED 6 LED 5 LED4 LED3 LED2 LED1 LED0 1

2 3

4 off on off on off on off on portA=&B01010101

5 6

11.18 Dice layout stage 1

In the diagram the 7 LEDs have been physically arranged to match the dots on the face of a dice, but to do that the middle LED has had its legs bent so that it lines up with the middle LED but does not share any breadboard connections with it

11.19 Dice layout stage 2

In this second stage the resistors have been added and the wiring has been started for the LEDs,

11.20 Dice Layout final

Before the rest of the wiring for the LEDs has been added the switch has been connected, agin note that it is switch wiring that confuses students the most.

11.21 First Dice Program flowchart

' DiceV1-random.bas

' 7 leds arranged in a pattern on a breadboard

$crystal = 1000000

$regfile = "attiny461.dat"

Config Porta = Output Config Pinb.6 = Input Blu_sw Alias Pinb.6

Dim Dicethrow As Byte 'a variable to hold the value Const Dicedisplay = 80

Const Displaytime = 3 'waiting time in seconds

In this case we don’t need any debounce timing because there is a long delay after the switch is pressed.

11.23 Modified dice

In this dice the number stays on the screen and when the switch is pressed it displays 30 random numbers before stopping on the 30th

' DiceV2-random.bas

' 7 leds arranged in a pattern on a breadboard

$crystal = 1000000

$regfile = "attiny461.dat"

Config Porta = Output Config Pinb.6 = Input Set Portb.6

Blu_sw Alias Pinb.6

Dim Dicethrow As Byte 'a variable to hold the value Dim I As Byte

Const Dicedisplay = 100

Dicethrow = 1 'initial display is 1

Exercises for the dice program

1. Do a trial of at least 200 presses and draw a tally of the results, how ‘fair’ is our dice?

2. Merge the two progams above so that random numbers are displayed until the button is pressed, then 10 random numbers are generated and it stops for 5 seconds

11.24 Modified Knightrider

A neat feature for the Knightrider program would be if the speed of the sequence could be varied.

So for the same reasons as before the switches need checking often; so after each led in the sequence of LEDs, read the switches, wait a preset amount of time, if one button is pressed increase the delay time, if the other button is pressed decrease the delay time.

The switches should be checked often so that they can detect user input and I have chosen 1mS because its easy to do the maths with 1mS.

To do this we implement a loop within the program that initially begins at the value of flashdelay and counts down to 0, a second variable checkdelay is needed as a copy of flashdelay

start

Dim Flashdelay As Word Dim Led As Byte

Dim Checkdelay As word dim direction as bit

Flashdelay = 1000