本帖最后由 405616736 于 2020-4-25 10:14 編輯
LCD1602 D0~D7接P0口,RS=P3.1,RW=P3.2,sbit E=P3.3。
電壓測試口接P1.0。供電電壓要穩定5V才能準確測量。另外因為沒有加電阻,只能測5V以下電壓。
廢話少說,直接上實物圖。
3.jpg (287.47 KB, 下載次數: 75)
下載附件
2020-4-25 10:13 上傳
2.jpg (281.22 KB, 下載次數: 68)
下載附件
2020-4-25 10:13 上傳
單片機源程序如下:
- #include "reg51.h"
- #include "intrins.h"
- #define LCD P0 //LCD1602數據接口
- sbit RS=P3^1; //設置RS引腳接口,RS=0,指令寄存器;RS=1,數據寄存器
- sbit RW=P3^2; //設置R/W引腳接口,R/W=0,寫;R/W=1,讀
- sbit E=P3^3; //設置E引腳接口,E允許信號
- /*Declare SFR associated with the ADC */
- sfr ADC_CONTR=0xBC; //ADC control register
- sfr ADC_RES=0xBD; //ADC high 8-bit result register
- sfr ADC_LOW2=0xBE; //ADC low 2-bit result register
- sfr P1ASF=0x9D; //P1 secondary function control register
- unsigned char V[]="000000";
- unsigned int ADC_temp=0;
- /*Define ADC operation const for ADC_CONTR*/
- #define ADC_POWER 0x80 //ADC power control bit
- #define ADC_FLAG 0x10 //ADC complete flag
- #define ADC_START 0x08 //ADC start control bit
- #define ADC_SPEEDLL 0x00 //420 clocks
- #define ADC_SPEEDL 0x20 //280 clocks
- #define ADC_SPEEDH 0x40 //140 clocks
- #define ADC_SPEEDHH 0x60 //70 clocks
- /******************************
- 延時函數
- ******************************/
- void Delay(unsigned int n)
- {
- unsigned int i=0,j=0;
- for(i=0;i<n;i++)
- for(j=0;j<123;j++);
- }
- /******************************
- 初始化ADC
- ******************************/
- void InitADC()
- {
- P1ASF=0xff;
- ADC_RES=0;
- ADC_CONTR=ADC_POWER | ADC_SPEEDLL;
- Delay(2);
- }
- /******************************
- 讀取ADC
- ******************************/
- unsigned char GetADCResult(unsigned char ch)
- {
- ADC_CONTR=ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- while (!(ADC_CONTR & ADC_FLAG));
- ADC_CONTR &=~ADC_FLAG;
- return ADC_RES;
- }
- /******************************
- LCD忙檢測
- ******************************/
- void CheckBusy(void)
- {
- unsigned int nTimeOut=0;
- RS=0;
- RW=1;
- E=0;
- E=1;
- while((LCD&0x80)&&(++nTimeOut !=0));
- E=0;
- RS=0;
- RW=1;
- }
- /******************************
- LCD發送命令或數據
- ******************************/
- void SendCmdorData(unsigned char byCmdorData,bit DI)
- {
- CheckBusy();
- RS=DI;
- RW=0;
- E=0;
- LCD=byCmdorData;
- Delay(5);
- E=1;
- Delay(5);
- E=0;
- RW=1;
- RS=0;
- }
- /******************************
- LCD初始化子
- ******************************/
- void Init(void)
- {
- SendCmdorData(0x38,0);
- Delay(50);
- SendCmdorData(0x01,0);
- Delay(50);
- SendCmdorData(0x06,0);
- Delay(50);
- SendCmdorData(0x0c,0);
- Delay(50);
- }
- /******************************
- 地址轉換
- ******************************/
- void SetAddress(unsigned char x,y)
- {
- unsigned char byAddress;
- switch(x)
- {
- case 1:
- byAddress=0x80+y;
- break;
- case 2:
- byAddress=0xC0+y;
- break;
- default:break;
- }
- SendCmdorData(byAddress,0);
- }
復制代碼 所有資料51hei提供下載:
5.zip
(1.43 KB, 下載次數: 175)
2020-4-25 00:17 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|