welcom ! Handel home

2014年6月24日 星期二

ST32 PWM demo2

include "stm32f10x.h"
BitAction  bbt=0;  //定義一個位變數 bbt,並清0
u16          i;

int main(void){
 u16 InitValue,Pre_Divide,ZKB;
 SystemInit(); //系統時鐘初始化,調用該函數後,系統時鐘為72M,函數原形在system_stm32f10x.c
 GPIO_init();

//****TIM1_PWM1通道設置******************************************
 InitValue=7200;          //初值1000
 Pre_Divide=1;            //預分頻1
 ZKB=1200;                 //占空比

 TIM1_PWM_1_init(InitValue,Pre_Divide,ZKB);
 TIM1->CCR1=2400;   //修改占空比
//*************************************************************************//

while(1)
 {          //
 }

}

//這是sysinit.c  系統程式
#include "stm32f10x.h"
void GPIO_init()
{
 GPIO_InitTypeDef GPIO_InitStructure;              //聲明埠結構,需要放在前面
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); 
// 開啟相對應的clock source Port A  and PortB

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

//**************A8,A9,A10TIM1_PWM波的輸出端***************************
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10; 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //PWM波設為複用推挽輸出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //埠頻率可設為21050  
 GPIO_Init(GPIOA, &GPIO_InitStructure);   //啟動A 



//*************B13,B14,B15TIM1_PWM波的互補輸出端**********************
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  //埠頻率可設為21050
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;   //PWM波的複用推挽輸出
 GPIO_Init(GPIOB,&GPIO_InitStructure);               //啟動B
}

//***TIM1_PWM 1通道初始化******************************************
void TIM1_PWM_1_init(u16 InitValue, u16 Pre_Divide, u16 CCR1_Val){

 TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
 TIM_OCInitTypeDef        TIM_OCInitStructure;
 TIM_BDTRInitTypeDef      TIM_BDTRInitStructure; 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
 TIM_TimeBaseStructure.TIM_Period =InitValue;
 TIM_TimeBaseStructure.TIM_Prescaler =Pre_Divide-1;
 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
 TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
 TIM_ARRPreloadConfig(TIM1,ENABLE);   //使能TIM1ARR上的預裝載寄存器 

 TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;// 選擇計時器模式 TI脈衝寬度調製模式
 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_Pulse = CCR1_Val;     //設置待裝入捕獲比較寄存器的脈衝值
 TIM_OC1Init(TIM1, &TIM_OCInitStructure);//捕獲比較匹配器結構1通道賦值
 TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //使能TIM1CCR1上的預裝載寄存器

//死區設置
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable
//TIM1_OSSRState設置在運行模式下非工作狀態選項
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
//TIM1_OSSIState設置在空閒模式下非工作狀態選項(使能TIM1 OSSI狀態

TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
 //TIM1_LOCKLevel設置了鎖電平參數(不鎖任何位

TIM_BDTRInitStructure.TIM_DeadTime = 0x90;
//這裡調整死區大小0-0xff,TIM1_DeadTIM1指定了輸出打開和關閉狀態之間的延時 

TIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable;
//TIM1_Break使能或者失能TIM1刹車輸入(失能TIM1刹車輸入
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
//TIM1_BreakPolarity設置TIM1刹車輸入管腳極性(TIM1刹車輸入管腳極性高) 
TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;
//TIM1_AutomaticOutput使能或者失能自動輸出功能(自動輸出功能使能)
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
//(設置刹車特性,死區時間,鎖電平,OSSIOSSR狀態和AOE(自動輸出使能)
TIM_Cmd(TIM1, ENABLE);         //使能或者失能指定的   
TIM_CtrlPWMOutputs(TIM1, ENABLE);  //使能或者失能TIM1外設的主輸出

}

沒有留言: