Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how do generate second interrupt

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
Hello
How to generate second interrupt for led2 in program
Code:
#include<reg51.h>
sbit led1 = P1^0; 		//LED connected to P0 of port 1
sbit led2 = P1^1; 		//LED connected to P1 of port 1

void timer(void) interrupt 1 		//interrupt no. 1 for Timer 0
{
	led1=~led1; 		//toggle LED on interrupt
	TH0=0xFC;		// initial values loaded to timer
	TL0=0x66;
}
main()
{
	TMOD = 0x01; 		// mode1 of Timer0
	TH0 = 0xFC;		// initial values loaded to timer
	TL0 = 0x66;
	IE = 0x82;		// enable interrupt
	TR0 = 1;		//start timer
	while(1);		// do nothing  
}
 

Hi,

Why a second interrupt?
Why not:
Code:
void timer(void) interrupt 1 		//interrupt no. 1 for Timer 0
{
	led1=~led1; 		//toggle LED on interrupt
[COLOR="#FF0000"]	led2=~led2; 		//toggle LED on interrupt[/COLOR]
	TH0=0xFC;		// initial values loaded to timer
	TL0=0x66;
}

Klaus
 
I presume it's a timer interrupt exercise, so we won't ask if the solution is reasonable in real life.

You can implement a second interrupt for timer 1.
 

actually my aim was different
For example
There are 3 LED lights, Red, Green, yellow
Step 1: I want Red LED to turn on and blink 4 times per second for 10 seconds, then turn off.
Step 2: After that, I want Green LED to turn on and blink 6 times per second for 15 seconds, then turn off.
Step 3: Finally, I want Yellow LED to turn on and blink 8 times per second for 20 seconds, then turn off.
I want to repeat steps 1 through 3 until I turn off the power

I think I can set interrupts for LED lights. I thought I need three interrupts. how to do it ?
 
Last edited:

Hi,

Don´t see the timer as "red LED blink timer" --> see it as global timer.. for many different functions.

For an exact mathematical solution.

Take the least_multiple_value of 4, 6 and 8 --> 24 (use the prime numbers method)
Multiply this value with 2 because you need two timer ticks for one "blink": one for ON, one for OFF. 24 --> 48
Then adujst the timer to 1/48s.
Within the ISR:
for red: use a counter 5 down to 0, decrement each timer tick. if 0: toggle LED. (gives 4 blink per second) use a second counter to count the seconds. (maybe: 80 LED toggles = 10 seconds)
for green: use the counter 3 down to 0, decrement each timer tick. if 0: toggle LED. use a second counter to count the seconds.
for yellow: use the counter 2 down to 0, decrement each timer tick. if 0: toggle LED. use a second counter to count the seconds.

Klaus
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
A not "mathematically exact" but practical solution would use an interrupt based millisecond tic and the different delays generated in software.
 

Actually I don't have much knowledge in programming. I understand how does timer interrupt generate and I have explained in Post #1. I want to complete these tasks step by step under your guidelines
I have searched on internet timer tick 8051 but I didn't understand. I would like to divide this task into small tasks because I think its very big task for me. please guide me to achieve this task according to my level of knowledge
 

for red:
ISR runs 48 times per second:
* counter 5 down to 0: decrement on every ISR run
* if counter = 0 then toggle LED
Code:
Counter: 5-4-3-2-1-0-5-4-3-2-1-0-5-4-3-2-1-0-5-4-3-2-1-0-
Zero:    . . . . . T. . . . . .T. . . . . .T. . . . . .T..
LED:     ON        OFF         ON          OFF         ON

Klaus
 

Do you mean like this
Code:
void timer(void) interrupt 1 		//interrupt no. 1 for Timer 0
{
	Red_led=~Red_led; 		//toggle LED on interrupt
	TH0=0x05;		       // initial values loaded to timer
	TL0=0x00;
}
 

Hi,

Two points doesn´t fit to your solution:
* your ISR doesn´t run 48 times per second
* You don´t have a counter within the ISR

I don´t write code for you. And this task is really simple.

Again step by step:
* your first task is to run the ISR 48 times per second. Try to solve this.
..then comes the next step.

