c8051f單片機ad采樣程序 作者: 時間:2016-12-01 來源:網(wǎng)絡(luò) 加入技術(shù)交流群 掃碼加入和技術(shù)大咖面對面交流海量資料庫查詢 收藏 /*void UART0_ISR(void) interrupt 4{static uchar Rcv;if(RI0){Rcv = SBUF0;RI0 =0; }if(Rcv == 0x7E) //{i = 1;buffer[0] = Rcv;}else{if(i < 13) //12個字節(jié){ buffer[i] = Rcv;i++;}} }*/void UART1_ISR(void) interrupt 20{static uchar tmp;if ((SCON1 & 0x01) == 0x01){tmp = SBUF1;SCON1 = (SCON1 & 0xFE); //RI1 = 0;switch(tmp){case $: //首字符{cmd_number=0; //命令類型清空mode=1; //接收命令模式byte_count=0; //接收位數(shù)清空}break;case ,:{seg_count++; //逗號計數(shù)加1byte_count=0;}break;case *: //數(shù)據(jù)幀結(jié)束標志{switch(cmd_number){case 1:buf_full|=0x01;break;case 2:buf_full|=0x02;break;case 3:buf_full|=0x04;break;}mode=0;}break;default:if(mode==1){ //命令種類判斷cmd[byte_count]=tmp; //接收字符放入類型緩存if(byte_count>=4){ //如果類型數(shù)據(jù)接收完畢,判斷類型if(cmd[0]==G){if(cmd[1]==P){if(cmd[2]==G){if(cmd[3]==G){if(cmd[4]==A){cmd_number=1;mode=2;seg_count=0;byte_count=0;}}}}}}}else if(mode==2){ //接收數(shù)據(jù)處理//類型1數(shù)據(jù)接收。GPGGAswitch(seg_count){case 2: //緯度處理if(byte_count<10){WD[byte_count]=tmp;}break;case 3: //緯度方向處理if(byte_count<1){WD_a=tmp;}break;case 4: //經(jīng)度處理if(byte_count<11){JD[byte_count]=tmp;}break;case 5: //經(jīng)度方向處理if(byte_count<1){JD_a=tmp;}break;/*case 6: //定位判斷if(byte_count<1){lock=tmp;}break;case 7: //定位使用的衛(wèi)星數(shù)if(byte_count<2){use_sat[byte_count]=tmp;}break;*///case 9: //高度處理//if(byte_count<6)//{// HIGH[byte_count]=tmp;// }//break;default:break;}}byte_count++; //接收數(shù)位加1break;}//temp=0;//byte_count++; //接收數(shù)位加1} }//=======================================================//TIMER2 10毫秒自動重裝//=======================================================void TIME2_ISR (void) interrupt 5 //20毫秒自動重裝{static uchar i,j1,j2,j3,j4,j5,j6,k,k1,m,m1,LTP1,LNY1;static uint c1,c2,c3,c4;TF2 = 0; //清標志位i++;if(i==50){P5=~(P5);i = 0; } LTP1 = P7; //接近開關(guān)不遮擋輸出高電平,LNY1 = P7;if((LTP1&0xFF) ==0xFF) //P70=1;遮擋上了,接近開關(guān)無輸出,此時P70=1;{j1++;if(j1>2) //2次以上還是原來狀態(tài)證明不是干擾脈沖{j1 = 0;k = 1; //檢測到P70為0,置位K }c3++;if(c3>9000) //超過1分半鐘沒變化認為停止了{c3 = 0;SPEEDcount1 = 0;SPEEDTP = 0;}}if(k) //在置位情況下等待下降沿 {if((LTP1&0xFF) ==0xFE) //P70=0;檢測到無遮擋,接近開關(guān)輸出24V,開始一個周期此時P70 = 0;{j2++;if(j2>2) //過濾脈沖干擾{j2 = 0;k1 = 1;}c1++;if(c1>9000) //超過1分半鐘沒變化認為停止了{c1 = 0;SPEEDcount1 = 0;SPEEDTP = 0;k1 = 0; k = 0;}if(k1){SPEEDcount1++; //發(fā)生了正跳變,開始計時}} if(k1){if((LTP1&0xFF) ==0xFF) //再一次檢測到有遮擋表示一次檢測周期結(jié)束,計時結(jié)束,開始計算速度{j3++;if(j3>2){j3 = 0;k1 = 0;k = 0;//SPEEDTP = SPEEDcount1/100; //(秒)SPEEDTP = (long)600*(Between[4]*100 + Between[3])/SPEEDcount1; //米/分;顯示部分已經(jīng)后移了兩位SPEEDcount1 = 0;c1 = 0; c3 = 0; TPcount++;TPrange = (long)TPcount*(Between[6]*100 + Between[5])/1000; }}}}if((LNY1&0xFF) ==0xFF) //P71=1;{j4++;if(j4>2) //2次以上還是原來狀態(tài)證明不是干擾脈沖{j4 = 0;m = 1; //檢測到P71為0,置位M }c4++;if(c4>9000) //超過1分半鐘沒變化認為停止了{c4 = 0;SPEEDcount2 = 0;SPEEDNY = 0;}}if(m) //在置位情況下等待下降沿{if((LNY1&0xFF) ==0xFD) //P71=0;{j5++;if(j5>2) //過濾脈沖干擾{j5 = 0;m1 = 1;}c2++;if(c2>9000) //等待了1分半鐘沒變化,認為停止了{c2 = 0;SPEEDcount2 = 0;SPEEDNY = 0;m1 = 0; m = 0;}if(m1){SPEEDcount2++; //發(fā)生了正跳變,開始計時}} if(m1){if((LNY1&0xFF) ==0xFF) //發(fā)生了負跳變,計時結(jié)束,開始計算速度{j6++;if(j6>2){j6 = 0;m1 = 0;m = 0;SPEEDNY = (long)36*(Between[6]*100 + Between[5])/SPEEDcount2; //km/h;顯示部分已經(jīng)后移了兩位SPEEDcount2 = 0;c2 = 0; c4 = 0;}}}} }void ADC0_ISR (void) interrupt 15{static uchar count0; // Loop counterAD0INT = 0; //清中斷標志位switch( AMX0SL ) //通道轉(zhuǎn)換{case 0x00: //AIN0.0ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 1;filter();TPHD1= (sum/(N-2));if(TPHD1>ADC04V){TPHD1 = TPHD1 - 5; //去掉偏移量a = (long)TPHD1*2500/4095; //放大1000倍,采集到1毫伏a = (a*469-37500)/1000; //超聲傳感器公式150=a*0.4+b 900=a*2.0+b a=468.75 b=37.5TPHD1 = a;}else {TPHD1 = 0;} }break; case 0x01: //AIN0.1ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 2;filter();TPHD2= (sum/(N-2));if(TPHD2>ADC04V){TPHD2 = TPHD2 - 5;a = (long)TPHD2*2500/4095; //放大1000倍,采集到1毫伏a = (a*469-37500)/1000; //超聲傳感器公式150=a*0.4+b 900=a*2.0+b a=468.75 b=37.5TPHD2 = a;}else {TPHD2 = 0;} }break; case 0x04: //AIN0.4ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 3;filter();TEMP1= (sum/(N-2));if(TEMP1>ADC04V){KT1F = 1;TEMP1 = TEMP1 - 5; //去掉偏移量a = (long)TEMP1*2500/4095; //放大1000倍,采集到1毫伏a = (long)(a*331-129000)/100; //線性方程0=0.4a-b;500=2.0a-b 求解得a = 312.5 b=125 分別取313和125 TEMP1 = a; //兩點標定法標定后取a = 368 b = 157}else {TEMP1 = 0;KT1F = 0;} }break; case 0x05: //AIN0.5ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 4;filter();TEMP2= (sum/(N-2));if(TEMP2>ADC04V){KT2F = 1;TEMP2 = TEMP2 - 5; //去掉偏移量a = (long)TEMP2*2500/4095; //放大1000倍,采集到1毫伏a = (long)(a*331-129000)/100; //線性方程0=0.4a-b;500=2.0a-b 求解得a = 312.5 b=125 分別取313和125 TEMP2 = a;}else {TEMP2 = 0;KT2F = 0;} }break; case 0x06: //AIN0.6ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 5;filter();TEMP3= (sum/(N-2));if(TEMP3>ADC04V){KT3F = 1;TEMP3 = TEMP3 - 5; //去掉偏移量a = (long)TEMP3*2500/4095; //放大1000倍,采集到1毫伏a = (long)(a*331-129000)/100; //線性方程0=0.4a-b;500=2.0a-b 求解得a = 312.5 b=125 分別取313和125 TEMP3 = a;}else {TEMP3 = 0;KT3F = 0;}}break; case 0x07: //AIN0.7ADCbuffer1[count0]=ADC0;count0++;if (count0 > N-1){count0 = 0;AD0EN = 0;ADcount = 6;filter();TEMP4= (sum/(N-2));if(TEMP4>ADC04V){KT4F = 1;TEMP4 = TEMP4 - 5; //去掉偏移量a = (long)TEMP4*2500/4095; //放大1000倍,采集到1毫伏a = (long)(a*331-129000)/100; //線性方程0=0.4a-b;500=2.0a-b 求解得a = 312.5 b=125 分別取313和125 TEMP4 = a;}else {TEMP4 = 0;KT4F = 0;}}break;default:break;}}void ADC1_ISR (void) interrupt 17{static uchar count1; // Loop counterADC1CN &=~(0x20); //清中斷標志位switch( AMX1SL ) //通道轉(zhuǎn)換{case 0x03: //AIN1.3ADCbuffer1[count1]=ADC1;count1++;if (count1 > N-1){count1 = 0;ADC1CN &=~(0x80);ADcount = 7;filter();HT= (sum/(N-2));if(HT>40){HTF = 1;HT = HT -4;HT = (long)(HT*HTA-HTB*100)/10;}else {HT = 0;HTF = 0;}}break;case 0x02: //AIN1.2ADCbuffer1[count1]=ADC1;count1++;if (count1 > N-1){count1 = 0;ADC1CN &=~(0x80);ADcount = 8;filter();HR= (sum/(N-2));if(HR>40){HRF = 1;HR = HR-4;HR = (long)(HR*HRA-HRB*100)/10;}else {HR = 0;HRF = 0;}}break;case 0x01: //AIN1.1ADCbuffer1[count1]=ADC1;count1++;if (count1 > N-1){count1 = 0;ADC1CN &=~(0x80);ADcount = 9;filter();HP1= (sum/(N-2));if((HP1>40)&&(HP1<123)) //負向傾角{HPF = 0;HP = HP1-4;HP = (long)(HPB*100-HP*HPA)/10;}if((HP1>127)&&(HP1<=255)) //正向傾角{HPF = 1;HP = HP1-4;HP = (long)(HP*HPA-HPB*100)/10;}if((HP1>=123)&&(HP1<=127)){HPF = 2; //只顯示0HP = 0;}if(HP1<40){HPF = 3; //未連接}}break; /* case 0x00: //AIN1.0ADCbuffer1[count1]=ADC1;count1++;//ADC0CN |=0x10; //寫ADC1BUSY位啟動轉(zhuǎn)換if (count1 > N-1){count1 = 0;ADC1CN &=~(0x80);ADcount = 8;}break;*/default:break;}}//-----------------------------------------//TIMER3中斷 25毫秒定時//-----------------------------------------void Timer3_ISR(void) interrupt 14{static uchar j,k,count2,count3;static uint count1;TMR3CN &=~(0x80); //清標志位count1++;count2++;count3++;j++;if(count1>2000) //兩分鐘串口發(fā)送一次數(shù)據(jù){count1 = 0;sendc = 1;}if(count2 == 6) //200ms{count2 = 0;Tset++;if(Tset>4){Tset = 0;} }if(count3 == 4) //100ms{count3 = 0;switch(ADcount){case 1:AMX0SL = 0x01; //指向下一個轉(zhuǎn)換通道AD0EN = 1;break;case 2:AMX0SL = 0x04; //指向下一個轉(zhuǎn)換通道AD0EN = 1;break; case 3:AMX0SL = 0x05; //指向下一個轉(zhuǎn)換通道AD0EN = 1;break;case 4:AMX0SL = 0x06; //下一個轉(zhuǎn)換通道AD0EN = 1;break;case 5:AMX0SL = 0x07; //下一個轉(zhuǎn)換通道AD0EN = 1;break;case 6:AMX1SL = 0x03; //下一個轉(zhuǎn)換通道AD0EN = 0;ADC1CN |= 0x80;break;case 7:AMX1SL = 0x02; //下一個轉(zhuǎn)換通道ADC1CN |= 0x80;break; case 8:AMX1SL = 0x01; //下一個轉(zhuǎn)換通道ADC1CN |= 0x80;break;case 9:AMX0SL = 0x00; //下一個轉(zhuǎn)換通道AD0EN = 1;ADC1CN &=~(0x80);break;default:break;}}if(j==40) //1秒誤差累積;正常是40{j = 0;if(BGset){k++;BLK = 1;if(k>120) //4分鐘關(guān)背光{k=0;BGset = 0;BLK = 0;} } } } 上一頁 1 2 3 下一頁
評論