Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- volatile uint32_t time;
- volatile uint8_t h = 0, min = 0, sec = 0;
- volatile uint8_t clock[8];
- USART_InitTypeDef USART_InitStructure;
- volatile uint8_t flaga = 0;
- uint8_t i;
- void SysTick_Handler(void);
- void Delay(uint32_t nTime);
- void EXTI9_5_IRQHandler(void);
- void NVIC_Configuration(void);
- void RTC_Configuration(void);
- void RTC_IRQHandler(void);
- void USART_Configuration(void);
- int main(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
- /* Enable USARTy Clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- RTC_Configuration();
- NVIC_Configuration();
- USART_Configuration();
- while (1)
- {
- if(flaga)
- {
- for(i = 0; i < 8; i++)
- {
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
- USART_SendData(USART2, clock[i]);
- }
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
- USART_SendData(USART2, 0x0D);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
- USART_SendData(USART2, 0x0A);
- flaga = 0;
- }
- }
- }
- void RTC_Configuration(void)
- {
- /* Enable PWR and BKP clocks */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
- /* Allow access to BKP Domain */
- PWR_BackupAccessCmd(ENABLE);
- /* Reset Backup Domain */
- BKP_DeInit();
- /* Enable LSE */
- RCC_LSEConfig(RCC_LSE_ON);
- /* Wait till LSE is ready */
- while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
- {}
- /* Select LSE as RTC Clock Source */
- RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
- /* Enable RTC Clock */
- RCC_RTCCLKCmd(ENABLE);
- /* Wait for RTC registers synchronization */
- RTC_WaitForSynchro();
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- /* Enable the RTC Second */
- RTC_ITConfig(RTC_IT_SEC, ENABLE);
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- /* Set RTC prescaler: set RTC period to 1sec */
- RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Configure one bit for preemption priority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
- /* Enable the RTC Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void RTC_IRQHandler(void)
- {
- if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
- {
- /* Toggle LED1 */
- GPIO_WriteBit(GPIOD, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7 | GPIO_Pin_13, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7 | GPIO_Pin_13)));
- sec += 1;
- flaga = 1;
- if (sec > 60){
- sec = 0;
- min += 1;
- }
- if (min > 60){
- min = 0;
- h += 1;
- }
- if (h > 24){
- h = 0;
- }
- clock[2] = ':';
- clock[5] = ':';
- clock[7] = (sec%10) + '0';
- clock[6] = (sec/10) + '0';
- clock[4] = (min%10) + '0';
- clock[3] = (min/10) + '0';
- clock[1] = (h%10) + '0';
- clock[0] = (h/10) + '0';
- /* Clear Interrupt pending bit */
- RTC_ClearITPendingBit(RTC_FLAG_SEC);
- }
- }
- void USART_Configuration(void)
- {
- USART_InitStructure.USART_BaudRate = 9600;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_Init(USART2, &USART_InitStructure);
- /* Enable the USARTy */
- USART_Cmd(USART2, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement