Forum: Mikrocontroller und Digitale Elektronik Hilfe bei Programmierung STM32F407VG bei TIM1


von Kaffeetasse (Gast)


Angehängte Dateien:

Lesenswert?

Hallo,
ich habe wie schon im Betreff beschrieben ein Problem bei der korrekten 
Programmierung des TIM1. Dieser soll im Encoder Modus 3 laufen und über 
Channel 1 und Channel 2 Encodersignale "zählen". Ich bin gerade von 
STM32F100RB auf den STM32F407VG umgestiegen und hier funktioniert mein 
gedachtes Programm leider nicht. Der Timer zählt keine Flanken... Ich 
programmiere den F4 über das Discovery-Board UM1472. Das Manuel für den 
STM32F407VG und für das Board sind im Anhang...

Hier zunächst mal das Programm (.c):
1
#include "enc_tim1.h"
2
#include "stm32f4xx.h"
3
#include "gpio.h"
4
//*********************************************************************************************************************************
5
//*********************************************************************************************************************************
6
//*********************************************************************************************************************************
7
//*********************************************************************************************************************************
8
// Definitionen :
9
10
// Variablen :
11
volatile s32 ENCODER_CNT=-32768; //s=zeichenkette=string
12
13
14
// TIM1, Global Interrupt :
15
16
////////////////////////////TIM1///////////////////////////
17
void TIM1_IRQHandler(void)
18
{
19
if(TIM1->SR & TIM_SR_UIF)        //UIF => Update_Interrupt_Flag
20
  {
21
  if(TIM1->CR1 & TIM_CR1_DIR) {ENCODER_CNT-=0x10000;}  //TIM_CR1_DIR => Richtungsregister 0=up/1=down
22
  else {ENCODER_CNT+=0x10000;}
23
  TIM1->SR &= ~TIM_SR_UIF;
24
  }
25
}
26
//////////////////////////////////////////////////////////
27
28
// Encoder, init:
29
30
void _enc_tim1_init(void)
31
{
32
//////Takt f?r PORTS anschalten//////
33
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
34
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN;
35
/////////////////////////////////////
36
37
////////////PINS-INIT://////////////////
38
39
_gpio_cfg(GPIO_PORT_B, GPIO_PIN_13, GPIO_MODE_AF, GPIO_TYPE_PULL, GPIO_SPEED_50MHz, GPIO_PUPDR_NOR);
40
_gpio_cfg(GPIO_PORT_B, GPIO_PIN_14, GPIO_MODE_AF, GPIO_TYPE_PULL, GPIO_SPEED_50MHz, GPIO_PUPDR_NOR);
41
42
GPIOB->AFR[1] |= GPIOB_AFRL13_AF1;
43
GPIOB->AFR[1] |= GPIOB_AFRL14_AF1;
44
45
////////////////////////////////////////
46
47
///////////////////////TIM1_INIT////////////////////
48
TIM1->SMCR |= TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0;      //Encoder-Mode: 3
49
TIM1->CCMR1 |= TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0;    //Channel_2 auf TI2 und Channel_1 auf TI1
50
TIM1->CCER |= TIM_CCER_CC1E | TIM_CCER_CC2E;      //Input-Channel anschalten
51
52
TIM1->CNT = 0x8000;
53
TIM1->CR1 |= TIM_CR1_CEN;          //Counter anschalten
54
55
nop;
56
////////////////////////////////////////////////////
57
58
}
59
//*********************************************************************************************************************************
60
//*********************************************************************************************************************************
61
//*********************************************************************************************************************************
62
//*********************************************************************************************************************************
63
// Encoder, ISR enable
64
65
void _enc_tim1_isr_enable(void)
66
{
67
TIM1->DIER |= TIM_DIER_UIE;                             // TIM1 Update Interrupt
68
NVIC->ISER[0] |= NVIC_ISER_SETENA_27;      // TIM1 Global Interrupt
69
}
70
//*********************************************************************************************************************************
71
//*********************************************************************************************************************************
72
//*********************************************************************************************************************************
73
//*********************************************************************************************************************************

