程序里面错的地方是我从别的程序移植过来的,想请大家帮我分析一下错在哪里,怎样解决呢?谢谢!
单片机源程序如下:
#include<reg52.h>
//宏定义
#define uint unsigned int
#define uchar unsigned char
#define Data_ADC0809 P0 //定义P0口为Data_ADC0809
//sbit PARSER = P2^0; //串并行控制位
//sbit BYTE = P2^1; //高低字节控制位
//sbit RC = P2^4; //读取转换控制位
//sbit BUSY = P2^2; //忙状态位
//函数声明
extern uchar ADC0809();
sbit ST=P2^0;
sbit EOC=P2^1;
sbit OE=P2^2;
sbit ADDR_A = P3^5; //低位地址控制位
sbit ADDR_B = P3^6; //低位地址控制位
sbit ADDR_C = P3^7; //高位地址控制位
void ConfigUART(unsigned int baud); //串行口配置函数
void ConfigTimer0(); //定时器0配置函数
void SendData(unsigned char ch); //字符发送函数
void SendString(char *s); //字符串发送函数
void GetVoltage(); //ADC电压获取函数
unsigned int Linear(double v); //线性插值函数,参数v为实测电压
void DataProcess(); //数据处理函数
void LedBufRefresh(); //数码管显示缓冲区刷新函数
void UartSend(); //串口数据发送函数
void delay(uint t);
unsigned char voltage[] = {'0','.','0','0','0',0};
unsigned char time_used[] = {'0','0','0',0};
unsigned char percentage[] = {'0','0','0',0};
unsigned long j,time_used_value,result,percentage_value,voltage_value;
unsigned int code time_sample[21]={0,18,36,54,72,90,108,126,144,162,180,198,216,234,252,270,288,306,324,342,360};
double code voltage_sample[21]={4.35,4.24,4.135,4.005,3.92,3.889,3.858,3.826,3.8,3.78,3.762,3.743,3.725,3.705,3.686,3.667,3.65,3.628,3.492,3.05,2};
//共阳数码管显示字符转换表
unsigned char code LedChar[] = {
0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
};
//数码管显示缓冲区,初值0x00确保启动时都不亮
unsigned char LedBuff[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
void main()
{
ConfigUART(9600); //配置串行口工作模式及参数
ConfigTimer0(); //配置定时器0用于数码管刷新
EA = 1; //打开总中断
while(1)
{
//正常模式
if(Mode==0)
{
//读取AD值
temp=ADC0809();
}
GetVoltage(); //获取ADC电压值
DataProcess(); //数据处理
LedBufRefresh(); //显示缓冲区刷新
UartSend(); //串口发送
for(j=0;j<30000;j++); //延时读取
}
}
/* 数据处理函数 */
void DataProcess()
{
/* 计算电压值 */
voltage_value = (unsigned long)(((double)result * 10 / 32767) * 1000 + 0.5);
/* 电压值数组 */
voltage[4] = '0' + voltage_value % 10;
voltage[3] = '0' + (voltage_value /10) % 10;
voltage[2] = '0' + (voltage_value /100) % 10;
voltage[0] = '0' + (voltage_value /1000) % 10;
/* 剩余用时数组 */
time_used_value = Linear((double)result * 10 / 32767);
time_used[2] = '0' + time_used_value % 10;
time_used[1] = '0' + (time_used_value / 10) % 10;
time_used[0] = '0' + (time_used_value / 100) % 10;
/* 百分比数组 */
percentage_value =
(unsigned long)((double)(180000 - time_used_value * 500) / 168000 * 100 + 0.5);//改过数据
percentage[2] = '0' + percentage_value % 10;
percentage[1] = '0' + (percentage_value / 10) % 10;
percentage[0] = '0' + (percentage_value / 100) % 10;
if((percentage_value / 100) % 10) //处理百分比最高位
{
percentage[0] = '0' + (percentage_value / 100) % 10;
}
else
{
percentage[0] = ' ';
}
}
/* 线性插值函数,参数v为实测电压,返回插值时间结果 */
unsigned int Linear(double v)
{
unsigned int i,t1,t2,t;
double v1,v2;
if(v >= 4.35) //大于最大电压
{
t = 0;
return t;
}
if(v <= 2) //小于最小电压
{
t = 360;
return t;
}
for(i=0; i<21; i++) //遍历插值范围
{
if(voltage_sample[i] < v)
{
v1 = voltage_sample[i-1];
v2 = voltage_sample[i];
t1 = time_sample[i-1];
t2 = time_sample[i];
t = t2 - (v - v2) * (double)(t2 - t1) / (v1 - v2);
break; //计算插值结果t
}
else if(voltage_sample[i] == v)
{
t = time_sample[i]; //恰好取采样值
break;
}
}
return t;
}
/* ADC电压获取函数 */
void GetVoltage()
uchar ADC0809()
{
uchar temp_=0x00;
//初始化高阻太
OE=0;
//转化初始化
ST=0;
//开始转换
ST=1;
ST=0;
//外部中断等待AD转换结束
while(EOC==0)
//读取转换的AD值
OE=1;
temp_=Data_ADC0809;
OE=0;
return temp_;
}
/* 定时器0中断服务函数 */
void InterruptTimer0() interrupt 1
{
static unsigned char i = 0; //动态扫描的索引
TH0 = 0xFC; //重新加载初值
TL0 = 0x67;
//以下代码完成数码管动态扫描刷新
P1 = 0x00; //显示消隐
switch(i) //动态扫描
{
case 0: ADDR_C = 0; ADDR_B = 0; ADDR_A = 0; i++; P1=LedBuff[0]; break;
case 1: ADDR_C = 0; ADDR_B = 0; ADDR_A = 1; i++; P1=LedBuff[1]; break;
case 2: ADDR_C = 0; ADDR_B = 1; ADDR_A = 0; i++; P1=LedBuff[2]; break;
case 3: ADDR_C = 0; ADDR_B = 1; ADDR_A = 1; i++; P1=LedBuff[3]; break;
case 4: ADDR_C = 1; ADDR_B = 0; ADDR_A = 0; i++; P1=LedBuff[4]; break;
case 5: ADDR_C = 1; ADDR_B = 0; ADDR_A = 1; i++; P1=LedBuff[5]; break;
case 6: ADDR_C = 1; ADDR_B = 1; ADDR_A = 0; i=0; P1=LedBuff[6]; break;
//case 7: ADDR_C = 1; ADDR_B = 1; ADDR_A = 1; i=0; P1=LedBuff[7]; break;
//保留最低为数码管不使用
default: break;
}
}
/* 数码管显示缓冲区刷新函数 */
void LedBufRefresh()
{
LedBuff[6] = ~LedChar[percentage_value % 10];
LedBuff[5] = ~LedChar[(percentage_value / 10) % 10];
if((percentage_value / 100) % 10) //百分比最高位处理
{
LedBuff[4] = ~LedChar[(percentage_value / 100) % 10]; //为1则显示
}
else
{
LedBuff[4] = 0; //否则不显示
}
LedBuff[3] = ~LedChar[voltage_value % 10];
LedBuff[2] = ~LedChar[(voltage_value /10) % 10];
LedBuff[1] = ~LedChar[(voltage_value /100) % 10];
LedBuff[0] = ~(LedChar[(voltage_value /1000) % 10] & 0x7F); //最高位小数点处理
}
void UartSend()
{
SendString("当前电压:");
SendString(voltage);
SendString("V");
// SendString(" 已用时间:");
// SendString(time_used);
// SendString("Min");
SendString(" 剩余电量:");
SendString(percentage);
SendString("%\r\n");
}
/* 串口配置函数,baud-通信波特率 */
void ConfigUART(unsigned int baud)
{
SCON = 0x50; //配置串口为模式1
TMOD &= 0x0F; //清零T1的控制位
TMOD |= 0x20; //配置T1为模式2
TH1 = 256 - (11059200/12/32)/baud; //计算T1重载值
TL1 = TH1; //初值等于重载值
ET1 = 0; //禁止T1中断
ES = 1; //使能串口中断
TR1 = 1; //启动T1
}
/* 定时器0配置函数 */
void ConfigTimer0()
{
TMOD &= 0xF0; //清零T0的控制位
TMOD |= 0x01; //设置T0为模式1
TH0 = 0xFC; //为T0赋初值0xFC67,定时1ms
TL0 = 0x67;
ET0 = 1; //使能T0中断
TR0 = 1; //启动T0
}
/* UART中断服务函数 */
void InterruptUART() interrupt 4
{
if(RI) //接收到字节
{
RI = 0; //清零接收中断标志位
}
if(TI) //字节发送完毕
{
TI = 0; //清零发送中断标志位
}
}
/* UART字符发送函数 */
void SendData(unsigned char ch)
{
SBUF = ch; //启动发送
while(!TI); //等待结束
}
/* UART字符串发送函数 */
void SendString(unsigned char *s)
{
while(*s) //循环发送
{
SendData(*s++);
}
}
编译后报错如下:
Build target 'Target 1'
compiling main.c...
MAIN.C(57): error C202: 'Mode': undefined identifier
MAIN.C(60): error C202: 'temp': undefined identifier
MAIN.C(138): error C132: 'ADC0809': not in formal parameter list
MAIN.C(138): error C141: syntax error near '{'
MAIN.C(139): error C244: 'temp_': can't initialize, bad type or class
MAIN.C(139): error C132: 'temp_': not in formal parameter list
MAIN.C(141): error C244: 'OE': can't initialize, bad type or class
MAIN.C(141): error C132: 'OE': not in formal parameter list
MAIN.C(143): error C244: 'ST': can't initialize, bad type or class
MAIN.C(143): error C132: 'ST': not in formal parameter list
MAIN.C(145): error C244: 'ST': can't initialize, bad type or class
MAIN.C(145): error C132: 'ST': not in formal parameter list
MAIN.C(146): error C244: 'ST': can't initialize, bad type or class
MAIN.C(146): error C132: 'ST': not in formal parameter list
MAIN.C(148): error C141: syntax error near 'while'
MAIN.C(148): error C141: syntax error near '==', expected ')'
MAIN.C(150): error C231: 'OE': redefinition
MAIN.C(150): error C231: 'OE': redefinition
MAIN.C(151): error C247: non-address/-constant initializer
MAIN.C(152): error C279: 'OE': multiple initialization
MAIN.C(152): error C231: 'OE': redefinition
MAIN.C(152): error C231: 'OE': redefinition
MAIN.C(153): error C141: syntax error near 'return'
MAIN.C(154): error C141: syntax error near '}'
Target not created