1. Use timer to print sinus of a number over serial connection.

This commit is contained in:
Marco 2022-05-27 02:21:11 +02:00
parent 180f4625a8
commit 15ff1cc339

View File

@ -1,6 +1,9 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <math.h>
#include <stdio.h>
#include "include/timer.h" #include "include/timer.h"
void initCtcTimer0(void) void initCtcTimer0(void)
@ -11,7 +14,7 @@ void initCtcTimer0(void)
/* Enable Counter0 Compare Match A Interrupt */ /* Enable Counter0 Compare Match A Interrupt */
TIMSK0 |= (1 << OCIE0A); TIMSK0 |= (1 << OCIE0A);
/* Select clock. Prescaler of 8 */ /* Select clock. Prescaler of 1024 */
TCCR0B |= (1 << CS02) | (1 << CS00); TCCR0B |= (1 << CS02) | (1 << CS00);
/* Use CTC Mode */ /* Use CTC Mode */
@ -20,7 +23,7 @@ void initCtcTimer0(void)
/* /*
* OCR0A contains TOP value for counter: * OCR0A contains TOP value for counter:
*/ */
OCR0A = 100; OCR0A = 255;
} }
void initOverflowTimer0(void) void initOverflowTimer0(void)
@ -38,7 +41,15 @@ void initOverflowTimer1(void)
TIMSK1 |= (1<<TOIE1); TIMSK1 |= (1<<TOIE1);
} }
volatile double sin_op = 0.0;
volatile uint8_t wait_cnt = 0;
ISR(TIMER0_COMPA_vect) ISR(TIMER0_COMPA_vect)
{ {
PORTB |= (1 << PB2); if (wait_cnt > 0) {
printf("%.2lf\n\r", sin(sin_op));
sin_op = ((sin_op + 0.01) > 2*M_PI) ? 0.0 : sin_op + .01;
wait_cnt = 0;
}
wait_cnt++;
} }