本帖最后由 405616736 于 2020-4-25 10:14 编辑
LCD1602 D0~D7接P0口,RS=P3.1,RW=P3.2,sbit E=P3.3。
电压测试口接P1.0。供电电压要稳定5V才能准确测量。另外因为没有加电阻,只能测5V以下电压。
废话少说,直接上实物图。
单片机源程序如下:
- #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)
|