环境keil5 、proteus8.6
如果版本过高无法打开 联系作者q1562335917
主要是8951运用AD1674和LCD1602来测量电压与数显电压值
LCD1602+AD1674仿真原理图如下(proteus仿真工程文件可到本帖附件中下载):
电路图
测量数据
单片机源程序如下:
- #include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
- #include<intrins.h>
- sbit RS = P3^4; //定义端口
- sbit RW = P3^5;
- sbit EN = P3^6;
- sbit STS=P3^3;
- sbit CE = P3^2;
- sbit CS =P3^1;
- sbit RC =P3^0;
- #define RS_CLR RS=0
- #define RS_SET RS=1
- #define RW_CLR RW=0
- #define RW_SET RW=1
- #define EN_CLR EN=0
- #define EN_SET EN=1
- #define uchar unsigned char
- #define uint unsigned int
- #define DataPort P1
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- void DelayMs(unsigned char t)
- {
-
- while(t--)
- {
- //大致延时1mS
- DelayUs2x(245);
- DelayUs2x(245);
- }
- }
- /*------------------------------------------------
- 判忙函数
- ------------------------------------------------*/
- bit LCD_Check_Busy(void)
- {
- DataPort= 0xFF;
- RS_CLR;
- RW_SET;
- EN_CLR;
- _nop_();
- EN_SET;
- return (bit)(DataPort & 0x80);
- }
- /*------------------------------------------------
- 写入命令函数
- ------------------------------------------------*/
- void LCD_Write_Com(unsigned char com)
- {
- // while(LCD_Check_Busy()); //忙则等待
- DelayMs(5);
- RS_CLR;
- RW_CLR;
- EN_SET;
- DataPort= com;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
- 写入数据函数
- ------------------------------------------------*/
- void LCD_Write_Data(unsigned char Data)
- {
- //while(LCD_Check_Busy()); //忙则等待
- DelayMs(5);
- RS_SET;
- RW_CLR;
- EN_SET;
- DataPort= Data;
- _nop_();
- EN_CLR;
- }
- /*------------------------------------------------
- 清屏函数
- ------------------------------------------------*/
- void LCD_Clear(void)
- {
- LCD_Write_Com(0x01);
- DelayMs(5);
- }
- /*------------------------------------------------
- 写入字符串函数
- ------------------------------------------------*/
- void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x); //表示第一行
- }
- else
- {
- LCD_Write_Com(0xC0 + x); //表示第二行
- }
- while (*s)
- {
- LCD_Write_Data( *s);
- s ++;
- }
- }
- /*------------------------------------------------
- 写入字符函数
- ------------------------------------------------*/
- void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
- {
- if (y == 0)
- {
- LCD_Write_Com(0x80 + x);
- }
- else
- {
- LCD_Write_Com(0xC0 + x);
- }
- LCD_Write_Data( Data);
- }
- /*------------------------------------------------
- 初始化函数
- ------------------------------------------------*/
- void LCD_Init(void)
- {
- LCD_Write_Com(0x38); /*显示模式设置*/
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- DelayMs(5);
- LCD_Write_Com(0x38);
- LCD_Write_Com(0x08); /*显示关闭*/
- LCD_Write_Com(0x01); /*显示清屏*/
- LCD_Write_Com(0x06); /*显示光标移动设置*/
- DelayMs(5);
- LCD_Write_Com(0x0C); /*显示开及光标设置*/
- }
- /*------------------------------------------------
- AD转换函数
- ------------------------------------------------*/
- double AD()
- {
- double i,j,k;
- CE=1;
- CS=0;
- RC=0;
- _nop_();
- while(STS==1); //等待数据采集结束
- CE=0;
- CE=1;
- CS=0;
- RC=1;
- _nop_();
- i=P0;
- j=P2;
- i=i*16;
- j=j/16;
- k=i+j;
- k=k*10;
- k=k/4096;//12位ad
- return k;
- }
- /*------------------------------------------------
- 数据处理
- ------------------------------------------------*/
- void data_process(double dat,uchar s[10])
- {
- int a,b,c,d;
- dat=dat*100;
- a=(int)(dat);
- b=a/100;
- s[0]=b+48;
- s[1]='.';
- d=a%10;
- s[3]=d+48;
- a=a/10;
- c=a%10;
- s[2]=c+48;
- s[4]='v';
- }
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
光敏测量电压.zip
(83.49 KB, 下载次数: 37)
|