Dazu noch die passende Header Datei(.h):
1
#ifndef _ENC_TIM1_H
2
#define _ENC_TIM1_H
3
4
#include "stm32f4xx.h"
5
#include "typedefs.h"
6
#include "gpio.h"
7
8
//*********************************************************************************************************************************
9
10
// Definitionen :
11
#define NVIC_ISER_SETENA_27    0x08000000
12
#define GPIOA_AFRL7_AF1      0x10000000
13
#define GPIOB_AFRL0_AF1      0x00000001
14
#define GPIOB_AFRL13_AF1    0x00100000
15
#define GPIOB_AFRL14_AF1    0x01000000
16
17
// Variablen :
18
volatile extern s32 ENCODER_CNT;
19
20
// Funktionen :
21
void _enc_tim1_init(void);
22
void _enc_tim1_isr_enable(void);
23
24
//*********************************************************************************************************************************
25
#endif

Für die Pins die init:
1
#include "gpio.h"
2
//*********************************************************************************************************************************
3
//*********************************************************************************************************************************
4
//*********************************************************************************************************************************
5
//*********************************************************************************************************************************
6
// Definitionen :
7
8
9
// Variablen
10
11
//*********************************************************************************************************************************
12
//*********************************************************************************************************************************
13
//*********************************************************************************************************************************
14
//*********************************************************************************************************************************
15
// GPIO?s initialisieren :
16
17
void _gpio_init(void)
18
{
19
RCC->AHB1ENR |=   RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIOBEN | RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN
20
            | RCC_AHB1ENR_GPIOEEN | RCC_AHB1ENR_GPIOFEN | RCC_AHB1ENR_GPIOGEN | RCC_AHB1ENR_GPIOHEN
21
            | RCC_AHB1ENR_GPIOIEN;
22
}
23
//*********************************************************************************************************************************
24
//*********************************************************************************************************************************
25
//*********************************************************************************************************************************
26
//*********************************************************************************************************************************
27
void _gpio_cfg(u08 port, u08 gpio, u08 mode, u08 type, u08 speed, u08 pudres)
28
{
29
if(port == GPIO_PORT_A) {GPIOA->MODER|=(mode<<(gpio*2)); GPIOA->OTYPER|=(type<<gpio); GPIOA->OSPEEDR|=(speed<<(gpio*2)); GPIOA->PUPDR|=(pudres<<(gpio*2));}
30
if(port == GPIO_PORT_B) {GPIOB->MODER|=(mode<<(gpio*2)); GPIOB->OTYPER|=(type<<gpio); GPIOB->OSPEEDR|=(speed<<(gpio*2)); GPIOB->PUPDR|=(pudres<<(gpio*2));}
31
if(port == GPIO_PORT_C) {GPIOC->MODER|=(mode<<(gpio*2)); GPIOC->OTYPER|=(type<<gpio); GPIOC->OSPEEDR|=(speed<<(gpio*2)); GPIOC->PUPDR|=(pudres<<(gpio*2));}
32
if(port == GPIO_PORT_D) {GPIOD->MODER|=(mode<<(gpio*2)); GPIOD->OTYPER|=(type<<gpio); GPIOD->OSPEEDR|=(speed<<(gpio*2)); GPIOD->PUPDR|=(pudres<<(gpio*2));}
33
if(port == GPIO_PORT_E) {GPIOE->MODER|=(mode<<(gpio*2)); GPIOE->OTYPER|=(type<<gpio); GPIOE->OSPEEDR|=(speed<<(gpio*2)); GPIOE->PUPDR|=(pudres<<(gpio*2));}
34
if(port == GPIO_PORT_F) {GPIOF->MODER|=(mode<<(gpio*2)); GPIOF->OTYPER|=(type<<gpio); GPIOF->OSPEEDR|=(speed<<(gpio*2)); GPIOF->PUPDR|=(pudres<<(gpio*2));}
35
if(port == GPIO_PORT_G) {GPIOG->MODER|=(mode<<(gpio*2)); GPIOG->OTYPER|=(type<<gpio); GPIOG->OSPEEDR|=(speed<<(gpio*2)); GPIOG->PUPDR|=(pudres<<(gpio*2));}
36
if(port == GPIO_PORT_H) {GPIOH->MODER|=(mode<<(gpio*2)); GPIOH->OTYPER|=(type<<gpio); GPIOH->OSPEEDR|=(speed<<(gpio*2)); GPIOH->PUPDR|=(pudres<<(gpio*2));}
37
if(port == GPIO_PORT_I) {GPIOI->MODER|=(mode<<(gpio*2)); GPIOI->OTYPER|=(type<<gpio); GPIOI->OSPEEDR|=(speed<<(gpio*2)); GPIOI->PUPDR|=(pudres<<(gpio*2));}
38
}
39
//*********************************************************************************************************************************
40
//*********************************************************************************************************************************
41
//*********************************************************************************************************************************
42
//*********************************************************************************************************************************

