-
assignment
- make serial communication and parallel programming cables, then modify the serial hello-world code to blink the LED and transmit a message when the button is pressed.
-
the cables
-
I built the serial and parallel port cables following the instructions that Neil gave us in class. David was around to help me start testing the serial cable with a voltmeter. One of the wires didn't catch properly the first time, but we found that with an exacto knife it was possible to take the cap off the parallel port cable so I could move the wires. I ended up having to start over anyway, though, because I had swapped two of the connections and broke the cap the next time I tried to get it off.
Making the serial cable was more difficult because there was no way to take the cap off the bed of pins, so I just had to hope everything was aligned properly before pushing in the wire. It took several tries for me to get it right, but in the end I did have a working cable.
-
-
writing assembly
-
In the spring last year I took a microcontroller lab, where most of the programming was in assembly for the 8051 chip family. Most of the time I spent writing code for this assignment was reading the ATTiny45 datasheet to figure out the names of the instructions I wanted to use, and also how to set up the PORTB pins as inputs and outputs.
For reference, here's my connection diagram for the ATTiny45 from last week:
The switch button is connected to PB0 while the LED is connected to PB1. The first thing I did was rename each of these to something that would be easier to read:
.equ switch_pin = PB0 ; pin connected to switch .equ led_pin = PB1 ; pin connected to LED
I set up PB1 to be an output with an internal pull-up, the same way that PB2 had been set up to be the TX pin. For PB0, I set it to be an input with an internal pull-up.
ldi temp, (1<<tx_pin)|(1<<led_pin)|(1<<switch_pin) ; internal pull-up = 1 out PORTB, temp ldi temp, (1<<tx_pin)|(1<<led_pin) ; output pins = 1, others = 0 (default) out DDRB, temp
When someone presses the switch, the value read from PB0 is low, because the pin is connected to ground. The main loop of the program checks to see if the input from the switch is low, and if it is, it jumps it skips over a branch call that would turn off the LED. If it skips the branch instruction, it then clears the pin connected to the LED to turn on the light (note that the LED is connected to 5V externally, so setting PB0 low allows current to flow through the LED). It also transmits the message "Hello World" over serial before starting the loop over again. Here is the code for the main loop:
loop: sbic PINB, switch_pin ; skip next line when button pressed rjmp light_off ; branch to turn light off cbi PORTB, led_pin ; set LED pin low to turn light on print message message: .db "Hello World!",10,0 rjmp loop ; start loop again. light_off: sbi PORTB, led_pin ; output high to turn light off. rjmp loop ; start loop again.
-
-
the product
-
When the button isn't pressed:
When the button is pressed:
-