xiaoxiaocheng_plc/users/PWM.c

113 lines
4.3 KiB
C
Raw Permalink Normal View History

2018-11-13 22:25:34 +08:00
#include "stm32f10x_it.h"
#include "stm32f10x_tim.h"
#include <stdio.h>
#include <absacc.h>
extern u16 all_data[];
void TIM3_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_Initstructure;
/* ---------------------------------------------------------------
ʹ<EFBFBD><EFBFBD>TIM3ʱ<EFBFBD><EFBFBD>
PCLK1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ΪTIM3<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>72MHz
--------------------------------------------------------------- */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* GPIOA<4F><41>GPIOB ʱ<><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_AF_PP; //<2F><><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstructure);
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOB,&GPIO_Initstructure);
}
void TIM3_Mode_Config(void)
{
/***************************************************************
TIM3<EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4·PWM<EFBFBD>ź<EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD>
TIM3ʱ<EFBFBD><EFBFBD> = 72<EFBFBD>׺գ<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>= 0x0<EFBFBD><EFBFBD>TIM3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>= 72<EFBFBD>׺<EFBFBD>
TIM3ARR<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>= 999 = > TIM3Ƶ<EFBFBD><EFBFBD>= TIM3<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>/<EFBFBD><EFBFBD>ARR + 1<EFBFBD><EFBFBD>
TIM3Ƶ<EFBFBD><EFBFBD>= 72 kHz<EFBFBD><EFBFBD>
****************************************************************/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitstructure;
// <20>Զ<EFBFBD><D4B6><EFBFBD>װ<EFBFBD>ؼĴ<D8BC><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD>ֵ(<28><>ʱʱ<CAB1><CAB1>)<29>ۼ<EFBFBD> 0xFFFF<46><46>Ƶ<EFBFBD>ʺ<EFBFBD><CABA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>»<EFBFBD><C2BB><EFBFBD><EFBFBD>ж<EFBFBD><><D2B2>˵<EFBFBD><CBB5>ʱʱ<CAB1>䵽)<29><>
TIM_TimeBaseStructure.TIM_Period =65535;
//ʱ<><CAB1>Ԥ<EFBFBD><D4A4>Ƶ<EFBFBD><C6B5> <20><><EFBFBD><EFBFBD> <20><>ʱ<EFBFBD><CAB1>Ƶ<EFBFBD><C6B5>=72/(ʱ<><CAB1>Ԥ<EFBFBD><D4A4>Ƶ+1)
TIM_TimeBaseStructure.TIM_Prescaler = 0;
// ʹ<>õIJ<C3B5><C4B2><EFBFBD>Ƶ<EFBFBD><C6B5>֮<EFBFBD><D6AE><EFBFBD>ķ<EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD><EFBFBD>
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//<2F><>ʱ<EFBFBD><CAB1>ģʽ<C4A3><CABD><EFBFBD>ϼ<EFBFBD><CFBC><EFBFBD><EFBFBD><EFBFBD>
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>3
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/**************<2A><><EFBFBD><EFBFBD>ΪPWM<57>Ƚ<EFBFBD><C8BD><EFBFBD><EFBFBD><EFBFBD>ģʽ****************************/
TIM_OCInitstructure.TIM_OCMode = TIM_OCMode_Toggle;
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitstructure.TIM_OutputState=TIM_OutputState_Enable;
/*<2A><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵС<D6B5><D0A1>CCR1_Va1<61><31>Ϊ<EFBFBD>ߵ<EFBFBD>Ƶ*/
TIM_OCInitstructure.TIM_OCPolarity=TIM_OCPolarity_High;
/*<2A><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>1<EFBFBD><31><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵʱ<D6B5><CAB1><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
TIM_OCInitstructure.TIM_Pulse= 4000;
/*ʹ<><CAB9>ͨ<EFBFBD><CDA8>1*/
TIM_OC1Init(TIM3,&TIM_OCInitstructure);
/* PWM1 Mode configuration: Channel2 */
/*<2A><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>2<EFBFBD><32><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵʱ<D6B5><CAB1><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
TIM_OCInitstructure.TIM_Pulse=100;
/*ʹ<><CAB9>ͨ<EFBFBD><CDA8>3*/
TIM_OC2Init(TIM3,&TIM_OCInitstructure);
/* PWM1 Mode configuration: Channel3 */
TIM_OCInitstructure.TIM_Pulse=1000;
/*ʹ<><CAB9>ͨ<EFBFBD><CDA8>3*/
TIM_OC3Init(TIM3,&TIM_OCInitstructure);
/* PWM1 Mode configuration: Channel4 */
/*<2A><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>2<EFBFBD><32><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵʱ<D6B5><CAB1><EFBFBD><EFBFBD>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>*/
TIM_OCInitstructure.TIM_Pulse=500;
/*ʹ<><CAB9>ͨ<EFBFBD><CDA8>4*/
TIM_OC4Init(TIM3,&TIM_OCInitstructure);
/*<2A><>ֹԤ<D6B9>ؼĴ<D8BC><C4B4><EFBFBD>*/
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Disable);
TIM_ITConfig(TIM3,TIM_IT_CC1|TIM_IT_CC2|TIM_IT_CC3|TIM_IT_CC4,ENABLE);
TIM_Cmd(TIM3, ENABLE); //ʹ<>ܶ<EFBFBD>ʱ<EFBFBD><CAB1>3
}
void TIM3_IRQHandler(void)//1ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ж<EFBFBD><D0B6>ź<EFBFBD>
{
u16 Capture;
if(TIM_GetITStatus(TIM3, TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
Capture=TIM_GetCapture1(TIM3);
TIM_SetCompare1(TIM3,Capture+4000);
}
if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
Capture=TIM_GetCapture2(TIM3);
TIM_SetCompare2(TIM3,Capture+100);
}
}
void TIM3_PWM_Init(void)
{
TIM3_GPIO_Config();
TIM3_Mode_Config();
}