哪位大哥可以帮我看看这个AVR单片机的程序
#include<iom16v.h>
#include<macros.h>
#include"math.h"
//8.00000MHZ
unsigned long led_7[10]={0x3f,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};//七段数码显示数组
unsigned long position[6]={0xfe,0xfd,0xfb,0xf7,0x0f,0xdf};//六位数码管位选
unsigned long Min[30]={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,
31,33,35,37,39,41,43,45,47,49,51,53,55,57,59};//使能充电停止数组
unsigned long k_value[60]={40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,//预充电压斜率数组
60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,};
unsigned long V_Table[12]={380,381,390,391,400,401,410,411,415,416,419,420};//电压比较判断
signed char time[3];//时间存储
unsigned char dis_buff[6];//时分秒六位分别存储
unsigned char PWMnum,a=0x15,m=0,X,Y,Z;//溢出中断次数,OCR0值,if判断,时分秒,电压
unsigned long K,n,Vref=4.846,T,t,V;//预充斜率,延迟时间,参考电压,时间,
signed long Cap;//电池容量
#pragma interrupt_handler adc_isr:iv_ADC
void adc_isr()
{
unsigned long ADNum=0,adc_data;
unsigned char Va;
ADNum=ADNum+1;//AD转换次数计数
if(ADNum>=98888)ADNum=110000;
adc_data=ADCH;
adc_data=adc_data*4;
if(ADNum==2)//初始电压值保存
{
Va=Vref*adc_data*100/1024;
}
V=Vref*adc_data*100/1024;//实时电压
if(V-Va==4)//斜率计算
{
K=(20000000/ADNum)%10;
}
}
void A_value()//OCR0实际值
{
if(K==k_value[60])
a=250-K;
if (K!=k_value[60])
a=0;
}
void Cap_value()//容量计算值
{
unsigned long Aa=2000,P;
if(K==k_value[60])
{
P=K/Aa;
Cap=(-1)*log(P)*200;
}
if (K!=k_value[60])
{
Cap=0;
}
}
void Time_Compu()//时间计算程序
{
t=3*OCR0;
T=Cap/t;
if(T>=60.000)
{
X=(T/60);
Y=(T%60);
Z=((T-60)-(T%60))*60;
}
if(T<60.000)
{
X=0;
Y=((T*10)%10);
Z=((T-(T*10)%10)*60);
}
}
void Delay_ms(n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<8000;j++);
}
}
void display(void)
{
unsigned char i;
for (i=0;1<5;i++)
{
PORTC=0XFF;
PORTD=led_7[dis_buff];
PORTC=position;
Delay_ms(1);
PORTC=0XFF;
}
}
#pragma interrupt_handler timer0_ovf_isr:iv_TIMER0_OVF
void timer0_ovf_isr(void)
{
PWMnum=PWMnum+1;
}
void time_to_disbuffer(void)
{
unsigned char i,j=0;
for(i=0;i<=2;i++)
{
dis_buff[j++]=time%10;
dis_buff[j++]=time/10;
}
}
void main(void)
{
//port_init
PORTB=0X00;
DDRB=0X08;//PB3PWM输出
PORTC=0X3F;//led输出
DDRC=0XFF;
PORTD=0X00;
DDRD=0XFF;
//timer0_init
TCCR0=0X00;
TCNT0=0X00;
OCR0=a;//OCR0实际值
TCCR0=0X6C;//快速PWM,256分频,比较匹配时OC0清零
TIMSK=0X03;//比较匹配中断,溢出中断
//adc_init
ADMUX=0X20;//外部引脚AREF,左对齐,PA0口
ADCSRA=0XAE;//ADC允许,自动触发转换,中断允许,ADCclk=125kHz
SFIOR&=0X1F;
SFIOR|=0X60;//选择T/C0比较匹配中断触发
SEI();
Delay_ms(600000);
time[2]=X;time[1]=Y;time[0]=Z;//时间
time_to_disbuffer();
while(1)
{
display();
if((m==1)||(V!=V_Table[8])||(a>=0X15))//倒计时
{
if(PWMnum>123)
{
PWMnum=0;
time[0]--;
if(time[0]<0)
{
time[0]=59;
time[1]--;
if(time[1]==Min[30])
{
m=0;
}
if(time[1]<0)
{
time[1]=59;
time[2]--;
}
}
}
time_to_disbuffer();
}
if((m==0)&&(V==V_Table[12]))//中途停车,休息电池
{
display();
OCR0=0X00;
Delay_ms(10000);
time[0]=(time[0]-10);
if(time[0]<=0)
{
time[0]=time[0]+59;
time[1]=time[1]-1;
}
time_to_disbuffer();
OCR0=a;
m=1;
}
if(V==420||V==421) //电池充满,时间清零
{
display();
OCR0=0X15;
time[2]=0;
time[1]=0;
time[0]=0;
time_to_disbuffer();
Delay_ms(600000);
}
}
}
像风一样自由(619738770) 17:01:56
谢谢
像风一样自由(619738770) 17:02:27
编译时出现[img]file:///D:/Program%20Files/Tencent/QQ/Users/619738770/Image/ebb1m8q[qg8mqw_z61lbf5x.jpg[/img] |