#include <AT89X51.H> #include <intrins.h> // 函數原形定義 #define uchar unsigned char #define uint unsigned int void main (void); // 主函數 void LED_OUT(uchar X); // LED單字節串行移位函數 void LED_print (uchar p, uchar x) ; unsigned char code LED_0F[]; // LED字模表 sbit DIO = P1^0; //串行數據輸入 sbit RCLK = P1^1; //時鐘脈衝信號——上升沿有效 sbit SCLK = P1^2; //打入信號————上升沿有效 uchar Buffer[4] = {0}; //從串口接收的數據 // 全局變量 uchar LED[8]; //用於LED的8位顯示緩存 void Delay_1ms(uint i)//1ms延時 { uchar x,j; for(j=0;j<i;j++) for(x=0;x<=148;x++); } void Init(void) { TMOD = 0x20; //8 位自動重裝計數 PCON &= 0x7f; SCON = 0x50;//串行口控制寄存器 SM1=1,SM0=0方式一波特率可變 TH1 = 0xFD; //設置波特率 9600 TL1 = 0xFD; TR1 = 1; //啓動定時器1 TI = 0; RI = 0; ES = 1; //開串口中斷 EA = 1; //開總中斷 } // 主程序 void main (void) { Init();//初始化 LED_print (0x80, 0x0F); //顯示啓動信息 while(1) { switch(Buffer[0]) { case 0x30: LED_print (0x01, 0); break; case 0x31: LED_print (0x01, 1); break; case 0x32: LED_print (0x01, 2); break; case 0x33: LED_print (0x01, 3); break; case 0x34: LED_print (0x01, 4); break; default : LED_print (0x80, 0x0E); break; } } } void Com_Int(void) interrupt 4 { EA = 0; if (RI == 1) //當硬件接收到一個數據時,RI會置位 { Buffer[0] = SBUF ; //這裏減去48是由於從電腦中發送過來的數據是ASCII碼。// Buffer[0] = SBUF-48 ; RI = 0; } EA = 1; } void LED_print (uchar p, uchar x) { unsigned char code *led_table; // 查表指針 uchar i; unsigned long loop; led_table = LED_0F + x; i = *led_table; LED_OUT(i); LED_OUT(p); RCLK = 0; RCLK = 1; for(loop = 0; loop < 230; loop++); } void LED_OUT(uchar X) { uchar i; for(i=8;i>=1;i--) { if (X&0x80) DIO=1; else DIO=0; X<<=1; SCLK = 0; SCLK = 1; } } unsigned char code LED_0F[] = { //0 1 2 3 4 5 6 7 8 9 A b C d E F - 0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E, 0xbf };