Advertisement
Guest User

Untitled

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