Klaus
 

Hi,

Again step by step:
* your first task is to run the ISR 48 times per second. Try to solve this.
..then comes the next step.

Klaus
Each count Takes = 1.085uS
Total count by 16 bit counter 65536 * 1.085us = 71.11ms
Total Cycles * 1.085uS = desired time
71.11* Total Cycles = desired time
71.11* Total Cycles = 48000 ms
Total Cycles = 675 times
its confusing how to calculate TH0 and TL0 value ?
 

Hi,

nobody knows what microcontrolelr you use and what clock source and frequency you use.

its confusing how to calculate TH0 and TL0 value ?
It surely is written in the datasheet. And surely there are application notes on how to calculate it. Video tutorials on youtube, online calculators....
What else do you need?

Klaus
 

Hi,

nobody knows what microcontrolelr you use and what clock source and frequency you use.


It surely is written in the datasheet. And surely there are application notes on how to calculate it. Video tutorials on youtube, online calculators....
What else do you need?

Klaus
Hello
I forgot to tell you about controller I have 8051 I thought you have seen header file in fist post. I looked some tutorials and manuals 8051. while searching I found this calculation but I didn't find out calculation for TH0 and TL0?
Each count Takes = 1.085uS
Total count by 16 bit counter 65536 * 1.085us = 71.11ms
Total Cycles * 1.085uS = desired time
71.11* Total Cycles = desired time
71.11* Total Cycles = 48000 ms
Total Cycles = 675 times
 

Hi,

I´m not familiar with 8051... Is there only one type? Maybe.

I think you misunderstood how a forum works. People will help you when you have proplems with a special task.
They help you to solve your problems, but they won´t solve the problem.
They show you where to find informations, but the don´t do your job.
A forum can´t replace a school. And forum members are not meant to do internet search for you and they don´t need to read a datasheet for you and write the datasheets contents in a post.

I´ve given you 4 very different sources where you can learn how to calculate the values. A simple internet search gives thousands of results.

Now it´s on you to use this information. It´s your job.

Klaus
 

One counter variable with 3 if() conditions inside ISR will do the task.

- - - Updated - - -

I have written a code but for PIC12F1840. I can show you my code if you are interested. It uses two variables.
 

Hi,

I think you misunderstood how a forum works. People will help you when you have proplems with a special task.
They help you to solve your problems, but they won´t solve the problem.
They show you where to find informations, but the don´t do your job.
A forum can´t replace a school. And forum members are not meant to do internet search for you and they don´t need to read a datasheet for you and write the datasheets contents in a post.

I´ve given you 4 very different sources where you can learn how to calculate the values. A simple internet search gives thousands of results.

Now it´s on you to use this information. It´s your job.

Klaus
I understand forums rule. I don't want someone to do my work. I am here for learning and I am trying my best. sometime it happen that you have lot of choices and you need one of them.
 

its confusing how to calculate TH0 and TL0 value ?

This calculation is somewhat straightfoward, and as said above, there are plenty online tools available on the web, but you have to agree that your specs are quite confusing, even reviewing many times I could not understand exactly what you want:

Each count Takes = 1.085uS
Total count by 16 bit counter 65536 * 1.085us = 71.11ms
Total Cycles * 1.085uS = desired time
71.11* Total Cycles = desired time
71.11* Total Cycles = 48000 ms
Total Cycles = 675 times
 

Again step by step:
* your first task is to run the ISR 48 times per second. Try to solve this.
..then comes the next step.

Klaus
unknown value are
TH0 = ?
TL.0 = ?
If we need to run loop 48 times
65487-48= 65487= FFCF
TL0 = 0xCF
TH0 = 0xFF
we need to set these value to run ISR 48 times
 

Hi,

It seems you have difficulties to understand "48 times per second"

= 48 Hz
it also means 1s/48 = 20.833ms

Unbelievable.

Klaus
 

As mentioned above, there are lot of online timer calculators which could give you insights on the parameter requirements aswell usual nomenclature that would help you to pose the specifications in a more universal terminology. As you will notice, there is missing informations such as crystal/oscillator frequency.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top