程序代码:
#include <reg52.h>
#include <intrins.h> /*库函数*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define N 0x0fff //十二位转换
sbit STS = P1^0;
sbit CS = P1^1;
sbit A0 = P1^2;
sbit RC = P1^3;
sbit CE = P1^4;
unsigned long result=0;
unsigned char seg[ ]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char buf[4];
char str[12];
void delay(n)
{
int i;
while(n--)
for(i=0;i<10;i++);
}
void inti()
{
CS=1;
CE=0;
}
//串初始化函数
void init_com( void )
{
ES=0;// 禁止串口中断
P0 = 0xff; //端口初始化
P1 = 0xff;
P2 = 0xff;
TMOD=0x21;
TH1=0Xfd;
TL1=0xfd;
SCON=0x50;
PCON=0x00;
IE=0x90;
ET0=1; //启用Timer0中断
TR0 = 1;
ET0 = 1;
TR0=1; //start T/C1
TR1=1; //start T/C1
REN=1; // 允许串口接收数据
ES=1;// 允许串口中断
EA=1;
}
/**********************************************************
整数转数字字符串子函数
**********************************************************/
void integer_string(unsigned long num)
{
unsigned char i = 0;
while(num)
{
str = num%10 + '0'; //从整数的最低位开始
i++;
num = num/10;
}
str = 0; //添加字符串结束符
}
//向串口发送一个字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 ) ;
TI= 0 ;
}
//***********************************************************************************
//向串口发送一个字符串,strlen为该字符串长度
void send_string_com( unsigned char *str, unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
} while (k < strlen);
send_char_com(13);
send_char_com(10);
}
//***********************************************************************************
unsigned int ad1674_convert()
{
unsigned int temp=0;
//STS=1;
inti();
CS=0;
delay(1);
A0=0;
delay(1);
RC=0;//
delay(1);
CE=1;
delay(1);
//while(STS==1);
{
CE=0;
delay(1);
RC=1;
delay(1);
A0=0;
delay(1);
CE=1;
delay(1);
temp=P0;
temp=P0<<4;
CE=0;
delay(1);
RC=1;
delay(1);
A0=1;
delay(1);
CE=1;
delay(1);
temp+=P0;
//temp+=P0>>4;
}
CE=0;
CS=1;
return temp;
}
void main()
{
init_com();
while(1)
{
result=ad1674_convert();
integer_string(result);
send_string_com(str,strlen(str));
}
}
我要做的是一个信号探测,设计思路是信号经过AD1674转换后送入单片机89S52,当信号大到一定程度时发出警报,可是现在无论信号怎么变化,经过AD转换后的数值都不变化,不知道怎么回事,请高人指点 |