Author Topic: atmega328p ctc mode timer  (Read 2029 times)

0 Members and 1 Guest are viewing this topic.

Offline altruan23Topic starter

  • Contributor
  • Posts: 17
  • Country: si
atmega328p ctc mode timer
« on: October 31, 2021, 07:20:35 pm »
datasheet: https://ww1.microchip.com/downloads/en/DeviceDoc/Atmel-7810-Automotive-Microcontrollers-ATmega328P_Datasheet.pdf
So 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.pdf With 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[128];

void setup() {

  TCCR0A |= (1 << WGM01);                // CTC Modus
  TCCR0B |= (1 << CS02) | (1 << CS00);  // Prescaler 64
 
  OCR0A = 155;

  // Compare Interrupt
  TIMSK0 |= (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-Routine
ISR(TIMER0_COMPA_vect) {
  millisekunden = millisekunden + 10;
}
 

Offline kripton2035

  • Super Contributor
  • ***
  • Posts: 2911
  • Country: fr
    • kripton2035 schematics repository
Re: atmega328p ctc mode timer
« Reply #1 on: October 31, 2021, 09:03:00 pm »
timer0 is used by the arduino system. you shouldn't use it for your code. use timer1 or 2 instead

Code: [Select]
// TIMER 1 for interrupt frequency 100 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1  = 0; // initialize counter value to 0
// set compare match register for 100 Hz increments
OCR1A = 19999; // = 16000000 / (8 * 100) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 8 prescaler
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei(); // allow interrupts
 

Offline altruan23Topic starter

  • Contributor
  • Posts: 17
  • Country: si
Re: atmega328p ctc mode timer
« Reply #2 on: October 31, 2021, 09:16:49 pm »
Hello kripton2035. Thank you very much for your answer. Unfortunately I still have the same problem. So i copied your code and created the Interrupt
service routine function : ISR(TIMER1_COMPA_vect) . In that function i increase the variable millisekunden always by 10. so after it reaches 1000, it schould be
printed. but it doenst work. i dont know why. our teacher told us that we need to solve this exercise with the ISR.
« Last Edit: October 31, 2021, 09:43:52 pm by altruan23 »
 

Offline altruan23Topic starter

  • Contributor
  • Posts: 17
  • Country: si
Re: atmega328p ctc mode timer
« Reply #3 on: October 31, 2021, 09:45:58 pm »
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[128];
void setup(){
// TIMER 1 for interrupt frequency 100 Hz:
cli(); // stop interrupts
TCCR1A = 0; // set entire TCCR1A register to 0
TCCR1B = 0; // same for TCCR1B
TCNT1  = 0; // initialize counter value to 0
// set compare match register for 100 Hz increments
OCR1A = 19999; // = 16000000 / (8 * 100) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12, CS11 and CS10 bits for 8 prescaler
TCCR1B |= (0 << CS12) | (1 << CS11) | (0 << CS10);
// enable timer compare interrupt
TIMSK1 |= (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;
}
 

Offline altruan23Topic starter

  • Contributor
  • Posts: 17
  • Country: si
Re: atmega328p ctc mode timer
« Reply #4 on: November 01, 2021, 07:41:14 am »
It works now thanks!!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->