ATmega328p Anync mode timer init issue

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I try to run async mode timer2 on ATmega328p.  And missed some value in registers init set. Little help needed.

 

#include "main.h"

unsigned char flag = 0;

void TIMER_Init(void) {
	/*
	// ATmega328P
	TCCR0A |= (1<<WGM01);
	OCR0A = 0xFF;
	TIMSK0 |= (1<<OCIE0A);
	TCCR0B |= (0<<CS02)|(1<<CS01)|(1<<CS00);
	*/
	
	// ATmega328P Async with 32768
	ASSR |= (1<<AS2);
	while(ASSR != 0b00001000) {
		asm("nop");
	}
	TCCR2A |=(1<<CS21)|(1<<CS22)|(1<<WGM21);
	OCR2A = 127; // f =  1 Hz (vector should run every second).
	TIMSK2 |=(1<<OCIE2A); 
	TIFR2 = 0;
}

int main(void) {
	TIMER_Init();

	PORTD = 0x00;
	DDRD = 0xFF;

    while(1) {

    }
}

ISR(TIMER2_COMPA_vect) {
	switch(flag) {
		case 0:
			LED_PORT |= (1<<LED1_PIN);
			LED_PORT &= ~(1<<LED2_PIN);
			flag = 1;
			break;
		case 1:
			LED_PORT |= (1<<LED2_PIN);
			LED_PORT &= ~(1<<LED1_PIN);
			flag = 0;
			break;
		default: break;
	}
}


main.h

#ifndef MAIN1_H_
#define MAIN1_H_

#define F_CPU 16000000UL

#define LED_PORT PORTD
#define LED_DIR DDRD
#define LED1_PIN 6
#define LED2_PIN 7

#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#endif /* MAIN1_H_ */AIN_H_ */

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Give a bit more information.  What do you expect to happen?  What >>is<< happening?  What is your schematic?  Do you know if your AVR is actually running?

 

Why do you use all the |= with register setup, rather than simple assignment with = of the value you desire?

 

Do you have an appropriate watch crystal?  What model?  What is the ESR?  How is it connected?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Darien wrote:

DDRD = 0xFF;

What is connected to all of these pins that you have made outputs?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Darien wrote:

// ATmega328P Async with 32768
	ASSR |= (1<<AS2);
	while(ASSR != 0b00001000) 

So, >>no<< other bits of ASSR could be nothing but 0?

Perhaps.  But why not do the comparison properly?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Darien wrote:
I try to run async mode timer2 on ATmega328p.
Darien wrote:

while(ASSR != 0b00001000) {

I'm not sure this is working the way you want, since bit 5 was set in the line above.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Give a bit more information.  What do you expect to happen?  What >>is<< happening?  What is your schematic?  Do you know if your AVR is actually running?

 

Why do you use all the |= with register setup, rather than simple assignment with = of the value you desire?

 

Do you have an appropriate watch crystal?  What model?  What is the ESR?  How is it connected?

To BP6 and PB7 I connect 32768 Hz cristal. Expectation is ISR vect running every 1 sec.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Darien wrote:

DDRD = 0xFF;

What is connected to all of these pins that you have made outputs?

Two LEDs and nothing else. It is dos't metter for timmer if port set to output.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

Darien wrote:

// ATmega328P Async with 32768
	ASSR |= (1<<AS2);
	while(ASSR != 0b00001000) 

So, >>no<< other bits of ASSR could be nothing but 0?

Perhaps.  But why not do the comparison properly?

Timer in async mode starts slowly, so I ann such waiting loop until timer set to asybc mode.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I find solution, but still research what all registers for.

void RTCInit(void)
{
    //Disable timer2 interrupts
    TIMSK2  = 0;
    //Enable asynchronous mode
    ASSR  = (1<<AS2);
    //set initial counter value
    TCNT2=0;
    //set prescaller 128
    TCCR2B |= (1<<CS22)|(1<<CS00);
    //wait for registers update
    while (ASSR & ((1<<TCN2UB)|(1<<TCR2BUB)));
    //clear interrupt flags
    TIFR2  = (1<<TOV2);
    //enable TOV2 interrupt
    TIMSK2  = (1<<TOIE2);
}
//Overflow ISR
ISR(TIMER2_OVF_vect)
{
    
}

 

Last Edited: Wed. Jun 26, 2019 - 07:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

My error in first place was that I use ext. 12MHz cristal and forget to set fuses on internal cristal while I connect 32768 Hz to chip.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Darien wrote:
My error in first place was that I use ext. 12MHz cristal and forget to set fuses on internal cristal while I connect 32768 Hz to chip.

So, even though I asked many questions in my first post, you didn't care to consider each one.  There is a reason for each question.  Depending on the answers there may be different avenues to explore.

 

In this case:

theusch wrote:
Do you know if your AVR is actually running?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Darien wrote:

theusch wrote:

Darien wrote:

DDRD = 0xFF;

What is connected to all of these pins that you have made outputs?

Two LEDs and nothing else. It is dos't metter for timmer if port set to output.

 

You think this question is valid for anync timer setup?

32768 cristal connected to  PB6 and PB7. It dos't metter why I set port D rof output. I try to blink LED on ISR vertor on pin every second to verify timer is running. From code you could figure out that.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Darien wrote:
It is dos't metter for timmer if port set to output.
Yeah but - suppose one or more of those pins is directly or strongly connected to Vcc? If you just set all 8 output and the PORTD register is also set to  0x00 then the pins are all connected to Gnd. If any are externally Vcc you have a direct Gnd-Vcc connection. Tons of current flows. Tons of smoke escapes. Etc etc.

 

I'm pretty sure Lee has a reason for asking anything he did (we've often seen such a scenario here before - maybe not smoke but high current can pull current which sags Vcc which has all kinds of unforeseen consequences). 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:

Darien wrote:
It is dos't metter for timmer if port set to output.
Yeah but - suppose one or more of those pins is directly or strongly connected to Vcc? If you just set all 8 output and the PORTD register is also set to  0x00 then the pins are all connected to Gnd. If any are externally Vcc you have a direct Gnd-Vcc connection. Tons of current flows. Tons of smoke escapes. Etc etc.

 

I'm pretty sure Lee has a reason for asking anything he did (we've often seen such a scenario here before - maybe not smoke but high current can pull current which sags Vcc which has all kinds of unforeseen consequences). 

 

I set all port D for output but only one (or two) LED is connected -> not hight port work. In should not effect on timer setup. I am interested what better use: ISR compare or ISR overflow. All endup in timer setup registers.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And valid question: fuses setup. PB6 and PB7 also use as connectors to ex. cristal. If 32768 Hz cristal is connected -> no pins to connect external cristal left and other timers will working differently I guess.  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Darien wrote:
I set all port D for output but only one (or two) LED is connected
Without seeing a schematic only you know that - no one over here on this side of the "web fence" knows. That's probably why questions are asked to get a better understanding of what your environment is.

 

As of the PB6/PB7 thing. All AVR datasheet have a section under "IO" about "alternative functions". For both PB6 and PB7 it says something like:

 

 

Obviously you can't use the pins for more than one thing. If the plan is TOSC[2:1] then you can't have a CKSEL setting that would try to use them as XTAL[2:1]. So it basically means it has to be an "internal" clock setting.

 

But that's exactly what you want with async T2 anyway. You will run the AVR from the intRC but you will use T2 to calibrate it using OSCCAL.