代码如下:
[mw_shl_code=applescript,true]#include "SPWM.h"
#include "led.h"
#include "usart.h"
u16 TimerPeriod = 7200;//72MHZ3yòÔÕa¸öêy×Ö±ãêÇÄ¿±êμÄÆμÂê 13yòÔ72M*TimerPeriodÎaÖüÆú걼䣬μ1êy±ãêÇÆμÂê
u16 DutyFactor = 50;
void TIM_Int_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4 ,ENABLE); //ê±Öóê1Äü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
/* GPIOÅäÖÃ:í¨μàPC.6 oíPA.7 ×÷Îaêä3öòy½Å*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //¼Æêy·½ê½
TIM_TimeBaseStructure.TIM_Period = TimerPeriod - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
TIM_DeInit(TIM4); //Õa±ßêÇÆÕí¨¶¨ê±Æ÷μÄ3õê¼»ˉoíÖD¶ÏéêÇë
TIM_TimeBaseStructure.TIM_Period=TimerPeriod - 1; //×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷μÄÖμ
TIM_TimeBaseStructure.TIM_Prescaler= 0; //ê±ÖóÔ¤·ÖÆμêy
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //2éÑù·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//¼Æêy·½ê½
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update); //Çå3yòç3öÖD¶Ï±êÖ¾
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); // ¼Æêyòç3öê±′¥·¢ÖD¶Ï
// //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //í¨μàTIM4
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//Õ¼óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //¸±óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void TIM4_IRQHandler(void)
{
static vu16 sign = 0;
static vu16 Counter_sine=0;
static vu16 Duty_Cycle_sinewavetable[128]={
3783,3952,4150,4320,4489,4658,4828,4997,5138,5308,
5449,5618,5760,5901,6014,6155,6268,6381,6494,6607,
6691,6776,6861,6917,7002,7058,7087,7143,7171,7171,
7200,7200,7200,7171,7171,7143,7087,7058,7002,6917,
6861,6776,6691,6607,6494,6381,6268,6155,6014,5901,
5760,5618,5449,5308,5138,4997,4828,4658,4489,4320,
4150,3952,3783,3614,3444,3275,3077,2908,2738,2569,
2400,2230,2089,1920,1778,1609,1468,1327,1214,1072,
960,847,734,621,536,451,367,310,225,169,141,84,56,56,
28,28,28,56,56,84,141,169,225,310,367,451,536,621,734,
847,960,1072,1214,1327,1468,1609,1778,1920,2089,2230,
2400,2569,2738,2908,3077,3275,3444,3614
};
if ( TIM_GetITStatus(TIM4 , TIM_IT_Update) == SET)
{
TIM_SetCompare1(TIM8,Duty_Cycle_sinewavetable[Counter_sine]);
Counter_sine++;
if(Counter_sine==100)
{
Counter_sine=0;}
}
TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);
}
/*¶¨ê±Æ÷4ÖD¶Ï·tÎñ3ìDò
// void TIM4_IRQHandler(void)
// {
// static vu16 sign = 0;
// static vu16 Counter_sine=0;
// static vu16 Duty_Cycle_sinewavetable[128]={0x0,0x83,0x105,0x187,0x209,0x28A,0x30A,0x38A,
// 0x409,0x486,0x502,0x57D,0x5F7,0x66F,0x6E5,0x75A,
// 0x7CC,0x83D,0x8AB,0x917,0x981,0x9E8,0xA4D,0xAAF,
// 0xB0E,0xB6B,0xBC4,0xC1B,0xC6E,0xCBE,0xD0B,0xD54,
// 0xD9A,0xDDD,0xE1C,0xE57,0xE8F,0xEC3,0xEF3,0xF1F,
// 0xF48,0xF6C,0xF8D,0xFA9,0xFC2,0xFD6,0xFE6,0xFF3,
// 0xFFB,0xFFF,0xFFF,0xFFB,0xFF3,0xFE6,0xFD6,0xFC2,
// 0xFA9,0xF8D,0xF6C,0xF48,0xF1F,0xEF3,0xEC3,0xE8F,
// 0xE57,0xE1C,0xDDD,0xD9A,0xD54,0xD0B,0xCBE,0xC6E,
// 0xC1B,0xBC4,0xB6B,0xB0E,0xAAF,0xA4D,0x9E8,0x981,
// 0x917,0x8AB,0x83D,0x7CC,0x75A,0x6E5,0x66F,0x5F7,
// 0x57D,0x502,0x486,0x409,0x38A,0x30A,0x28A,0x209,
// 0x187,0x105,0x83,0x0};
// if (TIM_GetITStatus(TIM4 ,TIM_IT_Update) == SET)
// {
// if(sign == 0)
// {
// TIM_SetCompare1(TIM4,Duty_Cycle_sinewavetable[Counter_sine]);
// TIM_SetCompare2(TIM4,0);
// Counter_sine++;
// if(Counter_sine==100) //°ëÖüÆú2éÑù100¸öμã
// {
// Counter_sine=0;
// sign = 1;
// }
// }else{
// TIM_SetCompare1(TIM4,0);
// TIM_SetCompare2(TIM4,Duty_Cycle_sinewavetable[Counter_sine]);
// Counter_sine++;
// if(Counter_sine==100)
// {
// Counter_sine=0;
// sign = 0;
// }
// }
// }
// TIM_ClearITPendingBit(TIM4 , TIM_FLAG_Update);
// }
*/
//TIM4 PWM2¿·Ö3õê¼»ˉ
//PWMêä3ö3õê¼»ˉ
//arr£o×Ô¶ˉÖØ×°Öμ
//psc£oê±ÖóÔ¤·ÖÆμêy
void TIM_PWM_Init(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_Pulse = DutyFactor * 7200 / 100;//éèÖÃÕ¼¿Õ±è
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//òÔéÏËÄDD′úÂëéèÖÃ
WMμÄ¿ÕÏDμçƽ¡¢2¨Dη½ê½μÄ£¡
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
// TIM_OC2Init(TIM1, &TIM_OCInitStructure); //3õê¼»ˉá½×黥21μÄ
WM
/* ×Ô¶ˉêä3öê1Äü,ÖD¶Ï,ËàÇøÏà1ØμÄéèÖÃ*/
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime = 100;//ËàÇøê±¼äÎa 12/SYSTEMCLK (ns)
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;//1رÕía2¿break1|Äü
// TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);//ê1ÄüԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM8,ENABLE); //ê1ÄüÖØ×°ÔؼÄ′æÆ÷
TIM_CtrlPWMOutputs(TIM8, ENABLE);
TIM_Cmd(TIM8, ENABLE);
TIM_Cmd(TIM4, ENABLE);
}
[/mw_shl_code]
这个是网上下的一个人的,PC6是TIM8的CH1通道,然后输出波形我能理解,但是不知道为什么PA7会输出与PC6相反的波形,难道是因为PA7是反通道吗?
一周热门 更多>