初次接触单片机,请大家帮给一个程序加上注解;

liushiying18 2014-09-27 04:00:22

#include <REG52.H>

sfr P2M1 = 0x95;
sfr P2M0 = 0x96;
sfr P3M1 = 0xb1;
sfr P3M0 = 0xb2;
sfr P1M1 = 0x91;
sfr P1M0 = 0x92;
sfr P0M1 = 0x93;
sfr P0M0 = 0x94;

sfr AUXR = 0x8e;
sfr WDT_CONTR = 0xc1;

//sbit CLR_WDT = WDT_CONTR^4;

sbit P20=P2^0;
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
sbit P24=P2^4;
sbit P25=P2^5;
sbit P26=P2^6;
sbit P27=P2^7;
sbit P30=P3^0;
sbit P31=P3^1;
sbit P32=P3^2;
sbit P33=P3^3;
sbit P34=P3^4;
sbit P35=P3^5;
sbit P36=P3^6;
sbit P37=P3^7;
sbit P10=P1^0;
sbit P11=P1^1;
sbit P12=P1^2;
sbit P13=P1^3;
sbit P14=P1^4;
sbit P15=P1^5;
sbit P16=P1^6;
sbit P17=P1^7;
sbit P01=P0^1;

unsigned char DispBuf[]={0,0,0,0};
unsigned char DispTemp1[4],DispTemp2[4];
unsigned int ChangeTM;
unsigned char i,j;
bit C_Flag;

#define WDT_EN WDT_CONTR=0x55
#define CLR_WDT WDT_CONTR|=0x10

#define D1_2 1/2
#define D3_4 3/2
#define D5_6 5/2
#define D7_8 7/2
#define D9_10 9/2
#define D11_12 11/2
#define D13_14 13/2
#define D15_16 15/2
#define D17_18 17/2
#define D19_20 19/2
#define D21_22 21/2
#define D23_24 23/2
#define D25_26 25/2
#define D27_28 27/2
#define D29_30 29/2
#define D31_32 31/2
#define D33_34 33/2
#define D35_36 35/2
#define D37_38 37/2
#define D39_40 39/2
#define D41_42 41/2
#define D43_44 43/2
#define D45_46 45/2
#define D47_48 47/2
#define D49_50 49/2
//======================================================
// 延时函数(MS)
//======================================================
void DelayXms(unsigned char n) //@12.000MHz
{
unsigned char i, j;
while(n)
{
CLR_WDT;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
n--;
}
}
//======================================================
// LED底层输出
//======================================================
void LEDDrive()
{
P25=DispBuf[0]&0x01;
P24=DispBuf[0]&0x02;
P23=DispBuf[0]&0x04;
P22=DispBuf[0]&0x08;
P21=DispBuf[0]&0x10;
P20=DispBuf[0]&0x20;
P37=DispBuf[0]&0x40;
P36=DispBuf[0]&0x80;
P35=DispBuf[1]&0x01;
P34=DispBuf[1]&0x02;
P33=DispBuf[1]&0x04;
P32=DispBuf[1]&0x08;
P31=DispBuf[1]&0x10;
P30=DispBuf[1]&0x20;
P01=DispBuf[1]&0x40;
P17=DispBuf[1]&0x80;
P16=DispBuf[2]&0x01;
P15=DispBuf[2]&0x02;
P14=DispBuf[2]&0x04;
P13=DispBuf[2]&0x08;
P12=DispBuf[2]&0x10;
P11=DispBuf[2]&0x20;
P10=DispBuf[2]&0x40;
P27=DispBuf[2]&0x80;
P26=DispBuf[3]&0x01;
}
//======================================================
// 带进位数据左移
//======================================================
unsigned char RLC(unsigned char Temp)
{
unsigned int V_Temp;
V_Temp = Temp;
V_Temp<<=1;
if(C_Flag)V_Temp|=0x01;
C_Flag=V_Temp&0x0100;
return V_Temp&0xff;
}
//======================================================
// 带进位数据右移
//======================================================
unsigned char RRC(unsigned char Temp)
{
unsigned int V_Temp;
V_Temp = (unsigned int)Temp<<7;
if(C_Flag)V_Temp|=0x8000;
C_Flag=V_Temp&0x0080;
return V_Temp>>8;
}
//======================================================
// 点亮所有LED灯
//======================================================
void LedAllOn(unsigned int xms)
{
DispBuf[0]=0xff;
DispBuf[1]=0xff;
DispBuf[2]=0xff;
DispBuf[3]=0xff;
LEDDrive();
DelayXms(xms);
}
//======================================================
// 关闭所有LED灯
//======================================================
void LedAllOff(unsigned int xms)
{
DispBuf[0]=0x00;
DispBuf[1]=0x00;
DispBuf[2]=0x00;
DispBuf[3]=0x00;
LEDDrive();
DelayXms(xms);
}
//======================================================
// 关闭所有LED灯
//======================================================
void LedAllOffA(unsigned int xms)
{
P0=0x00;
P1=0x00;
P2=0x00;
P3=0x00;
DelayXms(xms);
}
//======================================================
// 模式0:全部灯闪烁
//======================================================
void DisplayMode0(void)
{
LedAllOn(250);
LedAllOff(250);
LedAllOn(250);
LedAllOff(250);
LedAllOn(250);
LedAllOn(250);
LedAllOff(250);
}
//======================================================
// 模式1:双灯加速流动,最后快速流动20圈
//======================================================
void DisplayMode1(void)
{
LedAllOff(0);
i=100;
while(i)
{
for(j=0;j<25;j++)
{
DispBuf[j>>3] |= 0x01<<(j&0x07);
LEDDrive();
DelayXms(i);
i--;
DispBuf[j>>3] = 0x00;
if(i==0)break;
}
}
for(i=0;i<3;i++)
{
for(j=0;j<25;j++)
{
DispBuf[j>>3] |= 0x01<<(j&0x07);
LEDDrive();
DelayXms(1);
DispBuf[j>>3] = 0x00;
}
}
}
//======================================================
// 模式2:双灯灭
//======================================================
void DisplayMode2(void)
{
LedAllOn(200);
for(j=0;j<25;j++)
{
DispBuf[j>>3] &= ~(0x01<<(j&0x07));
LEDDrive();
DelayXms(30);
DispBuf[j>>3] = 0xff;
}
for(j=0;j<25;j++)
{
DispBuf[(24-j)>>3] &= ~(0x01<<((24-j)&0x07));
LEDDrive();
DelayXms(30);
DispBuf[(24-j)>>3] = 0xff;
}
}
//======================================================
// 模式3:依次灭灯(顺时针)
//======================================================
void DisplayMode3(void)
{
LedAllOff(0);
LedAllOn(1);
for(j=0;j<25;j++)
{
DispBuf[j>>3] &= ~(0x01<<(j&0x07));
LEDDrive();
DelayXms(60);
}
}
//======================================================
// 模式4:依次亮灯(逆时针)
//======================================================
void DisplayMode4(void)
{
LedAllOff(1);
for(j=0;j<25;j++)
{
DispBuf[(24-j)>>3] |= 0x01<<((24-j)&0x07);
LEDDrive();
DelayXms(60);
}
}
//======================================================
// 模式5:间隔点亮
//======================================================
void DisplayMode5(void)
{
for(j=0;j<4;j++)
{
DispBuf[0]=0x55;
DispBuf[1]=0x55;
DispBuf[2]=0x55;
DispBuf[3]=0x55;
LEDDrive();
DelayXms(150);
DispBuf[0]=0xaa;
DispBuf[1]=0xaa;
DispBuf[2]=0xaa;
DispBuf[3]=0xaa;
LEDDrive();
DelayXms(150);
}
}
...全文
1217 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
fangxqian 2015-01-05
  • 打赏
  • 举报
