1 | #include "stm32f0xx.h"
|
2 | #include "system_stm32f0xx.h"
|
3 | #include "stm32f0xx_gpio.h"
|
4 | #include "stm32f0xx_rcc.h"
|
5 | #include "stm32f0xx_tim.h"
|
6 | #include "stm32f0xx_misc.h"
|
7 |
|
8 | // Function Prototypes
|
9 | void ClockInit(void);
|
10 | void Timer1Init(void);
|
11 |
|
12 | // Read and refresh frequency in ms
|
13 | const uint16_t READ_FREQUENCE = 250;
|
14 |
|
15 | volatile uint8_t job;
|
16 | volatile uint32_t sensor_counter;
|
17 |
|
18 | int main(void)
|
19 | {
|
20 | //Init Variables
|
21 | job = 0x00;
|
22 |
|
23 | //Init the main clock
|
24 | SystemInit();
|
25 |
|
26 | //Init Periph Clock
|
27 | ClockInit();
|
28 |
|
29 | //Init Timer1
|
30 | Timer1Init();
|
31 |
|
32 | //Systick Config running MCU with 48 MHz generates 1ms Tick
|
33 | SysTick_Config(48000);
|
34 |
|
35 |
|
36 | while(1)
|
37 | {
|
38 | //Check if sensor must be read out
|
39 | if ((job & 0x01) == 1)
|
40 | {
|
41 | job = job ^ 0x01;
|
42 | sensor_counter = TIM_GetCounter(TIM1);
|
43 | TIM_SetCounter(TIM1,0);
|
44 | }
|
45 | }
|
46 |
|
47 | }
|
48 |
|
49 | void ClockInit(void)
|
50 | {
|
51 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
52 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
|
53 | }
|
54 |
|
55 | void Timer1Init(void)
|
56 | {
|
57 | GPIO_InitTypeDef GPIO_InitStructure;
|
58 | TIM_TimeBaseInitTypeDef TIM_InitStructure;
|
59 |
|
60 | // Config Timer1 Pin
|
61 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
62 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
63 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
64 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
65 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
66 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
67 |
|
68 | // Config Timer1 as Counter
|
69 | TIM_InitStructure.TIM_ClockDivision = 0;
|
70 | TIM_InitStructure.TIM_Period = 0xFFFF;
|
71 | TIM_InitStructure.TIM_Prescaler = 0;
|
72 | TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
73 | TIM_TimeBaseInit(TIM1, &TIM_InitStructure);
|
74 |
|
75 | TIM_TIxExternalClockConfig(TIM1, TIM_TS_TI2FP2, TIM_ICPolarity_Rising, 0);
|
76 | TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_External1);
|
77 |
|
78 | TIM_Cmd(TIM1,ENABLE);
|
79 | }
|
80 |
|
81 |
|
82 | void SysTick_Handler(void)
|
83 | {
|
84 | static uint16_t read_freq_counter;
|
85 |
|
86 | // Is used to check if a new Sensor readout must happen
|
87 | read_freq_counter++;
|
88 | if (read_freq_counter >= READ_FREQUENCE)
|
89 | {
|
90 | read_freq_counter = 0;
|
91 | if ((job & 0x01) != 0x01) job = job ^ 0x01;
|
92 | }
|
93 | }
|