Microcontroller I Seth Price Department of Chemical Engineering New Mexico Tech Rev. 9/22/14
Microcontrollers Miniature computer for a specific task Also called “embedded system” Examples: microwave, gas pump, alarm clock
BASIC Stamp 2 Microcontroller manufactured by Parallax, Inc. Programmed with a computer using PBASIC
PBASIC Program Format Microcontroller Information – Select “STAMP BS2” – Select “PBASIC Language 2.5 Declarations – Variables and constants Program END Subroutines
PBASIC Variables and Constants Variables must be declared – Variable Name, VAR, Size Variables have different sizes – Bit: a 1 or 0 – Nib: 4 bits – Byte: 8 bits – Word: 16 bits Constants must be declared – Constant Name, CON, Value
PBASIC Commands: DEBUG DEBUG: computer window will display info CR: “Carriage Return” Example Program DEBUG “Hello World!” DEBUG CR DEBUG “Welcome to PBASIC!” END
PBASIC Commands: DO Loop Executes code until a condition is met Format DO….LOOP UNTIL condition Example Program: DO DEBUG “Hello World Forever!” LOOP END
PBASIC Commands: FOR Loop Executes code a set number of times Format: FOR variable = start TO end STEP stepsize – End loop with NEXT Example Program: counter VAR word FOR counter = 1 TO 1000 DEBUG “Hello World!” DEBUG DEC counter NEXT END
PBASIC Variable Size Declaring variables sets aside memory Change “counter VAR word” to “counter VAR Byte” Run the program. What happened?
PBASIC Variable Size “WORD” is 16 bits – Has 2 16 states: to – Represent decimal numbers: 0 to (2 16 ) – 1 – This means: 0 to 65,535 “Byte” is 8 bits – Has 2 8 states: to – Represent decimal numbers: 0 to (2 8 ) – 1 – This means: 0 to 255 Counter “rolls over”, loop never reaches 1000
PBASIC Memory Press F8 to see the RAM Map There are 12 registers (16 bits), meaning: – 12 Words – 24 Bytes – 48 Nibs – 192 Bits
PBASIC Conditionals Can pick which piece of code is executed Format: IF THEN (action) ELSEIF (or ELSE) ENDIF
PBASIC IN and OUT We can pull digital data from the outside world – Format: IN – Example: IF (IN3 = 1) THEN We can push digital data to the outside world – Format: (HIGH or LOW) – Example: HIGH 14