具備AD檢測功能,LCD1602顯示功能。運放檢測,mcu處理,附件包含仿真proteus8 和源程序,造福小白
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
單片機源程序如下:
#include <reg51.h>
#include <stdio.h>
#include<intrins.h>
/*****************************程序引腳定義***********************************************/
/*定義LCD控制信號線*/
sbit SCL = P2^5;
sbit SDA = P2^6;
/*定義AD采集IC信號線*/
sbit ADE=P2^7; //輸出使能
sbit ADEOC=P3^7; //轉換完畢信號輸出1
sbit ADST=P3^6; //AD復位及開始信號
sbit ADSCK=P3^5; //時鐘信號輸入
sbit ADA=P2^2;
sbit ADB=P2^1;
sbit ADC=P2^0;
sbit DQ=P2^3;//定義DS18B20接口
#define KEYValueShowEN 0
bit ack;
unsigned char LCD_data=0x08;
#define uchar unsigned char
#define uint unsigned int
/*dat用于存儲AD采集回來的數字變量,范圍為0-255*/
unsigned int dat;
/***********************************************************************************************************************/
/***********************************************************************函數聲明*****************************************/
/************************************************************************************************************************/
/*LCD驅動函數名聲明*/
void LCD_Init(void); //初始化函數,設置LCD的顯示模式
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data); //寫入字符函數
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s); //寫入字符串函數
void LCD_Clear(void); //清屏函數
void LCD_Write_Data(unsigned char Data); //寫入數據函數
void LCD_Write_Com(unsigned char com); // 寫入命令函數
void LCD_PrintChar(unsigned char ucIdxX, unsigned char ucIdxY, unsigned char cData); //向LCD中寫入一個Char型數據
void LED_PrintValueI(unsigned char x, unsigned char y, int Data); //向LCD寫入一個整形數據
void LED_PrintValueF(short int x, unsigned short int y, float ddata, unsigned short int num);//向LCD中寫入一個浮點型數據
/*延時函數聲明*/
void DelayUs2x(unsigned char t);
void DelayMs(unsigned char t);
/*AD采樣函數聲明*/
void ADinit(); //定時器和AD初始化
void ADdress(unsigned char add); //八路模擬輸入選擇(0~7)add最大值為7
unsigned int readad(unsigned char add); //AD轉換函數
/***********************************************************************************************************************/
/***********************************************************************函數實現*****************************************/
/************************************************************************************************************************/
//*****************延時************************
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
_nop_();
}
void delay_nms(unsigned int n) //N ms延時函數
{
unsigned int i,j;
for (i=0;i<n;i++)
for (j=0;j<1140;j++);
}
/*------------------------------------------------
uS延時函數,含有輸入參數 unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編,大致延時
長度如下 T=tx2+5 uS
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
mS延時函數,含有輸入參數 unsigned char t,無返回值
unsigned char 是定義無符號字符變量,其值的范圍是
0~255 這里使用晶振12M,精確延時請使用匯編
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
//大致延時1mS
DelayUs2x(245);
DelayUs2x(245);
}
}
//****************************************************
//MS延時函數(12M晶振下測試)
//****************************************************
void Delay_ms(unsigned int n)
{
unsigned int i,j;
for(i=0;i<n;i++)
for(j=0;j<123;j++);
}
void nop4()
{
_nop_(); //等待一個機器周期
_nop_(); //等待一個機器周期
_nop_(); //等待一個機器周期
_nop_(); //等待一個機器周期
}
//***************************************
void Start()
{
SDA=1;
_nop_();
SCL=1;
nop4();
SDA=0;
nop4();
SCL=0;
_nop_();
_nop_();
}
void Stop()
{
SDA=0;
_nop_();
SCL=0;
nop4();//>4us后SCL跳變
SCL=1;
nop4();
SDA=1;
_nop_();
_nop_();
}
//******************************************
void Write_A_Byte(unsigned char c)
{
unsigned char BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) //要傳送的數據長度為8位
{
if((c<<BitCnt)&0x80) SDA=1; //判斷發送位
else SDA=0;
_nop_();
SCL=1; //置時鐘線為高,通知被控器開始接收數據位
nop4();
_nop_();
SCL=0;
}
_nop_();
_nop_();
SDA=1; //8位發送完后釋放數據線,準備接收應答位
_nop_();
_nop_();
SCL=1;
_nop_();
_nop_();
_nop_();
if(SDA==1)ack=0;
else
{
ack=1; //判斷是否接收到應答信號
// P1=0x00;
}
SCL=0;
_nop_();
_nop_();
}
bit Write_Random_Address_Byte(unsigned char add,unsigned char dat)
{
Start(); //啟動總線
Write_A_Byte(add<<1|0); //發送器件地址
if(ack==0)return(0);
Write_A_Byte(dat); //發送數據
if(ack==0)return(0);
Stop(); //結束總線
return(1);
}
//********************液晶屏使能*********************
void Enable_LCD_write()
{
LCD_data|=(1<<(3-1));//E=1;
Write_Random_Address_Byte(0x27,LCD_data); //
delay_nus(2);
LCD_data&=~(1<<(3-1));//E=0;
Write_Random_Address_Byte(0x27,LCD_data);
}
//*************寫命令****************************
void LCD_write_command(unsigned char command)
{
delay_nus(16);
LCD_data&=~(1<<(1-1));//RS=0;
LCD_data&=~(1<<(2-1));//RW=0;
//LCD_data&=~(1<<(4-1));
Write_Random_Address_Byte(0x27,LCD_data);
LCD_data&=0X0f; //清高四位
LCD_data|=command & 0xf0; //寫高四位
Write_Random_Address_Byte(0x27,LCD_data);
Enable_LCD_write();
command=command<<4; //低四位移到高四位
LCD_data&=0x0f; //清高四位
LCD_data|=command&0xf0; //寫低四位
Write_Random_Address_Byte(0x27,LCD_data);
Enable_LCD_write();
}
//*************寫數據****************************
void LCD_write_data(unsigned char value)
{
delay_nus(16);
LCD_data|=(1<<(1-1));//RS=1;
LCD_data&=~(1<<(2-1));//RW=0;
Write_Random_Address_Byte(0x27,LCD_data);
LCD_data&=0X0f; //清高四位
LCD_data|=value&0xf0; //寫高四位
Write_Random_Address_Byte(0x27,LCD_data);
Enable_LCD_write();
value=value<<4; //低四位移到高四位
LCD_data&=0x0f; //清高四位
LCD_data|=value&0xf0; //寫低四位
Write_Random_Address_Byte(0x27,LCD_data);
Enable_LCD_write();
}
//**********************設置顯示位置*********************************
void set_position(unsigned char x,unsigned char y)
{
unsigned char position;
if (y == 0)
position = 0x80 + x;
else
position = 0xc0 + x;
LCD_write_command(position);
}
/***************************************************
*函數名: void LCD_Clear(void)
*參數:無
*功能說明:清屏函數
****************************************************/
void LCD_Clear(void)
{
LCD_write_command(0x01);
DelayMs(5);
}
Keil代碼與Proteus仿真下載:
基于51單片機的溫度、電壓電流檢測系統程序 仿真.rar
(244.72 KB, 下載次數: 47)
2023-5-4 08:47 上傳
點擊文件名下載附件
|