/******************************************************************************//*Name:电子设计电源题功能:稳压,电路保护功能*//*Designedby:胡车,蒋睿,张佩小组Date:2013/9/521:09*//*开发环境:ICCAVR*//*PD0确定键,PD1增加键,PD2菜单键,PD7减小键,PD5输出PWM波,PD6接继电器*//*PA0~PA2液晶控制,PB口液晶数据口,PA5采样电压输入,PA6采样电流输入*//******************************************************************************/#includeiom16v.h#includemacros.h#include1602.h#defineucharunsignedchar#defineuintunsignedint#defineGet_Bit(val,bitn)(val&(1(bitn)))uintaddata;uintrec=1;ucharflag=1;//进入按键中断标志ucharkey_flag=0;//按键标志位ucharnum_flag=0;//数字改变标志ucharerr_flag=0;//出错类型标志uchardelay_flag=0;//适应电压标志uintvol_exp=368;//360理想输出电压uintnum1=1,num2=5;uintk1,k2;ucharcont1[]={LimitedCurrent:};ucharcont2[]={.A};uchartemp1[]={Voltage:.V},temp2[]={Current:.A},temp3[]={DUTYCYCLE:%};floattemp1float=0,temp2float=0;uinttemp1int=0,temp2int=0;voidport_init(void){PORTA=0xf8;//f8DDRA=0x07;PORTB=0x00;DDRB=0xFF;PORTC=0xFF;//m103outputonlyDDRC=0x00;PORTD=0x8F;DDRD=0x70;}//TIMER1initialize-prescale:1//WGM:5)PWM8bitfast,TOP=0x00FF//desiredvalue:31.25KHz//actualvalue:31.250KHz(0.0%)voidtimer1_init(void){TCCR1B=0x00;//stopTCNT1H=0xFF;//setupTCNT1L=0x01;OCR1AH=0x00;OCR1AL=0x30;//改变A占空比OCR1BH=0x00;OCR1BL=0x5E;//改变B占空比ICR1H=0x00;ICR1L=0xFF;TCCR1A=0xF1;TCCR1B=0x09;//startTimer}第2页共26页/*延时函数*/voiddelay_ms(unsignedintxms){inti,j;for(i=0;ixms;i++){for(j=0;j1140;j++);}}voiddelay(uintx){inti,j;for(i=0;ix;i++){for(j=0;j200;j++);}}//按下按键后待处理的程序voidkey_sol(void)//液晶界面{flag=0;key_flag=1;LcdInit();WriteChar(1,0,16,cont1);WriteChar(2,0,16,cont2);WriteNum(2,0,ASCII[num1]);WriteNum(2,2,ASCII[num2]);writecom(0x0f);//开光标显示writecom(0xc0);while(key_flag!=0){/*确定按键*/if(Get_Bit(PIND,PD0)==0){delay_ms(20);key_flag=key_flag+1;while(!(Get_Bit(PIND,PD0)));//等待按键松开delay_ms(20);}switch(key_flag){case1://调节电流NUM1/*增加按键*/if(Get_Bit(PIND,PD1)==0){num_flag=1;delay_ms(10);num1++;if(num1==10){num1=9;}WriteNum(2,0,ASCII[num1]);writecom(0xc0);while(!(Get_Bit(PIND,PD1)));//等待按键松开delay_ms(10);}/*减少按键*/if(Get_Bit(PIND,7)==0)第3页共26页{num_flag=1;delay_ms(10);num1--;if(num1==0){num1=1;}WriteNum(2,0,ASCII[num1]);writecom(0xc0);while(!(Get_Bit(PIND,PD7)));//等待按键松开delay_ms(10);}break;case2://调节电流NUM2writecom(0xc2);/*增加按键*/if(Get_Bit(PIND,PD1)==0){num_flag=1;delay_ms(10);num2++;if(num2==10){num2=9;}WriteNum(2,2,ASCII[num2]);writecom(0xc2);while(!(Get_Bit(PIND,PD1)));//等待按键松开delay_ms(10);}/*减少按键*/if(Get_Bit(PIND,7)==0){num_flag=1;delay_ms(10);num2--;if(num2==0){num2=1;}WriteNum(2,2,ASCII[num2]);writecom(0xc2);while(!(Get_Bit(PIND,PD7)));//等待按键松开delay_ms(10);}break;case3:key_flag=0;break;default:key_flag=0;}}}#pragmainterrupt_handlerint0_isr:iv_INT0voidint0_isr(void)第4页共26页{delay_ms(10);key_sol();//while(!(Get_Bit(PIND,PD2)));//等待按键松开delay_ms(10);key_flag=0;flag=1;LcdInit();}voidinit_devices(void){//stoperrantinterruptsuntilsetupCLI();//disableallinterruptsport_init();timer1_init();MCUCR=0x02;GICR=0x40;TIMSK=0x00;//timerinterruptsourcesSEI();//re-enableinterrupts//allperipheralsarenowinitialized}uintAD_GetData(uintad_input){ADMUX|=0x40;//ADC参考电压AVccADCSRA|=0x87;//使能ADC,128分频ADMUX=ad_input;//选择AD转换通道ADCSRA|=(1ADSC);//开始AD转换delay(1);while(!(ADCSRA&(1ADIF)));//等待转换结束ADCSRA|=(1ADIF);//关闭AD转换ADCSRA&=0x0f;returnADC;}/*系统修正*/voidsys_comp(){while(1){switch(delay_flag){case0:case1:if(temp1intvol_exp){OCR1AL=OCR1AL+3;delay_flag=0;}if(temp1intvol_exp){OCR1AL=OCR1AL-3;delay_flag++;}break;default:if(temp1intvol_exp){OCR1AL=OCR1AL+1;第5页共26页}if(temp1intvol_exp){OCR1AL=OCR1AL-1;}delay_ms(1);}ad_get();if(OCR1AL2){OCR1AL=2;delay_ms(20);if(temp1intvol_exp){delay_ms(30);if(temp1intvol_exp){err_flag=0;err_sol();PORTD&=0xbf;//打开继电器}}}if((temp2int(num1*100+num2*10))&&(OCR1AL150)){err_flag=1;err_sol();}}}voiderr_sol(){ucharcont3[]={SafeMode};ucharcont4[]={Caution:};ucharcont5[]={Voltage36V};switch(err_flag){case0:PORTD|=(1PD6);//关闭继电器LcdInit();WriteChar(1,0,14,cont4);WriteChar(2,0,14,cont5);OCR1AL=2;//初始化A占空比80%delay_flag=0;flag=0;while(!flag);//等待菜单按键按下delay_ms(50);break;case1:PORTD|=(1PD6);//关闭继电器LcdInit();WriteChar(1,0,14,cont3);//OCR1AL=0x51;//初始化A占空比80%delay_flag=0;flag=0;while(!flag);//等待菜单按键按下第6页共26页delay_ms(50);break;default:break;}}/*ad读取*/voidad_get(){//ucharASCII[]={0123456789};uchari;uchart;ucharr;addata=AD_GetData(0x45);//获得通道5数字量for(i=0;i9;i++){addata=addata+AD_GetData(0x45);}temp1float=addata*0.0048828125;temp1float=temp1float/9.08;temp1int=((uint)(temp1float*100)+1);temp1[8]=ASCII[temp1int/100];temp1[9]=ASCII[(temp1int%100)/10];temp1[11]=ASCII[temp1int%10];while(!flag);WriteChar(1,0,17,temp1);addata=AD_GetData(0x46);//获得通道6的数字量for(i=0;i9;i++){addata=addata+AD_GetData(0x46);}temp2float=addata*0.0048828125;temp2float=temp2float/21.0;temp2int=(uint)(temp2float*100);temp2[8]=ASCII[temp2int/100];temp2[10]=ASCII[(temp2int%100)/10];temp2[11]=ASCII[temp2int%10];while(!flag);WriteChar(2,0,17,temp2);r++;if(r==30){//LcdInit();}/*addata=AD_GetData(0x47);//获得通道7的数字量for(i=0;i9;i++){addata=addata+AD_GetData(0x47);}temp3float=addata*0.0048828125;temp3float=temp3float/21.