系统使用的模块有AT89C51单片机+LCD1602显示屏+光敏电阻模块+ADC0832+小灯。编写完整程序当检测到光照强度低于设定阈值小灯就会亮起。其中cs,clk,dl,模数分别接1.5/1.6/1.7,小灯接2.3
1个回答
关注
展开全部
以下是使用AT89C51单片机+LCD1602显示屏+光敏电阻模块+ADC0832+小灯的详细程序: #include #define LCD1602_DB P0 //LCD1602的数据口sbit LCD1602_RS = P2^0; //LCD1602的命令/字符选择端口sbit LCD1602_RW = P2^1; //LCD1602的读写命令选择端口sbit LCD1602_E = P2^2; //LCD1602的使能端口sbit lightSensorOut = P1^0; //光敏电阻模块的输出端口
咨询记录 · 回答于2023-06-22
系统使用的模块有AT89C51单片机+LCD1602显示屏+光敏电阻模块+ADC0832+小灯。编写完整程序当检测到光照强度低于设定阈值小灯就会亮起。其中cs,clk,dl,模数分别接1.5/1.6/1.7,小灯接2.3
以下是使用AT89C51单片机+LCD1602显示屏+光敏电阻模块+ADC0832+小灯的详细程序: #include #define LCD1602_DB P0 //LCD1602的数据口sbit LCD1602_RS = P2^0; //LCD1602的命令/字符选择端口sbit LCD1602_RW = P2^1; //LCD1602的读写命令选择端口sbit LCD1602_E = P2^2; //LCD1602的使能端口sbit lightSensorOut = P1^0; //光敏电阻模块的输出端口
sbit ADC0832_CS = P1^5; //ADC0832的片选端口sbit ADC0832_CLK = P1^6; //ADC0832的时钟端口sbit ADC0832_DIO = P1^7; //ADC0832的输入/输出端口sbit led = P2^3; //小灯的输出端口unsigned char disBuffer[] = "Bright:"; //在LCD1602上显示的字符串unsigned char result = 0; //保存光敏电阻模块测量的光照强度值//函数声明
void delayMs(unsigned int n);void initLCD1602();void writeLCD1602Cmd(unsigned char cmd);void writeLCD1602Dat(unsigned char dat);void readLightSensor();void initADC0832();unsigned char readADC0832(unsigned char ch);void main(){ initLCD1602(); //初始化LCD1602 initADC0832(); //初始化ADC0832 while(1) {
readLightSensor(); //读取光敏电阻模块的输出值 if(result < 50) //如果光照强度低于设定阈值,小灯亮起 { led = 0; } else { led = 1; } }}//延时函数,n为要延时的毫秒数void delayMs(unsigned int n){ unsigned int i, j; for(i = 0; i < n; i++) { for(j = 0; j < 125; j++); }}
//LCD1602初始化函数void initLCD1602(){ delayMs(15); //上电后延时15ms以上 writeLCD1602Cmd(0x38); //显示模式设置:16*2,5*7点阵,8位数据接口 writeLCD1602Cmd(0x0c); //显示控制:开显示,不显示光标,不闪烁 writeLCD1602Cmd(0x06); //光标/显示移动设置:光标右移,字符不移动 writeLCD1602Cmd(0x01); //清屏,将地址指针指向起始位置}//LCD1602发送命令
void writeLCD1602Cmd(unsigned char cmd){ LCD1602_RS = 0; //选择命令端口 LCD1602_RW = 0; //选择写入模式 LCD1602_E = 1; LCD1602_DB = cmd; //将命令写入数据口 delayMs(1); //延时 LCD1602_E = 0;}//LCD1602发送数据void writeLCD1602Dat(unsigned char dat){
LCD1602_RS = 1; //选择数据端口 LCD1602_RW = 0; //选择写入模式 LCD1602_E = 1; LCD1602_DB = dat; //将数据写入数据口 delayMs(1); //延时 LCD1602_E = 0;}//读取光敏电阻模块输出的模拟信号,转换成数字信号,并保存在result中
void readLightSensor(){ unsigned char lightValue = 0; ADC0832_CS = 0; //片选信号有效 ADC0832_DIO = 1; //启动转换,将输入信号置高 delayMs(1); ADC0832_CLK
亲您自己组合一下
太长了