气流量控制系统方案.docx
《气流量控制系统方案.docx》由会员分享,可在线阅读,更多相关《气流量控制系统方案.docx(18页珍藏版)》请在冰点文库上搜索。
气流量控制系统方案
//*******xtal=8AVR162011-5-11MS*huang**********
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineCPL_BIT(x,y)(x^=(1<__flashucharSEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};__flashucharSEG4[9]={0x77,0x7C,0x39,0x5e,0x79,0x71,0x6f,0x74,0x40};__flashucharACT[4]={0xef,0xdf,0xbf,0x7f};uintt1=1234,cnt2=11,cnt3=22,cnt4=33,cnt5=44,cnt6=0,cnt7=0,cnt8=0,cnt9=0;uintt1_1,cnt1_2,cnt1_3,cnt1_4,cnt2_1,cnt2_2,cnt2_3,cnt2_4;ucharstart_flag,start_flag1;uchari;longx;uintvoltage1,Disval1,voltage2,Disval2;uintvalue,dis_val;//无符号整型全局变量定义ucharstatus=4,status_1=0,status_2=0,flag;//无符号字符型全局变量定义ucharstatus_sw=0;ucharnum;//***************************************#defineS1(PINA&0x01)//递减#defineS2(PINA&0x02)//位数设定#defineS3(PINA&0x04)//运行#defineS4(PINA&0x08)//存储#defineS5(PINA&0x10)//M4#defineS6(PINA&0x20)//M3#defineS7(PINA&0x40)//M2//#defineS8(PINA&0x80)//M1#defineS9(PINC&0x02)//递增#defineS10(PINC&0x01)//切换#definextal8//***************************************#defineEEWE1#defineEEMWE2#defineEERE0//***************写EEPROM子函数***********voidWRITE_EEP(uintaddress,uchardat)//dat为待写数据,add为EEPROM的某单元地址{while(EECR&(1<EEAR=address;//设定单元地址EEDR=dat;//将数据写入EEDREECR|=(1<EECR|=(1<}//****************读EEPROM子函数*******************ucharREAD_EEP(uintaddress)//add为EEPROM的某单元地址{while(EECR&(1<EEAR=address;//设定单元地址EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
__flashucharSEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
__flashucharSEG4[9]={0x77,0x7C,0x39,0x5e,0x79,0x71,0x6f,0x74,0x40};
__flashucharACT[4]={0xef,0xdf,0xbf,0x7f};
uintt1=1234,cnt2=11,cnt3=22,cnt4=33,cnt5=44,cnt6=0,cnt7=0,cnt8=0,cnt9=0;
uintt1_1,cnt1_2,cnt1_3,cnt1_4,cnt2_1,cnt2_2,cnt2_3,cnt2_4;
ucharstart_flag,start_flag1;
uchari;
longx;
uintvoltage1,Disval1,voltage2,Disval2;
uintvalue,dis_val;//无符号整型全局变量定义
ucharstatus=4,status_1=0,status_2=0,flag;//无符号字符型全局变量定义
ucharstatus_sw=0;
ucharnum;
//***************************************
#defineS1(PINA&0x01)//递减
#defineS2(PINA&0x02)//位数设定
#defineS3(PINA&0x04)//运行
#defineS4(PINA&0x08)//存储
#defineS5(PINA&0x10)//M4
#defineS6(PINA&0x20)//M3
#defineS7(PINA&0x40)//M2
//#defineS8(PINA&0x80)//M1
#defineS9(PINC&0x02)//递增
#defineS10(PINC&0x01)//切换
#definextal8
#defineEEWE1
#defineEEMWE2
#defineEERE0
//***************写EEPROM子函数***********
voidWRITE_EEP(uintaddress,uchardat)//dat为待写数据,add为EEPROM的某单元地址
{
while(EECR&(1<EEAR=address;//设定单元地址EEDR=dat;//将数据写入EEDREECR|=(1<EECR|=(1<}//****************读EEPROM子函数*******************ucharREAD_EEP(uintaddress)//add为EEPROM的某单元地址{while(EECR&(1<EEAR=address;//设定单元地址EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
EEAR=address;//设定单元地址
EEDR=dat;//将数据写入EEDR
EECR|=(1<EECR|=(1<}//****************读EEPROM子函数*******************ucharREAD_EEP(uintaddress)//add为EEPROM的某单元地址{while(EECR&(1<EEAR=address;//设定单元地址EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
EECR|=(1<}//****************读EEPROM子函数*******************ucharREAD_EEP(uintaddress)//add为EEPROM的某单元地址{while(EECR&(1<EEAR=address;//设定单元地址EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
}
//****************读EEPROM子函数*******************
ucharREAD_EEP(uintaddress)//add为EEPROM的某单元地址
while(EECR&(1<EEAR=address;//设定单元地址EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
EECR|=(1<returnEEDR;//返回读出的数据}//****************************************voidDelay_1ms(void){uinti;for(i=1;i<(uint)(xtal*143-2);i++);}//========================================voidDelay_nms(uintn){uinti=0;while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
returnEEDR;//返回读出的数据
//****************************************
voidDelay_1ms(void)
{uinti;
for(i=1;i<(uint)(xtal*143-2);i++)
;
//========================================
voidDelay_nms(uintn)
uinti=0;
while(i{Delay_1ms();i++;}}/******************************************/uintconv(uintl)//数据转换子函数,定义i为无符号整型变量{longm;//定义x为无符号长整型局部变量uintn;//定义y为无符号整型局部变量m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量yreturnn;//返回y的值}/******************************************/voidport_init(void){PORTA=0x7F;DDRA=0x00;MCUCSR|=0x80;//使PORTB端口的JTAG功能无效PORTB=0x00;DDRB=0xFF;MCUCSR|=0x80;//使PORTC端口的JTAG功能无效PORTC=0x03;DDRC=0xfC;MCUCSR|=0x80;//使PORTD端口的JTAG功能无效PORTD=0x00;DDRD=0xff;}voidtimer0_init(void){TCNT0=0x83;TCCR0=0x04;}voidtimer1_init(void){TCCR1A=0xA3;TCCR1B=0x0A;OCR1A=0x00;OCR1B=0x00;TCNT1H=0xD8;TCNT1L=0xF0;}//***************看门狗初始化,分频系数为256K*************voidwatchdog_init(void){__watchdog_reset();WDTCR=0x1b;WDTCR=0x00;}voidadc_init(void)//模数转换初始化子函数{ADCSRA=0x00;ADMUX=0x07;//选择ADC输入通道为7ACSR=0x80;//关掉模拟比较器ADCSRA=0xCE;//单次转换\中断//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能}voidinit_devices(void){__disable_interrupt();//disableallinterruptsport_init();timer0_init();//timer1_init();adc_init();MCUCR=0x8A;//中断方式控制GICR=0xC0;//使能中断方式TIMSK=0x03;__enable_interrupt();//re-enableinterrupts11}/****************************************************/voidscan(){if(S10==0)//切换键{Delay_nms(10);if(S10==0){while(!S10);if(++status_2>2){status_2=0;}}}if(S9==0)//个十百位选择{Delay_nms(10);if(S9==0){while(!S9);if(++status_sw>3){status_sw=0;}if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}}}if(S2==0)//递增{Delay_nms(10);if(S2==0){while(!S2);if((status_2==0)&&(status_sw==0)){if(++cnt1_1>9)cnt1_1=0;}elseif((status_2==0)&&(status_sw==1)){if(++cnt1_2>9)cnt1_2=0;}elseif((status_2==0)&&(status_sw==2)){if(++cnt1_3>9)cnt1_3=0;}elseif((status_2==0)&&(status_sw==3)){if(++cnt1_4>9)cnt1_4=0;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(++cnt2_1>9)cnt2_1=0;}elseif((status_2==1)&&(status_sw==1)){if(++cnt2_2>9)cnt2_2=0;}elseif((status_2==1)&&(status_sw==2)){if(++cnt2_3>9)cnt2_3=0;}elseif((status_2==1)&&(status_sw==3)){if(++cnt2_4>9)cnt2_4=0;}}}if(S1==0)//递减{Delay_nms(10);if(S1==0){while(!S1);if((status_2==0)&&(status_sw==0)){if(--cnt1_1==0)cnt1_1=9;}elseif((status_2==0)&&(status_sw==1)){if(--cnt1_2==0)cnt1_2=9;}elseif((status_2==0)&&(status_sw==2)){if(--cnt1_3==0)cnt1_3=9;}elseif((status_2==0)&&(status_sw==3)){if(--cnt1_4==0)cnt1_4=9;}/////////////////////////////////////////if((status_2==1)&&(status_sw==0)){if(--cnt2_1==0)cnt2_1=9;}elseif((status_2==1)&&(status_sw==1)){if(--cnt2_2==0)cnt2_2=9;}elseif((status_2==1)&&(status_sw==2)){if(--cnt2_3==0)cnt2_3=9;}elseif((status_2==1)&&(status_sw==3)){if(--cnt2_4==0)cnt2_4=9;}}}if(S5==0)//储存{Delay_nms(10);if(S5==0){while(!S4);WRITE_EEP(345,cnt1_1);Delay_nms(10);WRITE_EEP(350,cnt1_2);Delay_nms(10);WRITE_EEP(355,cnt1_3);Delay_nms(10);WRITE_EEP(360,cnt1_4);Delay_nms(10);WRITE_EEP(365,cnt2_1);Delay_nms(10);WRITE_EEP(370,cnt2_2);Delay_nms(10);WRITE_EEP(375,cnt2_3);Delay_nms(10);WRITE_EEP(380,cnt2_4);Delay_nms(10);}}if(S3==0)//启动位翻转{Delay_nms(10);if(S3==0){while(!S3);PORTC|=(1<<4);//CPL_BIT(start_flag,0);}}/*if(start_flag==0x01)//启动{PORTC|=(1<<4);}if(start_flag==0x00)//停止{PORTC&=~(1<<4);}*/if(S4==0)//启动位翻转{Delay_nms(10);if(S4==0){while(!S4);PORTC&=~(1<<4);}}}voidmain(void){/*init_devices();cnt1_1=READ_EEP(345);Delay_nms(10);cnt1_2=READ_EEP(350);Delay_nms(10);cnt1_3=READ_EEP(355);Delay_nms(10);cnt1_4=READ_EEP(360);Delay_nms(10);cnt2_1=READ_EEP(365);Delay_nms(10);cnt2_2=READ_EEP(370);Delay_nms(10);cnt2_3=READ_EEP(375);Delay_nms(10);cnt2_4=READ_EEP(380);Delay_nms(10);*/while(1){scan();if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}Delay_nms(150);}//切换键*//*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);WRITE_EEP(350,cnt2);Delay_nms(10);WRITE_EEP(355,cnt3);Delay_nms(10);WRITE_EEP(360,cnt4);Delay_nms(10);WRITE_EEP(365,cnt5);Delay_nms(10);}//储存键*//*elseif(S9==0){if(status_2==0)if(++cnt1>99)cnt1=0;if(status_2==1)if(++cnt2>99)cnt2=0;if(status_2==2)if(++cnt3>99)cnt3=0;if(status_2==3)if(++cnt4>99)cnt4=0;if(status_2==4)if(++cnt5>99)cnt5=0;Delay_nms(150);}//递增键*//*elseif(S2==0){if(status_2==0)if(--cnt1==0)cnt1=99;if(status_2==1)if(--cnt2==0)cnt2=99;if(status_2==2)if(--cnt3==0)cnt3=99;if(status_2==3)if(--cnt4==0)cnt4=99;if(status_2==4)if(--cnt5==0)cnt5=99;Delay_nms(150);}//递减键elseif(S4==0){PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);}//QGelseif(S8==0){PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);}//M1elseif(S7==0){PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);}//M2elseif(S6==0){PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);}//M3elseif(S5==0){PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);}//M4elseif(S3==0){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);status_2=5;status=5;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*//*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}if(start_flag==0x01){PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/}}/************************************/#pragmavector=TIMER0_OVF_vect__interruptvoidtimer0_ovf_isr(void){//SREG=0x80;TCNT0=0x83;if(++i>3)i=0;switch(i){case0:if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}break;case1:if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}break;case2:if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
{Delay_1ms();
i++;
/******************************************/
uintconv(uintl)//数据转换子函数,定义i为无符号整型变量
longm;//定义x为无符号长整型局部变量
uintn;//定义y为无符号整型局部变量
m=(5000*(long)l)/1023;//将变量i转换成需要显示的形式
n=(uint)m;//将无符号长整型变量x强制转换成无符号整型变量y
returnn;//返回y的值
voidport_init(void)
PORTA=0x7F;
DDRA=0x00;
MCUCSR|=0x80;//使PORTB端口的JTAG功能无效
PORTB=0x00;
DDRB=0xFF;
MCUCSR|=0x80;//使PORTC端口的JTAG功能无效
PORTC=0x03;
DDRC=0xfC;
MCUCSR|=0x80;//使PORTD端口的JTAG功能无效
PORTD=0x00;
DDRD=0xff;
voidtimer0_init(void)
TCNT0=0x83;
TCCR0=0x04;
voidtimer1_init(void)
TCCR1A=0xA3;
TCCR1B=0x0A;
OCR1A=0x00;
OCR1B=0x00;
TCNT1H=0xD8;
TCNT1L=0xF0;
//***************看门狗初始化,分频系数为256K*************
voidwatchdog_init(void)
__watchdog_reset();
WDTCR=0x1b;
WDTCR=0x00;
voidadc_init(void)//模数转换初始化子函数
ADCSRA=0x00;
ADMUX=0x07;//选择ADC输入通道为7
ACSR=0x80;//关掉模拟比较器
ADCSRA=0xCE;//单次转换\中断
//ADCSRA=0xE9;//ADC中断使能,预分频器系数取2,ADC转换使能,自动触发使能
voidinit_devices(void)
__disable_interrupt();//disableallinterrupts
port_init();
timer0_init();
//timer1_init();
adc_init();
MCUCR=0x8A;//中断方式控制
GICR=0xC0;//使能中断方式
TIMSK=0x03;
__enable_interrupt();//re-enableinterrupts11
/****************************************************/
voidscan()
if(S10==0)//切换键
Delay_nms(10);
if(S10==0)
while(!
S10);
if(++status_2>2){status_2=0;}
if(S9==0)//个十百位选择
if(S9==0)
S9);
if(++status_sw>3){status_sw=0;}
if(status_sw==0){PORTB|=(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);}
elseif(status_sw==3){PORTB|=(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<2);PORTB&=~(1<<3);}
elseif(status_sw==2){PORTB|=(1<<2);PORTB&=~(1<<1);PORTB&=~(1<<0);PORTB&=~(1<<3);}
elseif(status_sw==1){PORTB|=(1<<3);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<0);}
if(S2==0)//递增
if(S2==0)
S2);
if((status_2==0)&&(status_sw==0))
if(++cnt1_1>9)cnt1_1=0;
elseif((status_2==0)&&(status_sw==1))
if(++cnt1_2>9)cnt1_2=0;
elseif((status_2==0)&&(status_sw==2))
if(++cnt1_3>9)cnt1_3=0;
elseif((status_2==0)&&(status_sw==3))
if(++cnt1_4>9)cnt1_4=0;
/////////////////////////////////////////
if((status_2==1)&&(status_sw==0))
if(++cnt2_1>9)cnt2_1=0;
elseif((status_2==1)&&(status_sw==1))
if(++cnt2_2>9)cnt2_2=0;
elseif((status_2==1)&&(status_sw==2))
if(++cnt2_3>9)cnt2_3=0;
elseif((status_2==1)&&(status_sw==3))
if(++cnt2_4>9)cnt2_4=0;
if(S1==0)//递减
if(S1==0)
S1);
if(--cnt1_1==0)cnt1_1=9;
if(--cnt1_2==0)cnt1_2=9;
if(--cnt1_3==0)cnt1_3=9;
if(--cnt1_4==0)cnt1_4=9;
if(--cnt2_1==0)cnt2_1=9;
if(--cnt2_2==0)cnt2_2=9;
if(--cnt2_3==0)cnt2_3=9;
if(--cnt2_4==0)cnt2_4=9;
if(S5==0)//储存
if(S5==0)
S4);
WRITE_EEP(345,cnt1_1);Delay_nms(10);
WRITE_EEP(350,cnt1_2);Delay_nms(10);
WRITE_EEP(355,cnt1_3);Delay_nms(10);
WRITE_EEP(360,cnt1_4);Delay_nms(10);
WRITE_EEP(365,cnt2_1);Delay_nms(10);
WRITE_EEP(370,cnt2_2);Delay_nms(10);
WRITE_EEP(375,cnt2_3);Delay_nms(10);
WRITE_EEP(380,cnt2_4);Delay_nms(10);
if(S3==0)//启动位翻转
if(S3==0)
S3);
PORTC|=(1<<4);
//CPL_BIT(start_flag,0);
/*if(start_flag==0x01)//启动
if(start_flag==0x00)//停止
PORTC&=~(1<<4);
}*/
if(S4==0)//启动位翻转
if(S4==0)
voidmain(void)
/*init_devices();
cnt1_1=READ_EEP(345);Delay_nms(10);
cnt1_2=READ_EEP(350);Delay_nms(10);
cnt1_3=READ_EEP(355);Delay_nms(10);
cnt1_4=READ_EEP(360);Delay_nms(10);
cnt2_1=READ_EEP(365);Delay_nms(10);
cnt2_2=READ_EEP(370);Delay_nms(10);
cnt2_3=READ_EEP(375);Delay_nms(10);
cnt2_4=READ_EEP(380);Delay_nms(10);*/
while
(1)
scan();
if(num>20){Delay_nms(10);if(flag==1){dis_val=(conv(value));flag=0;num=0;Delay_nms(100);}}
if(dis_val>(cnt1_1+cnt1_2*10+cnt1_3*100+cnt1_4*1000)){PORTC&=~(1<<4);}//M1驱动关闭
//if(dis_val<(cnt2_1+cnt2_2*10+cnt2_3*100+cnt2_4*1000)){PORTC|=(1<<4);}//M1驱动开起
/*if(S10==0){status_1=0;status=6;if(++status_2>4){status_2=0;}
Delay_nms(150);}//切换键*/
/*elseif(S1==0){WRITE_EEP(345,cnt1);Delay_nms(10);
WRITE_EEP(350,cnt2);Delay_nms(10);
WRITE_EEP(355,cnt3);Delay_nms(10);
WRITE_EEP(360,cnt4);Delay_nms(10);
WRITE_EEP(365,cnt5);Delay_nms(10);
}//储存键*/
/*elseif(S9==0){
if(status_2==0)if(++cnt1>99)cnt1=0;
if(status_2==1)if(++cnt2>99)cnt2=0;
if(status_2==2)if(++cnt3>99)cnt3=0;
if(status_2==3)if(++cnt4>99)cnt4=0;
if(status_2==4)if(++cnt5>99)cnt5=0;
Delay_nms(150);}//递增键*/
/*elseif(S2==0){
if(status_2==0)if(--cnt1==0)cnt1=99;
if(status_2==1)if(--cnt2==0)cnt2=99;
if(status_2==2)if(--cnt3==0)cnt3=99;
if(status_2==3)if(--cnt4==0)cnt4=99;
if(status_2==4)if(--cnt5==0)cnt5=99;
Delay_nms(150);}//递减键
elseif(S4==0){
PORTC|=(1<<3);PORTB|=(1<<0);status_2=0;status=0;status_1=1;Delay_nms(cnt1*100);PORTC&=~(1<<3);PORTB&=~(1<<0);
}//QG
elseif(S8==0){
PORTC|=(1<<4);PORTB|=(1<<1);status_2=1;status=1;status_1=1;Delay_nms(cnt2*100);PORTC&=~(1<<4);PORTB&=~(1<<1);
}//M1
elseif(S7==0){
PORTC|=(1<<5);PORTB|=(1<<2);status_2=2;status=2;status_1=1;Delay_nms(cnt3*100);PORTC&=~(1<<5);PORTB&=~(1<<2);
}//M2
elseif(S6==0){
PORTC|=(1<<6);PORTB|=(1<<3);status_2=3;status=3;status_1=1;Delay_nms(cnt4*100);PORTC&=~(1<<6);PORTB&=~(1<<3);
}//M3
elseif(S5==0){
PORTC|=(1<<7);PORTC|=(1<<2);status_2=4;status=4;status_1=1;Delay_nms(cnt5*100);PORTC&=~(1<<7);PORTC&=~(1<<2);
}//M4
elseif(S3==0){
PORTC|=(1<<4);PORTC|=(1<<5);PORTC|=(1<<6);PORTC|=(1<<7);
PORTC|=(1<<3);PORTB|=(1<<0);PORTB|=(1<<1);PORTB|=(1<<2);PORTB|=(1<<3);PORTC|=(1<<2);
status_2=5;status=5;
Delay_nms(cnt1*100);PORTC&=~(1<<3);
PORTC&=~(1<<4);PORTC&=~(1<<5);PORTC&=~(1<<6);PORTC&=~(1<<7);}
PORTB&=~(1<<0);PORTB&=~(1<<1);PORTB&=~(1<<2);PORTB&=~(1<<3);PORTC&=~(1<<2);*/
/*elseif(S3==0){Delay_nms(20);CPL_BIT(start_flag,0);Delay_nms(20);}
if(start_flag==0x01){
PORTC|=(1<<3);Delay_nms(cnt1*100);PORTC&=~(1<<3);
//PORTC=0x00;status_2=0;status=9;status_1=0;Delay_nms(cnt4*100);start_flag=0x00;}//运行键
if((start_flag==0x00)){watchdog_init();Delay_nms(400);}*/
/************************************/
#pragmavector=TIMER0_OVF_vect
__interruptvoidtimer0_ovf_isr(void)
//SREG=0x80;
if(++i>3)i=0;
switch(i)
case0:
if(status_2==0){PORTD=SEG7[cnt1_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}
if(status_2==1){PORTD=SEG7[cnt2_1];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}
if(status_2==2){PORTD=SEG7[0];PORTB&=~(1<<4);PORTB|=(1<<5)|(1<<6)|(1<<7);}
break;
case1:
if(status_2==0){PORTD=SEG7[cnt1_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}
if(status_2==1){PORTD=SEG7[cnt2_2];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}
if(status_2==2){PORTD=SEG7[(dis_val/10)%10];PORTB&=~(1<<5);PORTB|=(1<<4)|(1<<6)|(1<<7);}
case2:
if(status_2==0){PORTD=SEG7[cnt1_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}
if(status_2==1){PORTD=SEG7[cnt2_3];PORTB&=~(1<<7);PORTB|=(1<<4)|(1<<5)|(1<<6);}
if(status_2==2){PORTD=SEG7[(dis_val/100)%10];PORTB&=~(1<<7);PORTB|=(1<<
copyright@ 2008-2023 冰点文库 网站版权所有
经营许可证编号:鄂ICP备19020893号-2