/***************************************PFC886+MF520PWM**********************************************/
/***************************************QQ:774145445*************************************************/
/****************************************快手:共同学习STM8*******************************************/
/****************************************20210423****************************************************/
// _________ _________
// PWM2L-|1 pa2 \_/ pa1 20|-PWM2H
// PWM1L-|2 pa3 pa0 19|-PWM1H
// pwm0L-|3 pa4 pa7 18|-pwm0H
// |4 pa5 pa6 17|-led_1
// 与gnd相连|-|5 nc nc 16|-|与vdd相连
// |-|6 gnd vdd 15|-|
// k_1-|7 pb0 pb7 14|-led_2
// k_2-|8 pb1 pb6 13|-led_3
// k_3-|9 pb2 pb5 12|-led_4
// k_4-|10_pb3_________pb4_11|-led_5
#include "extern.h"
/****************************************************************************************************/
k_1 bit pb.0;
k_2 bit pb.1;
k_3 bit pb.2;
k_4 bit pb.3;
led_1 bit pa.6;
led_2 bit pb.7;
led_3 bit pb.6;
led_4 bit pb.5;
led_5 bit pb.4;
led_a bit pb.6;
led_b bit pb.5;
led_c bit pb.4;
/****************************************************************************************************/
void zhengxianbo(); //正弦波
void fuzhi(); //赋值
void yanshi(); //延时
void yanshi1(); //延时
/****************************************************************************************************/
word a1,b1,c1;
word yanshi_a1;
/****************************************************************************************************/
void FPPA0 (void)
{
.ADJUST_IC SYSCLK=IHRC/4 // SYSCLK=IHRC/4
pmode 31; ///8/8/8/8/8/8/8/8带宽共享
fppen = 0xFF; //8核全开有8个核心可以分时全部运行,可以分别干8件事8核真爽!
/****************************************************************************************************/
$ led_1 out,low;
$ k_1 in,pull;
$ k_2 in,pull;
$ k_3 in,pull;
$ k_4 in,pull;
/****************************************************************************************************/ //pwm配置
//f----------------0 f----------------0 f----------------0 f----------------0(word,int)
//f----------------0 f----------------0 f----------------0 //12位PWM0,1,2(左对齐)
// ↑上限(pwmcub) ↑占空比(pwmdt) |--死区(pwmdz)---|
//上限pwmcub=0xfff0,死区pwmdz=0xff,
//pwm0dtl(先写)pwm0dth,pwm1dtl(先写)pwm1dth,pwm2dtl(先写)pwm2dth,
//最大0xf010, pwm_h=1+死区, pwm_l=0,
//最小0x000,0x0010, pwm_h=0, pwm_l=1,
//PWM最大0x0fff-死区 =0xf000,
//PWM最小0x0000+2 =0x0020,
//pwm最大值取0xf000-0xff =<0xef00高脉冲4us
//pwm最小值取0x0020+0xff =>0x00ff高脉冲4us
/*
a1=0xf010;fuzhi();yanshi();
//a1=0x0000;fuzhi();yanshi();
a1=0x0010;fuzhi();yanshi();
a1=0xf000;fuzhi();yanshi();
a1=0x0020;fuzhi();yanshi();*/
/****************************************************************************************************/
$ pwmgc enable,updown,reset;
$ pwmgm protection_enable,ihrc,/4;
//pwmgc =0b1_0_1_1_0000; //PWMG控制寄存器位7,停用启用位6,输出状态位5,中心排列模式位4,PWM计数器清零
//pwmgm =0b0_0_1_0_10_10; //PWMG分频寄存器位7,上臂反极性位6,下臂反极性位5,臂保护位4,臂保护模式位32,PWM时钟源位10,PWM分频
//pwmgm =0b0_0_1_0_10_11; //PWMG分频寄存器
pwmgc1 =0b1_11_0_01_10; //PWMG控制寄存器1位7,互补发生器位65,三相电机位4,读零位32,PWM5.PWM+位10,PWM4.PWM-
pwmgc2 =0b01_10_01_10; //PWMG控制寄存器2位76,PWM3.PWM+位54,PWM2.PWM-位32,PWM1.PWM+位10,PWM0.PWM-
pwmcubl =0b1111_0000; //pwm上限寄存器l
pwmcubh =0b1111_1111; //pwm上限寄存器h
//pwmdz =0b1111_0000; //死区寄存器
pwmdz =0b1111_1111; //死区寄存器//*注意死区占上臂时间需要扣除
//pwm0dtl =0b1111_0000; //pwm0占空比低位寄存器//先写低位
//pwm0dth =0b1111_1111; //pwm0占空比高位寄存器
//pwm1dtl =0b0000_0000; //pwm1占空比低位寄存器//先写低位
//pwm1dth =0b1000_0000; //pwm1占空比高位寄存器
//pwm2dtl =0b1111_0000; //pwm2占空比低位寄存器//先写低位
//pwm2dth =0b0000_0000; //pwm2占空比高位寄存器
/****************************************************************************************************/
while (1)
{
if(led_1){led_1=0;} else{led_1=1;}
.delay 10000;
}
}
/****************************************************************************************************/
void FPPA1 (void)
{while(1)
{
zhengxianbo(); //正弦波
}
}
/****************************************************************************************************/
void FPPA2 (void)
{
while(1)
{
$ led_a out,high;
$ led_b out,low;
$ led_c in;
yanshi1();
$ led_a out,high;
$ led_b in;
$ led_c out,low;
yanshi1();
$ led_a in;
$ led_b out,high;
$ led_c out,low;
yanshi1();
$ led_a out,low;
$ led_b out,high;
$ led_c in;
yanshi1();
$ led_a out,low;
$ led_b in;
$ led_c out,high;
yanshi1();
$ led_a in;
$ led_b out,low;
$ led_c out,high;
yanshi1();
}
}
/****************************************************************************************************/
void FPPA3 (void)
{
byte anjian_a1;
yanshi_a1 =800;
while(1)
{
while(!k_1)
{
yanshi_a1 ++;
.delay 500;
}
while(!k_2)
{
yanshi_a1 --;
.delay 1000;
}
}
}
/****************************************************************************************************/
void FPPA4 (void)
{
goto $;
}
/****************************************************************************************************/
void FPPA5 (void)
{
goto $;
}
/****************************************************************************************************/
void FPPA6 (void)
{
goto $;
}
/****************************************************************************************************/
void FPPA7 (void)
{
goto $;
}
/****************************************************************************************************/
/*
void Interrupt (void)
{
pushaf;
if (Intrq.T16)
{ // T16 Trig
// User can add code
Intrq.T16 = 0;
//...
}
popaf;
}
*/
/****************************************************************************************************/
//pwm最大值取0xf000-0xff =<0xef00高脉冲4us
//pwm最小值取0x0020+0xff =>0x00ff高脉冲4us
void zhengxianbo() //正弦波
{
bit zx_a1,zx_b1,zx_c1; //状态机0减小1增大
a1 =0xed00; //a相
b1 =0x3900; //b相//0x9f000xb1c0
c1 =0x3900; //c相
zx_a1 =0; //减小
zx_b1 =0; //减小
zx_c1 =1; //增大
while(1)
{
if(zx_a1) //a相增大
{
pwm0dtl =a1 $0;
pwm0dth =a1 $1;
a1 +=0x01ff;
if(a1>0xed00) //a相变减小*注意减去死区时间
{
zx_a1=0;
/* a1 =0xed00; //a相
b1 =0x3900; //b相
c1 =0x3900; //c相
zx_a1 =0; //减小
zx_b1 =0; //减小
zx_c1 =1; //增大*/
}
}
if(!zx_a1) //a相减小
{
pwm0dtl =a1 $0;
pwm0dth =a1 $1;
a1 -=0x01ff;
if(a1<0x02ff){zx_a1=1;} //a相变增大0x001f0x00ff
}
if(zx_b1) //b相增大
{
pwm1dtl =b1 $0;
pwm1dth =b1 $1;
b1 +=0x01ff;
if(b1>0xed00){zx_b1=0;} //b相变减小
}
if(!zx_b1) //b相减小
{
pwm1dtl =b1 $0;
pwm1dth =b1 $1;
b1 -=0x01ff;
if(b1<0x02ff){zx_b1=1;} //b相变增大
}
if(zx_c1) //c相增大
{
pwm2dtl =c1 $0;
pwm2dth =c1 $1;
c1 +=0x01ff;
if(c1>0xed00){zx_c1=0;} //c相变减小
}
if(!zx_c1) //c相减小
{
pwm2dtl =c1 $0;
pwm2dth =c1 $1;
c1 -=0x01ff;
if(c1<0x02ff){zx_c1=1;} //c相变增大
}
yanshi();
}
}
/****************************************************************************************************/
void fuzhi() //赋值
{
pwm0dtl =a1 $0; //先写
pwm0dth =a1 $1;
pwm1dtl =b1 $0;
pwm1dth =b1 $1;
pwm2dtl =c1 $0;
pwm2dth =c1 $1;
}
/****************************************************************************************************/
void yanshi() //延时
{
word yanshi_b1;
yanshi_b1 =yanshi_a1;
while(yanshi_b1--){}
//.delay 300;
//while(k_1){.delay 1000;if(k_2==0){a1--;fuzhi();}} //定在这
//while(!k_1){.delay 1000;}
}
/****************************************************************************************************/
void yanshi1()
{
.delay 30000;
}
/****************************************************************************************************/ |
共1人点赞
|