Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
soul_keeper
PostPosted: Sep 30, 2011 - 07:24 AM
Newbie


Joined: Sep 30, 2011
Posts: 3


I am trying to make of program of 0 to 99 counter of about 1s delay using the timer1's ctc mode. I have declared a global variable called num . I wrote the program so that at every 1 second a compare match interrupt will be executed and in the ISR the value of the global variable will be increased by 1 . Then the value will be used in main function to use it for output .
Here is the code

Code:
#define F_CPU 1000000UL

#include<util/delay.h>

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


   unsigned int num=0;

/* signal handler for timer overflow interrupt TOV0 */
ISR(TIMER1_COMPA_vect) {

if(num<99)   ++num;
   else num=0;
   
}

void square_wave_init(void){
   

   /*Timer/Counter1, Output Compare A Match Interrupt Enable*/
   TIMSK |= (1<<OCIE1A);

   /*set timer counter initial value*/
   TCNT1=0;

   // Initialize Output Compare Register - 1A

   OCR1A = 487;

   // Waveform generation mode:4 : CTC : Clear Timer on Compare match
   TCCR1B |= (1<<WGM12);

   /*start timer with presscaler of clk/1024*/
   TCCR1B |= (1<<CS10) | (1<<CS12);
}

   
int main(void) {

   square_wave_init();
   /* enable interrupts */
   sei();

   unsigned int a=0,b=0;

   DDRD=0xFF;

   while(1)
   {
         a=num%10;
         b=num/10;
         
         PORTD=a;
         PORTD|=(1<<4);
            
         _delay_ms(10);

         PORTD&=(~(1<<4));
               
         PORTD=b;
         PORTD|=(1<<5);
            
         _delay_ms(10);
         PORTD&=(~(1<<5));

   }
}





But its not working . plz guys help me debug it ....[/quote]
 
 View user's profile Send private message  
Reply with quote Back to top
ka7ehk
PostPosted: Sep 30, 2011 - 07:26 AM
10k+ Postman


Joined: Nov 22, 2002
Posts: 12193
Location: Tangent, OR, USA

How is it not working? (1) not counting? (2) wrong count speed (fast? or slow?)

What is your actual clock speed? And, how do you know?

Jim

_________________
Jim Wagner
Oregon Research Electronics, Consulting Div.
Tangent, OR, USA

"The only thing standing between us and victory is defeat" P.G.Wodhouse in Wooster & Jeeves series
 
 View user's profile Send private message  
Reply with quote Back to top
Visovian
PostPosted: Sep 30, 2011 - 08:12 AM
Posting Freak


Joined: Aug 07, 2007
Posts: 1505
Location: Czech

The answer to your problem is in avr-libc-user-manual FAQ item 1.
http://www.nongnu.org/avr-libc/user-manual/FAQ.html
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Sep 30, 2011 - 09:14 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

The answer to your problem is in avr-libc-user-manual FAQ item 1.
http://www.nongnu.org/avr-libc/user-manual/FAQ.html

For the "long version of that read:

Optimzation and the importance of volatile in GCC

(I highly recommend it - but then I would!)

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
soul_keeper
PostPosted: Sep 30, 2011 - 07:23 PM
Newbie


Joined: Sep 30, 2011
Posts: 3


the value of the variable 'num' isnt being assigned to the variables a or b . the value of the variable 'num' isnt being assigned to the variables a or b .
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Sep 30, 2011 - 07:29 PM
10k+ Postman


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England

Quote:

the value of the variable 'num' isnt being assigned to the variables a or b . the value of the variable 'num' isnt being assigned to the variables a or b .

If you actually READ that link I just gave then you would know EXACTLY why this is. The optimiser will hunt out anything pointless in your program and ditch it.

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
soul_keeper
PostPosted: Sep 30, 2011 - 08:30 PM
Newbie


Joined: Sep 30, 2011
Posts: 3


i have just read it and understood what was happening in the code . Thnx a lot Very Happy
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits