Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. #include "stm32f10x.h"
  2.  
  3. volatile uint32_t time;
  4. volatile uint8_t h = 0, min = 0, sec = 0;
  5. volatile uint8_t clock[8];
  6.  
  7. USART_InitTypeDef USART_InitStructure;
  8.  
  9. volatile uint8_t flaga = 0;
  10.  
  11. uint8_t i;
  12.  
  13. void SysTick_Handler(void);
  14. void Delay(uint32_t nTime);
  15. void EXTI9_5_IRQHandler(void);
  16. void NVIC_Configuration(void);
  17. void RTC_Configuration(void);
  18. void RTC_IRQHandler(void);
  19. void USART_Configuration(void);
  20.  
  21. int main(void)
  22. {
  23. GPIO_InitTypeDef GPIO_InitStructure;
  24.  
  25. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);
  26.  
  27. /* Enable USARTy Clock */
  28. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  29.  
  30. GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
  31.  
  32. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  33. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  34. GPIO_Init(GPIOD, &GPIO_InitStructure);
  35.  
  36. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  37. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  39. GPIO_Init(GPIOD, &GPIO_InitStructure);
  40.  
  41. RTC_Configuration();
  42.  
  43. NVIC_Configuration();
  44.  
  45. USART_Configuration();
  46.  
  47. while (1)
  48. {
  49. if(flaga)
  50. {
  51. for(i = 0; i < 8; i++)
  52. {
  53. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
  54. USART_SendData(USART2, clock[i]);
  55. }
  56. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
  57. USART_SendData(USART2, 0x0D);
  58. while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET){};
  59. USART_SendData(USART2, 0x0A);
  60. flaga = 0;
  61. }
  62.  
  63. }
  64. }
  65.  
  66.  
  67.  
  68. void RTC_Configuration(void)
  69. {
  70. /* Enable PWR and BKP clocks */
  71. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  72.  
  73. /* Allow access to BKP Domain */
  74. PWR_BackupAccessCmd(ENABLE);
  75.  
  76. /* Reset Backup Domain */
  77. BKP_DeInit();
  78.  
  79. /* Enable LSE */
  80. RCC_LSEConfig(RCC_LSE_ON);
  81. /* Wait till LSE is ready */
  82. while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  83. {}
  84.  
  85. /* Select LSE as RTC Clock Source */
  86. RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
  87.  
  88. /* Enable RTC Clock */
  89. RCC_RTCCLKCmd(ENABLE);
  90.  
  91. /* Wait for RTC registers synchronization */
  92. RTC_WaitForSynchro();
  93.  
  94. /* Wait until last write operation on RTC registers has finished */
  95. RTC_WaitForLastTask();
  96.  
  97. /* Enable the RTC Second */
  98. RTC_ITConfig(RTC_IT_SEC, ENABLE);
  99.  
  100. /* Wait until last write operation on RTC registers has finished */
  101. RTC_WaitForLastTask();
  102.  
  103. /* Set RTC prescaler: set RTC period to 1sec */
  104. RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */
  105.  
  106. /* Wait until last write operation on RTC registers has finished */
  107. RTC_WaitForLastTask();
  108.  
  109. }
  110.  
  111. void NVIC_Configuration(void)
  112. {
  113. NVIC_InitTypeDef NVIC_InitStructure;
  114.  
  115. /* Configure one bit for preemption priority */
  116. NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  117.  
  118. /* Enable the RTC Interrupt */
  119. NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  120. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  121. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  122. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  123. NVIC_Init(&NVIC_InitStructure);
  124.  
  125. }
  126.  
  127. void RTC_IRQHandler(void)
  128. {
  129. if (RTC_GetITStatus(RTC_IT_SEC) != RESET)
  130. {
  131. /* Toggle LED1 */
  132. 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)));
  133. sec += 1;
  134. flaga = 1;
  135. if (sec > 60){
  136. sec = 0;
  137. min += 1;
  138. }
  139. if (min > 60){
  140. min = 0;
  141. h += 1;
  142. }
  143. if (h > 24){
  144. h = 0;
  145. }
  146. clock[2] = ':';
  147. clock[5] = ':';
  148. clock[7] = (sec%10) + '0';
  149. clock[6] = (sec/10) + '0';
  150. clock[4] = (min%10) + '0';
  151. clock[3] = (min/10) + '0';
  152. clock[1] = (h%10) + '0';
  153. clock[0] = (h/10) + '0';
  154. /* Clear Interrupt pending bit */
  155. RTC_ClearITPendingBit(RTC_FLAG_SEC);
  156. }
  157. }
  158.  
  159. void USART_Configuration(void)
  160. {
  161. USART_InitStructure.USART_BaudRate = 9600;
  162. USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  163. USART_InitStructure.USART_StopBits = USART_StopBits_1;
  164. USART_InitStructure.USART_Parity = USART_Parity_No;
  165. USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  166. USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  167. USART_Init(USART2, &USART_InitStructure);
  168. /* Enable the USARTy */
  169. USART_Cmd(USART2, ENABLE);
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement