?? 7705a.c
字號:
#include<reg51.h>
#include<absacc.h>
#include<intrins.h>
#include<math.h>
//#include<3310.h>
sbit D_DAT=P1^0; //這一段是用來指定數顯的,我用的是周立功dp-51開發機,該開發機有4個數碼管
sbit D_CLK=P1^1;
sbit DISPB0=P1^7;
sbit DISPB1=P1^6;
sbit DISPB2=P1^5;
sbit DISPB3=P1^4;
sbit DISPB4=P1^3;
sbit ADC_DIN=P3^0; //這一段是指定ad7705的信號引腳
sbit ADC_CLK=P3^1; //指定時鐘引腳
sbit ADC_DRDY=P3^2; //指定轉換標志位
float voltage; //這個全局變量是用來給數顯子程序傳遞采樣到的數據的
unsigned char data display_buffer[5]; //顯示緩沖
unsigned char code led[17]= //顯示段碼表
{0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xFF};
/*****************************************/
void send(unsigned char a) //通過P1口給數顯發送8位數據
{
unsigned char i;
for(i=0;i<8;i++)
{
if(_crol_(a,i)&0x80)
D_DAT=1;
else
D_DAT=0;
D_CLK=0;
D_CLK=1;
}
}
/*****************************************/
void DISP_DLEAY(void) //顯示延遲子程序
{
unsigned char i,j;
for(i=0;i<10;i++)
for(j=0;j<5;j++);
}
/*****************************************/
/*void disp(void) //給數碼管發顯示緩沖區的段碼
{
unsigned char a;
a=0xff;
send(a);
//DISPB0=0;
DISP_DLEAY();
//DISPB0=1;
a=led[display_buffer[1]];
send(a);
//DISPB1=0;
DISP_DLEAY();
//DISPB1=1;
a=led[display_buffer[2]];
send(a);
//DISPB2=0;
DISP_DLEAY();
//DISPB2=1;
a=led[display_buffer[3]];
send(a);
//DISPB3=0;
DISP_DLEAY();
//DISPB3=1;
a=led[display_buffer[4]];
send(a);
//DISPB4=0;
DISP_DLEAY();
//DISPB4=1;
}
*?
/*****************************************/
void display() /*將數據轉換成相應的段碼,
并給顯示緩沖區賦段碼*/
{
float y,z,x;
x=voltage;
x=x/10.0;
y=modf(x,&z);
display_buffer[1]=(unsigned char)z;
x=10.0*y;
y=modf(x,&z);
display_buffer[2]=(unsigned char)z;
x=10.0*y;
y=modf(x,&z);
display_buffer[3]=(unsigned char)z;
x=10.0*y;
y=modf(x,&z);
display_buffer[4]=(unsigned char)z;
/*disp();
disp();
disp();
disp();
disp();*/
}
/****************************************************************/
unsigned char rearrange(unsigned char a) /*將8位數據a的順序顛倒后賦給b。
因為串口發送順序和ad7705的接收順序剛好相反*/
{
unsigned char i,b;
b=a&0x01;
for(i=1; i<8; i++)
{
b=b<<1;
a=a>>1;
b=b+(a&0x01);
}
return(b);
}
/****************************************************************/
void WriteToReg_ADC(unsigned char a) //通過串口給ad7705發送命令字a
{
SCON=0;
TI=0;
SBUF=a;
while(!TI);
}
/*************************************************************/
void MX7705_Init() //ad7705初始化
{
unsigned char i;
ADC_CLK=1; //防止接口迷失
ADC_DIN=1;
for(i=0;i<100;i++)//prevent interface from losting
{
ADC_CLK=0;
ADC_CLK=1;
}
WriteToReg_ADC(0x04); //write 0x20 to communication register to choose channel 0
//and clock register for the next one to write
WriteToReg_ADC(0x20); //寫0x04到時鐘寄存器,指定晶振頻率為2.4576MHz,采樣率為50Hz.
WriteToReg_ADC(0x08); //write 0x10 to communication register to choose channel 0
//and setup register for the next one to write
WriteToReg_ADC(0x22); //寫0x44到設置寄存器,指定增益為1, buffer off, FSYNC=0, and self-calibration
}
/*************************************************************/
unsigned int ReadWord()
{
unsigned char high8,low8;
unsigned int out;
SCON=0;
REN=1; //allow serial port to recieve data
RI=0;
while(!RI); //waiting the end of recieve of 8 bit
high8=SBUF;
ADC_CLK=1;
RI=0;
while(!RI);
low8=SBUF;
REN=0;
out=rearrange(high8);
out=out<<8;
out=out+rearrange(low8);
return(out);
}
/*************************************************************/
/*************************************************************/
main()
{
unsigned int data_out;
MX7705_Init();
while(ADC_DRDY==0)
{
WriteToReg_ADC(0x1C); //給通訊寄存器發0x38,指定下一個讀數據寄存器
data_out=ReadWord(); //從數據寄存器中讀16位數據
voltage=5.0*(data_out/65536.0); //將所讀數據轉換為電壓值
display();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -