1 | #include <stm32f30x.h>
|
2 |
|
3 | #include <stm32f30x_gpio.h>
|
4 | #include <stm32f30x_rcc.h>
|
5 | #include <stm32f30x_tim.h> // for Timer
|
6 |
|
7 |
|
8 | // prototypes
|
9 | void InitializeTimer(int period);
|
10 | void InitializeLEDs(void);
|
11 | void InitializePWMChannel(void);
|
12 | void RCC_Configuration(void);
|
13 |
|
14 |
|
15 | void InitializeTimer(int period)
|
16 | {
|
17 | // 1kHz Signal
|
18 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
19 |
|
20 | TIM_TimeBaseInitTypeDef timerInitStructure;
|
21 | timerInitStructure.TIM_Prescaler = 71;
|
22 | timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
23 | timerInitStructure.TIM_Period = period;
|
24 | timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
25 | //timerInitStructure.TIM_RepetitionCounter = 0;
|
26 | TIM_TimeBaseInit(TIM2, &timerInitStructure);
|
27 | TIM_Cmd(TIM2, ENABLE);
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 | void InitializePWMChannel()
|
33 | {
|
34 | TIM_OCInitTypeDef outputChannelInit; // = {0,};
|
35 | outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
|
36 | outputChannelInit.TIM_OCIdleState = TIM_OCIdleState_Reset;
|
37 | outputChannelInit.TIM_OCNIdleState = TIM_OCNIdleState_Set;
|
38 | outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
|
39 | outputChannelInit.TIM_OutputNState = TIM_OutputNState_Disable;
|
40 | outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
|
41 | outputChannelInit.TIM_OCNPolarity = TIM_OCNPolarity_High;
|
42 | outputChannelInit.TIM_Pulse = 100;
|
43 |
|
44 | TIM_OC1Init(TIM2, &outputChannelInit);
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 | void InitializeLEDs()
|
50 | {
|
51 | GPIO_InitTypeDef gpioStructure;
|
52 | gpioStructure.GPIO_Pin = GPIO_Pin_0;
|
53 | gpioStructure.GPIO_Mode = GPIO_Mode_AF;
|
54 | gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
55 | GPIO_Init(GPIOA, &gpioStructure);
|
56 | //GPIO_PinAFConfig (GPIOA, GPIO_PinSource0, GPIO_AF_2); //??
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | void RCC_Configuration()
|
62 | {
|
63 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // Enable peripheral clock for Timer2
|
64 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable peripheral clock for GPIOA
|
65 | }
|
66 |
|
67 |
|
68 |
|
69 | int main ()
|
70 | {
|
71 | RCC_Configuration();
|
72 |
|
73 | InitializeLEDs();
|
74 | InitializeTimer(999);
|
75 | InitializePWMChannel();
|
76 |
|
77 |
|
78 |
|
79 | while(1)
|
80 | {
|
81 |
|
82 | } //while(1)
|
83 | } //main
|