/*********** (C) COPYRIGHT 2009 http://www.wang1jin.com ************
* 文件名称: main.c
* 程序作者: wish_btn(QQ:1323266146 ,Email:wish_btn@qq.com)
* 程序版本: V1.0
* 编制日期: 12-13-2009
* 功能描述: 双通道电压转换
* 编译器:Keil C uVision4
* 芯片:STC89C52,11.0592M晶振
* 技术支持:http://www.wang1jin.com
*
* 提供原创51单片机、PROTEL99SE视频教程下载,欢迎大家前往论坛下载。
* 论坛地址: http://www.wang1jin.com/bbs/
******************** http://www.wang1jin.com **********************/
#include <REG52.H>
sbit LCD_rs = P1^0;
sbit LCD_rw = P1^1;
sbit LCD_en = P1^2; //LCD1602控制信号
sbit ADC0832_CS = P2^0;
sbit ADC0832_CLK = P3^6;
sbit ADC0832_DODI = P3^7;//adc0832控制端
unsigned char temp;
float result;
unsigned char code Forum[] = "bbs.wang1jin.com";
void Delay(unsigned int uiCount); //延时函数
void LCD1602_CheckBusy(void); //忙检测
void LCD1602_WriteInformation(unsigned char Data,bit bComOrData); //写数据或者写命令
void LCD1602_Init(void); //初始化
void LCD1602_MoveTo(unsigned char x,unsigned char y); //坐标指定到位置
void LCD1602_DisplayString(unsigned char *str); //显示字符串
unsigned char ADC0832_ReadValue(bit ch); //读取相应通道的转换值
/******************* http://www.wang1jin.com **********************
* 函数名称: main
* 功能描述: main函数,程序入口
* 输入参数: 无
* 返回参数: 无
**********************************************************************/
void main(void)//测试用MAIN函数
{
LCD1602_Init();
LCD1602_MoveTo(1,0);
LCD1602_DisplayString(Forum);
while(1)
{
//LCD1602_Init();
LCD1602_MoveTo(0,0);
LCD1602_DisplayString("H0:");
temp = ADC0832_ReadValue(0);
result = temp*0.01953125;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1);
LCD1602_WriteInformation('.',1);
result = (result-temp)*10;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1);
result = (result-temp)*10;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1); //显示通道0的电压转换值
LCD1602_MoveTo(0,8);
LCD1602_DisplayString("H1:");
temp = ADC0832_ReadValue(1);
result = temp*0.01953125;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1);
LCD1602_WriteInformation('.',1);
result = (result-temp)*10;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1);
result = (result-temp)*10;
temp = (unsigned char )(result);
LCD1602_WriteInformation(temp+48,1); //显示通道1的电压转换值
}
}
/******************* http://www.wang1jin.com **********************
* 函数名称: LCD1602_CheckBusy
* 功能描述: 忙检测
* 输入参数: 无
* 返回参数: 无
**********************************************************************/
void LCD1602_CheckBusy(void) //read if busy
{
unsigned char i;
i = 250;
P0 = 0xff; //读之前先置位
LCD_rs = 0;
LCD_rw = 1;
LCD_en = 1;
while((i--) && (P0 & 0x80)); //忙检测
LCD_en = 0;
}
/******************* http://www.wang1jin.com **********************
* 函数名称: LCD1602_WriteInformation
* 功能描述: 写信息
* 输入参数: Data;bConOrData
* 返回参数: 无
**********************************************************************/
void LCD1602_WriteInformation(unsigned char Data,bit bComOrData) //rs = 0:command; rs = 1:data
{
LCD1602_CheckBusy();
P0 = Data;
LCD_rs = bComOrData;
LCD_rw = 0;
LCD_en = 1;
//delay(5);
LCD_en = 0;
}
/******************* http://www.wang1jin.com **********************
* 函数名称: LCD1602_Init
* 功能描述: 初始化
* 输入参数: 无
* 返回参数: 无
**********************************************************************/
void LCD1602_Init(void) //init 1602
{
LCD1602_WriteInformation(0x38,0);
Delay(1500);
LCD1602_WriteInformation(0x38,0);
Delay(500);
LCD1602_WriteInformation(0x38,0);
Delay(500);
LCD1602_WriteInformation(0x38,0);
Delay(150);
LCD1602_WriteInformation(0x0c,0);
Delay(100);
LCD1602_WriteInformation(0x06,0);
Delay(100);
LCD1602_WriteInformation(0x01,0);
Delay(100);
}
/******************* http://www.wang1jin.com **********************
* 函数名称: LCD1602_MoveTo
* 功能描述: 坐标指定到位置
* 输入参数: x,y
* 返回参数: 无
**********************************************************************/
void LCD1602_MoveTo(unsigned char x,unsigned char y)
{
if(0 == x)
LCD1602_WriteInformation((0x80 | y),0);
if(1 == x)
LCD1602_WriteInformation((0xC0 | y),0);
}
/******************* http://www.wang1jin.com **********************
* 函数名称: LCD1602_DisplayString
* 功能描述: 显示字符串
* 输入参数: *str
* 返回参数: 无
**********************************************************************/
void LCD1602_DisplayString(unsigned char *str) //显示字符串
{
while(*str != '\0')
{
LCD1602_WriteInformation(*str,1);
str++;
}
}
/******************* http://www.wang1jin.com **********************
* 函数名称: Delay
* 功能描述: 延时函数
* 输入参数: uiCount
* 返回参数: 无
**********************************************************************/
void Delay(unsigned int uiCount)
{
while(uiCount--);
}
/******************* http://www.wang1jin.com **********************
* 函数名称: ADC0832_ReadValue
* 功能描述: 获取ADC转换数值
* 输入参数: ch
* 返回参数: data1
**********************************************************************/
unsigned char ADC0832_ReadValue(bit ch)
{
unsigned char i,data1 = 0,data2 = 0;
ADC0832_CLK = 0;
ADC0832_DODI = 1;
ADC0832_CS = 0;//ADC0832_CS = 0时ADC0832有效
ADC0832_CLK = 1;
ADC0832_CLK = 0;//第一个脉冲,开始位
ADC0832_DODI = 1;
ADC0832_CLK = 1;
ADC0832_CLK = 0;//第二个脉冲,模式选择
ADC0832_DODI = ch;//通道选择
ADC0832_CLK = 1;
ADC0832_CLK = 0; //第三个脉冲,通道选择
ADC0832_DODI = 1;
for(i = 0;i<8;i++) //第一次读数从高到低 ,时钟下降沿有效
{
ADC0832_CLK = 1;
ADC0832_CLK = 0;
if(ADC0832_DODI)data1 |= 0x80>>i;
}
for(i = 0;i<8;i++) //第二次从低到高读数,下降沿有效
{
if(ADC0832_DODI)data2 |= 0x01<<i;
ADC0832_CLK = 1;
ADC0832_CLK = 0;
}
ADC0832_CS = 1;
ADC0832_DODI = 1;
ADC0832_CLK = 1;
if(data1 == data2)
return(data1);
else
return 0;
}
文章评论(0条评论)
登录后参与讨论