und die passende Header:
1
#ifndef _GPIO_H
2
#define _GPIO_H
3
4
#include "stm32f4xx.h"
5
#include "typedefs.h"
6
7
//*********************************************************************************************************************************
8
//*********************************************************************************************************************************
9
//*********************************************************************************************************************************
10
//*********************************************************************************************************************************
11
// Definitionen :
12
13
#define GPIO_MODE_INPUT      0x00
14
#define GPIO_MODE_OUTPUT    0x01
15
#define GPIO_MODE_AF      0x02
16
#define GPIO_MODE_ANALOG    0x03
17
18
#define GPIO_TYPE_PULL      0x00
19
#define GPIO_TYPE_OD      0x01
20
21
#define GPIO_SPEED_2MHz      0x00
22
#define GPIO_SPEED_25MHz    0x01
23
#define GPIO_SPEED_50MHz    0x02
24
#define GPIO_SPEED_100MHz    0x03
25
26
#define GPIO_PUPDR_NOR      0x00
27
#define GPIO_PUPDR_PU      0x01
28
#define GPIO_PUPDR_PD      0x02
29
30
#define GPIO_PORT_A        0x00
31
#define GPIO_PORT_B        0x01
32
#define GPIO_PORT_C        0x02
33
#define GPIO_PORT_D        0x03
34
#define GPIO_PORT_E        0x04
35
#define GPIO_PORT_F        0x05
36
#define GPIO_PORT_G        0x06
37
#define GPIO_PORT_H        0x07
38
#define GPIO_PORT_I        0x08
39
40
#define GPIO_PIN_0        0x00
41
#define GPIO_PIN_1        0x01
42
#define GPIO_PIN_2        0x02
43
#define GPIO_PIN_3        0x03
44
#define GPIO_PIN_4        0x04
45
#define GPIO_PIN_5        0x05
46
#define GPIO_PIN_6        0x06
47
#define GPIO_PIN_7        0x07
48
#define GPIO_PIN_8        0x08
49
#define GPIO_PIN_9        0x09
50
#define GPIO_PIN_10        0x0A
51
#define GPIO_PIN_11        0x0B
52
#define GPIO_PIN_12        0x0C
53
#define GPIO_PIN_13        0x0D
54
#define GPIO_PIN_14        0x0E
55
#define GPIO_PIN_15        0x0F
56
57
// Variablen :
58
59
// Funktionen :
60
void _gpio_init(void);
61
void _gpio_cfg(u08 port, u08 gpio, u08 mode, u08 type, u08 speed, u08 pudres);
62
63
//*********************************************************************************************************************************
64
//*********************************************************************************************************************************
65
//*********************************************************************************************************************************
66
//*********************************************************************************************************************************
67
#endif

Bin für jeden Hinweis dankbar!
LG Kaffee

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.