2-х фазный ШИМ генератор 400 Герц на STM32F103 с фазами, сдвинутыми на 90 градусов. Частота ШИМ генерации 40 кГц. Для генерации используется TIM4.
#include "stm32f10x.h"
#include "table.h"
const int table[126]={
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008,0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,
0x176a,0x1770,0x176a,0x1758,0x173b,0x1712,0x16dd,0x169d,
0x1652,0x15fd,0x159d,0x1533,0x14c0,0x1443,0x13be,0x1330,
0x129b,0x11ff,0x115d,0x10b5,0x1008,0xf57, 0xea2, 0xdea,
0xd30, 0xc74, 0xbb8, 0xafc, 0xa40, 0x986, 0x8ce, 0x819,
0x768, 0x6bb, 0x613, 0x571, 0x4d5, 0x440, 0x3b2, 0x32d,
0x2b0, 0x23d, 0x1d3, 0x173, 0x11e, 0xd3, 0x93, 0x5e,
0x35, 0x18, 0x6, 0x0, 0x6, 0x18, 0x35, 0x5e,
0x93, 0xd3, 0x11e, 0x173, 0x1d3, 0x23d, 0x2b0, 0x32d,
0x3b2, 0x440, 0x4d5, 0x571, 0x613, 0x6bb, 0x768, 0x819,
0x8ce, 0x986, 0xa40, 0xafc, 0xbb8,
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008, 0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,
0x176a};
void SystemInit1(void);
void TIM4_PWM_Init(void);
int t1,t2;
vu16 CCR1_Val=300;
vu16 CCR2_Val=1000;
vu16 CCR3_Val=2000;
vu16 CCR4_Val=6000;
TIM_OCInitTypeDef TIM_OCInitStructure;
void TIM4_GPIO_Configuration(void);
void TIM4_Mode_Configuration(void);
void TIM4_PWM_Init(void)
{
TIM4_GPIO_Configuration();
TIM4_Mode_Configuration();
// while(1);
}
void TIM4_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM4_Mode_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_DeInit(TIM4);
//TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Period=6000;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM4, 7199,TIM_PSCReloadMode_Immediate );
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle2
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle3
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle4
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
void SystemInit1(void)
{
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
int main(void)
{
SystemInit1(); // Config system timer 72MHZ
TIM4_PWM_Init(); // Init TIM4 PWM and out pwm and conig GPIO
SysTick_Config(1800);
__enable_irq ();
t1=0;
t2=0;
while(1)
{
// GPIOA->BSRR=GPIO_BSRR_BR0;
// GPIOA->BSRR=GPIO_BSRR_BS0;
};
}
void SysTick_Handler(void)
{
//nTime = 0;
// while(nTime <= 100)
// {
t1++;
if(t1>=100){t1=0;}
t2++;
if(t2>=100){t2=0;}
// }
GPIOA->BSRR=GPIO_BSRR_BS0;
TIM_OCInitStructure.TIM_Pulse=table[t2+25];
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t1];
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t2+25];
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t1];
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
GPIOA->BSRR=GPIO_BSRR_BR0;
}
#include "stm32f10x.h"
#include "table.h"
const int table[126]={
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008,0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,
0x176a,0x1770,0x176a,0x1758,0x173b,0x1712,0x16dd,0x169d,
0x1652,0x15fd,0x159d,0x1533,0x14c0,0x1443,0x13be,0x1330,
0x129b,0x11ff,0x115d,0x10b5,0x1008,0xf57, 0xea2, 0xdea,
0xd30, 0xc74, 0xbb8, 0xafc, 0xa40, 0x986, 0x8ce, 0x819,
0x768, 0x6bb, 0x613, 0x571, 0x4d5, 0x440, 0x3b2, 0x32d,
0x2b0, 0x23d, 0x1d3, 0x173, 0x11e, 0xd3, 0x93, 0x5e,
0x35, 0x18, 0x6, 0x0, 0x6, 0x18, 0x35, 0x5e,
0x93, 0xd3, 0x11e, 0x173, 0x1d3, 0x23d, 0x2b0, 0x32d,
0x3b2, 0x440, 0x4d5, 0x571, 0x613, 0x6bb, 0x768, 0x819,
0x8ce, 0x986, 0xa40, 0xafc, 0xbb8,
0xbb8, 0xc74, 0xd30, 0xdea, 0xea2, 0xf57, 0x1008, 0x10b5,
0x115d,0x11ff,0x129b,0x1330,0x13be,0x1443,0x14c0,0x1533,
0x159d,0x15fd,0x1652,0x169d,0x16dd,0x1712,0x173b,0x1758,
0x176a};
void SystemInit1(void);
void TIM4_PWM_Init(void);
int t1,t2;
vu16 CCR1_Val=300;
vu16 CCR2_Val=1000;
vu16 CCR3_Val=2000;
vu16 CCR4_Val=6000;
TIM_OCInitTypeDef TIM_OCInitStructure;
void TIM4_GPIO_Configuration(void);
void TIM4_Mode_Configuration(void);
void TIM4_PWM_Init(void)
{
TIM4_GPIO_Configuration();
TIM4_Mode_Configuration();
// while(1);
}
void TIM4_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM4_Mode_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
//TIM_DeInit(TIM4);
//TIM_InternalClockConfig(TIM4);
TIM_TimeBaseStructure.TIM_Period=6000;
TIM_TimeBaseStructure.TIM_Prescaler=0;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
//TIM_PrescalerConfig(TIM4, 7199,TIM_PSCReloadMode_Immediate );
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle2
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle3
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
//channle4
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse=CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_Cmd(TIM4,ENABLE);
}
void SystemInit1(void)
{
{
ErrorStatus HSEStartUpStatus;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus=RCC_WaitForHSEStartUp();
if(HSEStartUpStatus==SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource()!=0x08);
}
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
int main(void)
{
SystemInit1(); // Config system timer 72MHZ
TIM4_PWM_Init(); // Init TIM4 PWM and out pwm and conig GPIO
SysTick_Config(1800);
__enable_irq ();
t1=0;
t2=0;
while(1)
{
// GPIOA->BSRR=GPIO_BSRR_BR0;
// GPIOA->BSRR=GPIO_BSRR_BS0;
};
}
void SysTick_Handler(void)
{
//nTime = 0;
// while(nTime <= 100)
// {
t1++;
if(t1>=100){t1=0;}
t2++;
if(t2>=100){t2=0;}
// }
GPIOA->BSRR=GPIO_BSRR_BS0;
TIM_OCInitStructure.TIM_Pulse=table[t2+25];
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t1];
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t2+25];
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OCInitStructure.TIM_Pulse=table[t1];
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
GPIOA->BSRR=GPIO_BSRR_BR0;
}
А вы могли бы добавить две кнопки для регулировки фазы влево / вправо?
ОтветитьУдалитьКак менять частоту?
ОтветитьУдалить