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) {
case 1://调节电流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) {
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;
case 2://调节电流NUM2 writecom(0xc2); /*增加按键*/
if(Get_Bit(PIND,PD1)==0) {
num_flag=1; delay_ms(10); num2++; if(num2==10)
第21页 共26页
{ 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; case 3:
key_flag=0; break; default:
key_flag=0; } } }
#pragma interrupt_handler int0_isr:iv_INT0 void int0_isr(void) {
delay_ms(10); key_sol();
//while(!(Get_Bit(PIND,PD2)));//等待按键松开 delay_ms(10); key_flag=0; flag=1; LcdInit(); }
void init_devices(void) {
//stop errant interrupts until set up CLI(); //disable all interrupts port_init(); timer1_init(); MCUCR = 0x02; GICR = 0x40;
TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts
//all peripherals are now initialized }
uint AD_GetData(uint ad_input) {
ADMUX|=0x40; //ADC参考电压AVcc
第22页 共26页
  ADCSRA|=0x87;  //使能ADC,128分频   ADMUX=ad_input; //选择AD转换通道    ADCSRA|=(1<     while(!(ADCSRA&(1< /*系统修正*/ void sys_comp() {       while(1)  {    switch (delay_flag)   {     case 0:    case 1:     if(temp1int     OCR1AL=OCR1AL+3;     delay_flag=0;    }     if(temp1int>vol_exp)    {      OCR1AL=OCR1AL-3;     delay_flag++;    }     break;    default:     if(temp1int     OCR1AL=OCR1AL+1;    }     if(temp1int>vol_exp)    {      OCR1AL=OCR1AL-1;    }     delay_ms(1);   }    ad_get();    if(OCR1AL<2)   {     OCR1AL=2;    delay_ms(20);         if(temp1int>vol_exp)    {      delay_ms(30);  if(temp1int>vol_exp)     {   err_flag=0;      err_sol();       PORTD&=0xbf;//打开继电器  }    }    }    if((temp2int>(num1*100+num2*10))&&(OCR1AL>150))  第23页  共26页      {     err_flag=1;    err_sol();   }  }  }  void err_sol() {   uchar cont3[]={\       \  uchar cont4[]={\        \  uchar cont5[]={\oltage >36V     \  switch (err_flag)  {    case 0:    PORTD|=(1<   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;   case 1:    PORTD|=(1<   WriteChar(1,0,14,cont3);       // OCR1AL = 0x51;//初始化A占空比80%   delay_flag=0;     flag=0;    while(!flag);//等待菜单按键按下   delay_ms(50);   break;   default:   break;  } }   /*ad读取*/ void ad_get() {   //uchar ASCII[]={\  uchar i;  uchar t;  uchar r;   addata=AD_GetData(0x45); //获得通道5数字量  for(i=0;i<9;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[(temp1int0)/10];  temp1[11]=ASCII[temp1int];   while(!flag);   WriteChar(1,0,17,temp1);  第24页  共26页     addata=AD_GetData(0x46); //获得通道6的数字量  for(i=0;i<9;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[(temp2int0)/10];  temp2[11]=ASCII[temp2int];   while(!flag);      WriteChar(2,0,17,temp2);  r++;   if(r==30)  {    //LcdInit();  } /*   addata=AD_GetData(0x47); //获得通道7的数字量  for(i=0;i<9;i++)  {    addata=addata+AD_GetData(0x47);  }   temp3float=addata*0.0048828125;  temp3float=temp3float/21.0;   temp3int=(uint)(temp3float*100);  temp3[2]=ASCII[temp3int/100];   temp3[4]=ASCII[(temp3int0)/10];  temp3[5]=ASCII[temp3int];   while(!flag);    WriteChar(2,8,7,temp3);  */   //WriteChar(2,0,?,temp3);//显示占空比  delay_ms(1); }  /*主函数*/ void main(void) {     SREG|=0x80;    DDRA=0xff;    PORTA=0xff;    init_devices();    LcdInit();    while(1)    {        sys_comp();      } }  第25页  共26页