|
如標(biāo)題,屏幕上顯示有溫度條,超過設(shè)定溫度會報警,就是蜂鳴器會響
0.png (48.04 KB, 下載次數(shù): 152)
下載附件
2016-4-17 03:47 上傳
部分源碼如下(完整版本請下載附件):
- /*===================================================================================================
- 工程名稱: Ex1
- 功能描述: 控制TFT實現(xiàn)溫度計的顯示,以及使用獨立按鍵控制最高溫度、最低溫度,并在TFT上顯示最高溫度最低溫度及當(dāng)前溫度值
- 硬件連接: 查看和修改接口定義在NBCTFT.C中,請仔細檢查接口連線。
- ----------------------------------------
- | --------TFT控制接線---------- |
- | |
- | D10~D17 接 P0 |
- | RS 接 P2^5; |
- | RW 接 P2^4; |
- | RD 接 P2^3; |
- | CS 接 P2^2; |
- | RES 接 P2^1; |
- | |
- | LE 接 P2^0; |
- | |
- | --------電源供電接線--------- |
- | GND 接 電源負極 |
- | VIN 接 電源正極(5V) |
- |----------------------------------------|
- | 如果在NBC開發(fā)板上使用,以上線不需用飛 |
- | 線連接,直接插接在開發(fā)板上的12864接口 |
- ----------------------------------------
- 硬件連接: 用1位杜邦線將J8_7與J17_18b20連接;
- 用1位杜邦線將J8_6與J18_BZ連接;
- 用5位杜邦線將J9_7與J7_s17、J9_6與J7_s18、J9_5與J7_s19、J9_4與J7_s20、J9_3與J7_s21連接;
- 維護記錄: 2015-11-22
- ====================================================================================================*/
- //******************包含頭文件***************************
- #include"reg51.h" //包含單片機頭文件
- #include"intrins.h" //_nop_();延時函數(shù)用
- #include"NBCTFT.h" //包含TFT驅(qū)動頭文件
- //#include"aaaaaaaa.h"
- #define uchar unsigned char
- #define uint unsigned int
-
- //******************全局變量***************************
- #define White 0xFFFF //LCD color
- #define Black 0x0000
- #define Blue 0x001F
- #define Blue2 0x051F
- #define Red 0xF800
- #define Magenta 0xF81F
- #define Green 0x07E0
- #define Cyan 0x7FFF
- #define Yellow 0xFFE0
- unsigned char idata var=0;
- uchar idata kbuf=0;
- uchar idata kval=0;
- uchar idata kcnt=0;
- unsigned int Device_code; //TFT控制IC型號
- sbit DQ=P1^7; //溫度控制口
- sbit rs=P1^5; //命令/數(shù)據(jù)選擇
- sbit rw=P1^4; //讀寫口
- sbit e=P1^3; //鎖存控制
- sbit bz=P1^6; //定義蜂鳴器控制IO
- sbit s17=P3^7; //定義S17按鍵控制IO
- sbit s18=P3^6; //定義S18按鍵控制IO
- sbit s19=P3^5; //定義S19按鍵控制IO
- sbit s20=P3^4; //定義S20按鍵控制IO
- sbit s21=P3^3; //定義S21按鍵控制IO
- uchar data dis0[16]={'T','h','e',' ','t','e','m','p',' ','n','o','w',' ','i','s',':',}; //LCD第1行
- uchar data dis[10]={' ',' ',' ',0x00,0x00,0x00,'.',0x00,0xeb,'C'}; //LCD第2行
- //uchar keyValue; //定義掃描結(jié)果參數(shù)
- //***********************************************************************************************
- //精確延時函數(shù)
- //***********************************************************************************************
- void delay(uint t)
- {
- while(--t);
- }
- //**************************************************************************************************
- //延時函數(shù)
- //**************************************************************************************************
- delay1(uint time) //int數(shù)據(jù)位16位,所以max=65535
- {
- uint i,j;
- for(i=0;i<time;i++) //循環(huán)50*time次
- for(j=0;j<50;j++);//循環(huán)50次
- }
- //**************************************************************************************************
- //向LCD寫一命令
- //**************************************************************************************************
- wcode(uchar t)
- {
- rs=0;
- rw=0;
- e=1;
- P0=t;
- delay1(20);
- e=0;
- }
- //**************************************************************************************************
- //向LCD寫一數(shù)據(jù)
- //**************************************************************************************************
- wdata(uchar t)
- {
- rs=1;
- rw=0;
- e=1;
- P0=t;
- delay1(20);
- e=0;
- }
- //**************************************************************************************************
- //LCD顯示
- //**************************************************************************************************
- show()
- {
- uchar i;
- wcode(0x80);
- for(i=0;i<16;i++)
- {
- wdata(dis0[i]);
- }
- wcode(0xc6);
- for(i=0;i<10;i++)
- {
- wdata(dis[i]);
- }
- }
- //**************************************************************************************************
- //LCD初始化
- //**************************************************************************************************
- InitLCD()
- {
- wcode(0x01);
- wcode(0x06);
- wcode(0x0e);
- wcode(0x38);
- }
- //**************************************************************************************************
- //傳感器初始化
- //**************************************************************************************************
- init_18b20()
- {
- uchar flag;
- DQ=1;
- delay(10);
- DQ=0;
- delay(500);
- DQ=1;
- delay(200);
- flag=DQ;
- delay(10);
- }
- //**************************************************************************************************
- //寫一個字節(jié)函數(shù)
- //**************************************************************************************************
- write_byte(uchar t)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- DQ=0;
- delay(10);
- DQ=t&0x01;
- delay(50);
- DQ=1;
- t=t>>1; //右移一位
- }
- }
- //**************************************************************************************************
- //讀一個字節(jié)函數(shù)
- //**************************************************************************************************
- uchar read_byte()
- {
- uchar i,value=0;
- for(i=0;i<8;i++)
- {
- value=value>>1;
- DQ=0;
- delay(10);
- DQ=1;
- delay(10);
- if(DQ==1)value=value|0x80;
- delay(50);
- }
- return(value);
- }
- //**************************************************************************************************
- //數(shù)據(jù)處理子函數(shù)
- //**************************************************************************************************
- uint chuli(uint temperature)
- {
- float t;
- if(temperature&0x8000)
- {
- temperature=~temperature+1;
- dis[3]=0xb0;
- }
- else
- {
- dis[3]=0x2b;
- }
-
- t=temperature*0.0625+0.05;
- temperature=t*10;
- dis[7]=temperature%10;
- dis[4]=temperature/10;
- dis[5]=temperature%100/10;
- //m=dis[4]*10+dis[5];
- return t;
- }
- //**************************************************************************************************
- //溫度采集函數(shù)
- //**************************************************************************************************
- uint get_temp()
- {
- uint dat;
- uchar wenl,wenh;
- init_18b20();
- write_byte(0xcc);
- write_byte(0x44);
- init_18b20();
- write_byte(0xcc);
- write_byte(0xbe);
- wenl=read_byte();
- wenh=read_byte();
- dat=(wenh<<8)+wenl;
- return(dat);
- }
- void delay5(uint z)
- {
- uint i,j;
- for(i=z;i>0;i--)
- for(j=z;j>0;j--);
- }
- //**************************************************************************************************
- //1*5按鍵掃描函數(shù)
- //**************************************************************************************************
- keyScan()
- {
- /* var=P3|0xff;
- if(var==kbuf)
- {
- if(kcnt<100)
- { kcnt++;
- if(kcnt==10)
- {
- kval=kbuf;
- //return(kval);
- }
- }
- }
- else
- {
- kbuf=0xfffff;
- kcnt=0;
- }
- // else{kbuf=var;kcnt=0;}
- */
- P3=P3|0xff; //P3高5位置1,設(shè)置為輸入
- if((P3&0xff)!=0xff) //判斷是否有按鍵按下
- {
- delay1(10); //延時肖抖
- if((P3&0xff)!=0xff) //再次判斷是否有按鍵按下
- kval=(P3&0xff); //讀取掃描結(jié)果
- }
- else {return kval=0;}
- }
- //***************************************************
- //****************************************************
- fm()
- {
- bz=0; //關(guān)閉蜂鳴器
- //delay1(1000); //延時
- bz=1; //打開蜂鳴器
- delay1(100); //延時
- }
- //**************************************************************************************************
- //主函數(shù)
- //**************************************************************************************************
- main()
- {
- //var=P3|0xf8;
- unsigned char tempchar[]={'0','1','2','3','4','5','6','7','8','9'};
- uint temp,m,m1,n,n1,i,s,max,min;
- uchar temp222,g,g1;
- uchar *temp111;
- Device_code=0x9320;
- TFT_Initial();
- Show_RGB(0,240,0,320,Yellow);
- temp=get_temp();
- for(i=300;i>14;i--){ Put_pixel(48,i,Black);}
- for(i=48;i<64;i++){ Put_pixel(i,300,Black);}
- for(i=300;i>14;i--){ Put_pixel(64,i,Black);}
- // Show_RGB(49,63,20,140,Red);
- // Show_RGB(49,63,140,180,Green);
- // Show_RGB(49,63,180,299,Blue2);
- temp222=tempchar[0];
- temp111=&temp222;
- for(i=52;i<293;i=i+80){ LCD_PutString(40,i,temp111,Black,Yellow);}
- temp222=tempchar[1];
- temp111=&temp222;
- for(i=252;i<293;i=i+40){ LCD_PutString(32,i,temp111,Black,Yellow);}
- temp222=tempchar[2];
- temp111=&temp222;
- for(i=172;i<213;i=i+40){ LCD_PutString(32,i,temp111,Black,Yellow);}
- temp222=tempchar[3];
- temp111=&temp222;
- for(i=92;i<133;i=i+40){ LCD_PutString(32,i,temp111,Black,Yellow);}
- temp222=tempchar[4];
- temp111=&temp222;
- for(i=12;i<53;i=i+40){ LCD_PutString(32,i,temp111,Black,Yellow);}
- temp222=tempchar[5];
- temp111=&temp222;
- for(i=12;i<253;i=i+80){ LCD_PutString(40,i,temp111,Black,Yellow);}
- /*for(i=20;i<300;i=i+8)
- {
- Put_pixel(49,i,Black);
- Put_pixel(50,i,Black);
- Put_pixel(51,i,Black);
- }
- Put_pixel(52,20,Black);
- Put_pixel(52,140,Black);
- Put_pixel(52,180,Black);
- Put_pixel(52,320,Black);*/
- LCD_PutString(100,108,"max:",Black,Yellow);
- LCD_PutString(100,132,"min:",Black,Yellow);
- LCD_PutString(92,208,"當(dāng)前室溫:",Black,Green);
- LCD_PutString(164,240,"度",Black,Yellow);
- max=27;min=11;
- while(1)
- {
- temp=get_temp();
- m=chuli(temp);
- g=m/10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(148,240,temp111,Black,Yellow);
- g=m%10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(156,240,temp111,Black,Yellow);
- g=max/10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(132,108,temp111,Black,Yellow);
- g=max%10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(140,108,temp111,Black,Yellow);
- g=min/10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(132,132,temp111,Black,Yellow);
- g=min%10;
- g1=tempchar[g];
- temp111=&g1;
- LCD_PutString(140,132,temp111,Black,Yellow);
- n=380-(m/1*8);
- Show_RGB(49,63,n,299,Red);
- Show_RGB(49,63,14,n-1,Yellow);
-
- for(i=49;i<64;i++){ Put_pixel(i,n,Green);}
- Put_pixel(50,n+1,White);Put_pixel(50,n-1,Green);
- Put_pixel(51,n+2,White);Put_pixel(51,n-2,Green);
- Put_pixel(52,n+3,White);Put_pixel(52,n-3,Green);
- //delay5(300);
- //Show_RGB(49,63,20,140,Red);
- //Show_RGB(49,63,141,180,Green);
- //Show_RGB(49,63,180,299,Blue2);
- for(i=20;i<300;i=i+8)
- {
- Put_pixel(49,i,Black);
- Put_pixel(50,i,Black);
- Put_pixel(51,i,Black);
- }
- for(i=20;i<300;i=i+40)
- {
- Put_pixel(52,i,Black);
- Put_pixel(53,i,Black);
- }
- //max=29;min=18;
- keyScan(); //按鍵掃描
- switch(kval)
- {
- case 0x7f: //如果S17按鍵按下
- {
- min=min+1;
- break;
- }
- case 0xbf: //如果S18按鍵按下
- {
- max=max-1;
- break;
- }
- case 0xdf: //如果S19按鍵按下
- {
- break;
- }
- case 0xef: //如果S20按鍵按下
- {
- max=max+1;
- break;
- }
- case 0xf7: //如果S21按鍵按下
- {
- min=min-1;
- break;
- }
- default:
- break;
- }
- n=380-(max*8);
- for(i=49;i<64;i++){ Put_pixel(i,n,Red);}
- n=380-(min*8);
- for(i=49;i<64;i++){ Put_pixel(i,n,Blue2);}
- delay5(600);
- if(m/1>=max||m/1<=min)
- fm();
-
- }
- }
復(fù)制代碼
tft程序:
- /*===================================================================================================
- 文件功能描述:320x240TFT驅(qū)動程序,控制TFT實現(xiàn)漢字,字符顯示,畫點功能。
- ====================================================================================================*/
- //******************包含頭文件***************************
- #include"NBCTFT.h"
- #include"reg52.h"
- //**************控制端口定義********************
- #define DataPort P0 //數(shù)據(jù)口使用DataPort
- sbit RS =P2^5; //數(shù)據(jù)/命令選擇
- sbit RW =P2^4; //寫數(shù)據(jù)/命令
- sbit nRD =P2^3; //讀控制
- sbit CS =P2^2; //片選
- sbit RES =P2^1; //復(fù)位
- sbit LE =P2^0; //74HC573鎖存控制
- //**************聲明外部函數(shù)和變量**************
- extern unsigned int Device_code;
- //================================================================================================
- // 實現(xiàn)功能: 延時
- // 輸入?yún)?shù): count 設(shè)置延時時間
- //================================================================================================
- void delayms(unsigned int count)
- {
- int i,j;
- for(i=0;i<count;i++)
- {
- for(j=0;j<255;j++);
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 寫命令
- // 輸入?yún)?shù): DH 需要輸入16bits命令的高8位
- // DL 需要輸入16bits命令的低8位
- //================================================================================================
- void Write_Cmd(unsigned char DH,unsigned char DL)
- {
- CS=0;
- RS=0;
- nRD=1;
- RW=0;
- //注意:當(dāng)使用8位數(shù)據(jù)口驅(qū)動16位數(shù)據(jù)模式時,使用74HC573作為IO擴展,程序如下
- DataPort=DL; //送高8位命令給573待鎖存
- LE=1; //鎖存位
- LE=0; //斷開鎖存,位選573的Q7~Q0仍保持
- DataPort=DH; //送低8位命令給TFT
- /*
- //如果使用16位數(shù)據(jù)口驅(qū)動16位數(shù)據(jù)模式,則無需IO擴展,直接將數(shù)據(jù)送到數(shù)據(jù)口即可
- DataPort_L=DL;
- DataPort_H=DH;
- */
- RW=1;
- CS=1;
- }
- //================================================================================================
- // 實現(xiàn)功能: 寫數(shù)據(jù)(2*8bits)
- // 輸入?yún)?shù): DH 需要輸入16bits數(shù)據(jù)的高8位
- // DL 需要輸入16bits數(shù)據(jù)的低8位
- //================================================================================================
- void Write_Data(unsigned char DH,unsigned char DL)
- {
-
- CS=0;
- RS=1;
- //注意:當(dāng)使用8位數(shù)據(jù)口驅(qū)動16位數(shù)據(jù)模式時,使用74HC573作為IO擴展,程序如下
- DataPort=DL; //送高8位數(shù)據(jù)給573待鎖存
- LE=1; //鎖存位
- LE=0; //斷開鎖存,位選573的Q7~Q0仍保持
- DataPort=DH; //送低8位數(shù)據(jù)給TFT
- /*
- //如果使用16位數(shù)據(jù)口驅(qū)動16位數(shù)據(jù)模式,則無需IO擴展,直接將數(shù)據(jù)送到數(shù)據(jù)口即可
- DataPort_L=DL;
- DataPort_H=DH;
- */
- RW=0;
- RW=1;
- CS=1;
- }
- //================================================================================================
- // 實現(xiàn)功能: 寫數(shù)據(jù)(16位)
- // 輸入?yún)?shù): y 需要輸入16bits數(shù)據(jù)
- //================================================================================================
- void Write_Data_U16(unsigned int y)
- {
- unsigned char m,n;
- m=y>>8;
- n=y;
- Write_Data(m,n);
- }
- //================================================================================================
- // 實現(xiàn)功能: 向x寄存器寫入y數(shù)據(jù)
- // 輸入?yún)?shù): x 需要輸入的命令 16位
- // y 需要輸入的數(shù)據(jù) 16位
- //================================================================================================
- void Write_Cmd_Data (unsigned char x,unsigned int y)
- {
- unsigned char m,n;
- m=y>>8;
- n=y;
- Write_Cmd(0x00,x);
- Write_Data(m,n);
- }
- //================================================================================================
- // 實現(xiàn)功能: TFT清屏
- // 輸入?yún)?shù): bColor 清屏所使用的背景色
- //================================================================================================
- void CLR_Screen(unsigned int bColor)
- {
- unsigned int i,j;
- LCD_SetPos(0,240,0,320);//320x240
- for (i=0;i<320;i++)
- {
- for (j=0;j<240;j++)
- Write_Data_U16(bColor);
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 顯示Ascii字符
- // 輸入?yún)?shù): x 橫坐標(biāo)
- // y 縱坐標(biāo)
- // c 需要顯示的字符
- // fColor 字符顏色
- // bColor 字符背景顏色
- //================================================================================================
- #include "Ascii_8x16.h"
- void LCD_PutChar(unsigned short x, unsigned short y, char c, unsigned int fColor, unsigned int bColor)
- {
- unsigned int i,j;
- LCD_SetPos(x,x+8-1,y,y+16-1); //設(shè)置字符顯示位置
- for(i=0; i<16;i++) { //循環(huán)寫入16字節(jié),一個字符為16字節(jié)
- unsigned char m=Font8x16[(c-0x20)*16+i]; //提取c字符的第i個字節(jié)以,c減去0x20是由于Ascii碼庫中的0~1f被去掉
- for(j=0;j<8;j++) { //循環(huán)寫入8位,一個字節(jié)為8位
- if((m&0x80)==0x80) { //判斷最高位是否為1
- Write_Data_U16(fColor); //最高位為1,寫入字符顏色
- }
- else {
- Write_Data_U16(bColor); //最高位為0,寫入背景顏色
- }
- m<<=1; //左移1位,準備寫下一位
- }
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 顯示16x16漢字
- // 輸入?yún)?shù): x 橫坐標(biāo)
- // y 縱坐標(biāo)
- // g 需要顯示的字符編碼
- // fColor 字符顏色
- // bColor 字符背景顏色
- //================================================================================================
- #include "chinese.h" //包含16*16漢字字模
- void Put16x16(unsigned short x, unsigned short y, unsigned char g[2], unsigned int fColor,unsigned int bColor)
- {
- unsigned int i,j,k;
- LCD_SetPos(x, x+16-1,y, y+16-1); //設(shè)置漢字顯示位置
- for (k=0;k<64;k++) //循環(huán)64次,查詢漢字字模位置
- {
- if ((ch16[k].GBK[0]==g[0])&&(ch16[k].GBK[1]==g[1])) //判斷第k個漢字的編碼是否與輸入漢字g[2]相等
- {
- for(i=0;i<32;i++) //如果相等,既已找到待顯示字模位置,循環(huán)寫入32字節(jié)
- {
- unsigned short m=ch16[k].hz16[i]; //讀取32字節(jié)中的第i字節(jié)
- for(j=0;j<8;j++) //循環(huán)寫入8位數(shù)據(jù)
- {
- if((m&0x80)==0x80) Write_Data_U16(fColor); //判斷最高位是否為1,最高位為1,寫入字符顏色
- else Write_Data_U16(bColor); //最高位為0,寫入背景顏色
- m<<=1; //左移1位,準備寫下一位
- }
- }
- }
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 顯示中英文字符串
- // 輸入?yún)?shù): x 橫坐標(biāo)
- // y 縱坐標(biāo)
- // *s 待顯示的字符串,例如LCD_PutString(24,16,"123藍芯",White,Blue);即把"123藍芯"的第一個字符地址賦給指針變量s.
- // bColor 字符背景顏色
- //================================================================================================
- void LCD_PutString(unsigned short x, unsigned short y, unsigned char *s, unsigned int fColor, unsigned int bColor)
- {
- unsigned char l=0; //顯示屏位置增量
- while(*s)
- {
- if( *s < 0x80) //判斷s指向的字符串中的某字符的編碼值是否小于128,如果小于,即為ASCII字符
- {
- LCD_PutChar(x+l*8,y,*s,fColor,bColor);//顯示該字符
- s++;l++; //指針加1,位置加1
- }
- else
- {
- Put16x16(x+l*8,y,(unsigned char*)s,fColor,bColor);//顯示該漢字
- s+=2;l+=2; //因為漢字為編碼為2字節(jié),指針加2,顯示16x16所以位置加2
- }
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 指定位置顯示RGB顏色
- // 輸入?yún)?shù): x0,y0 起始坐標(biāo)
- // x1,y1 結(jié)束坐標(biāo)
- // Color 背景顏色
- //================================================================================================
- void Show_RGB (unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1,unsigned int Color)
- {
- unsigned int i,j;
- LCD_SetPos(x0,x1,y0,y1); //設(shè)置顯示位置
- for (i=y0;i<=y1;i++)
- {
- for (j=x0;j<=x1;j++)
- Write_Data_U16(Color);
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: TFT初始化
- //================================================================================================
- void TFT_Initial(void)
- {
- RES = 1;
- delayms(1); // Delay 1ms
- RES = 0;
- delayms(10); // Delay 10ms
- RES = 1;
- delayms(50); // Delay 50 ms
-
- if(Device_code==0x9320)
- {
- //************* Start Initial Sequence **********//
- Write_Cmd_Data(0x00,0x0001); //Set the OSC bit as ‘1’ to start the internal oscillator
- Write_Cmd_Data(0x01,0x0100); // set SS and SM bit
- Write_Cmd_Data(0x02,0x0700); // set 1 line inversion
- Write_Cmd_Data(0x03,0x1030); //set GRAM Write direction and BGR=1
- Write_Cmd_Data(0x04,0x0000); // Resize register
- Write_Cmd_Data(0x08,0x0202); // set the back porch and front porch
- Write_Cmd_Data(0x09,0x0000); // set non-display area refresh cycle ISC[3:0]
- Write_Cmd_Data(0x0A,0x0000); // FMARK function
- Write_Cmd_Data(0x0C,0x0000); // RGB interface setting
- Write_Cmd_Data(0x0D,0x0000); // Frame marker Position
- Write_Cmd_Data(0x0F,0x0000); // RGB interface polarity
- delayms(30);
- //*************Power On sequence ****************//
- Write_Cmd_Data(0x10, 0x16b0); // SAP, BT[3:0], AP, DSTB, SLP, STB
- delayms(30);
- Write_Cmd_Data(0x11, 0x0007); //Write final user’s setting values to VC bit
- Write_Cmd_Data(0x12, 0x013a); // set Internal reference voltage
- Write_Cmd_Data(0x13, 0x1a00); // VDV[4:0] for VCOM amplitude
- delayms(30);
- Write_Cmd_Data(0x29, 0x000c); // Set VCM[5:0] for VCOMH
- delayms(30); // Delay 50ms
- // ----------- Adjust the Gamma Curve ----------//
- Write_Cmd_Data(0x0030, 0x0000);
- Write_Cmd_Data(0x0031, 0x0505);
- Write_Cmd_Data(0x0032, 0x0304);
- Write_Cmd_Data(0x0035, 0x0006);
- Write_Cmd_Data(0x0036, 0x0707);
- Write_Cmd_Data(0x0037, 0x0105);
- Write_Cmd_Data(0x0038, 0x0002);
- Write_Cmd_Data(0x0039, 0x0707);
- Write_Cmd_Data(0x003C, 0x0704);
- Write_Cmd_Data(0x003D, 0x0807);
- //------------------ Set GRAM area ---------------//
- Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
- Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
- Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
- Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
- Write_Cmd_Data(0x0060, 0x2700); // Gate Scan Line
- Write_Cmd_Data(0x0061, 0x0001); // NDL,VLE, REV
- Write_Cmd_Data(0x006A, 0x0000); // set scrolling line
- Write_Cmd_Data(0x20, 0x0000); // GRAM horizontal Address
- Write_Cmd_Data(0x21, 0x0000); // GRAM Vertical Address
- //-------------- Partial Display Control ---------//
- Write_Cmd_Data(0x0080, 0x0000);
- Write_Cmd_Data(0x0081, 0x0000);
- Write_Cmd_Data(0x0082, 0x0000);
- Write_Cmd_Data(0x0083, 0x0000);
- Write_Cmd_Data(0x0084, 0x0000);
- Write_Cmd_Data(0x0085, 0x0000);
- //-------------- Panel Control ---------//
- Write_Cmd_Data(0x90,0x0010); //Frame Cycle Contral
- Write_Cmd_Data(0x92,0x0000); //Panel Interface Contral
- Write_Cmd_Data(0x93,0x0003); //Panel Interface Contral 3.
- Write_Cmd_Data(0x95,0x0110); //Frame Cycle Contral
- Write_Cmd_Data(0x97,0x0000); //
- Write_Cmd_Data(0x98,0x0000); //Frame Cycle Contral.
- //-------------- Display on ---------//
- Write_Cmd_Data(0x07,0x0173);
- }
- else if(Device_code==0x1505 )
- {
- //************* Start Initial Sequence **********//
- Write_Cmd_Data(0x00,0x0001); //Set the OSC bit as ‘1’ to start the internal oscillator
- Write_Cmd_Data(0x01,0x0100); // set SS and SM bit
- Write_Cmd_Data(0x02,0x0700); // set 1 line inversion
- Write_Cmd_Data(0x03,0x1030); //set GRAM Write direction and BGR=1
- Write_Cmd_Data(0x04,0x0000); // Resize register
- Write_Cmd_Data(0x08,0x0202); // set the back porch and front porch
- Write_Cmd_Data(0x09,0x0000); // set non-display area refresh cycle ISC[3:0]
- Write_Cmd_Data(0x0A,0x0000); // FMARK function
- Write_Cmd_Data(0x0C,0x0000); // RGB interface setting
- Write_Cmd_Data(0x0D,0x0000); // Frame marker Position
- Write_Cmd_Data(0x0F,0x0000); // RGB interface polarity
- delayms(30);
- //*************Power On sequence ****************//
- Write_Cmd_Data(0x10, 0x16b0); // SAP, BT[3:0], AP, DSTB, SLP, STB
- delayms(30);
- Write_Cmd_Data(0x11, 0x0007); //Write final user’s setting values to VC bit
- Write_Cmd_Data(0x12, 0x013a); // set Internal reference voltage
- Write_Cmd_Data(0x13, 0x1a00); // VDV[4:0] for VCOM amplitude
- delayms(30);
- Write_Cmd_Data(0x29, 0x000c); // Set VCM[5:0] for VCOMH
- delayms(30); // Delay 50ms
- // ----------- Adjust the Gamma Curve ----------//
- Write_Cmd_Data(0x0030, 0x0000);
- Write_Cmd_Data(0x0031, 0x0505);
- Write_Cmd_Data(0x0032, 0x0304);
- Write_Cmd_Data(0x0035, 0x0006);
- Write_Cmd_Data(0x0036, 0x0707);
- Write_Cmd_Data(0x0037, 0x0105);
- Write_Cmd_Data(0x0038, 0x0002);
- Write_Cmd_Data(0x0039, 0x0707);
- Write_Cmd_Data(0x003C, 0x0704);
- Write_Cmd_Data(0x003D, 0x0807);
- //------------------ Set GRAM area ---------------//
- Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
- Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
- Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
- Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
- Write_Cmd_Data(0x0060, 0x2700); // Gate Scan Line
- Write_Cmd_Data(0x0061, 0x0001); // NDL,VLE, REV
- Write_Cmd_Data(0x006A, 0x2700); // set scrolling line
- Write_Cmd_Data(0x20, 0x0000); // GRAM horizontal Address
- Write_Cmd_Data(0x21, 0x0000); // GRAM Vertical Address
- //-------------- Partial Display Control ---------//
- Write_Cmd_Data(0x0080, 0x0000);
- Write_Cmd_Data(0x0081, 0x0000);
- Write_Cmd_Data(0x0082, 0x0000);
- Write_Cmd_Data(0x0083, 0x0000);
- Write_Cmd_Data(0x0084, 0x0000);
- Write_Cmd_Data(0x0085, 0x0000);
- //-------------- Panel Control ---------//
- Write_Cmd_Data(0x90,0x0010); //Frame Cycle Contral
- Write_Cmd_Data(0x92,0x0000); //Panel Interface Contral
- Write_Cmd_Data(0x93,0x0003); //Panel Interface Contral 3.
- Write_Cmd_Data(0x95,0x0110); //Frame Cycle Contral
- Write_Cmd_Data(0x97,0x0000); //
- Write_Cmd_Data(0x98,0x0000); //Frame Cycle Contral.
- //-------------- Display on ---------//
- Write_Cmd_Data(0x07,0x0173);
- }
- else if(Device_code==0x9328)
- {
- //************* Start Initial Sequence **********//
- Write_Cmd_Data(0x0001,0x0100); //set SS and SM bit //設(shè)置掃描方向
- Write_Cmd_Data(0x0002,0x0700); //EOR=1 and B/C=1 to set the line inversion //設(shè)置行反轉(zhuǎn)
- Write_Cmd_Data(0x0003,0x1030); //set Entry Mode //設(shè)置進入模式
- Write_Cmd_Data(0x0004,0x0000); //
- Write_Cmd_Data(0x00A4,0x0001);
- Write_Cmd_Data(0x0008,0x0202); // set the back porch and front porch
- Write_Cmd_Data(0x0009,0x0000); // set non-display area refresh cycle ISC[3:0]
- Write_Cmd_Data(0x000A,0x0000); // FMARK function
- Write_Cmd_Data(0x000C,0x0000); // RGB interface setting
- Write_Cmd_Data(0x000D, 0x0000); // Frame marker Position
- Write_Cmd_Data(0x000F, 0x0000); // RGB interface polarity
- //*************Power On sequence ****************//
- Write_Cmd_Data(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
- Write_Cmd_Data(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
- Write_Cmd_Data(0x0012, 0x0000); // VREG1OUT voltage
- Write_Cmd_Data(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
- delayms(30);
- Write_Cmd_Data(0x0010, 0x1690); // SAP, BT[3:0], AP, DSTB, SLP, STB
- Write_Cmd_Data(0x0011, 0x0227); // R11h=0x0221 at VCI=3.3V, DC1[2:0], DC0[2:0], VC[2:0]
- delayms(30);
- Write_Cmd_Data(0x0012, 0x001C); // External reference voltage= Vci;
- delayms(30);
- Write_Cmd_Data(0x0013, 0x1800); // R13=1200 when R12=009D;VDV[4:0] for VCOM amplitude
- Write_Cmd_Data(0x0029, 0x001C); // R29=000C when R12=009D;VCM[5:0] for VCOMH
- Write_Cmd_Data(0x002B, 0x000D); // Frame Rate = 91Hz
- delayms(30);
- Write_Cmd_Data(0x0020, 0x0000); // GRAM horizontal Address
- Write_Cmd_Data(0x0021, 0x0000); // GRAM Vertical Address
- // ----------- Adjust the Gamma Curve ----------//
- Write_Cmd_Data(0x0030, 0x0007);
- Write_Cmd_Data(0x0031, 0x0302);
- Write_Cmd_Data(0x0032, 0x0105);
- Write_Cmd_Data(0x0035, 0x0206);
- Write_Cmd_Data(0x0036, 0x0808);
- Write_Cmd_Data(0x0037, 0x0206);
- Write_Cmd_Data(0x0038, 0x0504);
- Write_Cmd_Data(0x0039, 0x0007);
- Write_Cmd_Data(0x003C, 0x0105);
- Write_Cmd_Data(0x003D, 0x0808);
- //------------------ Set GRAM area ---------------//
- Write_Cmd_Data(0x0050, 0x0000); // Horizontal GRAM Start Address
- Write_Cmd_Data(0x0051, 0x00EF); // Horizontal GRAM End Address
- Write_Cmd_Data(0x0052, 0x0000); // Vertical GRAM Start Address
- delayms(30);
- Write_Cmd_Data(0x0053, 0x013F); // Vertical GRAM Start Address
- delayms(30);
- Write_Cmd_Data(0x0060, 0xA700); // Gate Scan Line
- Write_Cmd_Data(0x0061, 0x0001); // NDL,VLE, REV
- Write_Cmd_Data(0x006A, 0x0000); // set scrolling line
- //-------------- Partial Display Control ---------//
- Write_Cmd_Data(0x0080, 0x0000);
- Write_Cmd_Data(0x0081, 0x0000);
- Write_Cmd_Data(0x0082,0x0000);
- Write_Cmd_Data(0x0083,0x0000);
- Write_Cmd_Data(0x0084,0x0000);
- Write_Cmd_Data(0x0085,0x0000);
- //-------------- Panel Control -------------------//
- Write_Cmd_Data(0x0090, 0x0010);
- Write_Cmd_Data(0x0092, 0x0000);
- Write_Cmd_Data(0x0093, 0x0003);
- Write_Cmd_Data(0x0095, 0x0110);
- Write_Cmd_Data(0x0097, 0x0000);
- Write_Cmd_Data(0x0098, 0x0000);
- Write_Cmd_Data(0x0007, 0x0133); // 262K color and display ON
- }
- }
- //================================================================================================
- // 實現(xiàn)功能: 設(shè)置坐標(biāo)
- // 輸入?yún)?shù): x0,y0 起始坐標(biāo)
- // x1,y1 結(jié)束坐標(biāo)
- //================================================================================================
- void LCD_SetPos(unsigned int x0,unsigned int x1,unsigned int y0,unsigned int y1)
- {
- Write_Cmd_Data(0x50,x0); // Horizontal GRAM Start Address
- Write_Cmd_Data(0x51,x1); // Horizontal GRAM End Address
- Write_Cmd_Data(0x52,y0); // Vertical GRAM Start Address
- Write_Cmd_Data(0x53,y1); // Vertical GRAM Start Address
- Write_Cmd_Data(0x20,x0); // GRAM horizontal Address
- Write_Cmd_Data(0x21,y0); // GRAM Vertical Address
- Write_Cmd (0x00,0x22); // 0x0022,Start to Write Data to GRAM
- }
- //================================================================================================
- // 實現(xiàn)功能: 畫點
- // 輸入?yún)?shù): x,y 需要畫點坐標(biāo)
- // color 點的顏色
- //================================================================================================
- void Put_pixel(unsigned int x,unsigned int y,unsigned int color)
- {
- LCD_SetPos(x,x,y,y); //設(shè)置待畫點坐標(biāo)
- Write_Data_U16(color); //在指定點寫入顏色數(shù)據(jù)
- }
復(fù)制代碼
ds1302程序:
- #include <reg51.h>
- #include"intrins.h" //_nop_();延時函數(shù)用
- #define uchar unsigned char
- #define uint unsigned int
- sbit DS1302_SCLK =P1^0; //時鐘
- sbit DS1302_IO =P1^1; //數(shù)據(jù)輸入輸出
- sbit DS1302_RST =P1^2; //復(fù)位/片選線
- struct S_TIME
- {
- uchar SEC; //00~59
- uchar MIN; //00~59
- uchar HR; //00-23
- uchar DATE; //00-31
- uchar MONTH; //01-12
- uchar DAY; //01-07
- uchar YEAR; //00-99
- }TIME={0x01,0x01,0x01,0x07,0x05,0x05,0x10};//初始化時間參數(shù)
- uchar dtime[7][2]={
- {0x20,0x20}, //秒 十位和個位
- {0x20,0x20}, //分 十位和個位
- {0x20,0x20}, //時 十位和個位
- {0x20,0x20}, //日 十位和個位
- {0x20,0x20}, //月 十位和個位
- {0x20,0x20}, //周 十位和個位
- {0x20,0x20} //年 十位和個位
- };
- /********************************************************************
- //函數(shù)名稱: DS1302_WByte
- //函數(shù)功能: 往DS1302 寫入 1 Byte 數(shù)據(jù)
- //輸入值: ndata: 寄存器的數(shù)據(jù)或地址
- //返回值: 無
- ***********************************************************************/
- void DS1302_WByte(uchar ndata)
- {
- uchar i;
- for(i=8; i>0; i--) //循環(huán)8次寫入8位數(shù)據(jù)
- {
- DS1302_IO = (bit)(ndata&0x01); //取最低位數(shù)據(jù),從0位至7位依次傳送
- DS1302_SCLK = 1; //給一個脈沖,將數(shù)據(jù)寫入1302
- _nop_();
- DS1302_SCLK = 0;
- ndata>>=1; //即ndata = ndata >> 1;
- }
- }
- /********************************************************************
- //函數(shù)名稱: DS1302_RByte
- //函數(shù)功能: 從DS1302讀取 1 Byte數(shù)據(jù)
- //輸入值: 無
- //返回值: ndata:讀取的數(shù)據(jù)
- ***********************************************************************/
- uchar DS1302_RByte(void)
- {
- uchar i;
- uchar ndata=0;
- for(i=8;i>0;i--) //循環(huán)8次讀出8位數(shù)據(jù)
- {
- DS1302_IO=1; //初始化數(shù)據(jù)IO
- ndata>>=1; //即ndata = ndata >> 1;
- if(DS1302_IO) ndata|=0x80; //從數(shù)據(jù)口讀取1位數(shù)據(jù)
- DS1302_SCLK = 1; //給一個脈沖
- _nop_();
- DS1302_SCLK = 0;
- }
- return (ndata); //返回結(jié)果
- }
- /********************************************************************
- //函數(shù)名稱: DS1302_Wdata
- //函數(shù)功能: 往DS1302某地址寫入某數(shù)據(jù)
- //輸入值: nAddr: DS1302地址, ndata: 要寫的數(shù)據(jù)
- //返回值: 無
- ***********************************************************************/
- void DS1302_Wdata(uchar nAddr, uchar ndata)
- {
- DS1302_RST=0;
- DS1302_SCLK=0;
- DS1302_RST=1;
- DS1302_WByte(nAddr); // 寫1Byte地址
- DS1302_WByte(ndata); // 寫1Byte數(shù)據(jù)
- DS1302_SCLK=1;
- DS1302_RST=0;
- }
- /********************************************************************
- //函數(shù)名稱: DS1302_Rdata
- //函數(shù)功能: 從DS1302某地址讀取數(shù)據(jù)
- //輸入值: nAddr: DS1302地址
- //返回值: ndata: 讀取的數(shù)據(jù)
- ***********************************************************************/
- uchar DS1302_Rdata(uchar nAddr)
- {
- uchar ndata;
- DS1302_RST=0;
- DS1302_SCLK=0;
- DS1302_RST=1;
- DS1302_WByte(nAddr); /* 地址,命令 */
- ndata = DS1302_RByte(); /* 讀1Byte數(shù)據(jù) */
- DS1302_SCLK=1;
- DS1302_RST=0;
- return(ndata);
- }
- /********************************************************************
- //函數(shù)名稱: InitDS1302
- //函數(shù)功能: DS1302初始時間設(shè)定
- //輸入值: 無
- //返回值: 無
- ***********************************************************************/
- void InitDS1302(void)
- {
- DS1302_Wdata(0x8e,0x00); //控制命令,WP=0,寫操作
- DS1302_Wdata(0x90,0xa5);
- /*
- 地址0x90為充電寄存器,可以對充電電流進行限制,寫入
- 內(nèi)容高4位固定為1010(其他組合均不能充電),低4
- 位的首2位是選擇內(nèi)部降壓二極管的個數(shù)的,01代表在
- 充電回路串入1個二極管,10代表串入2個;最后2位可
- 設(shè)定串入的電阻的數(shù)值:01為2k歐,10為4k歐,11為8k歐。
- */
- DS1302_Wdata(0x80,TIME.SEC); //秒
- DS1302_Wdata(0x82,TIME.MIN); //分
- DS1302_Wdata(0x84,TIME.HR); //時
- DS1302_Wdata(0x86,TIME.DATE); //日
- DS1302_Wdata(0x88,TIME.MONTH);//月
- DS1302_Wdata(0x8a,TIME.DAY); //星期
- DS1302_Wdata(0x8c,TIME.YEAR); //年
- DS1302_Wdata(0x8e,0x80); //控制命令,WP=1,寫保護
- }
- /********************************************************************
- //函數(shù)名稱: GetDS1302
- //函數(shù)功能: DS1302當(dāng)前時間讀取
- //輸入值: 無
- //返回值: 無
- ***********************************************************************/
- void GetDS1302(void)
- {
- TIME.SEC = DS1302_Rdata(0x81); //從DS1302讀取秒數(shù)據(jù)
- dtime[0][0]=(TIME.SEC>>4)+0x30; //十位
- dtime[0][1]=(TIME.SEC&0x0F)+0x30; //個位
- TIME.MIN = DS1302_Rdata(0x83); //從DS1302讀取分數(shù)據(jù)
- dtime[1][0]=(TIME.MIN>>4)+0x30; //十位
- dtime[1][1]=(TIME.MIN&0x0F)+0x30; //個位
- TIME.HR = DS1302_Rdata(0x85); //從DS1302讀取時數(shù)據(jù)
- dtime[2][0]=(TIME.HR>>4)+0x30; //十位
- dtime[2][1]=(TIME.HR&0x0F)+0x30; //個位
- TIME.DATE = DS1302_Rdata(0x87); //從DS1302讀取日數(shù)據(jù)
- dtime[3][0]=(TIME.DATE>>4)+0x30; //十位
- dtime[3][1]=(TIME.DATE&0x0F)+0x30; //個位
- TIME.MONTH = DS1302_Rdata(0x89); //從DS1302讀取月數(shù)據(jù)
- dtime[4][0]=(TIME.MONTH>>4)+0x30; //十位
- dtime[4][1]=(TIME.MONTH&0x0F)+0x30; //個位
- TIME.DAY = DS1302_Rdata(0x8b); //從DS1302讀取星期數(shù)據(jù)
- dtime[5][0]=(TIME.DAY>>4)+0x30; //十位
- dtime[5][1]=(TIME.DAY&0x0F)+0x30; //個位
- TIME.YEAR = DS1302_Rdata(0x8d); //從DS1302讀取年數(shù)據(jù)
- dtime[6][0]=(TIME.YEAR>>4)+0x30; //十位
- dtime[6][1]=(TIME.YEAR&0x0F)+0x30; //個位
- }
復(fù)制代碼
|
-
-
程序1、.zip
2016-4-15 10:41 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
91.51 KB, 下載次數(shù): 28, 下載積分: 黑幣 -5
評分
-
查看全部評分
|