标题:
STC12C5A60S2两路AD采集用LCD1602显示程序源码
[打印本页]
作者:
a1234a5678a
时间:
2017-8-9 11:22
标题:
STC12C5A60S2两路AD采集用LCD1602显示程序源码
测试可行。
全部资料下载地址:
STC12C5A60S2两路AD采集用LCD1602显示.doc
(36.5 KB, 下载次数: 59)
2017-8-9 11:15 上传
点击文件名下载附件
STC12两路ad采样1602显示
下载积分: 黑币 -5
单片机源程序:
#include <STC12C5A.H>
#include<intrins.h>
#include "stdio.h"
#define ulong unsigned long
#define uint unsigned int
#define uchar unsigned char
uchar data adc _at_ 0x30;
uchar data adc1 _at_ 0x31;
sbit E=P2^7; //1602使能引脚
sbit RW=P2^6; //1602读写引脚
sbit RS=P2^5; //1602数据/命令选择引脚
void SendByte(unsigned char dat);
void SendStr(unsigned char *s);
void L1602_string(uchar hang,uchar lie,uchar *p);
void L1602_char(uchar hang,uchar lie,char sign);
uchar AD();
uchar AD1();
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);
}
}
void delay()
{
_nop_();
_nop_();
}
void Delay(uint del)
{
uint i,j;
for(i=0;i<del;i++)
for(j=0;j<=148;j++)
{
}
}
/********************************************************************
* 名称 : bit Busy(void)
* 功能 : 这个是一个读状态函数,读出函数是否处在忙状态
* 输入 : 输入的命令值
* 输出 : 无
***********************************************************************/
void Busy(void)
{
bit busy_flag = 1;
P0 = 0xff;
RS = 0;
delay();
RW = 1;
delay();
E = 1;
//Delay(1);
while(1)
{
busy_flag = (bit)(P0 & 0x80);
if(busy_flag == 0)
{
break;
}
}
E = 0;
}
/********************************************************************
* 名称 : wcmd(uchar del)
* 功能 : 1602命令函数
* 输入 : 输入的命令值
* 输出 : 无
***********************************************************************/
void wcmd(uchar del)
{
RS = 0;
delay();
RW = 0;
delay();
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
/********************************************************************
* 名称 : wdata(uchar del)
* 功能 : 1602写数据函数
* 输入 : 需要写入1602的数据
* 输出 : 无
***********************************************************************/
void wdata(uchar del)
{
delay();
RS = 1;
delay();
RW = 0;
delay();
E = 0;
delay();
P0 = del;
delay();
E = 1;
delay();
E = 0;
}
/********************************************************************
* 名称 : L1602_init()
* 功能 : 1602初始化,请参考1602的资料
* 输入 : 无
* 输出 : 无
***********************************************************************/
void L1602_init(void)
{
Delay(15);
wcmd(0x38);
Delay(5);
wcmd(0x38);
Delay(5);
wcmd(0x38);
wcmd(0x38);
Busy();
wcmd(0x08);
Busy();
wcmd(0x01);
Busy();
wcmd(0x06);
Busy();
wcmd(0x0c);
}
/********************************************************************
* 名称 : L1602_char(uchar hang,uchar lie,char sign)
* 功能 : 改变液晶中某位的值,如果要让第一行,第五个字符显示"b" ,调用该函数如下
L1602_char(1,5,'b')
* 输入 : 行,列,需要输入1602的数据
* 输出 : 无
***********************************************************************/
void L1602_char(uchar hang,uchar lie,char sign)
{
uchar a;
if(hang == 1)
{
a = 0x80;
}
if(hang == 2)
{
a = 0xc0;
}
a = a + lie - 1;
Busy();
wcmd(a);
Busy();
wdata(sign);
}
/********************************************************************
* 名称 : L1602_string(uchar hang,uchar lie,uchar *p)
* 功能 : 改变液晶中某位的值,如果要让第一行,第五个字符开始显示"ab cd ef" ,调用该函数如下
L1602_string(1,5,"ab cd ef;")
* 输入 : 行,列,需要输入1602的数据
* 输出 : 无
***********************************************************************/
void L1602_string(uchar hang,uchar lie,uchar *p)
{
uchar a;
if(hang == 1)
{
a = 0x80;
}
if(hang == 2)
{
a = 0xc0;
}
a = a + lie - 1;
while(1)
{
Busy();
wcmd(a);
Busy();
wdata(*p);
a++;
p++;
if((*p == '\0')||(a==0x90)||(a==0xd0))
{
break;
}
}
}
uchar AD()
{
ulong i;
uchar star;
ADC_CONTR|=0x80;
for(i=0;i<10000;i++);
P1ASF=0x01;
ADC_CONTR|=0XE1;
for(i=0;i<10000;i++);
while(1)
{
ADC_CONTR|=0x08;
star=0;
while(star==0)
{
star=ADC_CONTR&0x10;
}
ADC_CONTR&=0x00;
adc=ADC_RES;
//adc<<=2;
//adc+=ADC_RESL;
return adc;
}
}
uchar AD1()
{
ulong i;
uchar star;
ADC_CONTR|=0x80;
for(i=0;i<10000;i++);
P1ASF=0;
ADC_CONTR|=0XE0;
for(i=0;i<10000;i++);
while(1)
{
ADC_CONTR|=0x08;
star=0;
while(star==0)
{
star=ADC_CONTR&0x10;
}
ADC_CONTR&=0x00;
adc1=ADC_RES;
// adc1<<=2;
// adc1+=ADC_RESL;
return adc1;
}
}
void main()
{
float i,j;
unsigned char a[16];
unsigned char b[16];
L1602_init();
while(1)
{
{
i=AD()*5/256;
sprintf(a," DA1=%4.2f v",i);
L1602_string(2,0,a);
DelayMs(1);
j=AD1()*5/256;
sprintf(b," DC2=%4.2f v",j);
L1602_string(1,0,b);
}
}
}
复制代码
作者:
狼牙小帅
时间:
2018-2-25 14:30
谢谢楼主分享
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1