Grupo de Robótica Experimental do Instituto Militar de Engenharia Microcontroladores STM32 ARM Cortex-M3 Aula 04 Periféricos II
Roteiro USART RTC Systick
Arquitetura STM32F10X
USART
USART Universal Syncronous Asyncronous Receiver Transmitter
USART
USART - Registradores USART_SR Status Register USART_DR Data Register USART_BRR Baud Rate Register USART_CR1 - Control Register 1 USART_CR2 - Control Register 2 USART_CR3 - Control Register 3 USART_GTPR Guard Time and Prescaler Register
USART
RTC
RTC Real Time Clock Timer independente para utilização como relógio / calendário Prescaler programável: fator de divisão até 2 20 Contador de 32 bits para medição de longos períodos (calcular) Dois clocks separados: PCLK para APB1 e RTCCLK (sincronismo) Origens do RTCCLK: HSE clock dividido por 128 LSE LSI
RTC Dois modos de reset: APB1: reset do sistema RTC Core (Prescaler, Alarm, Counter and Divider): reset do domínio de Backup Três fontes de interrupção: Alarm interrupt Seconds interrupt Overflow interrupt
RTC
RTC Enable PWR and BKP clock(rtc is a Backup domain peipheral, so Backup interface clock should be enabled) by RCC_APB1PeriphClockCmd(RCC_APB1Periph _PWR RCC_APB1Periph_BKP, ENABLE) (see RCC). Allow access to RTC by PWR_BackupAccessCmd(ENABLE). Select RTC clock by RCC_RTCCLKConfig() (LSE,LSE/128,LSI are available.the LSE crystal is a 32.768 KHz Low Speed External crystal or ceramic resonator. see RCC).
RTC Enable RTC clock by RCC_RTCCLKCmd(ENABLE)(see RCC). Wait for RTC registers synchronization by RTC_WaitForSynchro(). Wait until last write operation on RTC registers has finished by RTC_WaitForLastTask() (After writting RTC register,you must call this function).
RTC Configure RTC interrupt by RTC_ITConfig() (RTC counter Overflow interrupt,alarm interrupt and Second interrupt(asserted on each RTC Core clock cycle) are available). Wait until last write operation on RTC registers has finished by RTC_WaitForLastTask(). Set RTC prescaler by RTC_SetPrescaler(), the clock source is based on RCC_RTCCLKConfig() configuration.
RTC Wait until last write operation on RTC registers has finished by RTC_WaitForLastTask(). Sets the RTC counter value by RTC_SetCounter(). Wait until last write operation on RTC registers has finished by RTC_WaitForLastTask(). Then you can get the RTC counter value by RTC_GetCounter().
RTC void RTC_Config(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR RCC_APB1Periph_BKP, ENABLE); PWR_BackupAccessCmd(ENABLE); RCC_LSEConfig(RCC_LSE_ON); while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); RCC_RTCCLKCmd(ENABLE); } RTC_WaitForSynchro(); RTC_WaitForLastTask(); RTC_ITConfig(RTC_IT_SEC, ENABLE); RTC_WaitForLastTask(); RTC_SetPrescaler(32767); RTC_WaitForLastTask();
Systick
Systick Contador e gerador de evento; Normalmente configurado para 1ms; Usado como base de tempo para rotinas de temporização e espera; Fácil de configurar; Uso através de uma interrupção;
Systick u32 temporizador=0; int main(void){ SysTick_Config(SystemCoreClock / 1000); //intervalos de 1ms espera(1000); //espera de 1s while(1){ } } void espera(u32 tempo_ms){ u32 tempo_t=temporizador+tempo_ms; while(tempo_t!=temporizador); } void SysTick_Handler(void){ temporizador++;//interrupção chamada a cada 1ms }
UART, SYSTICK e RTC Prática
Contato: 1º Ten Renault luiz@luizrenault.com Conclusão Grupo de Robótica Experimental do Instituto Militar de Engenharia...porque a prática é tão importante quanto a teoria