Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************************Necessary Calculation********************************************/
- (i) calculate the busclock period
- Tbus = 1 / fbus = 1 / (48 000 000) = 20.83ns
- (ii) calculate the no. of cycles need for required delay
- No. cycles = Delay / Tbus = 0.01 / (20.83x10^-9) = 480000
- (iii) calculate the Prescaler
- Prescaler = No. cycles/2^16
- = 48x10^4 / 65536 = 7.32 Rounded up value being 8
- (iv) calculate the ARR value
- ARR = No. cycles/Prescaler = 48x10^4 / 8 = 60000
- /************************************************************************************************/
- //********************************************************************
- //* EEE2046F C template *
- //*==================================================================*
- //* WRITTEN BY: David Kheri *
- //* DATE CREATED:02/05/2018 *
- //* MODIFIED:N/A *
- //*==================================================================*
- //* PROGRAMMED IN: Eclipse Luna Service Release 1 (4.4.1) *
- //* DEV. BOARD: UCT STM32 Development Board *
- //* TARGET: STMicroelectronics STM32F051C6 *
- //*==================================================================*
- //* DESCRIPTION: *
- //* *
- //********************************************************************
- // INCLUDE FILES
- //====================================================================
- #include "lcd_stm32f0.h"
- #include "stm32f0xx.h"
- //====================================================================
- // SYMBOLIC CONSTANTS
- //====================================================================
- #define DELAY1 255
- #define DELAY2 5000
- #define SW0 GPIO_IDR_0
- #define SW1 GPIO_IDR_1
- #define SW2 GPIO_IDR_2
- #define SW3 GPIO_IDR_3
- //====================================================================
- // GLOBAL VARIABLES
- //====================================================================
- unsigned int bitpattern = 0;
- int minutes=0;
- int seconds=0;
- int hundreths=0;
- int temp_display_minutes=0;
- int temp_display_seconds=0;
- int temp_display_hundreths=0;
- int display_temp_time_flag=0;
- //====================================================================
- // FUNCTION DECLARATIONS
- //====================================================================
- void init_ports();
- void init_TIM14();
- void Delay();
- void init_NVIC();
- void check_pb();
- void display();
- void updateTime();
- void welcome_message();
- void reinit_values();
- //====================================================================
- // MAIN FUNCTION
- //====================================================================
- void main(void) {
- init_LCD();
- init_ports();
- lcd_putstring("Stop Watch");
- lcd_command(LINE_TWO);
- lcd_putstring("Press SW0");
- while((GPIOA->IDR&SW0)!=0);
- init_TIM14();
- init_NVIC();// Initialise lcd
- GPIOB->ODR=1;
- for (;;) {
- //Turn PB0 on and keep it on
- check_pb();
- /*should be check pb ideally*/
- // display();
- }
- }// End of main
- ////====================================================================
- //// FUNCTION DEFINITIONS
- ////====================================================================
- void init_ports() {
- //Enable the clock for Port A so as to enable push buttons use
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- //setting the Push Buttons in Input Mode
- GPIOA->MODER &= ~(GPIO_MODER_MODER0 |
- GPIO_MODER_MODER1 |
- GPIO_MODER_MODER2 |
- GPIO_MODER_MODER3);
- //enable pull-up resistors, GPIOA_PUPDRx = 01
- GPIOA->PUPDR |= (GPIO_PUPDR_PUPDR0_0 |
- GPIO_PUPDR_PUPDR1_0 |
- GPIO_PUPDR_PUPDR2_0 |
- GPIO_PUPDR_PUPDR3_0);
- //enable clock for Port B
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- //setting the LEDS(0-3) in Output Mode
- GPIOB->MODER |= 0x00000055; //LED 0-4 initialized in OutPut Mode
- }
- void init_TIM14()
- {
- // connect the APB1 bus clock to the TIM14
- RCC->APB1ENR |= RCC_APB1ENR_TIM14EN;
- //set auto reload register (ARR) and Prescaler (PSC) for required delay
- TIM14->PSC = 8;
- TIM14->ARR = 60000;
- //enable Interupts
- TIM14->DIER |= TIM_DIER_UIE;
- //enable the Timer(start the Timer)
- TIM14->CR1 |= TIM_CR1_CEN;
- }
- void init_NVIC()
- {
- //Enable TIM14 interrupts
- NVIC_EnableIRQ(TIM14_IRQn);
- }
- void TIM14_IRQHandler(void)
- {
- //update the Time values
- updateTime();
- // call display() to put time Values on the LCD
- display();
- //reset the counter;
- TIM14->SR &= ~TIM_SR_UIF;
- }
- void updateTime()
- {
- hundreths++;
- if(hundreths==100)
- {
- seconds++;
- hundreths=0;
- }
- if(seconds==60)
- {
- minutes++;
- seconds=0;
- }
- if(minutes==60)
- {
- minutes=0;
- }
- }
- void display()
- {
- char time[8];
- if(display_temp_time_flag==0)
- {sprintf(time,"%02d:%02d:%02d",minutes,seconds,hundreths);}
- else
- {sprintf(time,"%02d:%02d:%02d",temp_display_minutes,temp_display_seconds,temp_display_hundreths);}
- lcd_command(CLEAR);
- lcd_putstring("Time");
- lcd_command(LINE_TWO);
- lcd_putstring(time);
- }
- void check_pb()
- {
- if((GPIOA->IDR & SW0)==0)
- {
- TIM14->SR &= ~TIM_SR_UIF;
- TIM14->CR1 |= TIM_CR1_CEN;
- display_temp_time_flag=0;
- GPIOB->ODR=1;
- }
- if((GPIOA->IDR & SW1)==0)
- {
- temp_display_minutes=minutes;
- temp_display_seconds=seconds;
- temp_display_hundreths=hundreths;
- display_temp_time_flag=1;
- GPIOB->ODR=1<<1;
- }
- if((GPIOA->IDR & SW2)==0)
- {
- display_temp_time_flag=0;
- TIM14->CR1 &= ~TIM_CR1_CEN;
- GPIOB->ODR=1<<2;
- }
- if((GPIOA->IDR & SW3)==0)
- {
- TIM14->CR1 &= ~TIM_CR1_CEN;
- reinit_values();
- welcome_message();
- display_temp_time_flag=0;
- GPIOB->ODR=1<<3;
- }
- }
- void reinit_values()
- {
- hundreths=0;minutes=0;seconds=0;
- }
- void welcome_message()
- {
- lcd_command(CLEAR);
- lcd_putstring("Stop Watch");
- lcd_command(LINE_TWO);
- lcd_putstring("Press SW0");
- }
- //Delay function used to delay counting with 1 second
- void Delay() {
- for (int i = 0; i < DELAY1; i++) {
- for (int j = 0; j < DELAY2; j++) {
- __asm__("nop");
- }
- }
- }
- //********************************************************************
- // END OF PROGRAM
- //********************************************************************
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement