2014-12-26 16 views
0

왜 내 코드가 작동하지 않는지 알 수 없습니다! 나는 TIM1과 함께 일했고 모든 것이 잘 작동하지만 TIM8로 변경하면 PC6과 PC7은 항상 켜져 있고 상보는 항상 꺼져 있습니다. 제발 저를 도와주세요!STM32F407 TIM8 complementary

/* Includes ------------------------------------------------------------------*/ 
#include "stm32f4xx.h" 
#include "stm32f4xx_gpio.h" 
#include "stm32f4xx_rcc.h" 
#include "stm32f4xx_tim.h" 
#include "misc.h" 

/* Private typedef -----------------------------------------------------------*/ 
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 
TIM_OCInitTypeDef TIM_OCInitStructure; 
TIM_BDTRInitTypeDef TIM_BDTRInitStructure; 

/* Private define ------------------------------------------------------------*/ 
#define frequency 42500 /* output frequency 42500 KHz */ 
#define f1 1/2 /* phase shift 90 degrees */ 

/* Private macro -------------------------------------------------------------*/ 
/* Private variables ---------------------------------------------------------*/ 
int TimerPeriod = 0; 

/* Private function prototypes -----------------------------------------------*/ 
void TIM_Config(void); 

/* Private functions ---------------------------------------------------------*/ 

/** 
    * Main program 
    */ 
int main(void) 
{ 

    /* TIM8 Configuration */ 
    TIM_Config(); 

    /* Compute the value to be set in ARR register to generate the desired signal frequency */ 
    TimerPeriod = ((SystemCoreClock/2)/frequency) - 1; 

    /* Time Base configuration */ 
    TIM_TimeBaseStructure.TIM_Prescaler = 0; 
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
    TIM_TimeBaseStructure.TIM_Period = TimerPeriod; 
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; 
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; 

    TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); 

    /* Channel 1 and 2 Configuration in Toggle mode */ 
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle; 
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; 
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; 
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; 
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; 
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset; 

    TIM_OCInitStructure.TIM_Pulse = (TimerPeriod/6)+ (TimerPeriod * f1); 
    TIM_OC1Init(TIM8, &TIM_OCInitStructure); 

    TIM_OCInitStructure.TIM_Pulse = (TimerPeriod/6) ; 
    TIM_OC2Init(TIM8, &TIM_OCInitStructure); 

    /* Automatic Output enable, Break, dead time and lock configuration*/ 
    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable; 
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable; 
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1; 
    TIM_BDTRInitStructure.TIM_DeadTime = 25; ///////// the right value for 250ns delay //////// 
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Enable; 
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High; 
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable; 

    TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure); 

    /* TIM8 counter enable */ 
    TIM_Cmd(TIM8, ENABLE); 

    /* Main Output Enable */ 
    TIM_CtrlPWMOutputs(TIM8, ENABLE); 

    while (1) 
    { 
    } 
} 

/** 
    * Configure the TIM8 Pins. 
    */ 
void TIM_Config(void) 
{ 
    GPIO_InitTypeDef GPIO_InitStructure; 

    /* GPIOA, GPIOB and GPIOC clocks enable */ 
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); 

    /* TIM8 clock enable */ 
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE); 

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; 
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; 
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 

    /*GPIOA Configuration: Channel 1N and BKIN as alternate function push-pull*/ 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6; 
    GPIO_Init(GPIOA, &GPIO_InitStructure); 

    /* GPIOA Configuration: Channel 1 and 2 as alternate function push-pull */ 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; 
    GPIO_Init(GPIOC, &GPIO_InitStructure); 

    /* GPIOB Configuration: Channel 2N as alternate function push-pull */ 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; 
    GPIO_Init(GPIOB, &GPIO_InitStructure); 

    /* Connect TIM pins to AF1 */ 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM8); 
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_TIM8); 
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM8); 
    GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM8); 
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource0, GPIO_AF_TIM8); 

} 
+0

어떤 보드를 사용하고 있습니까? F4 발견 일 경우 TIM8 채널 핀 중 일부는 해당 보드의 다른 주변 장치에서 가져옵니다. [회로도] (http://www.st.com/web-ui/static/active/en/resource/technical/document/user_manual/DM00039084.pdf)를 참조하고 "Free I /영형". –

+0

TIM1에 대한 코드를 변경하면 모든 핀이 "Free I/O"입니다. 음, 가장 중요한 핀은 BKIN이라고 생각합니다. 그러나 TIM8_BKIN을 검색하는 경우 "Free I/O"가 아닌 PA6 만 있습니다. – co2ark5

+0

@ co2ark5 안녕하세요 ...이 문제가 해결 되었습니까? 나도 똑같이 직면 해있다! – Swanand

답변

0

TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;

을 변경하고 알려하려고 @Swanand.