feat(ex01): ex01 is complete
This commit is contained in:
parent
49164a0c18
commit
7eb2daf73f
5 changed files with 129 additions and 0 deletions
44
ex01/main.c
Normal file
44
ex01/main.c
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
#include <avr/io.h>
|
||||
#include "mystd.h"
|
||||
#include "utils.h"
|
||||
|
||||
#define D1 PORTB0
|
||||
#define D2 PORTB1
|
||||
#define D3 PORTB2
|
||||
#define D4 PORTB4
|
||||
|
||||
#define PRESCALER 256
|
||||
#define TIMER_FREQ (F_CPU / PRESCALER)
|
||||
|
||||
|
||||
// at a high level:
|
||||
// Set the OC1B (PB2) pin as output
|
||||
// set the TIMER1 mode to COMPARE (CTC)
|
||||
// say to compare against OC1A
|
||||
// set the value to be compated at X count
|
||||
// say the presacler for the timer is 512
|
||||
//
|
||||
// all these information are on page ~140
|
||||
void timer1_init(void) {
|
||||
// Set PB2 (OC1B) as output
|
||||
DDRB |= _BV(PB2);
|
||||
|
||||
// CTC mode (WGM12 = 1)
|
||||
TCCR1B |= _BV(WGM12);
|
||||
|
||||
// Toggle OC1B on compare match (COM1B0 = 1)
|
||||
TCCR1A |= _BV(COM1B0);
|
||||
|
||||
// Set compare values
|
||||
OCR1A = TIMER_FREQ / 2;
|
||||
|
||||
// Start timer with prescaler 256 (CS12)
|
||||
TCCR1B |= _BV(CS12);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
timer1_init();
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue