📜 ⬆️ ⬇️

Almost real-time OS - 2

Now that the tool is ready, it can and should be used.

Previously a couple of words about the Main Timer.

The board has an external pulse generator (clock DS1307). The generator is tuned to a frequency of 4 kHz and the pulses are turned on INT0 - an external interrupt.

This interrupt is used as the main timer in the system. How? Yes, very simple:
')
unsigned int calc_delay; //

ISR(INT0_vect) {
if (calc_delay > 41) { // 10
calc_delay = 0;
dispatchTimer(); //
} else
calc_delay++;
}

* This source code was highlighted with Source Code Highlighter .


The macro ISR in avr-gcc describes the interrupt handler. This function is called 4096 times per second. And our timer manager is called once every 10 milliseconds (with a small error).
Those interested can use the built-in timer or another period of the timer, the essence does not change.

I also use ADC (keyboard polling) and UART (communication with a computer) interrupts. There, too, minimalism - put the value in the buffer and continue. The main work is done in the main program loop:

int main() {
init();

for (;;) {
dispatchMessage();
}
}

* This source code was highlighted with Source Code Highlighter .


I hope you don't even need to comment. Yes, there is a small but very important piece of code in the initialization procedure:

//

setHandler(MSG_LCD_REFRESH, &refreshLCD);
setTimer(MSG_LCD_REFRESH, 0, 20);

setHandler(MSG_ADC_CYCLE, &readKey);
setHandler(MSG_KEY_REPEAT, &repeatKey);

setHandler(MSG_KEY_PRESS, &analyseKey);

setHandler(MSG_1W_TEMP, &owTemp);
setTimer(MSG_1W_TEMP, 1, 10);

* This source code was highlighted with Source Code Highlighter .


In a nutshell - set the handler and cock the timer. Then the system is already running - it tracks the timer and creates an event. Events are processed in the order received. The handler function performs the actions and cocks the timer again (if necessary).

For example, this is the simplest screen redraw function:

unsigned char Disp[2][20]; //

unsigned char refreshLCD(msg_par par) {
lcd_gotoxy(0,0);
lcd_puts(Disp[0]); //
lcd_gotoxy(0,1);
lcd_puts(Disp[1]); //

setTimer(MSG_LCD_REFRESH, 0, 20);
return (0);
}


* This source code was highlighted with Source Code Highlighter .


The screen is updated, the keyboard is analyzed, the sensors are polled.

The next section is the user interface.

Source: https://habr.com/ru/post/58472/


All Articles