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.

[SOLVED] how to use Timer to generate many sequence

Status
Not open for further replies.

eng_ibrahim

Member level 1
Joined
Nov 21, 2012
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,555
hello
how i can use the timer in microcontroller to generate many sequences of signals such pinb0 out --> 1 msec , pinb1--> 20 msec and pinb2--> 25 msec .
thanks,
 

Code:
const unsignel int sequence[] = {1, 20, 25};
void timer_interrupt (void)
{
  static char seq_cnt = 0;
  timer_counter = sequence[seq_cnt];
  if (seq_cnt == sizeof(sequence))
    seq_cnt = 0;
  else
    seq_cnt++;
  // do something
}
 
Why not do that based on counting a timer running at 1ms ? Each time it is reached, performs toggle at 1ms intervals, and the other pins, does this toggling only after a certain number of counts, with independent counters. If you want something a little bit faster in the range of nanoseconds, would have to map in memory in the output decoding of the pins, but in this case in particular would waste for example 25 words (1ms ... 25ms).
 

There are two scenarios here, so far only one has been described, where the signals are generated one after another.
The other scenario is where the signals may be concurrent and it has to be done in a different way:

1. Set up a variable for each output signal, it has to be sized to hold the largest count of the smallest time interval you will use.
2. Set up a hardware timer in the MCU to generate an interrupt at the smallest time interval.
3. In the ISR, check each variable and if it is NOT zero, decrement it. This ensures it stops when the count down reaches zero.

You can either use the ISR to make the pin low as the counter reaches zero or you can check the variable for zero in your main program loop.
Now if you set the pin and load your time interval in it's timer, it will remain high for the time you speciifed. The advantage of this method is each pin can be programmed to any delay at any time and all the sequences run at the same time.

example pseudo code:
Code:
void MyISR()
{
reload the timer if necessary;
if(pin1_timer > 0) pin1_timer--; else pin1 =0;
if(pin2_timer > 0) pin2_timer--; else pin2 = 0;
if(pin3_timer > 0) pin3_timer--; else pin3 = 0;
}

void main()
{
pin1 = 1; pin1_timer = 10;  // pin 1 will stay high for 10 timer intervals from now
delay(some time);
pin2 = 1; pin2_timer = 100; // pin 2 will stay high for 100 timer intevals from now
delay(some other time);
pin3 = 1; pin3_timer = 20;  // pin 3 will stay high for 20 timer intervals from now
}

Note that all three intervals are timed simultaneously and independantly.

Brian.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top