Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.31 KB | None | 0 0
  1. /*******************************Necessary Calculation********************************************/
  2. (i) calculate the busclock period
  3.          Tbus = 1 / fbus = 1 / (48 000 000) = 20.83ns
  4. (ii) calculate the no. of cycles need for required delay
  5.          No. cycles = Delay / Tbus = 0.01 / (20.83x10^-9) = 480000
  6.  (iii) calculate the Prescaler
  7.  Prescaler = No. cycles/2^16
  8.  = 48x10^4 / 65536 = 7.32 Rounded up value being 8
  9.  (iv) calculate the ARR value
  10.  ARR = No. cycles/Prescaler = 48x10^4 / 8 = 60000
  11.  
  12. /************************************************************************************************/
  13.  
  14. //********************************************************************
  15. //*                    EEE2046F C template                           *
  16. //*==================================================================*
  17. //* WRITTEN BY: David Kheri                                          *
  18. //* DATE CREATED:02/05/2018                                          *
  19. //* MODIFIED:N/A                                                     *
  20. //*==================================================================*
  21. //* PROGRAMMED IN: Eclipse Luna Service Release 1 (4.4.1)            *
  22. //* DEV. BOARD:    UCT STM32 Development Board                       *
  23. //* TARGET:    STMicroelectronics STM32F051C6                        *
  24. //*==================================================================*
  25. //* DESCRIPTION:                                                     *
  26. //*                                                                  *
  27. //********************************************************************
  28. // INCLUDE FILES
  29. //====================================================================
  30. #include "lcd_stm32f0.h"
  31. #include "stm32f0xx.h"
  32. //====================================================================
  33. // SYMBOLIC CONSTANTS
  34. //====================================================================
  35. #define DELAY1 255
  36. #define DELAY2 5000
  37.  
  38. #define SW0 GPIO_IDR_0
  39. #define SW1 GPIO_IDR_1
  40. #define SW2 GPIO_IDR_2
  41. #define SW3 GPIO_IDR_3
  42.  
  43.  
  44. //====================================================================
  45. // GLOBAL VARIABLES
  46. //====================================================================
  47. unsigned int bitpattern = 0;
  48. int minutes=0;
  49. int seconds=0;
  50. int hundreths=0;
  51. int temp_display_minutes=0;
  52. int temp_display_seconds=0;
  53. int temp_display_hundreths=0;
  54. int display_temp_time_flag=0;
  55. //====================================================================
  56. // FUNCTION DECLARATIONS
  57. //====================================================================
  58. void init_ports();
  59. void init_TIM14();
  60. void Delay();
  61. void init_NVIC();
  62. void check_pb();
  63. void display();
  64. void updateTime();
  65. void welcome_message();
  66. void reinit_values();
  67. //====================================================================
  68. // MAIN FUNCTION
  69. //====================================================================
  70. void main(void) {
  71.  
  72.     init_LCD();
  73.     init_ports();
  74.     lcd_putstring("Stop Watch");
  75.     lcd_command(LINE_TWO);
  76.     lcd_putstring("Press SW0");
  77.     while((GPIOA->IDR&SW0)!=0);
  78.     init_TIM14();
  79.     init_NVIC();// Initialise lcd
  80.     GPIOB->ODR=1;
  81.     for (;;) {
  82.         //Turn PB0 on and keep it on
  83.         check_pb();
  84.         /*should be check pb ideally*/
  85. //  display();
  86.     }
  87. }// End of main
  88. ////====================================================================
  89. //// FUNCTION DEFINITIONS
  90. ////====================================================================
  91. void init_ports() {
  92.  
  93. //Enable the clock for Port A so as to enable push buttons use
  94.     RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  95.  
  96. //setting the Push Buttons in Input Mode
  97.     GPIOA->MODER &= ~(GPIO_MODER_MODER0 |
  98.     GPIO_MODER_MODER1 |
  99.     GPIO_MODER_MODER2 |
  100.     GPIO_MODER_MODER3);
  101.  
  102. //enable pull-up resistors, GPIOA_PUPDRx = 01
  103.     GPIOA->PUPDR |= (GPIO_PUPDR_PUPDR0_0 |
  104.     GPIO_PUPDR_PUPDR1_0 |
  105.     GPIO_PUPDR_PUPDR2_0 |
  106.     GPIO_PUPDR_PUPDR3_0);
  107.  
  108. //enable clock for Port B
  109.     RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  110.  
  111.     //setting the LEDS(0-3) in Output Mode
  112.     GPIOB->MODER |= 0x00000055;  //LED 0-4 initialized in OutPut Mode
  113.  
  114. }
  115. void init_TIM14()
  116. {
  117.         // connect the APB1 bus clock to the TIM14
  118.         RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
  119.         //set auto reload register (ARR) and Prescaler (PSC) for required delay
  120.         TIM14->PSC = 8;
  121.         TIM14->ARR = 60000;
  122.         //enable Interupts
  123.         TIM14->DIER |= TIM_DIER_UIE;
  124.         //enable the Timer(start the Timer)
  125.         TIM14->CR1 |= TIM_CR1_CEN;
  126. }
  127. void init_NVIC()
  128. {
  129.     //Enable TIM14 interrupts
  130.     NVIC_EnableIRQ(TIM14_IRQn);
  131. }
  132. void TIM14_IRQHandler(void)
  133.  {
  134.     //update the Time values
  135.     updateTime();
  136.     // call display() to put time Values on the LCD
  137.     display();
  138.     //reset the counter;
  139.     TIM14->SR &= ~TIM_SR_UIF;
  140.  }
  141. void updateTime()
  142. {
  143.     hundreths++;
  144.     if(hundreths==100)
  145.     {
  146.         seconds++;
  147.         hundreths=0;
  148.     }
  149.     if(seconds==60)
  150.     {
  151.         minutes++;
  152.         seconds=0;
  153.     }
  154.     if(minutes==60)
  155.     {
  156.         minutes=0;
  157.     }
  158.  
  159. }
  160.  
  161. void display()
  162. {
  163.     char time[8];
  164.     if(display_temp_time_flag==0)
  165.     {sprintf(time,"%02d:%02d:%02d",minutes,seconds,hundreths);}
  166.     else
  167.     {sprintf(time,"%02d:%02d:%02d",temp_display_minutes,temp_display_seconds,temp_display_hundreths);}
  168.     lcd_command(CLEAR);
  169.     lcd_putstring("Time");
  170.     lcd_command(LINE_TWO);
  171.     lcd_putstring(time);
  172. }
  173.  
  174.  
  175.  
  176.  
  177. void check_pb()
  178. {
  179.     if((GPIOA->IDR & SW0)==0)
  180.     {
  181.     TIM14->SR &= ~TIM_SR_UIF;
  182.     TIM14->CR1 |= TIM_CR1_CEN;
  183.     display_temp_time_flag=0;
  184.     GPIOB->ODR=1;
  185.     }
  186.  
  187.     if((GPIOA->IDR & SW1)==0)
  188.     {
  189.         temp_display_minutes=minutes;
  190.         temp_display_seconds=seconds;
  191.         temp_display_hundreths=hundreths;
  192.         display_temp_time_flag=1;
  193.         GPIOB->ODR=1<<1;
  194.     }
  195.     if((GPIOA->IDR & SW2)==0)
  196.     {
  197.         display_temp_time_flag=0;
  198.         TIM14->CR1 &= ~TIM_CR1_CEN;
  199.         GPIOB->ODR=1<<2;
  200.     }
  201.  
  202.     if((GPIOA->IDR & SW3)==0)
  203.         {
  204.             TIM14->CR1 &= ~TIM_CR1_CEN;
  205.             reinit_values();
  206.             welcome_message();
  207.             display_temp_time_flag=0;
  208.             GPIOB->ODR=1<<3;
  209.         }
  210. }
  211.  
  212. void reinit_values()
  213. {
  214.     hundreths=0;minutes=0;seconds=0;
  215. }
  216.  
  217. void welcome_message()
  218. {
  219.     lcd_command(CLEAR);
  220.     lcd_putstring("Stop Watch");
  221.     lcd_command(LINE_TWO);
  222.     lcd_putstring("Press SW0");
  223. }
  224. //Delay function used to delay counting with 1 second
  225. void Delay() {
  226.     for (int i = 0; i < DELAY1; i++) {
  227.         for (int j = 0; j < DELAY2; j++) {
  228.             __asm__("nop");
  229.         }
  230.     }
  231.   }
  232.  
  233. //********************************************************************
  234. // END OF PROGRAM
  235. //********************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement