📚 NÍVEL 3 - AVANÇADO (Semanas 11-14)
Tópicos Avançados
1. Watchdog Timer (Sistema Crítico)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdint.h>
#define IWDG_KR (*(volatile uint8_t *)0x50E0)
#define IWDG_PR (*(volatile uint8_t *)0x50E1)
#define IWDG_RLR (*(volatile uint8_t *)0x50E2)
void watchdog_init(void) {
// Desbloquear watchdog
IWDG_KR = 0x55;
// Prescaler = 64 -> ~250ms com RLR=255
IWDG_PR = 0x03;
IWDG_RLR = 255;
// Refresh
IWDG_KR = 0xAA;
// Habilitar
IWDG_KR = 0xCC;
}
void watchdog_refresh(void) {
IWDG_KR = 0xAA;
}
void main(void) {
watchdog_init();
while(1) {
// Código crítico
do_important_work();
// DEVE resetar watchdog regularmente
watchdog_refresh();
// Se travar, watchdog reseta MCU!
}
}
2. Low Power Modes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <stdint.h>
void enter_halt_mode(void) {
// Desabilitar periféricos desnecessários
CLK_PCKENR1 = 0x00;
// Entrar em halt (wake-up por interrupção)
__asm__("halt");
// Acordou! Reconfigurar clock
CLK_PCKENR1 = 0xFF;
}
void main(void) {
gpio_init();
// Configurar interrupção externa
// (ex: botão wake-up)
while(1) {
printf("Trabalhando...\n");
delay_ms(1000);
printf("Dormindo...\n");
delay_ms(100);
enter_halt_mode();
printf("Acordei!\n");
}
}
🎯 PROJETO NÍVEL 3: Sistema de Alarme Residencial
Descrição: Sistema completo de alarme com múltiplos sensores.
Componentes:
- STM8S103F3
- Sensores PIR (movimento)
- Sensores magnéticos (porta/janela)
- Sirene (buzzer potente)
- Teclado matricial 4x4
- Display LCD 16x2 (I2C)
- LED RGB (status)
- EEPROM externa (logs)
Funcionalidades:
- Monitorar 8 zonas diferentes
- Senha para armar/desarmar
- Log de eventos em EEPROM
- Envio de alertas via UART (módulo GSM)
- Watchdog para confiabilidade
- Bateria backup (funciona sem energia)
- Delay de saída/entrada
Características de Sistema Crítico:
- Código otimizado (memória limitada)
- Debounce robusto
- Watchdog sempre ativo
- Fail-safe mechanisms
- Consumo ultra-baixo em standby