| Author |
Message |
|
|
Posted: Sep 30, 2011 - 07:24 AM |
|

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] |
|
|
| |
|
|
|
|
|
Posted: Sep 30, 2011 - 07:26 AM |
|


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
|
| |
|
|
|
|
|
Posted: Sep 30, 2011 - 08:12 AM |
|

Joined: Aug 07, 2007
Posts: 1505
Location: Czech
|
|
|
|
|
|
|
Posted: Sep 30, 2011 - 09:14 AM |
|


Joined: Jul 18, 2005
Posts: 62922
Location: (using avr-gcc in) Finchingfield, Essex, England
|
|
|
|
|
|
|
Posted: Sep 30, 2011 - 07:23 PM |
|

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 . |
|
|
| |
|
|
|
|
|
Posted: Sep 30, 2011 - 07:29 PM |
|


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. |
_________________
|
| |
|
|
|
|
|
Posted: Sep 30, 2011 - 08:30 PM |
|

Joined: Sep 30, 2011
Posts: 3
|
|
i have just read it and understood what was happening in the code . Thnx a lot  |
|
|
| |
|
|
|
|
|