<p>
</p><p>int main()
{
// 首先建立一个正弦表,一个周期内的, 正弦值乘100, 如果是负数就加100
uint8_t table[]={
3,6,9,13,16,19,22,25,28,31,34,37,40,43,45,48,51,54,56,59,61,64,66,68,71,73,75,77,79,81,83,84,86,88,89,90,92,93,94,95,96,97,98,98,99,99,100,100,100,100,100,100,100,99,99,98,98,97,96,95,94,93,92,90,89,88,86,84,83,81,79,77,75,73,71,68,66,64,61,59,56,54,51,48,45,43,40,37,34,31,28,25,22,19,16,13,9,
6,3,1,97,94,91,87,84,81,78,75,72,69,66,63,60,57,55,52,49,46,44,41,39,36,34,32,29,27,25,23,21,19,17,16,14,12,11,10,8,7,6,5,4,3,2,2,1,1,0,0,0,0,0,0,0,1,1,2,2,3,4,5,6,7,8,10,11,12,14,16,17,19,21,23,25,27,29,32,34,36,39,41,44,46,49,52,55,57,60,63,66,
69,72,75,78,81,84,87,91,94,97,100};
int cnt = 0;</p><p>
tpm_general_config_t TPM_InitStructure; //TPM初始化结构体
tpm_pwm_param_t PWM_InitStructure; // PWM初始化结构体
CLOCK_SYS_EnablePortClock(PORTD_IDX); //打开PORT时钟</p><p> CLOCK_SYS_EnableTpmClock(0); // 开启TPM时钟
PORT_HAL_SetMuxMode(PORTD, 1u, kPortMuxAlt4); // 设置PTD1端口模式
TPM_DRV_SetClock(0, kTpmClockSourceModuleHighFreq, kTpmDividedBy2);
//设置TPM时钟, 使用TPM0,高速时钟, 分频系数为2 </p><p> TPM_InitStructure.isDBGMode = 0; //debug模式下,如果为0就暂停, 如果是1就继续运行
TPM_InitStructure.isGlobalTimeBase = 0; //全局时钟 , 禁止
TPM_InitStructure.isTriggerMode = 0; //触发模式, 禁止
TPM_InitStructure.isCountReloadOnTrig = 0; //触发后不重新装载, 这里没有用到触发模式
TPM_InitStructure.isStopCountOnOveflow = 0; // 在溢出后继续计数</p><p> PWM_InitStructure.mode = kTpmCenterAlignedPWM; // PWM 中心对齐
PWM_InitStructure.edgeMode = kTpmLowTrue; //
PWM_InitStructure.uFrequencyHZ = 100000U; //PWM频率100kHz
PWM_InitStructure.uDutyCyclePercent = 0U; //占空比 0
GPIO_Init(); // 初始化PTD2 , 默认高电平
TPM_DRV_Init(0,&TPM_InitStructure); //TPM初始化
TPM_DRV_PwmStart(0,&PWM_InitStructure,1); //开始输出PWM波, TPM0, 通道1
while(1)
{
for(cnt = 0;cnt < 100;cnt++)
{
PWM_InitStructure.uDutyCyclePercent = table[cnt]; //不断修改占空比
TPM_DRV_PwmStart(0,&PWM_InitStructure,1);
}</p><p>
GPIO_DRV_TogglePinOutput(ref); //到半个周期的时候翻转PTD2的电平
for(cnt=100;cnt < 200;cnt++)
{
PWM_InitStructure.uDutyCyclePercent = table[cnt];
TPM_DRV_PwmStart(0,&PWM_InitStructure,1);
} </p><p>
</p><p> GPIO_DRV_TogglePinOutput(ref); //再过半个周期翻转PTD2的电平
}
return 0;
}
</p>