编码与解码
ADC0832由于体积小,兼容性强,性价比高而深受单片机爱好者及企业欢迎,其目前已经有很高的普及率。学习并使用ADC0832可使我们了解A/D转换器的原理,有助于我们单片机技术水平的提高。本文为大家介绍几个adc0832简单应用c程序。
uchar ADC0832_Read(uchar ch)
{
/*读取ADC的 CH 通道电压值,ADC作为2路单独电压输入*/
uchar i;
uchar ADC_buff=0;
uchar temp=0;
EA = 0;
ADC0832_CS = 1;
ADC0832_DIO = 1; /*启动位*/
ADC0832_CLK = 0;
ADC0832_CS = 0;
nop();
ADC0832_CLK = 1;
nop();
ADC0832_CLK = 0;
ADC0832_DIO = 1; /*送 SGL/DIF 位 */
nop();
ADC0832_CLK = 1;
nop();
ADC0832_CLK = 0;
if(ch==0) ADC0832_DIO = 0; /*送转换通道值*/
else ADC0832_DIO = 1;
ADC0832_CLK = 1;
nop();
ADC0832_CLK = 0;
nop();
ADC0832_DIO = 1; /*释放DIO端口,转由ADC控制*/
nop();
ADC0832_CLK = 1;
nop();
for(i=0;i《8;i ){ /*准备读取8位数据*/
nop();
ADC0832_CLK = 0;
nop();
nop();
ADC_buff=ADC_buff《《1;
if(ADC0832_DIO==1) ADC_buff=ADC_buff 1; /*读取数据*/
ADC0832_CLK = 1;
}
for(i=0;i《8;i ){
temp = temp》》1;
if(ADC0832_DIO==1) temp = temp | 0x80; /*读取校验数据*/
ADC0832_CLK = 1;
nop();
ADC0832_CLK = 0;
nop();
}
ADC0832_CS = 1;
ADC0832_CLK = 1;
//if(temp == ADC_buff) ADC_flag = 1;
//else ADC_flag = 0;
EA = 1;
return ADC_buff; /*返回转换值*/
}
C程序如下:
#include
#include
#include
/**********************************8/
/**********LCD1602接口程序**********/
#define DD P2
sbit Rs=P3^0;
sbit Rw=P3^1;
sbit E=P3^2;
sbit busy_p=ACC^7;
/********************************/
void delay_1ms(unsigned char i) //最小延时1ms
{ unsigned char j;
while(i--)
for(j=0;j《125; j++);
}
void delay_10ns(unsigned char i) //最小延时10ns
{ unsigned char j;
while(i--)
for(j=0;j《10; j++);
}
void write_com(unsigned char com,bit p) //写指令
{if(p)
delay_10ns(5);
E=0;
Rs=0;
Rw=0;
DD=com;
delay_10ns(50); //》40ns
E=1;
delay_1ms(2); //》150ns
E=0;
delay_10ns(4); //》25+10ns
}
void write_date(unsigned char DATA) //写数据
{
delay_10ns(50);
E=0;
Rs=1;
Rw=0;
DD=DATA;
delay_10ns(50);
E=1;
delay_10ns(50);
E=0;
delay_10ns(4);
}
void addr_x_y(unsigned char x,bit y) //写坐标,定位置
{ unsigned char temp=0x80;
if(y)
{temp|=0x40;}
temp|=x;
write_com(temp,0);
}
void desplay_char(unsigned char x,bit y,unsigned char p)
//在指定位置显示一个字符。
{ addr_x_y(x,y);
write_date(p);
}
void init(void)
{delay_1ms(15);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,0);
delay_1ms(5);
write_com(0x38,1);
write_com(0x08,1);
write_com(0x01,1);
write_com(0x06,1);
write_com(0x0c,1);
}
void xs_int(unsigned int shuju,bit t) //显示一个数字
{unsigned char huancun[6]={0};
unsigned char biaozhi=0,i;
if (shuju 《 10) biaozhi = 1;
else if(shuju 《 100) biaozhi = 2;
else if(shuju 《 1000) biaozhi = 3;
else if(shuju 《 10000) biaozhi = 4;
else if(shuju 《 65535) biaozhi = 5;
switch(biaozhi)
{case 5:huancun[5] = shuju/10000;
case 4:huancun[3] = shuju%10000/1000;
case 3:huancun[2] = shuju%1000/100;
case 2:huancun[1] = shuju%100/10;
case 1:huancun[0] = shuju%10;
break;
default:break;
}
for(i=6;i》1;i--)
{if(i==5)desplay_char(10,1,‘。’);
else desplay_char(15-i,t,0x30+huancun[i-1]); }
desplay_char(15,t,‘V’);
}
/************************************************************/
/**********ADC0832接口程序************************************/
sbit ADC_CS =P3^4;
sbit ADC_CLK=P3^5;
sbit ADC_DO =P3^6;
sbit ADC_DI =P3^7;
/*******************************************************************/
void Delay(unsigned char j)
{
unsigned char i;
for(i=0;i《j;i++); 延时,脉冲一位持续的时间《=“” p=“”》
}
unsigned char ADC0832(void) //把模拟电压值转换成8位二进制数并返回
{
unsigned char i,data_c;
data_c=0;
ADC_CS=0;
ADC_DO=0;//片选,DO为高阻态
for(i=0;i《10;i++)
{;}
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第一个脉冲,起始位
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第二个脉冲,DI=1表示双通道单极性输入
ADC_CLK=0;
Delay(2);
ADC_DI=1;
ADC_CLK=1;
Delay(2); //第三个脉冲,DI=1表示选择通道1(CH2)
ADC_DI=0;
ADC_DO=1;//DI转为高阻态,DO脱离高阻态为输出数据作准备
ADC_CLK=1;
Delay(2);
ADC_CLK=0;
Delay(2);//经实验,这里加一个脉冲AD便能正确读出数据,
//不加的话读出的数据少一位(最低位d0读不出)
for (i=0; i《8; i++)
{
ADC_CLK=1;
Delay(2);
ADC_CLK=0;
Delay(2);
data_c=(data_c《《1)|ADC_DO;//在每个脉冲的下降沿DO输出一位数据,最终ch为8位二进制数
}
ADC_CS=1;//取消片选,一个转换周期结束
return(data_c);//把转换结果返回
}
void main(void)
{
unsigned int data_temp=0;
while(1)
{ data_temp=ADC0832();
init();
xs_int(196*data_temp,1);
}
}
#include《reg51.h》
sbit P10=P1^0;
void main()
{ EA=1;ET1=1;TR0=1;
TMOD=01;
TH0=65536-2000/256;
TL0=65536-2000%256;
while(1)
{
P10=~P10;
}
}
void NT1() interrupt 3
{
TH0=65536-2000/256;
TL0=65536-2000%256;
}
#include 《AT89X51.H》
#include《absacc.h》#define PP XBYTE[0x7fff]
#define uint unsigned int
uint i,j,k;
void delay(uint z);
void main()
{
while(1)
{
if(P1_0==0)
{ delay(10);
if(P1_0==0)
{ while(P1_2!=0&&P1_1!=0)
{ P1_3=0;
for(i=255;i》=0;i--)
}
while(P1_0!=1);
} }
if(P1_1==0)
{ delay(10);
if(P1_1==0)
{ P1_4=0;
while(P1_0!=0&&P1_2!=0)
{ for(j=0;j《=254;j++)
for(j=255;j》=0;j--)
}
while(P1_1!=1); }}
if(P1_2==0)
{ delay(10);
if(P1_2==0)
{ while(P1_0!=0&&P1_1!=0)
{P1_5=0;
PP=255;
delay(10);
PP=0;
delay(10);} } }
} }
void delay(uint z)
{ uint x,y;
for(x=z;x》0;x--)
for(y=120;y》0;y--);
}
全部0条评论
快来发表一下你的评论吧 !