-
Notifications
You must be signed in to change notification settings - Fork 1
/
stm32f0_timer
83 lines (71 loc) · 2.55 KB
/
stm32f0_timer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include <stm32f0xx_gpio.h>
#include <stm32f0xx_rcc.h>
#include <stm32f0xx_tim.h>
//Define onboard LED’s
#define GreenLED GPIO_Pin_8
#define BlueLED GPIO_Pin_9
#define LEDGPIO GPIOC
#define LEDToggleValue ((uint16_t) 3000)
//Initialization structs
GPIO_InitTypeDef LEDs;
TIM_TimeBaseInitTypeDef TTB;
int main(void)
{
//Initialize system and ensure core clock is up to date
SystemInit();
SystemCoreClockUpdate();
//Enable GPIO Clock
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Initialize LEDs
LEDs.GPIO_Pin = GreenLED | BlueLED;
LEDs.GPIO_Mode = GPIO_Mode_OUT;
LEDs.GPIO_OType = GPIO_OType_PP;
LEDs.GPIO_PuPd = GPIO_PuPd_NOPULL;
LEDs.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(LEDGPIO, &LEDs);
//Initialize and enable timer to have a maximum period for 16bits
//running at a frequency of 48MHz/(65535*(1000 – 1)) = 0.732Hz
TTB.TIM_ClockDivision = TIM_CKD_DIV1;
TTB.TIM_CounterMode = TIM_CounterMode_Up;
TTB.TIM_Period = 0xFFFF;
TTB.TIM_Prescaler = 1000;
TTB.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TTB);
TIM_Cmd(TIM1, ENABLE);
uint16_t CurrentTimerVal = 0;
while(1)
{
//Store current timer value in variable
CurrentTimerVal = TIM_GetCounter(TIM1);
//See if current timer value is more than LED toggle value
if(CurrentTimerVal>LEDToggleValue){
GPIO_SetBits(LEDGPIO, GreenLED);
GPIO_ResetBits(LEDGPIO, BlueLED);
}
else{
GPIO_ResetBits(LEDGPIO, GreenLED);
GPIO_SetBits(LEDGPIO, BlueLED);
}
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif