#include "stm32f4xx.h"
#include "stm32f4xx_tim.h"
#include "pwm.h"
#include "math.h"
#include "delay.h"
#include "stm32f4xx_dma.h"
#include "led.h"
#define Fsys 82000000ul // system freq 82MHz
#define Fpwm 25000 // PWM freq 25K
#define TIM1_CCR1_Address ((uint32_t)0x40012C34) /* 0x 4001 2C00 + 0x 34 */
#define TIM1_DMAR_Address 0x40012C4C
u8 Bridge_Flag = 0;
void DMA_Config();
void TIM1_GPIOConfig(void);
void TIM1_NVICConfig(void);
void BridgeStatus(void);
void Right_Bridge(void);
void Left_Bridge(void);
void LED_Init(void);
/*正弦函数DMA数组*/
u16 dmabuff[250]= {
0x0 ,0x24 ,0x49 ,0x6D ,0x92 ,0xB6 ,0xDA ,0xFF ,0x123,0x147,0x16B,0x18F,0x1B3,0x1D7,0x1FB,0x21F,0x242,
0x266,0x28A,0x2AD,0x2D0,0x2F3,0x316,0x339,0x35C,0x37F,0x3A1,0x3C4,0x3E6,0x408,0x42A,0x44B,0x46D,0x48E,
0x4AF,0x4D0,0x4F1,0x512,0x532,0x552,0x572,0x592,0x5B1,0x5D0,0x5EF,0x60E,0x62D,0x64B,0x669,0x687,0x6A4,
0x6C1,0x6DE,0x6FB,0x717,0x733,0x74F,0x76A,0x786,0x7A0,0x7BB,0x7D5,0x7EF,0x809,0x822,0x83B,0x854,0x86C,
0x884,0x89B,0x8B3,0x8C9,0x8E0,0x8F6,0x90C,0x921,0x936,0x94B,0x95F,0x973,0x987,0x99A,0x9AC,0x9BF,0x9D1,
0x9E2,0x9F3,0xA04,0xA14,0xA24,0xA34,0xA43,0xA52,0xA60,0xA6E,0xA7B,0xA88,0xA95,0xAA1,0xAAD,0xAB8,0xAC3,
0xACD,0xAD7,0xAE0,0xAE9,0xAF2,0xAFA,0xB02,0xB09,0xB10,0xB16,0xB1C,0xB22,0xB27,0xB2B,0xB30,0xB33,0xB36,
0xB39,0xB3B,0xB3D,0xB3F,0xB40,0xB40,0xB40,0xB40,0xB3F,0xB3D,0xB3B,0xB39,0xB36,0xB33,0xB30,0xB2B,0xB27,
0xB22,0xB1C,0xB16,0xB10,0xB09,0xB02,0xAFA,0xAF2,0xAE9,0xAE0,0xAD7,0xACD,0xAC3,0xAB8,0xAAD,0xAA1,0xA95,
0xA88,0xA7B,0xA6E,0xA60,0xA52,0xA43,0xA34,0xA24,0xA14,0xA04,0x9F3,0x9E2,0x9D1,0x9BF,0x9AC,0x99A,0x987,
0x973,0x95F,0x94B,0x936,0x921,0x90C,0x8F6,0x8E0,0x8C9,0x8B3,0x89B,0x884,0x86C,0x854,0x83B,0x822,0x809,
0x7EF,0x7D5,0x7BB,0x7A0,0x786,0x76A,0x74F,0x733,0x717,0x6FB,0x6DE,0x6C1,0x6A4,0x687,0x669,0x64B,0x62D,
0x60E,0x5EF,0x5D0,0x5B1,0x592,0x572,0x552,0x532,0x512,0x4F1,0x4D0,0x4AF,0x48E,0x46D,0x44B,0x42A,0x408,
0x3E6,0x3C4,0x3A1,0x37F,0x35C,0x339,0x316,0x2F3,0x2D0,0x2AD,0x28A,0x266,0x242,0x21F,0x1FB,0x1D7,0x1B3,
0x18F,0x16B,0x147,0x123,0xFF ,0xDA ,0xB6 ,0x92 ,0x6D ,0x49 ,0x24 ,0x0
};
/*正弦函数表格*/
const u16 SINE_CODE[250] = {
0x12 ,0x24 ,0x49 ,0x6D ,0x92 ,0xB6 ,0xDA ,0xFF ,0x123,0x147,0x16B,0x18F,0x1B3,0x1D7,0x1FB,0x21F,0x242,
0x266,0x28A,0x2AD,0x2D0,0x2F3,0x316,0x339,0x35C,0x37F,0x3A1,0x3C4,0x3E6,0x408,0x42A,0x44B,0x46D,0x48E,
0x4AF,0x4D0,0x4F1,0x512,0x532,0x552,0x572,0x592,0x5B1,0x5D0,0x5EF,0x60E,0x62D,0x64B,0x669,0x687,0x6A4,
0x6C1,0x6DE,0x6FB,0x717,0x733,0x74F,0x76A,0x786,0x7A0,0x7BB,0x7D5,0x7EF,0x809,0x822,0x83B,0x854,0x86C,
0x884,0x89B,0x8B3,0x8C9,0x8E0,0x8F6,0x90C,0x921,0x936,0x94B,0x95F,0x973,0x987,0x99A,0x9AC,0x9BF,0x9D1,
0x9E2,0x9F3,0xA04,0xA14,0xA24,0xA34,0xA43,0xA52,0xA60,0xA6E,0xA7B,0xA88,0xA95,0xAA1,0xAAD,0xAB8,0xAC3,
0xACD,0xAD7,0xAE0,0xAE9,0xAF2,0xAFA,0xB02,0xB09,0xB10,0xB16,0xB1C,0xB22,0xB27,0xB2B,0xB30,0xB33,0xB36,
0xB39,0xB3B,0xB3D,0xB3F,0xB40,0xB40,0xB40,0xB40,0xB3F,0xB3D,0xB3B,0xB39,0xB36,0xB33,0xB30,0xB2B,0xB27,
0xB22,0xB1C,0xB16,0xB10,0xB09,0xB02,0xAFA,0xAF2,0xAE9,0xAE0,0xAD7,0xACD,0xAC3,0xAB8,0xAAD,0xAA1,0xA95,
0xA88,0xA7B,0xA6E,0xA60,0xA52,0xA43,0xA34,0xA24,0xA14,0xA04,0x9F3,0x9E2,0x9D1,0x9BF,0x9AC,0x99A,0x987,
0x973,0x95F,0x94B,0x936,0x921,0x90C,0x8F6,0x8E0,0x8C9,0x8B3,0x89B,0x884,0x86C,0x854,0x83B,0x822,0x809,
0x7EF,0x7D5,0x7BB,0x7A0,0x786,0x76A,0x74F,0x733,0x717,0x6FB,0x6DE,0x6C1,0x6A4,0x687,0x669,0x64B,0x62D,
0x60E,0x5EF,0x5D0,0x5B1,0x592,0x572,0x552,0x532,0x512,0x4F1,0x4D0,0x4AF,0x48E,0x46D,0x44B,0x42A,0x408,
0x3E6,0x3C4,0x3A1,0x37F,0x35C,0x339,0x316,0x2F3,0x2D0,0x2AD,0x28A,0x266,0x242,0x21F,0x1FB,0x1D7,0x1B3,
0x18F,0x16B,0x147,0x123,0xFF ,0xDA ,0xB6 ,0x92 ,0x6D ,0x49 ,0x24 ,0x12
};
u16 dmabuff2[250] = {
0xb40,0xb1c,0xaf7,0xad3,0xaae,0xa8a,0xa66,0xa41,0xa1d,0x9f9,0x9d5,0x9b1,0x98d,0x969,0x945,0x921,0x8fe,
0x8da,0x8b6,0x893,0x870,0x84d,0x82a,0x807,0x7e4,0x7c1,0x79f,0x77c,0x75a,0x738,0x716,0x6f5,0x6d3,0x6b2,
0x691,0x670,0x64f,0x62e,0x60e,0x5ee,0x5ce,0x5ae,0x58f,0x570,0x551,0x532,0x513,0x4f5,0x4d7,0x4b9,0x49c,
0x47f,0x462,0x445,0x429,0x40d,0x3f1,0x3d6,0x3ba,0x3a0,0x385,0x36b,0x351,0x337,0x31e,0x305,0x2ec,0x2d4,
0x2bc,0x2a5,0x28d,0x277,0x260,0x24a,0x234,0x21f,0x20a,0x1f5,0x1e1,0x1cd,0x1b9,0x1a6,0x194,0x181,0x16f,
0x15e,0x14d,0x13c,0x12c,0x11c,0x10c,0xfd,0xee,0xe0,0xd2,0xc5,0xb8,0xab,0x9f,0x93,0x88,0x7d,0x73,0x69,
0x60,0x57,0x4e,0x46,0x3e,0x37,0x30,0x2a,0x24,0x1e,0x19,0x15,0x10,0xd,0xa,0x7,0x5,0x3,0x1,0x0,0x0,0x0,
0x0,0x1,0x3,0x5,0x7,0xa,0xd,0x10,0x15,0x19,0x1e,0x24,0x2a,0x30,0x37,0x3e,0x46,0x4e,0x57,0x60,0x69,0x73,
0x7d,0x88,0x93,0x9f,0xab,0xb8,0xc5,0xd2,0xe0,0xee,0xfd,0x10c,0x11c,0x12c,0x13c,0x14d,0x15e,0x16f,0x181,
0x194,0x1a6,0x1b9,0x1cd,0x1e1,0x1f5,0x20a,0x21f,0x234,0x24a,0x260,0x277,0x28d,0x2a5,0x2bc,0x2d4,0x2ec,
0x305,0x31e,0x337,0x351,0x36b,0x385,0x3a0,0x3ba,0x3d6,0x3f1,0x40d,0x429,0x445,0x462,0x47f,0x49c,0x4b9,
0x4d7,0x4f5,0x513,0x532,0x551,0x570,0x58f,0x5ae,0x5ce,0x5ee,0x60e,0x62e,0x64f,0x670,0x691,0x6b2,0x6d3,
0x6f5,0x716,0x738,0x75a,0x77c,0x79f,0x7c1,0x7e4,0x807,0x82a,0x84d,0x870,0x893,0x8b6,0x8da,0x8fe,0x921,
0x945,0x969,0x98d,0x9b1,0x9d5,0x9f9,0xa1d,0xa41,0xa66,0xa8a,0xaae,0xad3,0xaf7,0xb1c,0xb40,
};
const u16 SINE_CODE2[250] = {
0xb40,0xb1c,0xaf7,0xad3,0xaae,0xa8a,0xa66,0xa41,0xa1d,0x9f9,0x9d5,0x9b1,0x98d,0x969,0x945,0x921,0x8fe,
0x8da,0x8b6,0x893,0x870,0x84d,0x82a,0x807,0x7e4,0x7c1,0x79f,0x77c,0x75a,0x738,0x716,0x6f5,0x6d3,0x6b2,
0x691,0x670,0x64f,0x62e,0x60e,0x5ee,0x5ce,0x5ae,0x58f,0x570,0x551,0x532,0x513,0x4f5,0x4d7,0x4b9,0x49c,
0x47f,0x462,0x445,0x429,0x40d,0x3f1,0x3d6,0x3ba,0x3a0,0x385,0x36b,0x351,0x337,0x31e,0x305,0x2ec,0x2d4,
0x2bc,0x2a5,0x28d,0x277,0x260,0x24a,0x234,0x21f,0x20a,0x1f5,0x1e1,0x1cd,0x1b9,0x1a6,0x194,0x181,0x16f,
0x15e,0x14d,0x13c,0x12c,0x11c,0x10c,0xfd,0xee,0xe0,0xd2,0xc5,0xb8,0xab,0x9f,0x93,0x88,0x7d,0x73,0x69,
0x60,0x57,0x4e,0x46,0x3e,0x37,0x30,0x2a,0x24,0x1e,0x19,0x15,0x10,0xd,0xa,0x7,0x5,0x3,0x1,0x0,0x0,0x0,
0x0,0x1,0x3,0x5,0x7,0xa,0xd,0x10,0x15,0x19,0x1e,0x24,0x2a,0x30,0x37,0x3e,0x46,0x4e,0x57,0x60,0x69,0x73,
0x7d,0x88,0x93,0x9f,0xab,0xb8,0xc5,0xd2,0xe0,0xee,0xfd,0x10c,0x11c,0x12c,0x13c,0x14d,0x15e,0x16f,0x181,
0x194,0x1a6,0x1b9,0x1cd,0x1e1,0x1f5,0x20a,0x21f,0x234,0x24a,0x260,0x277,0x28d,0x2a5,0x2bc,0x2d4,0x2ec,
0x305,0x31e,0x337,0x351,0x36b,0x385,0x3a0,0x3ba,0x3d6,0x3f1,0x40d,0x429,0x445,0x462,0x47f,0x49c,0x4b9,
0x4d7,0x4f5,0x513,0x532,0x551,0x570,0x58f,0x5ae,0x5ce,0x5ee,0x60e,0x62e,0x64f,0x670,0x691,0x6b2,0x6d3,
0x6f5,0x716,0x738,0x75a,0x77c,0x79f,0x7c1,0x7e4,0x807,0x82a,0x84d,0x870,0x893,0x8b6,0x8da,0x8fe,0x921,
0x945,0x969,0x98d,0x9b1,0x9d5,0x9f9,0xa1d,0xa41,0xa66,0xa8a,0xaae,0xad3,0xaf7,0xb1c,0xb40,
};
void TIM1_PWM_Init()
{
//****PWM输出*****//
//****PA8_CH1 ****//
//****PA7_CH1N****//
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTR_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
//GPIO时钟使能
TIM1_GPIOConfig();
//时基初始化
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;//时钟分割
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Prescaler=0;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInitStructure.TIM_Period=Fsys / Fpwm;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
//
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=((Fsys / Fpwm) * 50) / 100;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
// 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=((Fsys / Fpwm) * 50) / 100;
// TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
// TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
// TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset;
// TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
// TIM_OC2Init(TIM1,&TIM_OCInitStructure);
//设置刹车特性,死区时间,锁电平,OSSI,OSSR 状态和AOE(自动输出使能)
TIM_BDTR_InitStructure.TIM_OSSRState = TIM_OSSRState_Disable;
TIM_BDTR_InitStructure.TIM_OSSIState = TIM_OSSIState_Disable;
TIM_BDTR_InitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
TIM_BDTR_InitStructure.TIM_DeadTime = 0x33;
TIM_BDTR_InitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTR_InitStructure.TIM_BreakPolarity =TIM_BreakPolarity_High;
TIM_BDTR_InitStructure.TIM_AutomaticOutput =TIM_AutomaticOutput_Disable ;
TIM_BDTRConfig(TIM1,&TIM_BDTR_InitStructure);
TIM_ARRPreloadConfig(TIM1,ENABLE);
// TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
TIM1_NVICConfig();
DMA_Config();
}
void TIM1_GPIOConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB,ENABLE);
/******************************************
TIM1_CH1 PA8
TIM1_CH1N PB13
TIM1_CH2 PA9
TIM1_CH2N PB14
******************************************/
//引脚复用
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_TIM1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_TIM1);
// GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_TIM1);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//开机保护 防止上下管同时导通
GPIO_ResetBits(GPIOA,GPIO_Pin_9);
delay_us(3);
GPIO_SetBits(GPIOB,GPIO_Pin_14);
}
void TIM1_NVICConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream1_IRQn; //DMA通道1中断允许
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
DMA_InitTypeDef DMA_InitStructure;
//DMAx的各通道配置
void DMA_Config()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//DMA2时钟使能
DMA_DeInit(DMA2_Stream1);
TIM_DMAConfig(TIM1, TIM_DMABase_CCR1, TIM_DMABurstLength_1Byte ); //设置TIM的DMA接口
TIM_DMACmd(TIM1,TIM_DMA_Update,ENABLE);
while (DMA_GetCmdStatus(DMA2_Stream1) != DISABLE){}//等待DMA可配置
/* 配置 DMA Stream */
DMA_InitStructure.DMA_Channel = DMA_Channel_6; //通道选择
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(TIM1_DMAR_Address);//DMA外设地址
DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)dmabuff;//DMA 存储器0地址
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;//存储器到外设模式
DMA_InitStructure.DMA_BufferSize = 250;//数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外设数据长度:16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//存储器数据长度:16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// 使用工作模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High;//中等优先级
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
// DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;//存储器突发单次传输
// DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;//外设突发单次传输
DMA_Init(DMA2_Stream1, &DMA_InitStructure);//初始化DMA Stream
DMA_Cmd(DMA2_Stream1, ENABLE);
DMA_ITConfig(DMA2_Stream1,DMA_IT_TC, ENABLE); //开传输完成中断
// DMA_GetITStatus(DMA2_Stream1, DMA_IT_TCIF1);
}
void DMA2_Stream1_IRQHandler(void) //开传输完成中断
{
LED0=0;
LED1=1;
delay_us(10);
// TIM_CtrlPWMOutputs(TIM1,DISABLE);
DMA_Cmd(DMA2_Stream1, DISABLE);
// if(DMA_IT_TCIF1==1)
// {
// LED0=0;
Bridge_Flag++;
BridgeStatus();
// }
DMA_ClearITPendingBit(DMA2_Stream1,DMA_IT_TCIF1);
// Bridge_Flag++;
// BridgeStatus();
// TIM_CtrlPWMOutputs(TIM1,ENABLE);
// DMA_ClearITPendingBit(DMA2_Stream1,DMA_IT_TCIF1);
}
DMA通过DMA2_Stream1数据流,将数据传输到TIM_DMABase_CCR1端口,完成这个动作后产生中断,问题就是这个中断无法产生,大神们帮帮忙啊,拜托拜托。。。
一周热门 更多>