Connect LEDs to pins D0 to D7 of your Arduino Uno board. Marquee lights are the blinkies you see outside of movie theatres. Patterns catch our attention.
On a real life Arduino there is a safe 5 volt pulse on each wire to light the LED. We could use a voltmeter if we delay our pulses. Oscilloscopes show the waveform.
New SketchOpen a new sketch. Replace the default code with this C language program.
#include <avr/io.h>
#include <util/delay.h>
int main() { DDRD = 0xFF; // portd is all output
while(1){ // shifts bit from PORTD.0 to PORTD.7 and back
for (int i=0; i < 8 ;i++){ PORTD = (1<<i); _delay_ms(100);}
for (int i=0; i < 8 ;i++){ PORTD =(128>>i); _delay_ms(100);}
}}
Upload and watch the on bit walk up and down the lamps. Change delay value, add repeat loops.
Alternating BitsNew sketch or change code. This makes the LEDs alternate.
#include <avr/io.h>
#include <util/delay.h>
int main() { DDRD = 0xFF; // portd is all output
while(1){ PORTD = 0x55; _delay_ms(200);
PORTD = 0xAA; _delay_ms(200); }}
Upload to your Uno board.
LEDs should blink in alternating pattern. Change delay value in sketch to change speed.
For this experiment we need to connect an oscilloscope to one of the pins of PORTD. Change the sketch to _delay_us(1) and upload. This calibrates the delay into microseconds (μs).
The oscilloscope shows our Uno board is outputting almost half a million clear pulses per second. This is close to the maximum clock speed of our board. At this speed a portion of the time is spent executing instructions and changing voltages.
New sketch or change code. This code makes the pins of PORTD do a binary count. We have other projects looking more closely at this code.
#include <avr/io.h>
#include <util/delay.h>
int main() { DDRD = 0xFF; // portd is all output
while(1){ PORTD++; _delay_ms(100); }}
Demo SketchThis code combines all of the shorter sketches we've done, so far. You can change speeds, delete lines, etc. Make your own project on binary operations.
#define reps 3
#include <avr/io.h>
#include <util/delay.h>
int main() {
DDRD = 0xFF; // PORTD is output
while (1) {
// decimal version of PORTD = 1 then PORTD = 0
PORTD = 1; _delay_ms(100); // led on
PORTD = 0; _delay_ms(100); // led off
// decimal version of PORTD = 255 then PORTD = 0
PORTD = 255; _delay_ms(100);
PORTD = 0; _delay_ms(100);
// binary version of PORTD = 1 then PORTD = 0
PORTD = 0b00000001; _delay_ms(100); // led on
PORTD = 0b00000000; _delay_ms(100); // led off
// binary version of PORTD = 255 then PORTD = 0
PORTD = 0b11111111; _delay_ms(100);
PORTD = 0b00000000; _delay_ms(100);
// hexadecimal version of PORTD = 1 then PORTD = 0
PORTD = 0x01; _delay_ms(100); // led on
PORTD = 0x00; _delay_ms(100); // led off
// hexadecimal version of PORTD = 255 then PORTD = 0
PORTD = 0xFF; _delay_ms(100); // led on
PORTD = 0x00; _delay_ms(100); // led off
// shifts single bit from PORTD.0 to PORTD.7
for (int j = 0; j < reps; j++) {
for (int i = 0; i < 8 ; i++) {
PORTD = (1 << i);
_delay_ms(100);
}
for (int i = 0; i < 8 ; i++) {
PORTD = (128 >> i);
_delay_ms(100);
}
}
// masking of bits with |= and &= operators
for (int j = 0; j < reps; j++) {
PORTD |= 0b11111111; _delay_ms(100); // mask PORTD = all 1s
PORTD &= 0b11111111; _delay_ms(100); // mask PORTD = all 0s
}
// alternate pattern
for (int j = 0; j < reps; j++) {
PORTD = 0x55; _delay_ms(100); // PORTD = 0b01010101
PORTD = 0xAA; _delay_ms(100); // PORTD = 0b10101010
}
// mathematical inverse of PORTD is put into PORTD
for (int j = 0; j < reps; j++) {
PORTD = !PORTD; _delay_ms(100);
}
}
}
MicroChip StudioMicroChip Studio is the same thing as Atmel Studio. It can import Arduino sketches but we can just compile our sketch because it is written in ordinary C language.
It will build and, if you configure an external tool you can upload directly to the Uno board. Atmel Studio also has a simulator for debugging so that you can look inside your code while it runs. Changing bits are shown in red.
Atmel Studio installs the avrasm2 assembler during installation. We can build, debug and upload this code, too. This assembly code is an attachment to the project.
Here's a minimal development board running the same code as an Arduino Uno.
Comments
Please log in or sign up to comment.