Hii;
I want to use timer 0 on normal mode and timer 2 on asynchronous mode so is it possible to use??
because timer 0 is working fine but timer 2 is not working together but I am able to use timer 2 in asynchronous mode in a separate program.
Hii;
I want to use timer 0 on normal mode and timer 2 on asynchronous mode so is it possible to use??
because timer 0 is working fine but timer 2 is not working together but I am able to use timer 2 in asynchronous mode in a separate program.
That sounds like a new question - so you should start a new thread!
Don't start a new thread, I've split this off!
Thanks.. Any solution for my issue??
I want to generate a pulse from timer 1 in normal mode and while this I want to use timer 2 on Asynchronous mode. It is possible??
I want to generate a pulse from timer 1 in normal mode and while this I want to use timer 2 on Asynchronous mode. It is possible??
Yes.
#define FREQ 16000000UL #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <util/delay.h> #include "lcd_cl.h" int t=0; char c[10]; double OCR0_VALUE=0,sps_dfg; void fegenerator (void); void t0_init(void); int main(void) { /* Initialize registers and configure RTC. */ DDRA = 0XFF; lcd_displays("Start"); _delay_ms(1000); MCUCR|=(1<<SM1)|(1<<SM0); OCR0_VALUE= ((((FREQ/2)/64)/(10000))-1); t0_init(); sei(); while(1); } ISR(TIMER1_COMPA_vect) { OCR1A=(uint16_t)OCR0_VALUE; //Disable timer2 interrupts TIMSK = 0; //Enable asynchronous mode ASSR = (1<<AS2); //set initial counter value TCNT2=0; //set prescaller 128 TCCR2 |= (1<<CS22)|(1<<CS00); //wait for registers update while (ASSR & ((1<<TCN2UB)|(1<<TCR2UB))); //clear interrupt flags TIFR = (1<<TOV2); //enable TOV2 interrupt // TIMSK = (1<<TOIE2); } ISR(TIMER2_OVF_vect) { t++; itoa (t,c,10); lcd_displays (c); } void t0_init() { if (sps_dfg<= 5000) TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); else if (sps_dfg>= 5001) TCCR1B=(1<<WGM12)|(1<<CS11); else if (sps_dfg>= 15001) TCCR1B=(1<<WGM12)|(1<<CS10); DDRD|=(1<<PD5); TCCR1A=(1<<COM1A0); TIMSK|=(1<<OCIE1A); }
This is my code. It is not working..
#define FREQ 16000000UL
So you have a properly set up external crystal?
This is my code. It is not working..
Which means what exactly? The board catches fire? Too fast? Too slow? Nothing is being outputted?
I want that timer 1 will work on 16 Mhz pulse and timer 2 will count the real time (RTC) using 32.768 Khz crystal.
I want that timer 1 will work on 16 Mhz pulse
What do you mean by that?
Why not post a timing diagram of the required output?
You alread have the instructions on How To Post Code and How To Post Images (eg, diagrams) - #3 in your other thread.
You still haven't explained what you mean by
It is not working
Why do you initialise T2 inside T1's ISR?
How have you connected your 32.768kHz crystal?
ISR(TIMER2_OVF_vect) { itoa (t,c,10); lcd_displays (c); }
I doubt you want to do such long work in the ISR. I haven't worked out how fast your timer is going but this display stuff could take 100's of milliseconds. Far better is to just do display updates in the while(1) loop in main(). Then it doesn't matter how long it takes.
Also in main(0 you call lcd_displays() without any kind of lcd_init()?!? Does the lcd_displays() have the intelligence to initialize the first time it is called?
32.768 Crystal is connected on TOSC1 and TOSC2 for making timer 2 on asynchronous mode.
Basically I want to generate a pulse from timer 1 with 16 Mhz and and count the time for which timer 1 generating the pulse. So for real time count I am using timer 2 on Asynchronous mode with 32.768 Khz crystal. So in both 16 Mhz and 32.768 khz crystals will work together.
I want to generate a pulse from timer 1 with 16 Mhz
I still have no idea what that actually means!
Draw a diagram of the output you are trying to generate.
EDIT
count the time for which timer 1 generating the pulse
doesn't make much sense, either: it's your code that generates the pule - so, surely, you know the time ?!
In timer 1 I am generating a 10 Khz pulse using 16 Mhz external crystal. After that I am counting the time let suppose I need to stop timer 1's pulse after 60 seconds. So for counting 60 Seconds with real time clock I am using Timer 2 with 32.768 Khz external crystal on Pin TOSC1 and TOSC2. I am making timer 2 on Asynchronous mode. Timer 1 is working fine but timer 2 is not switching to Asynchronous mode I guess. Because I am not able to get the count from timer 2.
ISR(TIMER2_OVF_vect)
{
t++;
itoa (t,c,10);
lcd_displays (c);
}
But you can do that just with timer 1. Every time the ISR triggers you decrement a local static variable. If that variable is non-zero you set the pulse; when that variable reaches zero you stop the pulse. To start the pulse again you simply set that variable back to the desired duration.
Time is the issue means if I am disabling Timer 1 in 60 seconds it is getting disable in 80 seconds and this time is also differs with the pulse value generated by the timer 1 (If timer 1 generates 10 Khz pulse then it will disable on 80 seconds and if Timer 1 generates 12 Khz pulse it get disable on around 92 seconds but I required to disable it on exact 60 Seconds.)
Timer 2 is generating real time on Asynchronous mode with 32.768 Khz crystal when I am not enabling timer 1. But Timer 2 is not giving any response if I am Enabling Timer 1 first and then enable timer 2.
Timer 2 is generating real time on Asynchronous mode with 32.768 Khz crystal when I am not enabling timer 1.
How? You initialise T2 inside T1's ISR so if you don't enable T1 then T1's ISR will never fire and T2 will never be initialised so it cannot run.
In post #14 you said that you are generating a 10kHz pulse. Are you now saying that you need to generate different frequencies with T1?
Perhaps you should specify EXACTLY what you want to do.
In an other program I tested timer 2 in Asynchronous mode without enabling Timer 1. Means for just testing purpose I have enabled Timer 2 only.
Yes, I need to generate many different pulses from timer 1, that I can do.But, I need the help to enable Timer 2 in asynchronous mode while Timer 1 is already generating the pulse
Perhaps you should specify EXACTLY what you want to do.
you should specify EXACTLY what you want to do.
Stop just throwing out random little scraps of information!
Write a full specification.
If nothing else, this will help you to understand your task!
For example...
I need to generate a waveform with a 30:70 duty cycle over the frequency range of 100MHz to 173MHz in 1MHz steps for a duration of between 17 days and 3 years in one day steps.
See I need to generate multiple value pulses from timer 1 using 16 Mhz crystal and stop that pulses on exact timings.
Note that a specification tells what is to be done - not how it is to be done.
You are getting bogged-down in the how (specific timers) without, apparently, a clear understanding of the what.
See I need to generate multiple value pulses from timer 1 using 16 Mhz crystal and stop that pulses on exact timings.
So is that 2 pulses or 1 million pulses?
Are these pulses all at the same frequency or just a different number of pulses at the same frequency (or both)?
How exact is exact?
And what are those timings? 1us? 1 minute? 1 year?
See my post #21 for what you should be telling us.
You can help me just telling the process to enable timer 2 on Asynchronous mode when Timer 1 is already generating pulse.
generate multiple value pulses
What does that mean.
Quantify it - see Brian's example.
from timer 1
That is an implementation detail - the "how" - it should not form part of the specification.
using 16 Mhz crystal
Another implementation detail.
Remember: WHAT - not HOW.
exact timings.
Again: What does that mean? Quantify it!
Arround 10 diffrenet pulse like 2 Mhz, 3Mhz, 5 Mhz, 6 Mhz etc. Time in seconds.
Could you be a little more vague?
Exact time means if I required to disable timer 1 after 60 seconds then I need to count the 60 second without any error.
I have already generated exact time in second and pulses with timer 1 separately means ---- IN ONE PROGRAM I AM ABLE TO GENERATE MULTIPLE REQUIRED PULSES FROM TIMER 1 ---and ---- IN OTHER PROGRAM I AM ABLE TO COUNT THE ERROR LESS TIME IN SECONDS.
What I need is to merge these both programs together.
In the program I am enabling timer 1 after that I need to activate TIMER 2 ON ASYNCHRONOUS MODE ------ THIS IS THE PLACE WHERE I NEED YOUR HELP.
You're not listening.
I'm out.
1) Go back and re-read #10 and then answer my first question.
2) You have several typos in your code which means things aren't set up as you think they are.
3) It is very rude to shout at people who are trying to teach you.
You're not listening. I'm out.
So am I.
A new proverb: "some threads will never be answered"!
Sorry if I sound rude.. I just need to know how to activate timer 2 on asynchronous mode when timer 1 is already ruing in normal mode.
You seem to know that already but have the overflow interrupt bit setting commented out therefore you never get the interrupt.
ISR(TIMER1_COMPA_vect) { OCR1A=(uint16_t)OCR0_VALUE; //Disable timer2 interrupts TIMSK = 0; //Enable asynchronous mode ASSR = (1<<AS2); //set initial counter value TCNT2=0; //set prescaller 128 TCCR2 |= (1<<CS22)|(1<<CS00); //wait for registers update while (ASSR & ((1<<TCN2UB)|(1<<TCR2UB))); //clear interrupt flags TIFR = (1<<TOV2); //enable TOV2 interrupt // TIMSK = (1<<TOIE2); }
so do you have a JTAG debugger? Put a breakpoint in the above ISR at
//Disable timer2 interrupts
TIMSK = 0;
to see if the first interrupt works and timer 2 gets initiliased properly, ie look at the timer 2 registers in the I/O view just before leaving the interrupt.
Then put another breakpoint at the beginning of ISR(TIMER2_OVF_vect) to see if the timer is running.
.... continue from there.
Sorry sir but I also tried with making enable interrupt issue is the same.
TIMSK = (1<<TOIE2)
It not getting any response from the Timer 2.
No I don't have JTAG debugger.. I s there any other method to test it??
#define FREQ 16000000UL #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <util/delay.h> #include "lcd_cl.h" void init(void); void t2_int (void); int t=0; char c[10]; double OCR0_VALUE=0,sps_dfg=10; void t0_init(void); int main(void) { /* Initialize registers and configure RTC. */ DDRA = 0XFF; lcd_displays("Start"); sh_ti=20; _delay_ms(1000); MCUCR|=(1<<SM1)|(1<<SM0); sps_dfg = 10000; OCR0_VALUE= ((((FREQ/2)/64)/(sps_dfg))-1); t0_init(); sei(); //Initialise the timer2 init(); //Enable global interrupts sei(); while(1); } void init(void) { if (sps_dfg<= 5000) TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); else if (sps_dfg>= 5001) TCCR1B=(1<<WGM12)|(1<<CS11); else if (sps_dfg>= 15001) TCCR1B=(1<<WGM12)|(1<<CS10); DDRD|=(1<<PD5); TCCR1A=(1<<COM1A0); TIMSK|=(1<<OCIE1A); } void t2_int (void) { /////////// //Disable timer2 interrupts TIMSK = 0; //Enable asynchronous mode ASSR = (1<<AS2); //set initial counter value TCNT2=0; //set prescaller 128 TCCR2 |= (1<<CS22)|(1<<CS00); //wait for registers update while (ASSR & ((1<<TCN2UB)|(1<<TCR2UB))); //clear interrupt flags TIFR = (1<<TOV2); //enable TOV2 interrupt TIMSK = (1<<TOIE2); } ISR(TIMER1_COMPA_vect) { OCR1A=(uint16_t)OCR0_VALUE; t2_int(); } ISR(TIMER2_OVF_vect) { t++; lcd_cmd(0xD9); itoa (t,c,10); lcd_display (c); if (t==sh_ti) { TCCR1B=0; TCCR1A=0; TIMSK = 0; lcd_displays("STOP"); } } void t0_init() { if (sps_dfg<= 5000) TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); else if (sps_dfg>= 5001) TCCR1B=(1<<WGM12)|(1<<CS11); else if (sps_dfg>= 15001) TCCR1B=(1<<WGM12)|(1<<CS10); DDRD|=(1<<PD5); TCCR1A=(1<<COM1A0); TIMSK|=(1<<OCIE1A); }
This program is working fine on Proteus simulator but it is not working on hardware.
This is my code. It is not working..
It is not only not working but even not compiling.
I need to activate TIMER 2 ON ASYNCHRONOUS MODE
In timer1 interrupt you clear timer2 value. (TCNT2 = 0;)
This interrupt happens 20000 times in a sec.
So how can timer2 overflow?
Thank You every one for the support my issue is solved and program is working properly.
It is a good news that miracles still happen.
No I don't have JTAG debugger
That's like a mechanic not having any spanners. It's the only way I know to debug.
But it looks like your code is not even compiling so you have other problems.
And by the way is this a Mega644 chip or a Mega644P (or PA)? The later has more bits.
#define FREQ 16000000UL #include <avr/io.h> #include <avr/interrupt.h> #include <avr/sleep.h> #include <util/delay.h> #include "lcd_cl.h" void init(void); void t2_int (void); int t=0; char c[10]; double OCR0_VALUE=0,sps_dfg=10; int main(void) { /* Initialize registers and configure RTC. */ DDRA = 0XFF; lcd_displays("Start"); sh_ti=1500; _delay_ms(1000); MCUCR|=(1<<SM1)|(1<<SM0); sps_dfg = 10000; OCR0_VALUE= ((((FREQ/2)/8)/(sps_dfg))-1); t0_init(); sei(); //Initialise the timer2 init(); //Enable global interrupts sei(); while(1); } void init(void) { if (sps_dfg<= 5000) TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); else if (sps_dfg>= 5001) TCCR1B=(1<<WGM12)|(1<<CS11); else if (sps_dfg>= 15001) TCCR1B=(1<<WGM12)|(1<<CS10); DDRD|=(1<<PD5); TCCR1A=(1<<COM1A0); TIMSK|=(1<<OCIE1A); } void t2_int (void) { lcd_cmd(0xC0); lcd_display ("Timer2 intialize"); _delay_ms(500); /////////// //Disable timer2 interrupts TIMSK = 0; //Enable asynchronous mode ASSR = (1<<AS2); //set initial counter value TCNT2=0; //set prescaller 128 TCCR2 |= (1<<CS22)|(1<<CS00); //wait for registers update while (ASSR & ((1<<TCN2UB)|(1<<TCR2UB))); //clear interrupt flags TIFR = (1<<TOV2); //enable TOV2 interrupt TIMSK = (1<<TOIE2); } ISR(TIMER1_COMPA_vect) { OCR1A=(uint16_t)OCR0_VALUE; t2_int(); lcd_cmd(0xC0); lcd_display ("Timer6"); } ISR(TIMER2_OVF_vect) { t++; lcd_cmd(0xD4); itoa (t,c,10); lcd_display (c); if (t==sh_ti) { TCCR1B=0; TCCR1A=0; TIMSK = 0; lcd_display("STOP"); } } void t0_init() { if (sps_dfg<= 5000) TCCR1B=(1<<WGM12)|(1<<CS11)|(1<<CS10); else if (sps_dfg>= 5001) TCCR1B=(1<<WGM12)|(1<<CS11); else if (sps_dfg>= 15001) TCCR1B=(1<<WGM12)|(1<<CS10); DDRD|=(1<<PD5); TCCR1A=(1<<COM1A0); TIMSK|=(1<<OCIE1A); }
This is the program which is working fine for atmega32 with 16mhz crystal on XTAL1 and XTAL2 and 32.768khz crystal on TOSC1 AND TOSC2.
My money is compiling and testing on Proteus as a mega32 but having mega644 as hardware.
No I don't have JTAG debugger
That's like a mechanic not having any spanners.
+1000
This is the program which is working fine for atmega32
But you are telling us that you are using a Mega644! The 2 are NOT the same.
Several things used in the code do NOT exist in the Mega644! So how do you expect things to work at all?
Fix your code for the Mega644 and then try again, people have better things to do in life than mess around with wrong information for days.