Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- USART_InitTypeDef USART_InitStructure;
- uint8_t Data;
- uint8_t flag;
- uint8_t h = 0;
- uint8_t m = 0;
- uint8_t s = 0;
- uint8_t clock[10];
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_ClocksTypeDef RCC_Clocks;
- __IO uint32_t PeriodValue = 0, LsiFreq = 0;
- __IO uint32_t OperationComplete = 0;
- void NVIC_Configuration(void);
- void RCC_Configuration(void);
- void RTC_Configuration(void);
- void GPIO_Configuration(void);
- void USART_Configuration(void);
- int main(void)
- {
- RCC_Configuration();
- RTC_Configuration();
- NVIC_Configuration();
- GPIO_Configuration();
- USART_Configuration();
- while (1)
- {
- // while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET){}
- // Data = USART_ReceiveData(USART2);
- if (flag) {
- int i;
- for(i=0; i<10; i++) {
- while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){}
- USART_SendData(USART2, clock[i]);
- }
- flag = 0;
- }
- }
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable the USART2 Pins Software Remapping */
- GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
- /* Configure USARTy Rx as input floating */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* Configure USARTy Tx as alternate function push-pull */
- 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);
- }
- void RCC_Configuration(void)
- {
- /* Enable GPIO clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
- /* Enable USARTy Clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- }
- void RTC_IRQHandler(void)
- {
- if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
- {
- RTC_ClearITPendingBit(RTC_FLAG_SEC);
- s+=1;
- if (s == 60) {
- s = 0;
- if(m < 59) {
- m+=1;
- } else {
- m = 0;
- if(h < 23) {
- h+=1;
- } else {
- h =0;
- }
- }
- }
- clock[0] = h/10 + '0';
- clock[1] = h%10 + '0';
- clock[2] = ':';
- clock[3] = m/10 + '0';
- clock[4] = m%10 + '0';
- clock[5] = ':';
- clock[6] = s/10 + '0';
- clock[7] = s%10 + '0';
- clock[8] = 0x0D;
- clock[9] = 0x0A;
- flag++;
- }
- }
- 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 the LSI OSC */
- RCC_LSEConfig(RCC_LSE_ON);
- /* Wait till LSI is ready */
- while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
- {}
- /* Select the 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(32768);
- /* Wait until last write operation on RTC registers has finished */
- RTC_WaitForLastTask();
- }
- 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);
- }
- 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement