Refactored timer functions. Change baud of serial connection. Some minor IO changes.

This commit is contained in:
Marco 2022-06-28 02:50:01 +02:00
parent f941df08c0
commit 5114887f1c
4 changed files with 25 additions and 43 deletions

View File

@ -179,9 +179,9 @@ int cmd_handle_switch_timer(void)
static int timer1_on = 1; static int timer1_on = 1;
if (timer1_on) if (timer1_on)
timer_disableT1(); tmr_disableT1();
else else
timer_enableT1(); tmr_enableT1();
timer1_on = !timer1_on; timer1_on = !timer1_on;

View File

@ -8,15 +8,13 @@ typedef struct
int index; int index;
} timer_notify_t; } timer_notify_t;
void initOverflowTimer1(void);
void timer_initT1(void); void tmr_initT1(void);
void initCtcTimer0(void); void tmr_initT0(void);
void initOverflowTimer0(void);
void timer_subToT1Overflow(void (*)()); void tmr_subToT1Overflow(void (*)());
void timer_enableT1(void); void tmr_enableT1(void);
void timer_disableT1(void); void tmr_disableT1(void);
void timer_setT1CompareMatch(uint16_t cm); void tmr_setT1CompareMatch(uint16_t cm);
uint16_t timer_getT1Top(void); uint16_t tmr_getT1Top(void);

View File

@ -1,5 +1,4 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <math.h> #include <math.h>
@ -13,13 +12,15 @@ int main()
{ {
sei(); sei();
/* With this MCU frequency (8MHz), 12 is a baudrate of 38.4k baud */ /* With this MCU frequency (16 MHz), 25 is a baudrate of 38.4k baud */
uart_init(12); uart_init(25);
pwm_init(); pwm_init();
timer_initT1(); tmr_initT1();
tmr_enableT1();
DDRB |= (1 << DDB5); /* Arduino Leonardo has a LED that we turn off here */
DDRD |= (1 << DDD7); DDRC |= (1 << DDC7);
unsetPin(&PORTC, PIN7);
printf("Up and running.\n\r"); printf("Up and running.\n\r");

View File

@ -14,7 +14,7 @@ timer_notify_t timer1 =
}; };
void timer_initT1(void) void tmr_initT1(void)
{ {
/* /*
* We are using Timer 1 Compare Match Unit B. * We are using Timer 1 Compare Match Unit B.
@ -36,36 +36,34 @@ void timer_initT1(void)
TIMSK1 |= (1 << TOIE1); TIMSK1 |= (1 << TOIE1);
/* TOP value */ /* TOP value */
OCR1A = 50; OCR1A = 40000;
/* Compare Match with OCR1B */ /* Compare Match with OCR1B */
OCR1B = 25; OCR1B = 20000;
timer_enableT1();
} }
void timer_enableT1(void) void tmr_enableT1(void)
{ {
/* Clock prescaler of 8 */ /* Clock prescaler of 8 */
TCCR1B |= (1 << CS11); TCCR1B |= (1 << CS11);
} }
void timer_disableT1(void) void tmr_disableT1(void)
{ {
TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12)); TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12));
} }
void timer_setT1CompareMatch(uint16_t cm) void tmr_setT1CompareMatch(uint16_t cm)
{ {
OCR1B = cm; OCR1B = cm;
} }
uint16_t timer_getT1Top(void) uint16_t tmr_getT1Top(void)
{ {
return OCR1A; return OCR1A;
} }
void initCtcTimer0(void) void tmr_initT0(void)
{ {
/* Initialize counter 0 */ /* Initialize counter 0 */
TCNT0 = 0; TCNT0 = 0;
@ -85,21 +83,6 @@ void initCtcTimer0(void)
OCR0A = 255; OCR0A = 255;
} }
void initOverflowTimer0(void)
{
/* Prescaler = 0 */
TCCR0B |= (1<<CS00);
/* Enable Timer Overflow Interrupt */
TIMSK0 |= (1<<TOIE0);
}
void initOverflowTimer1(void)
{
TCCR1B |= (1<<CS11);
TIMSK1 |= (1<<TOIE1);
}
ISR(TIMER1_OVF_vect) ISR(TIMER1_OVF_vect)
{ {
for(int i = 0; i < timer1.index; i++) for(int i = 0; i < timer1.index; i++)
@ -111,7 +94,7 @@ ISR(TIMER1_COMPB_vect)
PORTD ^= (1 << PD7); PORTD ^= (1 << PD7);
} }
void timer_subToT1Overflow(void (*call)()) void tmr_subToT1Overflow(void (*call)())
{ {
if (timer1.index < NOTIFY_ARRAY_SIZE) { if (timer1.index < NOTIFY_ARRAY_SIZE) {
timer1.callOnInterrupt[timer1.index] = call; timer1.callOnInterrupt[timer1.index] = call;