/*************************************************************
将0832 两种单项通道的结果输出,adc-data,ada-data1前后采样的比较值最后一位有误差的
cs低电平开始采样,高电平结束一次转换,每次转换都是两次采样,msb和lsb可以两个都要,
也可以只要其中一组,由cs拉高决定。
clk一入低电平,开始接收和发送,然后开始低电平维持。高电平开始和高电平维持不做动作。
DO和DI共用一条线,输入输出同时使用。单片机做输入口时,必须先给电平拉高
DI有三个输入脉冲(输入输出对0832来说),然后做DO使用,拉高一个高电平等待
在8BIT的最后一位,LSB,MSB共用的。
****************************************************************/
#include <at89x52.h>
#include<intrins.h>
#define CS P1_5
#define CLK P1_6
#define DI P1_7
#define DO P1_7
#define LCM_RS P1_2
#define LCM_RW P1_1
#define LCM_E P1_0
#define LCM_Data P2
#define Busy 0x80
unsigned char adc_data;
unsigned char adc_datal;
void WriteDataLCM(unsigned char WDLCM);
void WriteCommandLCM(unsigned char WCLCM,BuysC);
unsigned char ReadStatusLCM(void);
void LCMInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
void Delay5Ms(void);
void Delay400Ms(void);
void nop(void)
{
_nop_();
_nop_();
}
void clock(void)
{
nop();
CLK=1;
nop();
CLK=0;
}
void adc0832(unsigned char ch)
{
unsigned char adc_datatemp=0;
unsigned char i;
ch|=0x03; //ch=3,通道1 ;ch=2,通道0
adc_data=0;
CLK=0;
DI=1;
CS=0;
clock();
DI=((ch|0x02)>>1);
clock();
DI=(ch|0x01);
clock();
DI=1;
clock();
for(i=0;i<7;i++)
{
adc_data|=DO;
adc_data<<=1;
clock();
}
adc_data|=DO;
//adc_data|=0xf0;
adc_datal|=DO;
clock();
for(i=0;i<6;i++)
{
adc_datatemp=((adc_datatemp=DO)<<7);
adc_datal|=adc_datatemp;
adc_datal>>=1;
clock();
}
adc_datatemp=((adc_datatemp=DO)<<7);
adc_datal|=adc_datatemp;
clock();
CS=1;
nop();
}
//unsigned char code Y1[]={31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46};
//unsigned char code Y2[]={109,101,103,32,105,32,108,105,107,101,32,109,111,117,32,0};
void WriteDataLCM(unsigned char WDLCM) {
ReadStatusLCM(); //检测忙
LCM_RS = 1; //产生写操作时序
LCM_RW = 0;
_nop_();
LCM_Data = WDLCM;
_nop_();
LCM_E = 1; //若晶振速度太高可以在这后加小的延时
_nop_();_nop_();
LCM_E = 0; //延时
}
void WriteCommandLCM(unsigned char WCLCM,BuysC){
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_RS = 0; //产生写指令时序
LCM_RW = 0; //RW=0,时序写
_nop_();
LCM_Data = WCLCM;
_nop_();
LCM_E = 1; //下降沿
_nop_();_nop_();
LCM_E = 0; //E置位
}
unsigned char ReadStatusLCM(void) {
LCM_Data = 0xFF;
LCM_RS = 0; //rs=0
LCM_RW = 1; //rw=1
_nop_();
LCM_E = 1; //LCM_E=1
_nop_(); // DB7=0 LCD控制器空闲 , DB7=1 LCD控制器忙
_nop_();
while (LCM_Data & Busy); //检测忙信号
return(LCM_Data);
}
void LCMInit(void) {
LCM_Data = 0;
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,0);
Delay5Ms();
WriteCommandLCM(0x38,1);
WriteCommandLCM(0x08,1);
WriteCommandLCM(0x01,1);
WriteCommandLCM(0x06,1);
WriteCommandLCM(0x0C,1);
}
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData) {
Y &= 0x1;
X &= 0xF;
if (Y) X |= 0x40;
X |= 0x80;
WriteCommandLCM(X, 0);
WriteDataLCM(DData);
}
//按指定位置显示一串字符
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData) {
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF;
//while (DData[ListLength]>0x20) {
while (ListLength<0x0f) {
if (X <= 0xF) {
DisplayOneChar(X, Y, DData[ListLength]);
ListLength++;
X++;
}
}
}
//5ms延时
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//400ms延时
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
void main(void)
{unsigned char j;
for(j=0;j<21;j++)
{
adc0832(3);
Delay400Ms(); }
LCMInit();
Delay5Ms();
DisplayOneChar(1, 0, adc_datal);
DisplayOneChar(0, 0, adc_data);
//DisplayListChar(0, 1, Y2);
while(1);
}