- #include <reg51.h>
- #include <intrins.h>
- #define U8 unsigned char
- #define U16 unsigned int
- #define LCD_DATA P0
- U8 getdata;
- U8 X1,X2,X3,X4;
- U8 f;
- sbit OE=P3^7;
- sbit star=P3^6;
- sbit eoc=P3^3;
- sbit ALE=P2^6;
- sbit stoid=P3^5;
- sbit adda=P2^3;
- sbit addb=P2^4;
- sbit addc=P2^5;
- sbit ksad=P3^0;
- sbit LCD_RS=P2^0;
- sbit LCD_RW=P2^1;
- sbit LCD_EN=P2^2;
- sfr aaa=0x80;
- U8 code tab[] = {0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};
- U8 dispbuf[4]={0,0,0,0};
- //void XSQ(void);
- void delay(unsigned char t) {
- unsigned char i;
- while(t--)
- for(i = 200; i > 0; i--);
- }
- void LCD_write_instruction(unsigned char LCD_instruction) { //写指令到LCD
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_DATA = LCD_instruction;
- LCD_EN = 1;
- _nop_();
- delay(1);
- LCD_EN = 0;
- //delay(10);
- }
- void LCD_write_data(unsigned char LCD_data) { //输出一个字节数据到LCD
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_DATA = LCD_data;
- LCD_EN = 1;
- delay(1);
- _nop_();
- LCD_EN = 0;
- delay(1);
- }
- void LCD_set_position(unsigned char x) { //LCD光标定位到x处
- LCD_write_instruction(0x80|x);
- }
- void LCD_printc(unsigned char lcd_data) { //输出一个字符到LCD
- LCD_write_data(lcd_data);
- }
- void LCD_prints(unsigned char *lcd_string) { //输出一个字符串到 LCD
- unsigned char i = 0;
- while(lcd_string[i] != 0x00) {
- LCD_write_data(lcd_string[i]);
- i++;
- }
- }
- void LCD_initial(void) { //初始化 LCD
- LCD_write_instruction(0x38);
- LCD_write_instruction(0x0c);
- LCD_write_instruction(0x06);
- LCD_write_instruction(0x01);
- }
- void XSQ(void)
- {
- LCD_set_position(0x80);
-
- while(1) {
- U8 temp1,temp2,temp3,temp4;
- LCD_set_position(0x80);
- LCD_prints(" NOW: OF");
- LCD_set_position(0xC0); //LCD换行显示
- LCD_prints("Number:");
-
- // X1=X2=X3=X4=0;
- LCD_initial();
- temp1=tab[dispbuf[0]];
- temp2=tab[dispbuf[1]];
- temp3=tab[dispbuf[2]];
- temp4=tab[dispbuf[3]];
- LCD_set_position(0xc9); //LCD换行显示
- LCD_printc(temp1);
- LCD_set_position(0xcA); //LCD换行显示
- LCD_printc(temp2);
- LCD_set_position(0xCB); //LCD换行显示
- LCD_printc(temp3);
- LCD_set_position(0xCC); //LCD换行显示
- LCD_printc(temp4);
- }
- }
- void InitTimer1(void)
- { TMOD = 0x20;
- TH1 = 0xFE;
- TL1 = 0xFE;
- EA = 1;
- ET1 = 1;
- //if(ksad==0){
- // TR1 = 0;// }else{TR1=0;}
- }
- void main(void)
- {adda=0;addb=1;addc=0;
- InitTimer1();
- LCD_initial();
- TR1=1;
- while(1){
- OE=0;
- star=0;
- star=1;
- star=0;
- while(eoc==0);
- { OE=1;
- delay(1);
- getdata=P1;
- OE=0;
- dispbuf[0]=getdata/1000;
- dispbuf[1]=getdata%1000/100;
- dispbuf[2]=getdata%1000%100/10;
- dispbuf[3]=getdata%1000%100%10;}
- //TR1=0 // aaa=getdata;
- XSQ( ); // f=(5/256)*aaa*1000 ;
- LCD_set_position(0x80);
- LCD_prints(" NOW: OF");
- LCD_set_position(0xC0); //LCD换行显示
- LCD_prints("Number:"); }
- }
- void Timer1Interrupt(void) interrupt 3
- {InitTimer1();
- ALE=~ALE;//add your code here!
- //OE=~OE;
- //delay(8);
- //eoc=~eoc;
- }
复制代码 |