菜鸟学单片机




通过USB转串口通讯   将C代码编译生成16进制的机器码,再将机器码 烧进ROM
 
 1 /********************************************************************
 2 * 文件名 :流水灯.c
 3 * 描述   : 
 4 * 创建人 :WuZhuojun,年月日
 5 * 版本号 :1.0
 6 ***********************************************************************/
 7 #include<reg52.h>
 8 #define uchar unsigned char
 9 #define uint  unsigned int
10  
11 /********************************************************************
12 * 名称: Delay()
13 * 功能: 延时,延时时间为10ms * del
14 * 输入: del
15 * 输出: 无
16 ***********************************************************************/
17 void Delay(uint del)
18 {
19     uint i,j;
20     for(i=0; i<del; i++)
21     for(j=0; j<1827; j++)   
22     ;
23 }
24  
25 /********************************************************************
26 * 名称: Main()
27 * 功能: 实现灯的闪烁
28 * 输入: 无
29 * 输出: 无
30 ***********************************************************************/
31 void Main(void)
32 {
33     P0 = 0xff;       //关闭所有LED
34     while(1)
35     {
36        P0 = 0xfe;  
37        Delay(100);
38  
39        P0 = 0xfd; 
40        Delay(100);
41  
42        P0 = 0xfb;
43        Delay(100);
44  
45        P0 = 0xf7;
46        Delay(100);
47  
48        P0 = 0xef;
49        Delay(100);
50  
51        P0 = 0xdf;
52        Delay(100);
53  
54        P0 = 0xbf;
55        Delay(100);
56  
57        P0 = 0x7f;
58        Delay(100);
59  
60        P0 = 0x00;
61        Delay(200);
62     }
63 }
51单片机


P0 是特殊功能寄存器 每个位对应控制一个LED灯 高电平为灭,低电平亮 

代码很简单,但想写的高效和简练却要花很多心思,毕竟内存是有限的、CPU的运行能力也是有限的
原文地址:https://www.cnblogs.com/zhuojun/p/4067044.html