ADC0832
环境:
软件:
芯片
仿真图

相关函数编写电平时
代码
1602
头文件
#include<reg51.h>
#include<intrins.h>
#include<string.h>
宏定义
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
初始化
void init()
{
write_cmd(0x38);
write_cmd(0x01);
write_cmd(0x06);
write_cmd(0x0C);
}
检测BF(busy flag)位状态
void test_BF()
{
unsigned char LCD_status;
do{
P0 = 0xFF;
EN = 0; RS = 0; RW = 1;
EN=1;
LCD_status = P0;
_nop_(); _nop_();
EN = 0;
}while(LCD_status&0x80);
}
写数据
void write_data(unsigned char data8)
{
test_BF();
EN = 0; RS = 1; RW = 0;
P0 = data8;
EN = 1; _nop_(); EN = 0;
}
写命令
void write_cmd(unsigned char cmd8)
{
test_BF();
EN = 0; RS = 0; RW = 0;
P0 = cmd8;
EN = 1; _nop_(); EN = 0;
}
写字符串
void write_str(int r, int c, char *str)
{
int i=0;
unsigned char Addressx[] = {0x80, 0xC0};
unsigned char StartAdd = (Addressx[r] | c);
write_cmd(StartAdd);
for(i = 0; i < 16; i++){
if(str[i]==0) break;
write_data(str[i]);
}
for(;i < 16; i++){
write_data(' ');
}
}
0832
头文件
sbit CS = P3^2;
sbit CLK = P3^3;
sbit DIDO = P3^4;
转换方法
uchar Get_AD_Result()
{
uchar i, dat;
CS = 1;
_nop_;
CLK =1;
CS = 0;
DIDO = 1; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 1; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 0; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 1;
for(i = 0; i < 8; i++)
{
CLK = 0;
_nop_;
if(DIDO) dat |= 0x01;
CLK = 1; _nop_;
dat <<= 1;
}
return (dat);
CS = 1;
}
主函数编写
头文件
#include <reg52.h>
#include <1602.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
宏定义
sbit CS = P3^2;
sbit CLK = P3^3;
sbit DIDO = P3^4;
LCD1602.h
#include <reg52.h>
#define LCD1602_DB P2
sbit LCD1602_RS = P1^0;
sbit LCD1602_RW = P1^1;
sbit LCD1602_E = P1^2;
void InitLcd1602();
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str);
void LcdStar()
{
unsigned char str[] = "Voltage measure";
unsigned char tab[]="Voltage= ";
InitLcd1602();
LcdShowStr(1, 0, str);
LcdShowStr(1, 1, tab);
LcdShowStr(9, 1, "...");
LcdShowStr(13, 1, "V");
}
void delay(int z){
int x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void LcdWaitReady()
{
unsigned char LCD_status;
do{
LCD1602_DB = 0xFF;
LCD1602_E = 0; LCD1602_RS = 0; LCD1602_RW = 1;
LCD1602_E=1;
LCD_status = LCD1602_DB;
_nop_(); _nop_();
LCD1602_E = 0;
}while(LCD_status&0x80);
}
void LcdWriteCmd(unsigned char cmd)
{
LcdWaitReady();
LCD1602_RS = 0;
LCD1602_RW = 0;
LCD1602_DB = cmd;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdWriteDat(unsigned char dat)
{
LcdWaitReady();
LCD1602_RS = 1;
LCD1602_RW = 0;
LCD1602_DB = dat;
LCD1602_E = 1;
LCD1602_E = 0;
}
void LcdSetCursor(unsigned char x, unsigned char y)
{
unsigned char addr;
if (y == 0)
addr = 0x00 + x;
else
addr = 0x40 + x;
LcdWriteCmd(addr | 0x80);
}
void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str)
{
LcdSetCursor(x, y);
while (*str != '\0')
{
LcdWriteDat(*str++);
}
}
void InitLcd1602()
{
LcdWriteCmd(0x38);
LcdWriteCmd(0x0C);
LcdWriteCmd(0x06);
LcdWriteCmd(0x01);
}
Main_0832.c
#include <reg52.h>
#include <1602.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit CS = P3^2;
sbit CLK = P3^3;
sbit DIDO = P3^4;
uchar len;
uchar Display_Buffer[4];
uchar Get_AD_Result();
void DelayMS(uint ms)
{
uchar t;
while(ms--)
{
for(t=0;t<120;t++);
}
}
uchar Get_AD_Result()
{
uchar i, dat;
CS = 1;
_nop_;
CLK =1;
CS = 0;
DIDO = 1; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 1; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 0; CLK = 0; _nop_; CLK = 1; _nop_;
DIDO = 1;
for(i = 0; i < 8; i++)
{
CLK = 0;
_nop_;
if(DIDO) dat |= 0x01;
CLK = 1; _nop_;
dat <<= 1;
}
return (dat);
CS = 1;
}
void main()
{
uint Data;
InitLcd1602();
LcdStar();
DelayMS(10);
while(1)
{
Data = Get_AD_Result()*500.0/256;
Display_Buffer[0]= Data /100+'0';
Display_Buffer[1] = '.';
Display_Buffer[2] = Data /10%10+'0';
Display_Buffer[3] = Data %10+'0';
LcdShowStr(9, 1,Display_Buffer);
}
}