+ Post New Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 21
  1. #1
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    how do generate second interrupt

    Hello
    How to generate second interrupt for led2 in program
    Code:
    #include
    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  
    }

  2. #2
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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
    	led2=~led2; 		//toggle LED on interrupt
    	TH0=0xFC;		// initial values loaded to timer
    	TL0=0x66;
    }
    Klaus


    1 members found this post helpful.

  3. #3
    Super Moderator
    Points: 263,400, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,024
    Helped
    13996 / 13996
    Points
    263,400
    Level
    100

    Re: how do generate second interrupt

    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.



  4. #4
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    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 by vead; 12th September 2017 at 08:22.



    •   AltAdvertisement

        
       

  5. #5
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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


    1 members found this post helpful.

  6. #6
    Super Moderator
    Points: 263,400, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    46,024
    Helped
    13996 / 13996
    Points
    263,400
    Level
    100

    Re: how do generate second interrupt

    A not "mathematically exact" but practical solution would use an interrupt based millisecond tic and the different delays generated in software.



    •   AltAdvertisement

        
       

  7. #7
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    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



  8. #8
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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



  9. #9
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    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;
    }



    •   AltAdvertisement

        
       

  10. #10
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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



  11. #11
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    Quote Originally Posted by KlausST View Post
    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 ?



  12. #12
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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



  13. #13
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    Quote Originally Posted by KlausST View Post
    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



  14. #14
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    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



  15. #15
    Advanced Member level 3
    Points: 3,481, Level: 13
    baileychic's Avatar
    Join Date
    Aug 2017
    Posts
    700
    Helped
    51 / 51
    Points
    3,481
    Level
    13

    Re: how do generate second interrupt

    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.



  16. #16
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    Quote Originally Posted by KlausST View Post
    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.



  17. #17
    Super Moderator
    Points: 29,989, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,945
    Helped
    1134 / 1134
    Points
    29,989
    Level
    42
    Blog Entries
    9

    Re: how do generate second interrupt

    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
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



  18. #18
    Full Member level 5
    Points: 2,763, Level: 12
    Achievements:
    7 years registered

    Join Date
    Nov 2011
    Location
    india
    Posts
    285
    Helped
    3 / 3
    Points
    2,763
    Level
    12

    Re: how do generate second interrupt

    Quote Originally Posted by KlausST View Post

    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



    •   AltAdvertisement

        
       

  19. #19
    Super Moderator
    Points: 80,237, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,278
    Helped
    3692 / 3692
    Points
    80,237
    Level
    69

    Re: how do generate second interrupt

    Hi,

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

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

    Unbelievable.

    Klaus



  20. #20
    Super Moderator
    Points: 29,989, Level: 42
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,945
    Helped
    1134 / 1134
    Points
    29,989
    Level
    42
    Blog Entries
    9

    Re: how do generate second interrupt

    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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--