|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
我最近在编一个程序,根据需要要在P3.5引脚输出20K的方波,使用的晶振是2M的。
我的程序如下:
#include <STC12C2052.H>
#include <stdio.h>
/*三相交流电机,4个键:走时、正快、反快、停钟,指示灯:走时时1秒1闪,加快时闪烁,停钟时亮2秒灭2秒*/
//含看门狗,看门狗定时器溢出时间393.216ms
//20K的方波已通过验证0
//P1.3走时键
#define ZouShiYouXiao (P1 & 0x08)==0
//P1.2正快键
#define ZhengKuaiYouXiao (P1 & 0x04)==0
//P1.1反快键
#define FanKuaiYouXiao (P1 & 0x02)==0
//P1.0停钟键
#define TingZhongYouXiao (P1 & 0x01)==0
//停钟
#define TingZhongChaoZuo P1 &= 0x1F
#define ZhiShiDengLiang P3 |= 0x80
#define ZhiShiDengMie P3 &= 0x7F
int Miao=0;
int j_2ms=0;
int j_2S=0;
int j_Timer0=0;
int j_DingShi=0;//0.5ms定时记数
int j_WuJianCiShu=0;
bit b_JianYiChuLi=0;
bit b_JiShu=0;
int j_JianYouXiaoCiShu=0;
int c_YunXingZhuangTai=1;
#define ZouShi 1
#define ZhengKuai 2
#define FanKuai 3
#define TingZhong 4
//A--AB--B--BC--C--CA
#define Xu_1 P1=(P1 | 0x80) & 0x9F
#define Xu_2 P1=(P1 | 0x0C0) & 0x0DF
#define Xu_3 P1=(P1 | 0x40) & 0x5F
#define Xu_4 P1=(P1 | 0x60) & 0x7F
#define Xu_5 P1=(P1 | 0x20) & 0x3F
#define Xu_6 P1=(P1 | 0x0A0) & 0xBF
//匹配值
#define PiPeiZhi 25
int c_JiePai = 0; //相序循环记数
void main(void)
{
TMOD = 0x20;//定时器1工作模式8位自动重装载,定时器0工作在13位定时模式。
//TCON = 0x51;
SCON = 0x50;
AUXR = 0xC0;//定时器0和1工作在1T模式
P3M0 =0x01;
P3M1 =0x20;
P1M0 =0x00;
P1M1 =0x10;
TL0 = 0x18;
TH0 = 0x1C;
//TL1 = 0xF3;//波特率4.8K
//TH1 = 0xF3;//定时器1做波特率发生器
TR1 = 0;
IT0 = 1;
//ES = 1;
EX0 = 1;
EX1 = 0;
ET0 = 1;
//ET1 = 1;
CMOD = 0x02;
CCON =0;
CL = 0;
CH = 0;
CCAP1L = PiPeiZhi;
CCAP1H = 0;
CCAPM1 = 0x4D;
IP = 0x40;
IPH = 0x40;
EPCA_LVD=1;
EA = 1;
WDT_CONTR=0x38;
TR0=1;
CR = 1;
loop:
if (CCF1==1)
{
CCF1 =0;//清匹配中断标志CCF1
if (CCAP1L<=(255-PiPeiZhi)) CCAP1L =CCAP1L + PiPeiZhi;
else
{
CCAP1L=PiPeiZhi-(256-CCAP1L);
if (CCAP1H<255) CCAP1H++;
else CCAP1H=0;
}
}
PCON=PCON | 0x01;
goto loop;
}
int c_JieShouZhi=0;
//---------------------
void Int0(void) interrupt 0
{
PCON=0x02;//进入掉电模式
}
//波特率4.8K,误差0.16%
//--------------------
/*void ChuanKou(void) interrupt 4 //串口中断
{
if (RI==1)
{
RI=0;
c_JieShouZhi=SBUF;
}
else
{
TI=0;
}
}*/ //-------------------
void QuDong(void) //驱动
{
switch (c_JiePai)
{
case 0: Xu_1; break;
case 1: Xu_2; break;
case 2: Xu_3; break;
case 3: Xu_4; break;
case 4: Xu_5; break;
case 5: Xu_6; break;
}
}
//-------------------------------------
void ZhiShiDeng(int l_YunXingZhuangTai) //运行指示灯
{
switch (l_YunXingZhuangTai)
{
case 1:{if (j_2ms<50) ZhiShiDengLiang; else ZhiShiDengMie;}break;
case 2:{
if (j_2ms<50 || (j_2ms >100 && j_2ms<150) || (j_2ms>200 && j_2ms<250)
||(j_2ms>300 && j_2ms<350) ||(j_2ms>400 && j_2ms<450))
ZhiShiDengLiang;
else ZhiShiDengMie;
}break;
case 3:{
if (j_2ms<50 || (j_2ms >100 && j_2ms<150) || (j_2ms>200 && j_2ms<250)
||(j_2ms>300 && j_2ms<350) ||(j_2ms>400 && j_2ms<450))
ZhiShiDengLiang;
else ZhiShiDengMie;
}break;
case 4:{
if (b_JiShu==1) ZhiShiDengLiang;
else ZhiShiDengMie;
}break;
}
}
//-------------------------
void ZhengXiangYunXing(void) //正向运行
{
if (c_JiePai<5) c_JiePai++;
else c_JiePai=0;
QuDong();
}
//----------------------------
void FanXiangYunXing(void) //反向运行
{
if (c_JiePai>0) c_JiePai--;
else c_JiePai=5;
QuDong();
}
//-----------------------------
init_Timer0(void)interrupt 1 //0.5ms定时中断
{
//----------------------------
if (j_Timer0<4) j_Timer0++;
else j_Timer0=0;//计5个数,即每2.5ms电机走一步,1S400步。
if (c_YunXingZhuangTai<5)
{
switch (c_YunXingZhuangTai)
{
case 1:{if (j_Timer0==0) ZhengXiangYunXing();}break;//走时
case 2:{ZhengXiangYunXing();}break;//正快
case 3:{FanXiangYunXing();}break;//反快
case 4:{TingZhongChaoZuo;}break;//停钟
}
}
//-----------------------
if (ZouShiYouXiao)
{
j_WuJianCiShu=0;
if (j_JianYouXiaoCiShu>1)
{
if (b_JianYiChuLi==0)
{
c_YunXingZhuangTai=ZouShi;
b_JianYiChuLi=1;
}
}
else j_JianYouXiaoCiShu++;
}
else
{
if (j_WuJianCiShu>10)
{
j_JianYouXiaoCiShu=0;
b_JianYiChuLi=0;
}
else j_WuJianCiShu++;
}
//-------------------------
if (ZhengKuaiYouXiao)
{
j_WuJianCiShu=0;
if (j_JianYouXiaoCiShu>1)
{
if (b_JianYiChuLi==0)
{
c_YunXingZhuangTai=ZhengKuai;
b_JianYiChuLi=1;
}
}
else j_JianYouXiaoCiShu++;
}
else
{
if (j_WuJianCiShu>10)
{
j_JianYouXiaoCiShu=0;
b_JianYiChuLi=0;
}
else j_WuJianCiShu++;
}
//-------------------------
if (FanKuaiYouXiao)
{
j_WuJianCiShu=0;
if (j_JianYouXiaoCiShu>1)
{
if (b_JianYiChuLi==0)
{
c_YunXingZhuangTai=FanKuai;
b_JianYiChuLi=1;
}
}
else j_JianYouXiaoCiShu++;
}
else
{
if (j_WuJianCiShu>10)
{
j_JianYouXiaoCiShu=0;
b_JianYiChuLi=0;
}
else j_WuJianCiShu++;
}
//-------------------------
if (TingZhongYouXiao)
{
j_WuJianCiShu=0;
if (j_JianYouXiaoCiShu>1)
{
if (b_JianYiChuLi==0)
{
c_YunXingZhuangTai=TingZhong;
b_JianYiChuLi=1;
}
}
else j_JianYouXiaoCiShu++;
}
else
{
if (j_WuJianCiShu>10)
{
j_JianYouXiaoCiShu=0;
b_JianYiChuLi=0;
}
else j_WuJianCiShu++;
}
//----------------------
WDT_CONTR=0x38;//喂狗
if (j_DingShi<3) j_DingShi++;
else
{
j_DingShi=0;
if (j_2ms<499) j_2ms++;
else
{
j_2ms=0;
if (Miao<59) Miao++;
else Miao=0;
if (j_2S<1) j_2S++;
else
{
j_2S=0;
b_JiShu=!b_JiShu;
}
}
}
if (CH!=CCAP1H || (CH==CCAP1H && (CL>CCAP1L))) ZhiShiDeng(c_YunXingZhuangTai);
} |
|