#include "reg52.h" #include "LCD_code.h" #include "intrins.h" #include "math.h" #include "AD7896.h" #define nop() _nop_() #define uchar unsigned char #define uint unsigned int sbit A0 = 0xb3; sbit E2 = 0xb4; sbit E1 = 0xb5; sbit _WR= P2^6; sbit LED=P2^3; sbit up=P1^6;//S1 sbit down=P1^7;//S3 /* sbit A0 = P2^5; sbit _WR = P2^6; sbit E1 = P2^4; sbit E2 = P2^7; */ #define lcd_data P0 //#define lcd_data PORTA //data; #define set_E1() (E1=1) #define set_E2() (E2=1) #define set_A0() (A0=1) #define set_WR() (_WR=1) #define clr_E1() (E1=0) #define clr_E2() (E2=0) #define clr_A0() (A0=0) #define clr_WR() (_WR=0) #define disp_off 0xae #define disp_on 0xaf #define disp_start_line 0xc0 #define page_addr_set 0Xb8 #define col_addr_set 0x00 #define status_busy 0x80 #define write_mode 0xee #define dynamic_driver 0xa4 #define adc_select 0xA0 #define clk32 0xa9 #define clk16 0xa8 #define reset 0xe2 #define uchar unsigned char #define uint unsigned int uchar time_counter=0; uchar key=0; uchar serial_counter; bit disp_flag=0; #define AD_Vol_PER 2.5 #define AD_Loop_PickVol_PER 0.0024414 sbit SCLK=P1^1; sbit WDI=P1^2; sbit SDATA=P1^0; bit r_flag=0; unsigned char idata serial_buff[77]; void lcd_init(void); void lcd_clr(void); void wait_ready(void); void draw_bmp(uchar col,uchar layer,uchar width,uchar *bmp); void ASCII2BCD(void); void **(void); extern uint read7896(void); void delay_nu(uchar n) { uchar i; for(i=0;i<n;i++) { _nop_(); } } uint read7896(void) { uint result; uchar h; SCLK=0; WDI=0; delay_nu(30); WDI=1; delay_nu(30); SDATA=1; for(h=0;h<16;h++) { result<<=1; SCLK=1; _nop_(); if(SDATA)result++; SCLK=0; } result=result&0xfff; return result;
} void delay(uchar i) { uchar j; for(i=i<<1;i>0;i--) for(j=0xf3;j>0;j--) {} } void int_t0() interrupt 1 using 1 { TH0=0X4C; TL0=0X00; time_counter++; if(time_counter==200) { time_counter=0; LED=1; TR0=0; } } void send_mi(uchar instruction) { clr_E2(); set_E1(); wait_ready(); clr_A0(); clr_WR(); nop(); lcd_data=instruction; nop(); clr_E1(); } void send_md(uchar c) { clr_E2(); set_E1(); wait_ready(); set_A0(); clr_WR(); nop(); lcd_data=c; nop(); clr_E1(); } void send_si(uchar instruction) { clr_E1(); set_E2(); wait_ready(); clr_A0(); clr_WR(); nop(); lcd_data=instruction; nop(); clr_E2(); } void send_sd(uchar c) { clr_E1(); set_E2(); wait_ready(); set_A0(); clr_WR(); lcd_data=c; nop(); clr_E2(); } void wait_ready(void) { clr_A0(); set_WR(); nop(); nop(); //while(lcd_data&status_busy); } void lcd_init(void) { send_mi(reset); send_si(reset); send_mi(disp_off); send_si(disp_off); send_mi(dynamic_driver); send_si(dynamic_driver); send_mi(clk32); send_si(clk32); send_mi(adc_select); send_si(adc_select); send_mi(write_mode); send_si(write_mode); send_mi(col_addr_set); send_mi(disp_start_line); send_si(col_addr_set); send_si(disp_start_line); send_mi(disp_on); send_si(disp_on); }
void lcd_clr(void) { uchar i,page; for(page=0;page<4;page++) { send_mi(page_addr_set|page); send_si(page_addr_set|page); send_mi(0); send_si(0); for(i=0;i<61;i++) { send_md(0x00); send_sd(0x00); } } } void set_page(uchar page) { send_mi(page_addr_set|page); send_si(page_addr_set|page);
}
void set_address(uchar address) { send_mi(address&0x7f); send_si(address&0x7f); }
void putchar_l(uchar c) { send_md(c); }
void putchar_r(uchar c) { send_sd(c);
}
void draw_bmp(uchar col,uchar layer,uchar width,uchar *bmp) { uchar x; uchar address; //显存的物理地址 uchar p=0; // uchar page=0; //上,下两页 uchar window=0; //左,右窗口 if(layer) page=2; for(x=col;x<col+width;x++) { if(x>121)return; if(x>60) { window=1; address=x%61; } else { address=x; } set_page(page); set_address(address); if(window) putchar_r(bmp[p]); else putchar_l(bmp[p]); set_page(page+1); set_address(address); if(window) putchar_r(bmp[p+width]); else putchar_l(bmp[p+width]); p++; } }
void display(uint v) { unsigned char row, count; float tt; unsigned int tmp ; int value; tt=v*AD_Loop_PickVol_PER; value = tt * 100 + (value > 0 ? 0.5 : -0.5); tmp = abs(value); serial_buff[0] = tmp / 10000; serial_buff[1] = tmp % 10000 / 1000; serial_buff[2] = tmp % 1000 / 100; serial_buff[3] = tmp % 100 / 10; serial_buff[4] = tmp % 10; draw_bmp(0,1,8,ELAPHT[0]);// 显示“I” draw_bmp(8,1,8,num[15]);//显示“:” /* if(value< 0) { draw_bmp(16,1,8,num[14]);//显示“-” } else { draw_bmp(16,1,8,num[13]);//显示“+” } */ for(count = 0; count != 5; count++) { draw_bmp(16+row*8,1,8,num[serial_buff][count]]);//显示读数 row++; if((count == 2))////定位小数位 { draw_bmp(16+row*8,1,8,num[10]); row++; } } if((serial_buff[0]==0)&&(serial_buff[1]==0)&&(serial_buff[2]==0)) { for(row=0;row !=4;row++) { draw_bmp(16+row*8,1,8,num[12]);//消隐设置“空格” } if(serial_buff[3]==0) { draw_bmp(16+row*8,1,8,num[12]);//消隐设置“空格” } draw_bmp(104,1,8,ELAPHT[10]);//显示“m” draw_bmp(112,1,8,ELAPHT[1]);//显示“A” } else { draw_bmp(104,1,8,num[12]);//消隐设置“空格” draw_bmp(112,1,8,ELAPHT[1]);//显示“A” if(serial_buff[0]==0) { row=0; draw_bmp(16+row*8,1,8,num[12]);//消隐设置“空格” if(serial_buff[1]==0) { row++; draw_bmp(16 +row*8,1,8,num[12]);//消隐设置“空格” } } } }
void **(void) { uchar i,j,h; LED = 0; TR0 = 1; lcd_clr(); draw_bmp(0,0,112,welcome_1); draw_bmp(0,1,104,welcome_2); for(i=0;i<10;i++)for(j=0;j;j++)for(h=1;h;h++); lcd_clr(); draw_bmp(0,0,112 ,welcome_3); for(i = 0;i<5;i++)for(j = 1;j;j++)for(h = 1;h;h++); } void main (void) { int i; P0 =0xff; P1 =0xff; P2 =0xff; P3 =0xff; delay(255); lcd_init(); lcd_init(); lcd_init(); lcd_clr(); **(); //TMOD:GATE|C/! T|M1|MO|GATE|C/! T|M1|M0 // 0 0 1 0 0 0 0 1 TMOD = 0x21; TH0 = 0X4C; TL0 = 0x00; //SCON:SM0|SM1|SM2|REN|TB8|RB8|TI|RI // 0 1 0 0 0 0 0 0 ET0 = 1; ES = 1; EA = 1; while(1) { display(read7896()); } } |