本帖最后由 finish2 于 2020-8-13 08:50 编辑
贴一下程序,还是没解决。我做的是一个按键控制电源,卡在PWM这里,麻烦路过的大佬指点一下
#include "stm8l051f3.h"
_Bool PD5@PD_ODR:5;
_Bool PD6@PD_ODR:6;
void TIM3_PWM_SET(float a,unsigned long F_PWM_SET);
void qiuwei(float c);
void display(void);
void keypress(void);
typedef unsigned int u16;
int shiwei,gewei,number;
unsigned char duan[10]={0xd7,0x11,0xcb,0x5b,0x1d,0x5e,0xde,0x13,0xdf,0x5f};//PB口0-9段
unsigned char san[10]={0xf7,0x31,0xeb,0x7b,0x3d,0x7e,0xfe,0x33,0xff,0x7f};//数码管3小数点常亮的段码
void TIM3_PWM_SET(float a,unsigned long F_PWM_SET)
{
float d;
TIM3_ARRH=F_PWM_SET/256;//配置自动重装载寄存器高位“ARRH"
TIM3_ARRL=F_PWM_SET%256;//配置自动重装载寄存器低位“ARRL”
d=a*F_PWM_SET;//“1持续的时间”
TIM3_CCR2H=((u16)(d))/256;//配置捕获/比较寄存器1高位“CCR1H”
TIM3_CCR2L=((u16)(d))%256;//配置捕获/比较寄存器1低位“CCR1L"
TIM3_CR1&=0x8F;//向上对齐模式边沿对齐
TIM3_CCMR2=0x60;//配置为PWM模式1
TIM3_CCER1&=0xFD;//配置CC1P=0,OC1信号高电平有效
TIM3_CCER1|=0x01;//配置CC1E=1,使能OC1输出
TIM3_OISR|=0x01;//空闲状态时OC1为高电平
TIM3_CR1|=0x01;//使能TIM3计数器功能"CEN=1"
TIM3_BKR=0x80;//打开"主输出"开关输出PWM信号"MOE=1"
}
void delay(unsigned int time)
{
while(time--);
}
main()
{
float a,c;
unsigned long F_PWM_SET;
//段码
PB_ODR = 0X00;
PB_DDR = 0XFF;
PB_CR1 = 0XFF;
PB_CR2 = 0XFF;
//位码
PC_ODR =0XFF;
PC_DDR =0XFF;
PC_CR1 =0XFF;
PC_CR2 =0XFF;
//PD0的初始化
PD_DDR=0x01;
PD_CR1=-0xff;
PD_CR2=0xff;
CLK_PCKENR1=0xff;//打开....加了这条语句就可以了
CLK_CKDIVR =0x18;//内部HIS时钟不分频,f=2MHZ
while(1)
{
a=0.9;//占空比
F_PWM_SET=0xA1A2;//周期
TIM3_PWM_SET(a,F_PWM_SET);
c=a*3.3;//输出的电压值
qiuwei(c);
display();
}
}
void qiuwei(float c)
{
shiwei=c/10;
gewei=c-10*shiwei;
number=(c-10*shiwei-gewei)/0.1;
}
//电压值数码显示
void display(void)
{
PC_ODR=0xef;
PB_ODR=duan[shiwei];
delay(500);
PC_ODR=0xfd;
PB_ODR=san[gewei];
delay(500);
PC_ODR=0xfe;
PB_ODR=duan[number];
delay(500);
}
|