Guest User

Untitled

a guest
May 25th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.89 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    Project/STM32F10x_StdPeriph_Template/main.c
  4.   * @author  MCD Application Team
  5.   * @version V3.5.0
  6.   * @date    08-April-2011
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  19.   ******************************************************************************
  20.   */  
  21.  
  22. /* Includes ------------------------------------------------------------------*/
  23. #include "stm32f10x.h"
  24. #include "stm32_eval.h"
  25. #include <stdio.h>
  26. #include "stm3210b_eval_lcd.h"
  27. #include "main.h"
  28. #include "math.h"
  29.  
  30.   #define MESSAGE1   "STM32 Medium Density"
  31.   #define MESSAGE2   " Device running on  "
  32.   #define MESSAGE3   "   STM3210B-EVAL    "
  33.  
  34. GPIO_InitTypeDef GPIO_InitStructure;
  35. RCC_ClocksTypeDef RCC_Clocks;
  36. NVIC_InitTypeDef NVIC_InitStructure;
  37. USART_InitTypeDef USART_InitStructure;
  38.  
  39. int s;
  40. int m;
  41. int h;
  42.  
  43. double  M_PI=3.141592654;
  44. int centerX = 50;
  45. int centerY = 50;
  46. double status_degree;
  47.  
  48. double oldS, oldM, oldH;
  49.  
  50. int hr_x_coordinate,hr_y_coordinate,min_x_coordinate,min_y_coordinate,sec_x_coordinate,sec_y_coordinate;
  51.  
  52. double degree_to_radian(double degree)
  53. {
  54.     double radian;
  55.     radian=(degree*(0.0174));
  56.     return radian;
  57. }
  58.  
  59. void Draw_clock_hand(int xcoordinate,int ycoordinate)
  60. {
  61.     LCD_DrawUniLine(centerX, centerY, xcoordinate, ycoordinate);
  62. }
  63.  
  64. int min_x_coordinate_finder(int min, int radius, double degree)
  65. {
  66.     int xcoordinate,intermediate;
  67.     double actualdegree;
  68.     double radian;
  69.     actualdegree= (90-(degree*min));
  70.     status_degree=actualdegree;
  71.     radian= degree_to_radian(actualdegree);
  72.     intermediate= radius*(cos(radian));
  73.     xcoordinate= (64+intermediate);
  74.     return xcoordinate;
  75. }
  76. int min_y_coordinate_finder(int min, int radius, double degree)
  77. {
  78.     int ycoordinate,intermediate;
  79.     double actualdegree;
  80.     double radian;
  81.     actualdegree= (90-(degree*min));
  82.     status_degree=actualdegree;
  83.     radian= degree_to_radian(actualdegree);
  84.     intermediate= radius*(sin(radian));
  85.     ycoordinate= (32-intermediate);
  86.     return ycoordinate;
  87. }
  88.  
  89. int sec_x_coordinate_finder(int sec, int radius, double degree)
  90. {
  91.     int ycoordinate,intermediate;
  92.     double actualdegree;
  93.     double radian;
  94.     actualdegree= (90-(degree*sec));
  95.     radian= degree_to_radian(actualdegree);
  96.     intermediate= radius*(cos(radian));
  97.     ycoordinate= (64+intermediate);
  98.     return ycoordinate;
  99. }
  100.  
  101. int sec_y_coordinate_finder(int sec, int radius, double degree)
  102. {
  103.     int ycoordinate,intermediate;
  104.     double actualdegree;
  105.     double radian;
  106.     actualdegree= (90-(degree*sec));
  107.     radian= degree_to_radian(actualdegree);
  108.     intermediate= radius*(sin(radian));
  109.     ycoordinate= (32-intermediate);
  110.     return ycoordinate;
  111. }
  112.  
  113. int hr_x_coordinate_finder(int hr, int radius, double degree)
  114. {
  115.     int xcoordinate,intermediate;
  116.     double actualdegree;
  117.     double radian;
  118.     actualdegree= (90-(degree*hr));
  119.     actualdegree= (actualdegree-((90-status_degree)/12));
  120.     radian= degree_to_radian(actualdegree);
  121.     intermediate= radius*(cos(radian));
  122.     xcoordinate= (64+intermediate);
  123.     return xcoordinate;
  124. }
  125.  
  126. int hr_y_coordinate_finder(int hr, int radius, double degree)
  127. {
  128.     int ycoordinate,intermediate;
  129.     double actualdegree;
  130.     double radian;
  131.     actualdegree= (90-(degree*hr));
  132.     actualdegree= (actualdegree-((90-status_degree)/12));
  133.     radian= degree_to_radian(actualdegree);
  134.     intermediate= radius*(sin(radian));
  135.     ycoordinate= (32-intermediate);
  136.     return ycoordinate;
  137. }
  138.  
  139. /*
  140. double angle = 0;
  141. double radius = 50;
  142.  
  143. double hourSize = .9;
  144. double minuteSize = .6;
  145. double secondSize = .3;
  146.  
  147. double xCoord = 50;
  148. double yCoord = 50;
  149.  
  150. double xCoordHour, yCoordHour;
  151. double xCoordSecond, yCoordSecond;
  152. double xCoordMinute, yCoordMinute;
  153.  
  154. double oldS, oldM, oldH;
  155.  
  156. void drawClock()
  157. {
  158. //godziny
  159. angle = ((M_PI / 2) - ((double)h)/12 * 2 *M_PI);
  160. xCoordHour = xCoord + (int)(radius * cos(angle) * hourSize);
  161. yCoordHour = yCoord - (int)(radius * sin(angle) * hourSize);
  162. LCD_DrawUniLine(xCoord, yCoord, xCoordHour, yCoordHour);
  163.  
  164.  
  165. //minuty
  166. angle = ((M_PI / 2) - (((double)m)/60) * 2 * M_PI);
  167. xCoordMinute = xCoord + (int)(radius * cos(angle) * minuteSize);
  168. yCoordMinute = yCoord - (int)(radius * sin(angle) * minuteSize);
  169. LCD_DrawUniLine(xCoord, yCoord, xCoordMinute, yCoordMinute);
  170.  
  171.  
  172. //sekundy
  173. angle = ((M_PI / 2) - (((double)s)/60) * 2 * M_PI);
  174. xCoordSecond = xCoord + (int)(radius * cos(angle) * secondSize);
  175. yCoordSecond = yCoord - (int)(radius * sin(angle) * secondSize);
  176. LCD_DrawUniLine(xCoord, yCoord, xCoordSecond, yCoordSecond);
  177.  
  178. oldS = s;
  179. oldM = m;
  180. oldH = h;
  181.  
  182. }
  183.  
  184. void clearClock()
  185. {
  186. //godziny
  187. angle = ((M_PI / 2) - ((double)oldH)/12 * 2 *M_PI);
  188. xCoordHour = xCoord + (int)(radius * cos(angle) * hourSize);
  189. yCoordHour = yCoord + (int)(radius * sin(angle) * hourSize);
  190. LCD_DrawUniLine(xCoord, yCoord, xCoordHour, yCoordHour);
  191.  
  192.  
  193. //minuty
  194. angle = ((M_PI / 2) - (((double)oldM)/60) * 2 * M_PI);
  195. xCoordMinute = xCoord + (int)(radius * cos(angle) * minuteSize);
  196. yCoordMinute = yCoord - (int)(radius * sin(angle) * minuteSize);
  197. LCD_DrawUniLine(xCoord, yCoord, xCoordMinute, yCoordMinute);
  198.  
  199.  
  200. //sekundy
  201. angle = ((M_PI / 2) - (((double)oldS)/60) * 2 * M_PI);
  202. xCoordSecond = xCoord + (int)(radius * cos(angle) * secondSize);
  203. yCoordSecond = yCoord - (int)(radius * sin(angle) * secondSize);
  204. LCD_DrawUniLine(xCoord, yCoord, xCoordSecond, yCoordSecond);
  205.  
  206. }
  207.   */
  208. int main(void)
  209. {
  210.   STM_EVAL_LEDInit(LED1);
  211.   STM_EVAL_LEDInit(LED2);
  212.   STM_EVAL_LEDInit(LED3);
  213.   STM_EVAL_LEDInit(LED4);
  214.  
  215.   USART_InitStructure.USART_BaudRate = 115200;
  216.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  217.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  218.   USART_InitStructure.USART_Parity = USART_Parity_No;
  219.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  220.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  221.  
  222.   STM_EVAL_COMInit(COM1, &USART_InitStructure);
  223.  
  224.   STM3210B_LCD_Init();
  225.   LCD_Clear(LCD_COLOR_WHITE);
  226.   LCD_SetBackColor(LCD_COLOR_BLUE);
  227.   LCD_SetTextColor(LCD_COLOR_WHITE);
  228.   //LCD_DisplayStringLine(LCD_LINE_0, (uint8_t *)MESSAGE1);
  229.   //LCD_DisplayStringLine(LCD_LINE_1, (uint8_t *)MESSAGE2);
  230.   //LCD_DisplayStringLine(LCD_LINE_2, (uint8_t *)MESSAGE3);
  231.  
  232.   STM_EVAL_LEDOn(LED1);
  233.   STM_EVAL_LEDOn(LED2);
  234.   STM_EVAL_LEDOn(LED3);
  235.   STM_EVAL_LEDOn(LED4);
  236.  
  237.     /* Enable PWR and BKP clocks */
  238.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  239.  
  240.   /* Allow access to BKP Domain */
  241.   PWR_BackupAccessCmd(ENABLE);
  242.  
  243.   /* Reset Backup Domain */
  244.   BKP_DeInit();
  245.  
  246.   /* Enable the LSI OSC */
  247.   RCC_LSICmd(ENABLE);
  248.   /* Wait till LSI is ready */
  249.   while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET){}
  250.  
  251.   /* Select the RTC Clock Source */
  252.   RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
  253.  
  254.   /* Enable RTC Clock */
  255.   RCC_RTCCLKCmd(ENABLE);
  256.  
  257.   /* Wait for RTC registers synchronization */
  258.   RTC_WaitForSynchro();
  259.  
  260.   /* Wait until last write operation on RTC registers has finished */
  261.   RTC_WaitForLastTask();
  262.  
  263.   /* Enable the RTC Second */
  264.   RTC_ITConfig(RTC_IT_SEC, ENABLE);
  265.  
  266.   /* Wait until last write operation on RTC registers has finished */
  267.   RTC_WaitForLastTask();
  268.  
  269.   /* Set RTC prescaler: set RTC period to 1sec */
  270.   RTC_SetPrescaler(40000);
  271.  
  272.   /* Wait until last write operation on RTC registers has finished */
  273.   RTC_WaitForLastTask();
  274.  
  275.   /* To output second signal on Tamper pin, the tamper functionality
  276.        must be disabled (by default this functionality is disabled) */
  277.   BKP_TamperPinCmd(DISABLE);
  278.  
  279.   /* Enable the RTC Second Output on Tamper Pin */
  280.   BKP_RTCOutputConfig(BKP_RTCOutputSource_Second);
  281.  
  282.   /* Adjust the RTC prescaler value */
  283.   RTC_SetPrescaler(32768 - 1);
  284.  
  285.   /* Wait until last write operation on RTC registers has finished */
  286.   RTC_WaitForLastTask();
  287.  
  288.   /* Configure one bit for preemption priority */
  289.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  290.  
  291.   /* Enable the RTC Interrupt */
  292.   NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
  293.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  294.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  295.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  296.   NVIC_Init(&NVIC_InitStructure);
  297.  
  298.   /* Infinite loop */
  299.   while (1)
  300.   {
  301.     if(Trigger == 1)
  302.     {
  303.     Trigger = 0;
  304.  
  305.     //przekazywanie poprzednich wartosci by malowac wskazowki na kolor tla
  306.     oldS = s;
  307.     oldM = m;
  308.     oldH = h;
  309.  
  310.     //inkrementacja czasowa
  311.     s++;
  312.     if(s==60)
  313.     {
  314.         s=0;
  315.         m++;
  316.     }
  317.     if(m==60)
  318.     {
  319.         m=0;
  320.         h++;
  321.     }
  322.     if(h==24)
  323.     {
  324.         h=0;
  325.     }
  326.  
  327.     LCD_SetTextColor(LCD_COLOR_RED);
  328.     LCD_DrawCircle(centerX,centerY,30);
  329.  
  330.     //zamalowanie wskazowek na bialo
  331.     LCD_SetTextColor(LCD_COLOR_WHITE);
  332.     if(oldH<12)
  333.     {
  334.         hr_x_coordinate= hr_x_coordinate_finder(oldH,10,30);
  335.         hr_y_coordinate= hr_y_coordinate_finder(oldH,10,30);
  336.         Draw_clock_hand(hr_x_coordinate,hr_y_coordinate);
  337.     }
  338.     else if(oldH >= 12)
  339.     {
  340.         hr_x_coordinate= hr_x_coordinate_finder( (oldH-12),10,30);
  341.         hr_y_coordinate= hr_y_coordinate_finder( (oldH-12),10,30);
  342.         Draw_clock_hand(hr_x_coordinate,hr_y_coordinate);
  343.     }
  344.  
  345.     min_x_coordinate=min_x_coordinate_finder(oldM,30,6);
  346.     min_y_coordinate=min_y_coordinate_finder(oldM,30,6);
  347.     Draw_clock_hand(min_x_coordinate,min_y_coordinate);
  348.    
  349.     sec_x_coordinate=sec_x_coordinate_finder(oldS,20,6);
  350.     sec_y_coordinate=sec_y_coordinate_finder(oldS,20,6);
  351.     Draw_clock_hand(sec_x_coordinate,sec_y_coordinate);
  352.  
  353.     //rysowanie wskazowek
  354.     LCD_SetTextColor(LCD_COLOR_GREEN);   //kolor zielony
  355.     if(h<12)
  356.     {
  357.         hr_x_coordinate= hr_x_coordinate_finder(h,10,30);
  358.         hr_y_coordinate= hr_y_coordinate_finder(h,10,30);
  359.         Draw_clock_hand(hr_x_coordinate,hr_y_coordinate);
  360.     }
  361.     else if(h >= 12)
  362.     {
  363.         hr_x_coordinate= hr_x_coordinate_finder( (h-12),10,30);
  364.         hr_y_coordinate= hr_y_coordinate_finder( (h-12),10,30);
  365.         Draw_clock_hand(hr_x_coordinate,hr_y_coordinate);
  366.     }
  367.  
  368.     min_x_coordinate=min_x_coordinate_finder(m,30,6);
  369.     min_y_coordinate=min_y_coordinate_finder(m,30,6);
  370.     Draw_clock_hand(min_x_coordinate,min_y_coordinate);
  371.    
  372.     sec_x_coordinate=sec_x_coordinate_finder(s,20,6);
  373.     sec_y_coordinate=sec_y_coordinate_finder(s,20,6);
  374.     Draw_clock_hand(sec_x_coordinate,sec_y_coordinate);
  375.  
  376. //  LCD_SetTextColor(LCD_COLOR_WHITE);
  377. //  clearClock();
  378. //  LCD_SetTextColor(LCD_COLOR_RED);
  379. //  drawClock();
  380. //  LCD_SetTextColor(LCD_COLOR_WHITE);
  381.  
  382.  
  383.  
  384.  
  385.       /* Add your application code here
  386.          */
  387.  
  388.  
  389.     }
  390.   }
  391. }
  392.  
  393. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
Add Comment
Please, Sign In to add comment