Skip to content
Snippets Groups Projects
Commit 249bdbe0 authored by Erik Strand's avatar Erik Strand
Browse files

Move different experiments to different files

parent d7aa12a6
No related branches found
No related tags found
No related merge requests found
Makefile 0 → 100644
# This Makefile compiles and flashes four different programs:
# - led_on
# - button_contact
# - timer_test
# - button_toggle
#
# To program a board, first make sure the PROGRAMMER variable is set correctly.
# (If you're using your own HTM(a)A programmer board, use usbtiny.) Each board
# will also have to have its fuses set once, which can be done by running
# "make program-fuses". Finally, run make program_<program_name>. For example,
# "make program_button_toggle" will build and flash the button_toggle program.
MMCU=attiny44
F_CPU = 20000000
CFLAGS=-mmcu=$(MMCU) -Wall -Os -DF_CPU=$(F_CPU)
PROGRAMMER = atmelice_isp
%.hex: %.out
avr-objcopy -O ihex $< $@;\
avr-size --mcu=$(MMCU) --format=avr $@
%.out: %.c
avr-gcc $(CFLAGS) -I./ -o $@ $<
program_%: %.hex
avrdude -p t44 -P usb -c $(PROGRAMMER) -U flash:w:$<
.PHONY: default
default: program_button_toggle
program-fuses:
avrdude -p t44 -P usb -c $(PROGRAMMER) -U lfuse:w:0x5E:m
#include <avr/io.h>
#define led_pin (1 << PB2)
#define button_pin (1 << PA7)
int main(void) {
// Configure led_pin as an output.
DDRB |= led_pin;
// Configure button_pin as an output.
DDRA &= ~button_pin;
// Activate button_pin's pullup resistor.
PORTA |= button_pin;
// Nothing left to do, so just spin.
while (1) {
// Turn on the LED when the button is pressed.
if (PINA & button_pin) {
// Turn off the LED.
PORTB &= ~led_pin;
} else {
PORTB |= led_pin;
}
}
return 0;
}
#include <avr/io.h> #include <avr/io.h>
#define switch_pin (1 << PA7)
#define led_pin (1 << PB2) #define led_pin (1 << PB2)
#define button_pin (1 << PA7)
int main(void) { int main(void) {
// Set the clock prescaler to 1. // Set the clock prescaler to 1.
...@@ -14,65 +14,36 @@ int main(void) { ...@@ -14,65 +14,36 @@ int main(void) {
// Configure led_pin as an output. // Configure led_pin as an output.
DDRB |= led_pin; DDRB |= led_pin;
// Configure switch_pin as an input. // Configure button_pin as an output.
DDRA |= switch_pin; DDRA &= ~button_pin;
// Activate switch_pin's pullup resistor. // Activate button_pin's pullup resistor.
PORTA |= switch_pin; PORTA |= button_pin;
//int count1 = 0;
//int count2 = 0;
int bouncy_switch_state = 0; int bouncy_switch_state = 0;
int debounced_switch_state = 0; int debounced_switch_state = 0;
// Nothing left to do, so just spin.
while (1) { while (1) {
// Turn on the led when the button is pressed // Use the timer to count how long it's been since button_pin changed state.
/* if ((PINA & button_pin) != bouncy_switch_state) {
if (PINA & switch_pin) { bouncy_switch_state = PINA & button_pin;
PORTB &= ~led_pin;
} else {
PORTB |= led_pin;
}
*/
// ~5s blink period based on counting
/*
if (++count1 == 255) {
if (++count2 == 255) {
PORTB ^= led_pin;
count1 = 0;
count2 = 0;
}
}
*/
// ~2.5s second blink period
// 255 * 200 * (1024 / 20M) = 2.6112
/*
if (TCNT0 >= 200) {
TCNT0 = 0; TCNT0 = 0;
if (++count1 == 255) {
PORTB ^= led_pin;
count1 = 0;
}
} }
*/
// the button toggles the led (with debouncing) // If it's been 10ms or more since PA7 changed state, // it's not bouncing.
if ((PINA & switch_pin) != bouncy_switch_state) {
bouncy_switch_state = PINA & switch_pin;
TCNT0 = 0;
}
if (TCNT0 >= 195) { if (TCNT0 >= 195) {
// It's been 10ms since the switch changed. // It's been 10ms since the switch changed.
if (bouncy_switch_state != debounced_switch_state) { if (bouncy_switch_state != debounced_switch_state) {
debounced_switch_state = bouncy_switch_state; debounced_switch_state = bouncy_switch_state;
// If the button is newly pressed, toggle the LED.
if (debounced_switch_state == 0) { if (debounced_switch_state == 0) {
PORTB ^= led_pin; PORTB ^= led_pin;
} }
} }
} }
} }
return 0;
} }
led_on.c 0 → 100644
#include <avr/io.h>
#define led_pin (1 << PB2)
int main(void) {
// Configure led_pin as an output.
DDRB |= led_pin;
// Set led_pin high.
PORTB |= led_pin;
// Nothing left to do, so just spin.
while (1) {}
return 0;
}
#include <avr/io.h>
#define led_pin (1 << PB2)
int main(void) {
// Set the clock prescaler to 1.
CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS3) | (0 << CLKPS2) | (0 << CLKPS1) | (0 << CLKPS0);
// Set the 8 bit timer's prescaler to 1/1024.
TCCR0B |= 0b00000101;
// Configure led_pin as an output.
DDRB |= led_pin;
// Blink the LED with ~2.5s half-period.
// 250 * 200 * 1024 / 20M ~ 2.5s
int count = 0;
while (1) {
// If it's been 10ms, reset the timer and increment count.
if (TCNT0 >= 200) {
TCNT0 = 0;
// When count reaches 250, reset it and toggle the LED.
if (++count == 250) {
PORTB ^= led_pin;
count = 0;
}
}
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment