TIMER0 on Arduino Mega

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

Hi everybody,
I'm Fabio from Biella and I'm new on Atmel Studio and GCC.
I tested this code on AtMega 328p inside a Arduino uno, works fine, led blink correctly.

 

#include <avr/io.h>
#include <avr/interrupt.h>

int count;

int main()
{
	//FCPU/1024
	TCCR0B|=(1<<CS02)|(1<<CS00);

	//Enable Interrupt 
	TIMSK0|=(1<<TOIE0);

	//Initialize Counter
	TCNT0=0;


	count=0;

	//Port B 
	DDRB = 0xFF ;//(1 << DDB5);

	//Enable Global Interrupts
	sei();

	//Infinite loop
	while(1);
}

ISR(TIMER0_OVF_vect)
{
	//Increment 
	count++;
	if(count==30)
	{
		PORTB ^=(1<<PB5);
		count=0;
	}
}

 

I tested it on arduino mega but don't work, naturaly I changed PB5  in PB7.
I readed the datasheet but I didn't find a solution.

 

Thx in advance 
Best regards,

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

I have tested your code in the simulator for Mega2560 and it worked.

Did you compile for M2560?

 

You can try a simple code on Arduino Mega to see if the port works:

 

#include <util/delay.h>
...
...
DDRB = 0xff;

while(1)
{
    PORTB ^= 0xff;  / toggle all pins of portb
    _delay_ms(1000);
}

 

Last Edited: Wed. Jul 27, 2016 - 06:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx Visovian for your reply,
I'm using avr dude setted for m2560.
I used your code:

 

#include <avr/io.h>
#include <util/delay.h>


int main(void)
{
	DDRB = 0xff;
    /* Replace with your application code */
    while (1) 
    {
		    PORTB ^= 0xff;  
		    _delay_ms(1000);
    }
}

 

and led's blink correctly.

I don't understand why timr0 code don't work

 

 

 

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

I would try to toggle whole port in isr and test all pins.

 

#include <avr/io.h> 
#include <avr/interrupt.h>

unsigned char count; // use smallest suitable type

int main()
{
   //FCPU/1024
   TCCR0B = (1<<CS02)|(1<<CS00);  // use "=", not "|=" for first writing to register  

   //Enable Interrupt 
   TIMSK0 = (1<<TOIE0);

   //Initialize Counter
   TCNT0 = 0;


   count = 0;

   //Port B 
   DDRB = 0xFF ;//(1 << DDB5);

   //Enable Global Interrupts
   sei();

      //Infinite loop
      while(1)
      {
      }
}


ISR(TIMER0_OVF_vect)
{
   //Increment 
   count++;

   if(count >= 30)
   {       
      PORTB ^= 0xff);  // toggle all portb
      count = 0;
   }
}

 

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

Thx for your support, I used new code and checked the output with oscilloscope but nothing in addition I used another new Arduino mega 
but to no avail.  

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

..I cheked all PORTB pin .. 

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

Thx for support :)
Today I spend more times for testing more code and I discover that the problem is the usbasp programmer ....

The code works ..

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

Hey guys. I got the same problem with the timer3 library for arduino. 
My boss uses this library for a long time and never had any problem with it until last week. 

It doesn't compiling anymore, either with the new and the old version or Arduino IDE (1.6.3)

I got the library from this gitHub website : https://github.com/PaulStoffrege...

This guy really know what he is doing my boss uses his work to be faster but something wrong since a few weeks.

Maybe it is the same problem

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

