atmega328p ctc mode timer - Page 1
Forum section: electronics / general

author: altruan23
author_rank: Contributor
timestamp: October 31, 2021, 07:20:35 pm
content: datasheet:https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfSo i wanted to make a timer on the atmega328p µC using the CTC Modus. The idea was, that every 10milliseconds when the interrupt function is called , in that function i schould increase a variable millisekunden by 10. and once it reaches 1000 it schould be printed out . The datasheet can be found here:https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdfWith registers i can change the Mode to the CTC Mode and set up the right Prescaler on the timer. It is a 16Mhz CPU. So the formula is : T_clock * Prescaler * OCR0A = time( unit is seconds) So i calculated: (1/ 1610^6) * 1024 * x = 1010^-3(i wanted 10 milli seconds). and x is then 155. With the bits CS00 and CS02 i set up the prescaler to 1024. OCR0A was then set to 155 as the formula says. CTC mode was enabled by setting the BIT WGM01. And the last thing was that i increase the variable millisekunden in the interrupt function. For some reason it doesnt want to work. Can anyone pease help me?code is here: #include "Arduino.h"volatile unsigned long int millisekunden;unsigned long int last_msg;char buffer;void setup() {TCCR0A |= (1 << WGM01); // CTC ModusTCCR0B |= (1 << CS02) | (1 << CS00); // Prescaler 64OCR0A = 155;// Compare InterruptTIMSK0 |= (1 << OCIE0A);Serial.begin(9600);}void loop() {if (millisekunden - last_msg >= 1000) {sprintf(buffer, "t=[%lu]", millisekunden);Serial.println(buffer);last_msg = millisekunden;}}// Timer-Interrupt-RoutineISR(TIMER0_COMPA_vect) {millisekunden = millisekunden + 10;}

author: kripton2035
author_rank: Super Contributor
timestamp: October 31, 2021, 09:03:00 pm
content: timer0 is used by the arduino system. you shouldn't use it for your code. use timer1 or 2 insteadCode:// TIMER 1 for interrupt frequency 100 Hz:cli(); // stop interruptsTCCR1A = 0; // set entire TCCR1A register to 0TCCR1B = 0; // same for TCCR1BTCNT1 = 0; // initialize counter value to 0// set compare match register for 100 Hz incrementsOCR1A = 19999; // = 16000000 / (8 * 100) - 1 (must be <65536)// turn on CTC modeTCCR1B |= (1 << WGM12);// Set CS12, CS11 and CS10 bits for 8 prescalerTCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);// enable timer compare interruptTIMSK1 |= (1 << OCIE1A);sei(); // allow interrupts

author: altruan23
author_rank: Contributor
timestamp: October 31, 2021, 09:16:49 pm
content: Hello kripton2035. Thank you very much for your answer. Unfortunately I still have the same problem. So i copied your code and created the Interruptservice routine function : ISR(TIMER1_COMPA_vect) . In that function i increase the variable millisekunden always by 10. so after it reaches 1000, it schould beprinted. but it doenst work. i dont know why. our teacher told us that we need to solve this exercise with the ISR.

author: altruan23
author_rank: Contributor
timestamp: October 31, 2021, 09:45:58 pm
content: So my code is now: #include "Arduino.h"#include <avr/io.h>#include <avr/interrupt.h>volatile unsigned long int millisekunden = 0;unsigned long int last_msg;char buffer;void setup(){// TIMER 1 for interrupt frequency 100 Hz:cli(); // stop interruptsTCCR1A = 0; // set entire TCCR1A register to 0TCCR1B = 0; // same for TCCR1BTCNT1 = 0; // initialize counter value to 0// set compare match register for 100 Hz incrementsOCR1A = 19999; // = 16000000 / (8 * 100) - 1 (must be <65536)// turn on CTC modeTCCR1B |= (1 << WGM12);// Set CS12, CS11 and CS10 bits for 8 prescalerTCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);// enable timer compare interruptTIMSK1 |= (1 << OCIE1A);sei(); // allow interrupts}void loop() {if (millisekunden - last_msg >= 1000) {sprintf(buffer, "t=[%lu]", millisekunden);Serial.println(buffer);last_msg = millisekunden;}}ISR(TIMER1_COMPA_vect){millisekunden = millisekunden + 10;}

author: altruan23
author_rank: Contributor
timestamp: November 01, 2021, 07:41:14 am
content: It works now thanks!!