- /*作用:顯示 I love you*/
- #include<reg52.h> //頭文件
- #define uchar unsigned char //宏定義
- #define uint unsigned int
- uchar code table[]= // 定義數組常量,該常量用于數碼管顯示
- {0x30,0x38,0x3f,0x3e,0x79,//l love you,前面加code該常量在程序
- 0x6e,0x3f,0x3e}; //代碼中存放,不占用RAM
- void delay(uint x) //延時子函數,時間約為0.002xS
- {
- uint i,j;
- for(i=x;i>0;i--)
- for(j=200;j>0;j--);
- }
- void display(uchar *lp,uchar lc) //顯示子函數
- {
- uchar i; //定義局部變量
- while(1)
- {
- P1=0xf8; //點亮第一個數碼管
- P2=0;
- for(i=0;i<lc;i++)
- {
- P2=lp[i];
- delay(1);
- P2=0; //消隱
- P1++;
- }
- }
- }
- void main() //主函數
- {
- while(1)
- {
- display(table,8); //條用子函數顯示 l love you
- }
- }
- /*注:
- 數碼管與P2口相連,低電平有效
- */
復制代碼
|