Severity    Code    Description    Project    File    Line    Column    Source    Project Rank
Error        'CS31' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        recipe for target 'TimerThree.o' failed    SoftPWM_Arduino_LED_Blink    
Error        recipe for target 'src/libraries/TimerThree/Timer3/TimerThree.o' failed    SoftPWM_Arduino_LED_Blink    
Error        recipe for target 'src/libraries/TimerThree/TimerThree.o' failed    SoftPWM_Arduino_LED_Blink   
Error        candidate is: void TimerThree::initialize(long unsigned int)    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        candidate is: void TimerThree::setPeriod(long unsigned int)    SoftPWM_Arduino_LED_Blink    
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS31' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'CS31' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS31' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'CS32' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'CS30' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'ICR3' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'ICR3' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Message        'void TimerThree::start()' previously defined here    SoftPWM_Arduino_LED_Blink  
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TCNT3' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TCNT3' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Message        'void TimerThree::stop()' previously defined here    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink 
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Message        'void TimerThree::restart()' previously defined here    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        candidate is: void TimerThree::setPwmDuty(char, unsigned int)    SoftPWM_Arduino_LED_Blink    
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink  
Error        'OCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink 
Error        'OCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink 
Error        void TimerThree::pwm(char, unsigned int)    SoftPWM_Arduino_LED_Blink   
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'COM3A1' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'COM3A1' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TCCR3B' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'WGM33' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        candidates are: void TimerThree::pwm(char, unsigned int, long unsigned int)    SoftPWM_Arduino_LED_Blink   
Message        'void TimerThree::disablePwm(char)' previously defined here    SoftPWM_Arduino_LED_Blink   
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TIMER3_A_PIN' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TCCR3A' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'COM3A1' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'COM3A1' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        void TimerThree::attachInterrupt(void (*)())    SoftPWM_Arduino_LED_Blink   
Error        'TIMSK3' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TIMSK3' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TOIE3' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        'TOIE3' was not declared in this scope    SoftPWM_Arduino_LED_Blink    
Error        candidates are: void TimerThree::attachInterrupt(void (*)(), long unsigned int)    
Message        'void TimerThree::detachInterrupt()' previously defined here    SoftPWM_Arduino_LED_Blink    
Error        'TIMSK3' was not declared in this scope    SoftPWM_Arduino_LED_Blink   
Error        'TIMSK3' was not declared in this scope    SoftPWM_Arduino_LED_Blink    

 

Last Edited: Mon. Aug 8, 2016 - 02:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

it seems that no registers are declared anywhere

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

Do you use Atmel Studio?

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

Yes I do. But it is the same with the Arduino IDE

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

I think is a library not included correctly.

Do you can share your code?

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

haha it is funny it seems that you are speaking like a french guy and I am also.

My code is exactly the same as the one on gitHub : https://github.com/PaulStoffrege...
I just tried to compile it and I got the errors you can see on my previous comment 

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

Lambi, which model of AVR are you trying to build for? The thread title says "Arduino Mega" which implies ATmega2560 but is that the AVR you are trying to use?

 

There are not that many models of AVR that have TIMSK3 (and other things related to timer 3):

windows/avr8-gnu-toolchain-linux_x86_64/avr/include/avr$ grep define[[:space:]]TIMSK3 iom*.h
iom1284.h:#define TIMSK3  _SFR_MEM8(0x71)
iom1284p.h:#define TIMSK3 _SFR_MEM8(0x71)
iom1284rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iom128rfa1.h:#define TIMSK3                          _SFR_MEM8(0x71)
iom128rfa1.h:#define TIMSK3_struct _SFR_MEM8_STRUCT(0x71, struct __reg_TIMSK3)
iom128rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iom16u4.h:#define TIMSK3 _SFR_MEM8(0x71)
iom2564rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iom256rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iom32u4.h:#define TIMSK3 _SFR_MEM8(0x71)
iom32u6.h:#define TIMSK3 _SFR_MEM8(0x71)
iom644rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iom64rfr2.h:#define TIMSK3  _SFR_MEM8(0x71)
iomxx0_1.h:#define TIMSK3  _SFR_MEM8(0x71)

The last file there caters for mega640/1280/1281/2560/2561.

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

I am using an atmega328 on an arduino pro mini

 

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

Yeah I did not create this post at first I just try to find someone who had a problem with a timer on an Arduino and see how he fixes it

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

If you are using a 328 why do you expect to be able to use code for a feature YOUR micro does not have (there is no "timer 3")?:

windows/avr8-gnu-toolchain-linux_x86_64/avr/include/avr$ grep TIMSK iom328p.h 
#define TIMSK0 _SFR_MEM8(0x6E)
#define TIMSK1 _SFR_MEM8(0x6F)
#define TIMSK2 _SFR_MEM8(0x70)

Bottom line. You cannot use https://github.com/PaulStoffrege...

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

You are right I did not pay attention to this. I thought it meant the third timer because 328 has 3 timers.

Thanks I will try with Timer1