生成一个 10bit的 sin函数表 720项
const INT32U sintab[]={
0x0200,0x0204,0x0209,0x020d,0x0212,0x0216,0x021b,0x021f,0x0224,0x0228,0x022d,0x0231,0x0235,0x023a,0x023e,0x0243,0x0247,0x024c,0x0250,0x0254,0x0259,0x025d,0x0262,0x0266,0x026a,0x026f,0x0273,0x0277,0x027c,0x0280,0x0284,0x0289,0x028d,0x0291,0x0295,0x029a,0x029e,0x02a2,0x02a6,0x02ab,0x02af,0x02b3,0x02b7,0x02bb,0x02bf,0x02c4,0x02c8,0x02cc,0x02d0,0x02d4,0x02d8,0x02dc,0x02e0,0x02e4,0x02e8,0x02ec,0x02f0,0x02f4,0x02f8,0x02fc,0x02ff,0x0303,0x0307,0x030b,0x030f,0x0313,0x0316,0x031a,0x031e,0x0321,0x0325,0x0329,0x032c,0x0330,0x0334,0x0337,0x033b,0x033e,0x0342,0x0345,0x0348,0x034c,0x034f,0x0353,0x0356,0x0359,0x035d,0x0360,0x0363,0x0366,0x0369,0x036c,0x0370,0x0373,0x0376,0x0379,0x037c,0x037f,0x0382,0x0385,0x0387,0x038a,0x038d,0x0390,0x0393,0x0395,0x0398,0x039b,0x039d,0x03a0,0x03a3,0x03a5,0x03a8,0x03aa,0x03ad,0x03af,0x03b1,0x03b4,0x03b6,0x03b8,0x03bb,0x03bd,0x03bf,0x03c1,0x03c3,0x03c5,0x03c7,0x03c9,
0x03cb,0x03cd,0x03cf,0x03d1,0x03d3,0x03d5,0x03d6,0x03d8,0x03da,0x03db,0x03dd,0x03df,0x03e0,0x03e2,0x03e3,0x03e5,0x03e6,0x03e7,0x03e9,0x03ea,0x03eb,0x03ec,0x03ee,0x03ef,0x03f0,0x03f1,0x03f2,0x03f3,0x03f4,0x03f5,0x03f6,0x03f6,0x03f7,0x03f8,0x03f9,0x03f9,0x03fa,0x03fb,0x03fb,0x03fc,0x03fc,0x03fd,0x03fd,0x03fd,0x03fe,0x03fe,0x03fe,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03ff,0x03fe,0x03fe,0x03fe,0x03fd,0x03fd,0x03fd,0x03fc,0x03fc,0x03fb,0x03fb,0x03fa,0x03f9,0x03f9,0x03f8,0x03f7,0x03f6,0x03f6,0x03f5,0x03f4,0x03f3,0x03f2,0x03f1,0x03f0,0x03ef,0x03ee,0x03ec,0x03eb,0x03ea,0x03e9,0x03e7,0x03e6,0x03e5,0x03e3,0x03e2,0x03e0,0x03df,0x03dd,0x03db,0x03da,0x03d8,0x03d6,0x03d5,0x03d3,0x03d1,0x03cf,0x03cd,0x03cb,0x03c9,0x03c7,0x03c5,0x03c3,0x03c1,0x03bf,0x03bd,0x03bb,0x03b8,0x03b6,0x03b4,0x03b1,0x03af,0x03ad,0x03aa,0x03a8,0x03a5,0x03a3,0x03a0,0x039d,0x039b,0x0398,0x0395,
0x0393,0x0390,0x038d,0x038a,0x0387,0x0385,0x0382,0x037f,0x037c,0x0379,0x0376,0x0373,0x0370,0x036c,0x0369,0x0366,0x0363,0x0360,0x035d,0x0359,0x0356,0x0353,0x034f,0x034c,0x0348,0x0345,0x0342,0x033e,0x033b,0x0337,0x0334,0x0330,0x032c,0x0329,0x0325,0x0321,0x031e,0x031a,0x0316,0x0313,0x030f,0x030b,0x0307,0x0303,0x02ff,0x02fc,0x02f8,0x02f4,0x02f0,0x02ec,0x02e8,0x02e4,0x02e0,0x02dc,0x02d8,0x02d4,0x02d0,0x02cc,0x02c8,0x02c4,0x02bf,0x02bb,0x02b7,0x02b3,0x02af,0x02ab,0x02a6,0x02a2,0x029e,0x029a,0x0295,0x0291,0x028d,0x0289,0x0284,0x0280,0x027c,0x0277,0x0273,0x026f,0x026a,0x0266,0x0262,0x025d,0x0259,0x0254,0x0250,0x024c,0x0247,0x0243,0x023e,0x023a,0x0235,0x0231,0x022d,0x0228,0x0224,0x021f,0x021b,0x0216,0x0212,0x020d,0x0209,0x0204,0x0200,0x01fc,0x01f7,0x01f3,0x01ee,0x01ea,0x01e5,0x01e1,0x01dc,0x01d8,0x01d3,0x01cf,0x01cb,0x01c6,0x01c2,0x01bd,0x01b9,0x01b4,0x01b0,0x01ac,0x01a7,0x01a3,0x019e,0x019a,
0x0196,0x0191,0x018d,0x0189,0x0184,0x0180,0x017c,0x0177,0x0173,0x016f,0x016b,0x0166,0x0162,0x015e,0x015a,0x0155,0x0151,0x014d,0x0149,0x0145,0x0141,0x013c,0x0138,0x0134,0x0130,0x012c,0x0128,0x0124,0x0120,0x011c,0x0118,0x0114,0x0110,0x010c,0x0108,0x0104,0x0100,0x00fd,0x00f9,0x00f5,0x00f1,0x00ed,0x00ea,0x00e6,0x00e2,0x00df,0x00db,0x00d7,0x00d4,0x00d0,0x00cc,0x00c9,0x00c5,0x00c2,0x00be,0x00bb,0x00b8,0x00b4,0x00b1,0x00ad,0x00aa,0x00a7,0x00a3,0x00a0,0x009d,0x009a,0x0097,0x0094,0x0090,0x008d,0x008a,0x0087,0x0084,0x0081,0x007e,0x007b,0x0079,0x0076,0x0073,0x0070,0x006d,0x006b,0x0068,0x0065,0x0063,0x0060,0x005d,0x005b,0x0058,0x0056,0x0053,0x0051,0x004f,0x004c,0x004a,0x0048,0x0045,0x0043,0x0041,0x003f,0x003d,0x003b,0x0039,0x0037,0x0035,0x0033,0x0031,0x002f,0x002d,0x002b,0x002a,0x0028,0x0026,0x0025,0x0023,0x0021,0x0020,0x001e,0x001d,0x001b,0x001a,0x0019,0x0017,0x0016,0x0015,0x0014,0x0012,0x0011,
0x0010,0x000f,0x000e,0x000d,0x000c,0x000b,0x000a,0x000a,0x0009,0x0008,0x0007,0x0007,0x0006,0x0005,0x0005,0x0004,0x0004,0x0003,0x0003,0x0003,0x0002,0x0002,0x0002,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0001,0x0002,0x0002,0x0002,0x0003,0x0003,0x0003,0x0004,0x0004,0x0005,0x0005,0x0006,0x0007,0x0007,0x0008,0x0009,0x000a,0x000a,0x000b,0x000c,0x000d,0x000e,0x000f,0x0010,0x0011,0x0012,0x0014,0x0015,0x0016,0x0017,0x0019,0x001a,0x001b,0x001d,0x001e,0x0020,0x0021,0x0023,0x0025,0x0026,0x0028,0x002a,0x002b,0x002d,0x002f,0x0031,0x0033,0x0035,0x0037,0x0039,0x003b,0x003d,0x003f,0x0041,0x0043,0x0045,0x0048,0x004a,0x004c,0x004f,0x0051,0x0053,0x0056,0x0058,0x005b,0x005d,0x0060,0x0063,0x0065,0x0068,0x006b,0x006d,0x0070,0x0073,0x0076,0x0079,0x007b,0x007e,0x0081,0x0084,0x0087,0x008a,0x008d,0x0090,0x0094,0x0097,0x009a,0x009d,0x00a0,0x00a3,0x00a7,0x00aa,0x00ad,0x00b1,0x00b4,
0x00b8,0x00bb,0x00be,0x00c2,0x00c5,0x00c9,0x00cc,0x00d0,0x00d4,0x00d7,0x00db,0x00df,0x00e2,0x00e6,0x00ea,0x00ed,0x00f1,0x00f5,0x00f9,0x00fd,0x0100,0x0104,0x0108,0x010c,0x0110,0x0114,0x0118,0x011c,0x0120,0x0124,0x0128,0x012c,0x0130,0x0134,0x0138,0x013c,0x0141,0x0145,0x0149,0x014d,0x0151,0x0155,0x015a,0x015e,0x0162,0x0166,0x016b,0x016f,0x0173,0x0177,0x017c,0x0180,0x0184,0x0189,0x018d,0x0191,0x0196,0x019a,0x019e,0x01a3,0x01a7,0x01ac,0x01b0,0x01b4,0x01b9,0x01bd,0x01c2,0x01c6,0x01cb,0x01cf,0x01d3,0x01d8,0x01dc,0x01e1,0x01e5,0x01ea,0x01ee,0x01f3,0x01f7,0x01fc,
};
初始化 TIM1PWM 输出
#include "includes.h"
#ifndef TMRNCLK
#define TMRNCLK SYSCLK
#endif
void Tim1PwmGPIOInit(void)
{
SetPinState(GPIOA,GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10,GPIO_Mode_AF_PP);
}
void DcMotorTim1Init(INT32U pwm_period,INT32U frequency)
{
// NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
Tim1PwmGPIOInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //使能 TIMx 外设
TIM_DeInit(TIM1);
TIM_TimeBaseStructure.TIM_Period = pwm_period-1; //设置自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =TMRNCLK/frequency/pwm_period-1; //设置预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim 这里是 32M
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向下计数
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //初始化 TIMx
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 =0; //PWM占空比
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //上桥臂
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low ; //下桥臂
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
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 = 0x00; //0x6A; //死区1.8us
TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_Low;
TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM1, TIM_IT_Update); //清中断标志位
TIM_ITConfig(TIM1,TIM_IT_Update ,ENABLE); //打开中断
TIM_Cmd(TIM1, ENABLE);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
初始化 DMA
void Dma4Pwm(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //启动DMA时钟
/* DMA1 DMA1_Channel5 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (INT32U)(&TIM1->CCR1); //(u32)USART2_DR_Address; //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (INT32U)sintab; //内存地址,发送时候
DMA_InitStructure.DMA_DIR =DMA_DIR_PeripheralDST; // DMA_DIR_PeripheralSRC; //
DMA_InitStructure.DMA_BufferSize =GetItemSum(sintab);
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
/* Enable DMA channel1 */
DMA_Cmd(DMA1_Channel5, ENABLE);
}
启动任务,通过TIM1_UPDATE 触发dma,TIM1_OC1 就输出占空比根据 sin 变化的波形
void SinTask(void *pdata)
{
SysTick_CounterCmd(SysTick_Counter_Enable); //系统时钟开始计数
Dma4Pwm();
DcMotorTim1Init(1024,SYSCLK/1024);
TIM_DMAConfig(TIM1,TIM_DMABase_CCR1,TIM_DMABurstLength_1Byte);
TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE);
printf("dma2sin测试\r\n");
while(1) OSTimeDly(OS_TICKS_PER_SEC);
}
理论是这样做 但是 硬件可能要做一个低通滤波 现在采用示波器看到还是 PWM 的波形 没有看到sin波
|