📄 a test.c
字号:
//--------------------------------------------Copyright(C)-----------------------------------------------
// ------------------------------------------文件信息------------------------------------------------
// 文件
// 创建人
// 描述
// 最后修改日期
//---------------------------------------------------------------------------------------------------
//-----------------------函数声明,变量定义--------------------------------------------------------
#include <reg51.h>
#include <absacc.h>
#define uint unsigned int
#define uchar unsigned char
#define En 1 //使能
#define Dis 0 //关闭
#define COUNT 8 //测量数据长度
#define AD_H P2 //定义AD读取高位
#define AD_L P0 //定义AD读取低位
#define autotest 0x5a //自动测试请求标志
#define handtest1 0x5d //手动线路1测试请求标志
#define handtest2 0x5e //手动线路2测试请求标志
#define handtest3 0x5f //手动线路3测试请求标志
#define handtest4 0x60 //手动线路4测试请求标志
#define handtest5 0x61 //手动线路5测试请求标志
#define handtest6 0x62 //手动线路6测试请求标志
#define handtest7 0x63 //手动线路7测试请求标志
#define handtest8 0x64 //手动线路8测试请求标志
#define handtest9 0x65 //手动线路9测试请求标志
#define resent 0x50 //重发请求标志
sbit CS =P1^0; //AD使能--接AD976
sbit RC =P1^2; //AD转换触发控制--接AD976
sbit Ctrlu =P1^1; //5V电压源接通控制--接ADG801
sbit Ctrl2 =P1^3; //1mA电流源接通控制--接ADG801
//sbit Ctrl3 =P3^4; //线路1干扰源接通控制--接ADG801
//sbit LED1 =P1^4; //测试灯1
//sbit LED2 =P1^7; //测试灯2
//sbit LED3 =P3^2; //测试灯3 (以上三个LED为了检查程序时候用)
sbit TESTA =P1^5; //R1电压测试点A接通控制--接ADG801
sbit TESTB =P1^6; //R1+R2电压测试点B接通控制--接ADG801
sbit TESTC =P3^5; //正弦电压峰峰值测试点C接通控制--接ADG801
sbit BusyBit = P3^3; //INT1中断请求判断
sbit relay1 =P1^7; //线路2干扰源接通控制--接relay1
sbit relay3 =P1^4; //relay3
sbit relay5 =P3^7; //relay5
sbit relay4 =P3^6; //relay4
sbit relay2 =P3^2; //relay2
//变量===================================================================
unsigned char ad_dataH,ad_dataL; //AD读取值
unsigned char buffer2[COUNT]={
0x00, //B点电压低8位
0x00, //B点电压高8位
0x00, //A点电压低8位
0x00, //A点电压高8位
0x00, //C点(最大)峰峰值低8位
0x00, //C点(最大)峰峰值高8位
0x00, //校验位或线号
0x00, //校验位
};
unsigned char WFlag = 0; //定义工作状态
unsigned char RECEIVR_buffer; //串口接收BUFFER
unsigned char MFlag = 0; //定义手动和自动模式区分位
//函数===============================================================================
void UART_init(); //串口初始化函数
void COM_send(unsigned char len); //串口发送函数
uint read_ad(void); //AD读操作
void HandTestV(); //手动测量操作处理函数
void delay(unsigned int N); //延时子程序
uint ctestv(); //C点测试子程序
void AutoTestV(); //自动测量操作处理函数
//--------------------------------------------------------------------------------------------------
// 函数名称: UART_init()串口初始化函数
// 函数功能: 在系统时钟为11.0592MHZ时,设定串口波特率为9600bit/s
// 串口接收中断允许,发送中断禁止
//--------------------------------------------------------------------------------------------------
void UART_init()
{
//初始化串行口和波特率发生器
SCON =0x51; //选择串口工作方式,打开接收允许
TMOD |=0x20;
TCON =0x04; //定时器1工作在方式2,定时器0工作在方式0
TH1 =0xfd; //实现波特率9600b/s(系统时钟11.0592MHZ)
TL1 = 0xfd;
TR1 =En; //启动定时器T1
ET1 =Dis; //关闭定时器T1中断
ES =En; //允许串行口中断
PS =En; //设计串行口中断优先级
EA =En; //单片机中断允许
}
//--------------------------------------------------------------------------------------------------
// 函数名称: com_interrup()串口接收中断处理函数
// 函数功能: 接收包括校验位在内的七个字节数据到数据缓冲区
//--------------------------------------------------------------------------------------------------
com_interrupt(void) interrupt 4 using 3
{
if(RI) //处理接收中断
{
RI=0; //清除中断标志位
RECEIVR_buffer = SBUF; //接收串口数据 //jjjj=SBUF;
if(RECEIVR_buffer==autotest) //如果是自动测量请求
{
WFlag=autotest;
MFlag=0x55; //建立自动测量请求标志和自动模式标志
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest1) //如果是手动线路1测量请求
{
WFlag=handtest1; //建立手动线路1测量请求标志
MFlag=0xaa; //建立手动模式标志
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest2) //如果是手动线路2测量请求
{
WFlag=handtest2; //建立手动线路2测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest3) //如果是手动线路3测量请求
{
WFlag=handtest3; //建立手动线路3测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest4) //如果是手动线路4测量请求
{
WFlag=handtest4; //建立手动线路4测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest5) //如果是手动线路5测量请求
{
WFlag=handtest5; //建立手动线路5测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest6) //如果是手动线路6测量请求
{
WFlag=handtest6; //建立手动线路6测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest7) //如果是手动线路7测量请求
{
WFlag=handtest7; //建立手动线路7测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest8) //如果是手动线路8测量请求
{
WFlag=handtest8; //建立手动线路8测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==handtest9) //如果是手动线路9测量请求
{
WFlag=handtest9; //建立手动线路9测量请求标志
MFlag=0xaa;
ES=0; //关串口中断
}
else if(RECEIVR_buffer==resent) //如果是重发请求
{
WFlag=resent; //建立重发请求标志
ES=0; //关串口中断
}
}
if(TI) //处理发送中断
{
TI=0;
}
}
//--------------------------------------------------------------------------------------------------
// 函数名称: COM_send()串口发送函数
// 函数功能: 把数据缓冲区的七个字节的数据发送出去
//--------------------------------------------------------------------------------------------------
void COM_send(unsigned char len)
{
unsigned char poi;
for(poi=0;poi<len;poi++) //把缓存区的七个(或八个)字节数据都发送到串口
{
SBUF=buffer2[poi];
while(TI==0){;} //等待发送完毕
TI=0; //清除标志位
}
}
/**********************************************************************************/
//--------------------------------------------------------------------------------------------------
// 函数名称: uint LvBo()软件滤波函数
// 函数功能: 连续测量7个值,然后比较出最大最小值,并将其去掉,求剩余的平均值
//--------------------------------------------------------------------------------------------------
uint LvBo(uint *PadData)
{
uchar i;
uint tempMax,tempMin;
unsigned long temp;
temp = 0;
for(i=0; i<7; i++) //连续测试7个数据
{
if(*(PadData+i) > 0x8000) //判断测试的数据是否负值
*(PadData+i) = 0; //舍弃负值
temp += *(PadData+i); //求和
}
tempMax=tempMin=*PadData;
for(i=1; i<7; i++)
{
if(tempMax < *(PadData+i))
tempMax = *(PadData+i); //判断最大值
if(tempMin > *(PadData+i))
tempMin = *(PadData+i); //判断最小值
}
temp = temp - tempMax - tempMin;
temp = (uint) (temp/5); //舍弃最大最小值求剩余的平均值
return temp;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: ctestv()
// 函数功能: 完成对c点电压的测量
//--------------------------------------------------------------------------------------------------
uint ctestv()
{
unsigned int i;
// float j;
// uint temp1;
// uint temp2;
uint temp;
uint Ad_Buffer[7];
// delay(200);
// TESTC=1; //接通干扰测试点C
// delay(3000);
// delay(1000);
for(i=0; i<7; i++)
{
Ad_Buffer[i] = read_ad(); //读AD
delay(50); //延时
}
temp = LvBo(&Ad_Buffer[0]);
if ((temp>0x006c) && (temp<0x0093))
{
temp =temp - 0x0069;
}
// TESTC=0; //关闭相应测试点
// relay1=0;
// relay2=0;
// relay3=0;
// relay4=0;
// relay5=0;
delay(500);
// j = (float) (temp1);
// temp2 = (uint)(j *2.82843);
// temp = temp2 - 0x0031+0x83;
return temp;
}
//--------------------------------------------------------------------------------------------------
// 函数名称: HandTestV()手动测量操作函数
// 函数功能: 在系统时钟为11.0592MHZ时
//--------------------------------------------------------------------------------------------------
void HandTestV()
{
unsigned int i;
uint Ad_Buffer[7];
uint HighValue;
uint Lowvalue;
uint temp;
HighValue=0x0;
Lowvalue=0x7fff;
// Ctrlu=0; //初始化控制测试端口
// Ctrl2=0;
// relay1=0;
// relay2=0;
// relay3=0;
// relay4=0;
// relay5=0;
// TESTB=0;
// TESTA=0;
// TESTC=0;
// delay(1000);
Ctrl2=1; //接通1mA电流源
delay(3000);
TESTB=1; //接通B点
delay(3000); //延时等待电压建立
for(i=0; i<7; i++)
{
Ad_Buffer[i] = read_ad(); //读AD
delay(30); //延时
}
i = LvBo(&Ad_Buffer[0]);
buffer2[0]=(uchar) (i&0x00ff); //存AD数据低8位
buffer2[1]=(uchar) ((i>>8) & 0x00ff); //存AD数据高8位
TESTB=0;
delay(2000);
TESTA=1; //接通A点
delay(3000); //延时等待电压建立
for(i=0; i<7; i++)
{
Ad_Buffer[i] = read_ad(); //读AD
delay(30);
}
i = LvBo(&Ad_Buffer[0]);
buffer2[2]=(uchar) (i&0x00ff); //存AD数据低8位
buffer2[3]=(uchar) ((i>>8) & 0x00ff); //存AD数据高8位
Ctrl2=0; //断开电流源
TESTA=0; //断开A点
// TESTB=0; //断开B点
delay(2000);
Ctrlu=1; //接通电压源
delay(2000);
if(WFlag==handtest1) //根据串口接收到的数据选择并接通干扰源
{
relay1=1;
// relay2=0;
// relay3=0;
// relay4=0;
// relay5=0;
TESTC=1;
delay(30000);
//delay(30000);
}
else if(WFlag==handtest2)
{
// relay1=0;
// relay2=0;
relay3=1;
// relay4=0;
// relay5=0;
TESTC=1;
delay(30000);
// delay(30000);
}
else if(WFlag==handtest3)
{
relay1=1;
// relay2=0;
relay3=1;
// relay4=0;
// relay5=0;
TESTC=1;
delay(30000);
// delay(30000);
}
else if(WFlag==handtest4)
{
// relay1=0;
// relay2=0;
// relay3=0;
relay4=1;
// relay5=0;
TESTC=1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -