由于我才接觸單片機不到20天,我的編程和理解程序的能力都很菜,我就看電子大賽的題目有一個是,《電阻自動測試儀,》在百度文庫上搜了一下,按著人家的那個方案,做,現在把電路什么的都搭好了,顯示模塊用的是128*64,采樣用的是AD7705,但是人家的程序我復制過來,有錯誤,就卡到這里了,我研究了好幾天都不會,我希望有人可以幫幫我,不勝感激!
我把人家的程序給各位前輩們帶著,
#include"LCD12864.h "
#include"reg51.h"
#include"ADC.h"
#define uchar unsigned char
#define uint unsigned int
#defineTHO_int 0xff
#define TL0_int 0x15
#define TMOD_int 0x01//方式1
#define TCON_int 0x10
uchar time_4ms;
uchar cont_200ms;
uchar stop_run_delay;
uchar set_read_cont;
uchar key;
uchar key_buf;
uchar key_cont;
uchar set_step;
uchar tast_delay;
//************************************
uint moto_cont;
uint moto_time;
uchar P1_buf;
uchar beef_delay;
uchar beef_cont;
uint pdata buf[50];
uchar mode;
uchar set_mode;
uchar R_wc;
//************************************
uchar AD_data[2];
char dsp_buf[10];
unsigned longAD_BUF1,R1,RA,A0,set_R;
uint AD_UOR;
uchar ad_cont;
//***********************************
sbitout1=P1^4;
sbit out2=P1^5;
sbit out3=P1^6;
sbit out4=P1^7;
sbit buz= P3^6;
sbit led= P3^7;
uchar codemoto_tap[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09,};//步進電機時序表格
uchar codemoto_tap2[8]={0x08,0x0a,0x02,0x06,0x04,0x05,0x01,0x09,};//步進電機時序表格
char code LCD_TAP2[10]={"012,3456789"};
uchar code dsp_[16]={" "};
uchar bdata FLG;
sbit FLG_200MS=FLG^0;
sbit run_flg= FLG^1;
sbit beef_en= FLG^2;
sbit colck= FLG^3;
sbit key_out= FLG^4;
sbit ad_out= FLG^5;
sbit call_en= FLG^6;
void Ad_read(void);
void T0_int(void)
{
TMOD=TMOD_int; //設置定時器工作模式
TCON=TCON_int; //啟運定時器
TL0=TL0_int; //裝入定時器初值
TH0=THO_int; //裝入定時器初值
ET0=1; //允許定時器0中斷
EA=1; //開總斷
}
void timer0_(void)interrupt 1 using 1 // 定時器溢出周期為125us已加入16t
{
TR0=0;//
TL0=TL0_int;
TH0=THO_int;
TR0=1;
time_4ms++;
if(beef_en)buz=!buz;
else
buz=1;
if(time_4ms>=16)
{ //4ms平臺
time_4ms=0;
if(set_mode==0)led=0;
else
led=1;
if(beef_delay)beef_delay--;
if(stop_run_delay)stop_run_delay--;
if(set_step==0)
{
out1=0;
out2=1;
out3=1;
out4=1;
}
else
if(set_step==1
{
out1=1;
out2=0;
out3=1;
out4=1;
}
else
if(set_step==2)
{ out1=1;
out2=1;
out3=0;
out4=1;
}
else
if(set_step==3)
{ out1=1;
out2=1;
out3=1;
out4=0;
}
else
if(set_step==4)
{ out1=1;
out2=1;
out3=1;
out4=0;
}
colck=1;
cont_200ms++;
if(cont_200ms>=25)
{
cont_200ms=0;
FLG_200MS=1;
}
if(moto_cont)
{
moto_cont--;
P1_buf=P1&0xf0;
if(run_flg)
{
P1=P1_buf|moto_tap[moto_cont];
}
else
P1=P1_buf|moto_tap[7-moto_cont];
}
else
if(moto_time)
{
moto_time--;
moto_cont=8;
P1=P1&0xf0;
if(moto_time==0)
{
stop_run_delay=20;
}
}
else
if(moto_time==0)
{
P1=P1&0xf0;
}
}
}
void moto_control(uint cont,ucharfx)
{
if(fx==1)
{
run_flg=1;
}
else
run_flg=0;
moto_time=cont;
}
void tast_control(void)
{
unsignedchar i,j=0;
chardsp[10];
delay_ms(200);
if((ad_out)&&(tast_delay==0))
{
ad_out=0;
lcd_dsp(0,0," 電阻測試儀",12);
lcd_dsp(1,0,"檢測值: ",8);
lcd_dsp(2,0,"篩選值: ",8);
lcd_dsp(3,0,"誤 差:",10);
dsp_buf[0]=set_R/1000000;
dsp_buf[1]=(set_R%1000000)/100000;
dsp_buf[2]=((set_R%1000000)%100000)/10000;
dsp_buf[3]=(((set_R%1000000)%100000)%10000)/1000;
dsp_buf[4]=((((set_R%1000000)%100000)%10000)%1000)/100;
dsp_buf[5]=(((((set_R%1000000)%100000)%10000)%1000)%100)/10;
dsp_buf[6]=(((((set_R%1000000)%100000)%10000)%1000)%100)%10;
for(i=0;i<3;i++)
{
if(dsp_buf==0)
{
dsp_buf='';
j++;
}
}
for(i=0;i<7-j;i++)
{
dsp=dsp_buf[i+j];
}
lcd_no_write2(2,4,dsp,7-j);
dsp_buf[0]=R_wc/10;
dsp_buf[1]=R_wc%10;
lcd_no_write2(3,4,dsp_buf,2);
lcd_dsp(3,5,"%",1);
A0=AD_UOR*0.038147;
R1=((2475000)/A0)-990;
if(set_step==0)
{
RA=R1/10;
}
else
if(set_step==1)
{
RA=R1;
}
else
if(set_step==2)
{
RA=R1*10;
}
else
if(set_step==3)
{
RA=R1*100;
}
else
if(set_step==4)
{
RA=set_R;
}
if(RA>set_R)
{ if((RA-set_R)>(set_R*R_wc)/100)
if(call_en)beef_cont=1;
}
else
if(RA<set_R)
{ if((set_R-RA)>(set_R*R_wc)/100)
if(call_en)beef_cont=1;
}
if(set_step==0)
{
if(R1>1010)
{ set_step=1;
wcode(0x01); //清除顯示
}
}
else
if(set_step==1)
{ if(R1>1010)
{ set_step=2;
wcode(0x01); //清除顯示
}
else
if(R1<100)
{ set_step=0;
wcode(0x01); //清除顯示
}
}
else
if(set_step==2)
{ if(R1>1050)
{ set_step=3;
wcode(0x01); //清除顯示
}
else
if(R1<100)
{
set_step=1;
wcode(0x01); //清除顯示
}
}
else
if(set_step==3)
{
if(R1<99)
{ set_step=2;
wcode(0x01); //清除顯示
}
else
if(R1>99999)
{ wcode(0x01); //清除顯示
set_step=4;
}
}
else
if(set_step==4)
{
if(R1<99999)
{ set_step=3;
wcode(0x01); //清除顯示
}
}
if(set_step==0)
{
dsp_buf[0]=LCD_TAP2[(R1%10000)/1000];
dsp_buf[1]=LCD_TAP2[((R1%10000)%1000)/100];
dsp_buf[2]=LCD_TAP2[(((R1%10000)%1000)%100)/10];
dsp_buf[3]='.';
dsp_buf[4]=LCD_TAP2[(((R1%10000)%1000)%100)%10];
if(((R1%10000)/1000)==0)
dsp_buf[0]='';
lcd_dsp(1,4,dsp_buf,5);
}
else
if(set_step==1)
{ dsp_buf[0]=LCD_TAP2[(R1%10000)/1000];
dsp_buf[1]=LCD_TAP2[((R1%10000)%1000)/100];
dsp_buf[2]=LCD_TAP2[(((R1%10000)%1000)%100)/10];
dsp_buf[3]=LCD_TAP2[(((R1%10000)%1000)%100)%10];
if(((R1%10000)/1000)==0)
dsp_buf[0]='';
lcd_dsp(1,4,dsp_buf,4);
}
else
if(set_step==2)
{
dsp_buf[0]=LCD_TAP2[(R1%10000)/1000];
dsp_buf[1]=LCD_TAP2[((R1%10000)%1000)/100];
dsp_buf[2]='.';
dsp_buf[3]=LCD_TAP2[(((R1%10000)%1000)%100)/10];
dsp_buf[4]=LCD_TAP2[(((R1%10000)%1000)%100)%10];
if(((R1%10000)/1000)==0)
{
for(i=0;i<4;i++)
{
dsp_buf=dsp_buf[i+1];
}
}
lcd_dsp(1,4,dsp_buf,4);
lcd_dsp(1,7,"K",1);
}
else
if(set_step==3)
{ dsp_buf[0]=LCD_TAP2[R1/10000];
dsp_buf[1]='.';
dsp_buf[2]=LCD_TAP2[(R1%10000)/1000];
dsp_buf[3]=LCD_TAP2[((R1%10000)%1000)/100];
dsp_buf[4]=LCD_TAP2[(((R1%10000)%1000)%100)/10];
dsp_buf[5]=LCD_TAP2[(((R1%10000)%1000)%100)%10];
lcd_dsp(1,4,dsp_buf,4);
lcd_dsp(1,7,"M",1);
}
else
if(set_step==4)
{
lcd_dsp(1,4,"_____M",6);
}
}
}
void key_control(void)
{ uchari;
if(key_out)
{
key_out=0;
if(key==0x0f)
{
wcode(0x01); //清除顯示
mode=0;
}
else
if(key==0x17)
{ mode=1;
beef_en=0;
beef_cont=0;
beef_delay=0;
set_step=1;
out1=1;
out2=0;
out3=1;
out4=1;
wcode(0x01); //清除顯示
lcd_dsp(0,0,dsp_,16);
lcd_dsp(1,0,dsp_,16);
lcd_dsp(2,0,dsp_,16);
lcd_dsp(3,0,dsp_,16);
lcd_dsp(1,0," 請稍等候!",12);
set_read_cont=50;
for(i=0;i<=30;i++)buf=3*i;
ad_out=0;
for(i=0;i<4;)
{
if((stop_run_delay==0)&&(moto_time==0))
{ if(set_read_cont)
{ set_read_cont--;
if(set_read_cont==0)i=4;
moto_time=8;
ad_out=0;
while(ad_out==0)
{ Ad_read();
delay_ms(100);
}
A0=AD_UOR*0.038147;
R1=((2480000)/A0)-992;
buf[49-set_read_cont]=R1/10;
}
}
}
lcd_dsp(0,0,dsp_,16);
lcd_dsp(1,0,dsp_,16);
lcd_dsp(2,0,dsp_,16);
lcd_dsp(3,0,dsp_,16);
show_line(buf);
}
else
if(key==0x1b)
{ beef_cont=1;
if(set_mode==0)
{
set_R++;
}
else
if(set_mode==1)
{ R_wc++;
}
}
else
if(key==0x3b)
{ key_cont=180;
beef_cont=1;
if(set_mode==0)
{
set_R++;
}
else
if(set_mode==1)
{ R_wc++;
}
}
else
if(key==0x3d)
{ key_cont=180;
if(set_mode==0)
{ if(set_R)
set_R--;
}
else
if(set_mode==1)
{ if(R_wc)
R_wc--;
}
beef_cont=1;
}
else
if(key==0x1d)
{
if(set_mode==0)
{ if(set_R)
set_R--;
}
else
if(set_mode==1)
{ if(R_wc)
R_wc--;
}
beef_cont=1;
}
else
if(key==0x1e)
{ set_mode=set_mode^0x01; beef_cont=1;
}
else
if(key==0x3e)
{
call_en=!call_en;
if(call_en)beef_cont=0x81;
else
beef_cont=1;
}
}
}
void beef_control(void)
{
if(beef_delay==0)
{
if(beef_en)
{
beef_en=0;
beef_delay=20;
}
else
{
if(beef_cont&0x0f)
{
beef_cont--;
beef_en=1;
if(beef_cont&0x80)
beef_delay=100;
else
beef_delay=20;
}
}
}
}
void key_scan(void)
{
if((P2&0x1f)!=0x1f)
{
if(key_cont<200)
{
key_cont++;
if(key_cont==10)
{
key_buf=P2&0x1f;
}
else
if(key_cont==200)
{
key=key_buf|0x20;
key_out=1;
}
}
}
else
{ if((key_cont>=10)&&(key_cont<200))
{
key=key_buf;
key_out=1;
}
key_cont=0;
}
}
void Ad_read(void)
{ uinttemp,i,cons;// max0,min0,max1,min1,
uintdata0[5],data1[5];//,t0,t1;
longtemp0=0,temp1=0;
cons=5;
if(FLG_200MS)
{
FLG_200MS=0;
for(i=0;i<cons;i++)
{
AD7705_Write_Reg(0x38); //啟動AD7705AD轉換
delay_ms(2);
AD_data[0]=AD7705_Read_Reg(); //保存高8位數據
data0=AD_data[0];
delay_ms(2);
AD_data[1]=AD7705_Read_Reg(); //保存低8位數據
data1=AD_data[1];
}
for(i=0;i<cons;i++)
{
if(data0!=0xffff)
{ temp0=temp0+data0; }
if(data1!=0xffff)
{ temp1=temp1+data1; }
}
temp0=temp0/(cons);
temp1=temp1/(cons);
temp=(uint)temp0*256+(uint)temp1;//保存16位AD
AD_BUF1=temp+AD_BUF1;
ad_cont++;
if(ad_cont>=2)
{
ad_cont=0;
AD_UOR=AD_BUF1/2;
AD_BUF1=0;
ad_out=1;
if(tast_delay)tast_delay--;
}
}
}
void main(void)
{
AD7705_Reset(1);
setup();
T0_int();
run_flg=0;
moto_cont=8;
set_step=3;
beef_cont=1;
set_R=4000;
R_wc=10;
set_mode=0;
call_en=0;
while(1)
{
if(colck)
{
colck=0;
if(mode==0)
{
tast_control();
}
delay_ms(2);
Ad_read();
beef_control();
key_scan();
key_control();
}
}
} 謝謝大家了!!!.0.0.
|