Hello!
My intent is to understand the description given in the Wikipedia page, that's why I showed of my tasks.
At the end of the day, you have to begin programming. And I would even say at the
beginning of the day. You cannot learn how to ride a bicycle from a book. You have
to get some kind of feeling on how this works and what you can do with it. So you should
start about tasks, intertupts by actually coding.
In this case I will assume that I have any analog input that I need to read within 100us,
if I am not reading within the time limit the I will be lost data.
As said earlier, on recent processors there is a way to setup the ADC to have data
at the rate you want (within the CPU capability). Everytime data is ready, the ADC
rings the CPU, that's the interrupts Klaus was talking about.
You can even have a program without any loop, it works fine.
Back to the ADC / interrupt. In your example you can program ADC to call interrupts
every 100µs. From there, it's simple. You do what you have to do with the result, and
then you put the CPU in sleep mode until the next interrupt. This assumes that the
whole processing takes less than 100µs. As soon as you cross this time limit, you will
have one interrupt every 200µs, although this depends on the processor and the way
you program it.
You mean something like this
MAIN loop.
Etc..
Not exactly. If you setup an interrupt, then an interrupt service routine (ISR) will
be called automatically. In this routine, you set up a state variable that will be
used in your main loop.
It would look like this (in pseudo code), with an ADC and a timer, both with interrupts:
Code:
volatile int event = NO_EVENT;
enum {NO_EVENT, ADC_EVENT, TIMER_EVENT};
void main(void) {
setup();
while(1) {
switch(event) {
case ADC_EVENT :
process_adc();
break;
case TIMER_EVENT:
process_timer();
break;
}
event = NO_EVENT;
}
}
[some ISR definitions, pragmas, etc...]
void my_ADC_isr(void) {
event = ADC_EVENT;
}
[some other ISR definitions, pragmas, etc...]
void my_TIMER_isr(void) {
event = TIMER_EVENT;
}
Another way to do this:
Code:
void main() { // NB: Usually it's int main, but it doesn't make sense if you don't use an OS.
setup();
__enable_interrupts();
};
So The only task of the main function is to start your program, and then do nothing.
Then, for example if you want to do something with ADC every 100 µs:
Code:
[some ISR definitions, pragmas, etc...]
void my_ADC_isr(void) {
__disable_interrupts();
process_adc();
__enable_interrupts();
}
As long as process_adc() takes less than 100µs, it will just run fine.
So again, my advice would be to start programming. Academic concepts certainly don't hurt, but there is
a great deal of practice needed to really understand what you want to do and how you can achieve
your target.
Dora