1 | void InitializeLEDs()
|
2 | {
|
3 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
|
4 |
|
5 | GPIO_InitTypeDef gpioStructure;
|
6 | gpioStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6;
|
7 | gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
|
8 | gpioStructure.GPIO_Speed = GPIO_Fast_Speed;
|
9 | GPIO_Init(GPIOA, &gpioStructure);
|
10 |
|
11 | GPIO_WriteBit(GPIOA, GPIO_Pin_7, Bit_RESET);
|
12 | GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_RESET);
|
13 | }
|
14 |
|
15 | void InitializeTimer()
|
16 | {
|
17 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
18 |
|
19 | TIM_TimeBaseInitTypeDef timerInitStructure;
|
20 | timerInitStructure.TIM_Prescaler = 8400;
|
21 | timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
22 | timerInitStructure.TIM_Period = 10000;
|
23 | timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
24 | timerInitStructure.TIM_RepetitionCounter = 0;
|
25 | TIM_TimeBaseInit(TIM2, &timerInitStructure);
|
26 |
|
27 |
|
28 | NVIC_InitTypeDef nvicStructure;
|
29 |
|
30 | nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
|
31 | nvicStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
|
32 | nvicStructure.NVIC_IRQChannelSubPriority = 0x0F;
|
33 | nvicStructure.NVIC_IRQChannelCmd = ENABLE;
|
34 |
|
35 | TIM_Cmd(TIM2, ENABLE);
|
36 |
|
37 | // Bleibt hängen!
|
38 | GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_SET); // klappt
|
39 | TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
|
40 | NVIC_Init(&nvicStructure);
|
41 | GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_RESET); // klappt nicht
|
42 | }
|
43 |
|
44 | void TIM2_IRQHandler(void){
|
45 | if(GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_7)){
|
46 | GPIO_WriteBit(GPIOA, GPIO_Pin_7, RESET);
|
47 | }
|
48 | else{
|
49 | GPIO_WriteBit(GPIOA, GPIO_Pin_7, SET);
|
50 | }
|
51 | TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
52 | }
|
53 |
|
54 | int main()
|
55 | {
|
56 | SystemInit();
|
57 |
|
58 | InitializeLEDs();
|
59 | InitializeTimer();
|
60 |
|
61 | for (;;)
|
62 | {
|
63 | int timerValue = TIM_GetCounter(TIM2);
|
64 | if (timerValue == 10)
|
65 | GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_SET);
|
66 | else if (timerValue == 5000)
|
67 | GPIO_WriteBit(GPIOA, GPIO_Pin_6, Bit_RESET);
|
68 | }
|
69 | }
|