回复
太复杂,一点一点分析吧。
tiege87186 2015-01-04
  • 打赏
  • 举报
回复
没细看,但是进位数据左移和右移错了 //====================================================== // 带进位数据左移 //====================================================== unsigned char RLC(unsigned char Temp) { unsigned int V_Temp; V_Temp = Temp; V_Temp<<=1; if(C_Flag)V_Temp|=0x01; //需要和下一行换位置 C_Flag=V_Temp&0x0100;// return V_Temp&0xff; } //====================================================== // 带进位数据右移 //====================================================== unsigned char RRC(unsigned char Temp) { unsigned int V_Temp; V_Temp = (unsigned int)Temp<<7; if(C_Flag)V_Temp|=0x8000;//需要和下一行换位置 C_Flag=V_Temp&0x0080; // return V_Temp>>8; }
mwh1993 2014-12-26
  • 打赏
  • 举报
回复
当我看到延时函数,我就知道楼主是新手。堵住单片机真的好吗
-Early 2014-12-26
  • 打赏
  • 举报
回复
既然你说是新手,建议应该一步步来,先去看看简单的短的程序,一口是吃不成胖子的.
liuchang910781261 2014-12-26
  • 打赏
  • 举报
回复
这是来炫耀自己代码写的工整吗???
wangyicsdn 2014-12-26
  • 打赏
  • 举报
回复
算法是核心,编程语言是用来实现算法的工具。
sprawling 2014-12-16
  • 打赏
  • 举报
回复
注释已经有了,没什么问题,如果想要更详细的可以自己加,不知道你想要怎么样的注释.
Dexter_Dream 2014-12-14
  • 打赏
  • 举报
回复
仿佛看到了原来的自己~~~~~~~~~
worldy 2014-12-08
  • 打赏
  • 举报
回复
不是已经有很好的注释了,还想怎样?
suckmebeauty 2014-12-07
  • 打赏
  • 举报
回复
这么长的程序,自己慢慢研究吧,爱莫能助
皑刈苑 2014-12-07
  • 打赏
  • 举报
回复
话说我也有强迫症,代码不对齐心里不舒服,所以每次敲代码必须要整整齐齐的,我看很多都是宏定义和位操作,看一些视频就可以快速入门,比如10天学会avr,51和AVI其实差不多,看了也都会了
dahema0010 2014-12-07
  • 打赏
  • 举报
回复
楼主羡慕啊
zhjzhj888 2014-12-06
  • 打赏
  • 举报
回复
楼主单片机程序写的这么整齐,羡慕中。。。
lemonsland 2014-11-26
  • 打赏
  • 举报
回复
格式很好,这是个好习惯
sdy0725 2014-11-15
  • 打赏
  • 举报
回复
代码很整齐的
jielin2012 2014-11-10
  • 打赏
  • 举报
回复
百年难遇的软件奇才!
ruantianyu 2014-11-05
  • 打赏
  • 举报
回复
楼主单片机程序写的这么规矩,一定是女孩子吧~哈哈~ 态度非常好,比好多卖单片机的家伙写的代码漂亮多了~ 要是代码行间加一些空行,就完美了。
unsv29 2014-11-02
  • 打赏
  • 举报
回复
格式很漂亮。写的很认真。就是说态度不错。 但是总体属于刚开始学习的阶段。
wang35753 2014-10-08
  • 打赏
  • 举报
回复
厉害,注释就不加了,51玩的不多!
qq_15079249 2014-10-06
  • 打赏
  • 举报
回复
这是51单片机的程序
加载更多回复(3)

27,374

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