新手入门
- 积分
- 11
- 金钱
- 11
- 注册时间
- 2022-8-3
- 在线时间
- 2 小时
|
1金钱
信号断点的情况
信号断点时的情况
正常的输出情况
正常的输出情况
这其中,黄色(1通道)信号是外部中断电平,蓝色和红色信号时双通道互补输出PWM。
代码如下:
#include "stm32f4xx.h"
#include "sys.h"
#include "delay.h"
#include "led.h"
#include "trigger.h"
#include "usart.h"
#include "math.h"
#include "arm_math.h"
#include "exti.h"
#include "iwdg.h"
unsigned int arr,g_i,a;
unsigned int Pulse_array[5]={0}; // Pulse_array[ ]={336,3024};
void Calculate_SPWM_Array(float fc)
{
arr=SystemCoreClock/fc-1;//APB2
}
void TIM8_Int_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM8);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_TIM8);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_6|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_High_Speed;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler=0;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_Pulse=Pulse_array[g_i];
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM8,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);
TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTRInitStructure.TIM_DeadTime =0x34; //dead Time
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM8, &TIM_BDTRInitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM8_UP_TIM13_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x20;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x20;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ARRPreloadConfig(TIM8,DISABLE);
TIM_Cmd(TIM8,ENABLE);
TIM_ITConfig(TIM8,TIM_IT_Update,ENABLE);
TIM_CtrlPWMOutputs(TIM8, ENABLE);
//TIM_CCxCmd(TIM8,TIM_Channel_1,TIM_CCx_Enable);
//TIM_CCxCmd(TIM8,TIM_Channel_1,TIM_CCxN_Enable);
}
void TIM8_UP_TIM13_IRQHandler(void)
{
TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);
//static unsigned int i=0;
if(TIM_GetITStatus(TIM8,TIM_IT_Update)!=RESET)
{
PFout(9)=!PFout(9);
//a=0;
//i++;
if(g_i==4)
{g_i=0;}
else
{
g_i++;
}
}
TIM_SetCompare1(TIM8,Pulse_array[g_i]);
TIM_ClearITPendingBit(TIM8,TIM_IT_Update);
}
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
IWDG_SetPrescaler(prer);
IWDG_SetReload(rlr);
IWDG_ReloadCounter();
IWDG_Enable();
}
void IWDG_Feed(void)
{
IWDG_ReloadCounter();//reload
}
void EXTIX_Init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
TRI();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource10);
/* ÅäÖÃEXTI_Line2 */
EXTI_InitStructure.EXTI_Line = EXTI_Line10;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);//ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void EXTI15_10_IRQHandler(void)
{
int i,j;
TIM8_Int_Init();
PFout(10)=!PFout(10);
//a=1;
//delay_us(10);
if(Pulse_array[1]==672)
{
for(i=0;i<5;i++)
{
Pulse_array=2687;
};
}
else
{
for(j=0;j<5;j++)
{
Pulse_array[j]=672;
};
}
//TIM8_UP_TIM13_IRQHandler( );
IWDG_Feed();//Î11·
//i=!i;
EXTI_ClearITPendingBit(EXTI_Line10);
}
int main(void)
{
int i;
for(i=0;i<5;i++)
{
Pulse_array=672;
};
delay_init(168);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Calculate_SPWM_Array(50000);
g_i=0;
IWDG_Init(0,15);
EXTIX_Init();
while(1)
{
};
}
|
|