Lab3A
// Lab 3A
// lightChaser.ino
// CPU used in Arduino Uno
#define __AVR_ATmega328P__
// CPU frequency in Hz.
#define F_CPU 16000000UL
// define LED sequence delay
#define SEQ_DELAY 200
#include <avr/io.h>
#include <util/delay.h>
int main (void) {
// configure PORT B PIN 0~5 as output
DDRB = (1 << DDB5) | (1 << DDB4) | (1 << DDB3) |
(1 << DDB2) | (1 << DDB1) | (1 << DDB0);
DDRD = (1 << DDD7) | (1 << DDD6) | (1 << DDD5) |
(1 << DDD4);
// forever loop
while (1) {
PORTD = (1 << PIN4); // LED 1
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN5); // LED 2
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN6); // LED 3
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN7); // LED 4
_delay_ms(SEQ_DELAY);
PORTD = 0x00; // turn off Port D
PORTB = (1 << PIN0); // LED 5
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN1); // LED 6
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN2); // LED 7
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN3); // LED 8
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN4); // LED 9
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN5); // LED 10
_delay_ms(SEQ_DELAY);
PORTB = 0x00; // turn off Port B
}
}
// lightChaser.ino
// CPU used in Arduino Uno
#define __AVR_ATmega328P__
// CPU frequency in Hz.
#define F_CPU 16000000UL
// define LED sequence delay
#define SEQ_DELAY 200
#include <avr/io.h>
#include <util/delay.h>
int main (void) {
// configure PORT B PIN 0~5 as output
DDRB = (1 << DDB5) | (1 << DDB4) | (1 << DDB3) |
(1 << DDB2) | (1 << DDB1) | (1 << DDB0);
DDRD = (1 << DDD7) | (1 << DDD6) | (1 << DDD5) |
(1 << DDD4);
// forever loop
while (1) {
PORTD = (1 << PIN4); // LED 1
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN5); // LED 2
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN6); // LED 3
_delay_ms(SEQ_DELAY);
PORTD = (1 << PIN7); // LED 4
_delay_ms(SEQ_DELAY);
PORTD = 0x00; // turn off Port D
PORTB = (1 << PIN0); // LED 5
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN1); // LED 6
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN2); // LED 7
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN3); // LED 8
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN4); // LED 9
_delay_ms(SEQ_DELAY);
PORTB = (1 << PIN5); // LED 10
_delay_ms(SEQ_DELAY);
PORTB = 0x00; // turn off Port B
}
}
Comments