?? vter_bus.c
字號:
//智能顯示終端并行接口通信演示程序之一總線方式。
//仿真器需選擇:"總線設置(Bus Option)"的"僅使用數據總線(Use XBus Only)"。
#include <reg51.h>
#include <stdio.h>
#include <string.h>
#define uchar unsigned char
//==========================================================
//定義P0口為數據線,P3.1, P3.2,P3.3為控制線。
sbit CS0 =P3^1; //片選信號
sbit PINT =P3^2; //觸摸屏中斷信號
uchar Fcolor; //背景色
uchar Bcolor; //前景色
unsigned short CHX_DAT,CHY_DAT; //觸摸屏坐標
unsigned char Date[8]; //讀時間值
//==========================================================
//寫一個數據子程序:
Write_Byte(uchar dc_data)
{
uchar xdata DC; //定義一個外部RAM變量
uchar i;
CS0 = 0;
DC = dc_data; //數據dc_data寫到外部RAM(即LCD控制板)。
CS0 = 1;
//加入適當的空操作延時
//當采用更高速的單片機時應加入更多的空操作延時
//這里加上8個空操作延時,在AT89C51,22MHz下是完全沒問題的。
for(i=0;i<8;i++){}
}
//==========================================================
//讀一個字節數據子程序:
uchar Read_Byte(void)
{
uchar xdata DC;
CS0 = 0;
return DC;
}
//=================================================
/*外中斷處理程序*/
void Ex1_int( ) interrupt 2 using 1
{
uchar temp1,temp2,temp3,temp4;
temp1 = Read_Byte();
temp2 = Read_Byte();
temp3 = Read_Byte();
temp4 = Read_Byte();
if(temp1 == 0xF8) //如果接收到的第一個字節是0xF8, 則收到的數據是年月日。
{
Date[0] = temp1;
Date[1] = temp2;
Date[2] = temp3;
Date[3] = temp4;
}
else if(temp1 == 0xF9) //如果接收到的第一個字節是0xF9, 則收到的數據是時分秒。
{
Date[4] = temp1;
Date[5] = temp2;
Date[6] = temp3;
Date[7] = temp4;
}
else //收到的數據觸摸屏的座標值。
{
CHX_DAT = temp1 * 100;
CHX_DAT = temp2 + CHX_DAT; //高低兩字節組合(高低兩個字節100進1)
CHY_DAT = temp3 * 100;
CHY_DAT = temp4 + CHY_DAT; //高低兩字節組合(高低兩個字節100進1)
}
}
//==============================================
//函數聲明
Lcd_Clr();
Read_time();
Read_date();
DIS_Ellipse(unsigned short x0,unsigned short y0,unsigned short xr,unsigned short yr);
DIS_Line(unsigned short x0,unsigned short y0,unsigned short x1,unsigned short y1,unsigned short with);
void Clr_squ(unsigned short x0,unsigned short y0,unsigned short width,unsigned short heith);
void inv_squ(unsigned short x0,unsigned short y0,unsigned short width,unsigned short heith);
void Set_time(uchar year,uchar month,uchar day,uchar hour,uchar minute,uchar second,uchar date);
AUTO_BMP(unsigned short x0,unsigned short y0,uchar N1,uchar N2,uchar T);
DIS_BMP(unsigned short x0,unsigned short y0,uchar N1);
void Dis_time(unsigned short x0,unsigned short y0,unsigned char i);
DIS_Value(unsigned short x0,unsigned short y0,unsigned short Va,unsigned char i);
DIS_String(unsigned short x0,unsigned short y0,char *fmt);
test_timer();
void push_windows(unsigned char mun_dc); //保存當前窗口至后臺子程序
void pop_windows(unsigned char mun_dc); //恢復之前保存在后臺的窗口
//==============================================
//LCD 分辨率為 320X240 640X480 或800X600
#define LCD_XSIZE 320
#define LCD_YSIZE 240
main(void)
{
unsigned int i;
CS0 = 0;
PINT = 1;
Fcolor = 0x0;
Bcolor = 0xff;
i = 0;
//顯示終端在上電就緒時發送F0.用仿真器時不要檢測F0.
//當把程序燒寫到單片機上時,顯示終端和單片機同時上電,所以一定
//要檢測是否收到F0。以判斷顯示終端是否上電就緒
//while(!(i == 0xF0))
//{i = Read_Byte();}
IT1 = 0; //外部中斷1為電平觸發方式。
EA=1; //全局中斷使能,(EA=0,關全局中斷;EA=1,開全局中斷)
EX1 = 1; //外部中斷使能置1,允許外部中斷
Lcd_Clr(); //清除全屏。
//DIS_BMP(LCD_XSIZE-50,LCD_YSIZE-50,0);
for(i=10;i<40;i+=4)
{
DIS_Line(10,i,(LCD_XSIZE-10),i,1); //畫直線
}
for(i=10;i<40;i+=4)
{
DIS_Ellipse(LCD_XSIZE/4*3,LCD_YSIZE/2,i,i); //畫圓
}
DIS_String(0,0,"并行接口通信演示程序之一總線方式");//顯示字符
Dis_time(0,16,3);
DIS_String(0,32,"讀取年月日:");
DIS_String(0,48,"讀取時分秒:");
test_timer();
Set_time(05,11,9,17,22,30,3); //設置時間為2005年11月9日,17:22:30,星期三。
DIS_String(0,64,"X軸座標值:");
DIS_String(0,80,"Y軸座標值:");
Bcolor = 0x03;
Fcolor = 0xE0;
Clr_squ((LCD_XSIZE/4*1)-35,(LCD_YSIZE/2)-10,70,20);
DIS_String((LCD_XSIZE/4*1)-32,(LCD_YSIZE/2)-8,"按鍵測試");
AUTO_BMP(LCD_XSIZE-50,LCD_YSIZE-50,0,3,2);
while(1)
{
PINT = 1; //將PINT設為輸入端口
//判斷是否有觸摸事件發生。
if(PINT ==0)
{
DIS_Value((6*16),64,CHX_DAT,3);
DIS_Value((6*16),80,CHY_DAT,3);
if(CHX_DAT > ((LCD_XSIZE/4*1)-35) && CHX_DAT < ((LCD_XSIZE/4*1)+35) &&
CHY_DAT > ((LCD_YSIZE/2)-10) && CHY_DAT < ((LCD_YSIZE/2)+10)) //判斷點擊處是否在按鍵區域,如果是往下執行
{
inv_squ((LCD_XSIZE/4*1)-35,(LCD_YSIZE/2)-10,70,20); //反色按鈕。
//用戶可在這里加上自己的處理程序,實現按鍵功能。
test_timer();
while(PINT ==0){} //等待PINT為高電平,即放開按鍵。
inv_squ((LCD_XSIZE/4*1)-35,(LCD_YSIZE/2)-10,70,20); //反色按鈕(恢復原來顏色)。
}
else
{DIS_Ellipse(CHX_DAT,CHY_DAT,4,4);} //如果沒有點擊到按鍵,則在按下觸摸屏的位置顯示一個小圓形。
while(PINT ==0){} //等待PINT為高電平。即放開按鈕。
}
}
}
//=========================================================
//測試從顯示終端機讀取時間。
test_timer()
{
uchar i;
Read_date();
while(Date[0] == 0){} //等待顯示終端機送年月日數據到單片機
for(i=0;i<3;i++)
{
DIS_Value((6*16)+(i*24),32,Date[i+1],2);
}
Read_time();
while(Date[4] == 0){} //等待顯示終端機送時分秒數據到單片機
for(i=0;i<3;i++)
{
DIS_Value((6*16)+(i*24),48,Date[i+5],2);
}
}
//===================================================================
//指令頭,每一條指令都規定為"0x81, command1,command2,Fcolor,Bcolor"
command_head(unsigned char command1,unsigned char command2)
{
Write_Byte(0x81);
Write_Byte(command1);
Write_Byte(command2);
Write_Byte(Fcolor);
Write_Byte(Bcolor);
}
//===================================================================
//清屏
Lcd_Clr()
{
command_head(0x43,0x4c); //總共發送了五個字節:0x81,0x43,0x4c,Fcolor,Bcolor
Write_Byte(0x84);
}
//===================================================================
// 清矩形
void Clr_squ(unsigned short x0,unsigned short y0,unsigned short width,unsigned short heith)
{
command_head(0x43,0x58);
Write_Byte(x0/100); //x軸坐標,取整運算,高低兩個字節100進1
Write_Byte(x0%100); //x軸坐標,取模運算
Write_Byte(y0/100); //Y軸坐標
Write_Byte(y0%100);
Write_Byte(width/100); //矩形的寬,取整運算,高低兩個字節100進1
Write_Byte(width%100);
Write_Byte(heith/100); //矩形的高
Write_Byte(heith%100);
Write_Byte(0x84);
}
//=================================================
// 反色矩形
void inv_squ(unsigned short x0,unsigned short y0,unsigned short width,unsigned short heith)
{
command_head(0x43,0x4e);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(width/100);
Write_Byte(width%100);
Write_Byte(heith/100);
Write_Byte(heith%100);
Write_Byte(0x84);
}
//================================================
//讀取年月日
Read_date()
{
Date[0] = 0; //Date[0]清零,當年月日數據更新后將賦值0xf8,可用查詢方式確認數據是否已經更新
command_head(0x52,0x44);
Write_Byte(0x84);
}
//================================================
//讀取時分秒
Read_time()
{
Date[4] = 0; //Date[4]清零,當時分秒數據更新后將賦值0xf9,可用查詢方式確認數據是否已經更新
command_head(0x52,0x54);
Write_Byte(0x84);
}
//===============================================
//畫圓子程序。
DIS_Ellipse(unsigned short x0,unsigned short y0,unsigned short xr,unsigned short yr)
{
command_head(0x44,0x45);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(xr/100);
Write_Byte(xr%100);
Write_Byte(yr/100);
Write_Byte(yr%100);
Write_Byte(x0/100);
Write_Byte(0x84);
}
//================================================
//畫直線子程序。
DIS_Line(unsigned short x0,unsigned short y0,unsigned short x1,unsigned short y1,unsigned short with)
{
command_head(0x44,0x4c);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(x1/100);
Write_Byte(x1%100);
Write_Byte(y1/100);
Write_Byte(y1%100);
Write_Byte(with/100);
Write_Byte(with%100);
Write_Byte(0x84);
}
//===============================================
//在指定的開始位置(x0,y0)顯示字符串子程序。
//當x0大于800且已打開光標功能時在以光標為開始位置顯示字符串子程序。
DIS_String(unsigned short x0,unsigned short y0,char *fmt)
{
command_head(0x44,0x57);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
while(*fmt) //為0時(字符串結束)退出
{
Write_Byte(*fmt);
fmt++;
}
Write_Byte(0x84);
}
//==============================================
//開(關)顯示時間子程序
void Dis_time(unsigned short x0,unsigned short y0,unsigned char i)
{
command_head(0x44,0x54);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(i); //顯示方式,若是0則關時間顯示
Write_Byte(0x84);
}
//==============================================
//設置時間子程序;
//年,月,日,時 ,分,秒,星期。
void Set_time(uchar year,uchar month,uchar day,uchar hour,uchar minute,uchar second,uchar date)
{
command_head(0x53,0x54);
Write_Byte(year);
Write_Byte(month);
Write_Byte(day);
Write_Byte(hour);
Write_Byte(minute);
Write_Byte(second);
Write_Byte(date);
Write_Byte(0x84);
}
//================================================
//顯示變量子程序
DIS_Value(unsigned short x0,unsigned short y0,unsigned short Va,unsigned char i)
{
command_head(0x44,0x56);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(Va/100);
Write_Byte(Va%100);
Write_Byte(i); //顯示的域寬
Write_Byte(0x84);
}
//================================================
//顯示位圖子程序
DIS_BMP(unsigned short x0,unsigned short y0,uchar N1)
{
command_head(0x44,0x53);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(N1); //位圖在Flash中的編號
Write_Byte(0x84);
}
//================================================
//自動顯示位圖子程序
AUTO_BMP(unsigned short x0,unsigned short y0,uchar N1,uchar N2,uchar T)
{
command_head(0x5a,0x44);
Write_Byte(x0/100);
Write_Byte(x0%100);
Write_Byte(y0/100);
Write_Byte(y0%100);
Write_Byte(N1); //開始的位圖編號
Write_Byte(N2); //結束的位圖編號
Write_Byte(T); //位圖顯示的切換間隔時間
Write_Byte(0x84);
}
//================================================
//保存當前窗口至后臺子程序
void push_windows(unsigned char mun_dc)
{command_head(0x43,0x44);
Write_Byte(mun_dc); //后臺編號
Write_Byte(0x84);
}
//================================================
//恢復之前保存在后臺的窗口
void pop_windows(unsigned char mun_dc)
{command_head(0x45,0x44);
Write_Byte(mun_dc); //后臺編號
Write_Byte(0x84);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -