atmega16电压电流表.docx
《atmega16电压电流表.docx》由会员分享,可在线阅读,更多相关《atmega16电压电流表.docx(23页珍藏版)》请在冰点文库上搜索。
atmega16电压电流表
/*
*dianyadianliubiao.c
*
*Created:
2015/1/419:
09:
40
*Author:
Administrator
*/
//////////////////////////////////////////////////////////////////////////
//端口说明//
//PA0u+,PA1u-,PA3I+,PA4I-//
//PB0~PB7LCD数据//
//PC0Um=5V,PC1Um=20V,PC2Im=2A,PC3Im=20A//
//PD0E,PD1RW,PD2RS//
//////////////////////////////////////////////////////////////////////////
#include
#include
#defineBIT(x)1<#include
//////////////////////////////////////////////////////////////////////////
//液晶宏定义//
//////////////////////////////////////////////////////////////////////////
#defineRS_HPORTD|=BIT
(2)
#defineRS_LPORTD&=~(BIT
(2))
#defineRW_HPORTD|=BIT
(1)
#defineRW_LPORTD&=~(BIT
(1))
#defineE_HPORTD|=BIT(0)
#defineE_LPORTD&=~(BIT(0))
#defineSHUJUPORTB
//////////////////////////////////////////////////////////////////////////
#defineCHONGFU100
inta=0,umax=1,imax=1,xuanze=1;
unsignedintjunzhi=0;
charcishu=0;
voidlcd_chushihua()
{
charu[]="UVV",
i[]="IAA";
DDRD=0xff;
PORTD=0;
DDRB=0xff;
PORTB=0;
_delay_ms(15);
zhiling(0x38);
_delay_ms(5);
zhiling(0x38);
zhiling(0x08);
zhiling(0x01);
zhiling(0x06);
zhiling(0x0c);
zhiling(0x80);
for(intj=0;j<16;j++)
{
shuju(u[j]);
}
zhiling(0x80+0x40);
for(intj=0;j<16;j++)
{
shuju(i[j]);
}
}
voidadc_chushihua()
{
ADMUX=BIT(REFS0)|BIT(REFS1)|(0x10);//2.56V片内参考电压//差分输入AD0+AD1-
ADCSRA|=BIT(ADEN)|BIT(ADATE)|BIT(ADPS0)|BIT(ADPS1)|BIT(ADIE)|BIT(ADSC);//使能ADC功能8分频,连续。
_delay_ms
(1);
}
voidjianpan_chushihua()
{
DDRC&=0xf0;
PORTC|=0x0f;
}
intjiance()
{
inti;
i=PINC|0xf0;
if(i==0xff)
{
return0;
}
else
{
return1;
}
}
voidshibie()
{
inti;
i=(PINC|0xf0);
switch(i)
{
case0xfd:
umax=2;break;
case0xfe:
umax=1;break;
case0xfb:
imax=1;break;
case0xf7:
imax=2;break;
default:
;
}
}
voidzhiling(intcom)
{
RS_L;
RW_L;
SHUJU=com;
E_H;
_delay_us(100);
E_L;
_delay_ms
(1);
}
voidshuju(intdata)
{
RS_H;
RW_L;
SHUJU=data;
E_H;
_delay_us(100);
E_L;
RS_L;
_delay_ms
(1);
}
SIGNAL(SIG_ADC)
{
if(jiance())
{
_delay_ms(10);
if(jiance())
{
shibie();
while(jiance());
}
}
if(xuanze==1)
{
ADMUX=BIT(REFS0)|BIT(REFS1)|(0x10);//2.56V片内参考电压//差分输入AD0+AD1-
_delay_ms
(1);
if(umax==1)
{
u_5v();
}
else
{
u_20v();
}
}
else
{
ADMUX=BIT(REFS0)|BIT(REFS1)|(0x1b);//2.56V片内参考电压//差分输入AD3+AD2-
_delay_ms
(1);
if(imax==1)
{
i_5a();
}
else
{
i_200ma();
}
}
}
voidu_20v()//x10倍
{
zhiling(0x80+12);
shuju('2');
shuju('0');
inte;
charb[6]="";
a=ADCL;
a+=(ADCH<<8);
if((a>>9)==0)
{
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560/512*a;
e=a;
if(a>2000)
{
zhiling(0x80+3);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
for(inti=3;i>=0;i--)
{
if(a==0)
{
b[i]=b[4];
}
else
{
b[i]=((a%10)+'0');
a/=10;
}
}
if(e<100)
{
if(a<50)
{
b[2]='0';
b[3]='0';
}
b[1]='0';
}
zhiling(0x80+0x02);
shuju('');
shuju(b[0]);
shuju(b[1]);
shuju('.');
shuju(b[2]);
shuju(b[3]);
shuju('');
}
xuanze=-xuanze;
}
}
else
{
a|=0xfc00;
a=~(a-1);
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560/512*a;
e=a;
if(a>2000)
{
zhiling(0x80+3);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
for(inti=3;i>=0;i--)
{
if(a==0)
{
b[i]=b[4];
}
else
{
b[i]=((a%10)+'0');
a/=10;
}
}
if(e<100)
{
b[1]='0';
if(a<50)
{
b[2]='0';
b[3]='0';
}
}
zhiling(0x80+0x02);
shuju('-');
shuju(b[0]);
shuju(b[1]);
shuju('.');
shuju(b[2]);
shuju(b[3]);
shuju('');
}
xuanze=-xuanze;
}
}
}
voidu_5v()//x2倍
{
zhiling(0x80+12);
shuju('');
shuju('5');
inte;
charb[6]="";
a=ADCL;
a+=(ADCH<<8);
if((a>>9)==0)
{
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
e=a;
if(a>500)
{
zhiling(0x80+3);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<5)
{
a=0;
}
for(inti=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x02);
shuju('');
shuju('');
shuju(b[0]);
shuju('.');
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
else
{
a|=0xfc00;
a=~(a-1);
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
e=a;
if(a>500)
{
zhiling(0x80+3);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<5)
{
a=0;
}
for(inti=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x02);
shuju('');
shuju('-');
shuju(b[0]);
shuju('.');
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
}
voidi_200ma()
{
zhiling(0x80+0x4b);
shuju('0');
shuju('.');
shuju('2');
inte;
charb[6]="";
a=ADCL;
a+=(ADCH<<8);
if((a>>9)==0)
{
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560/512*a;
e=a;
if(a>2000)
{
zhiling(0x80+0x43);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<20)
{
a=0;
}
for(inti=3;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju('');
shuju('0');
shuju('.');
shuju(b[0]);
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
else
{
a|=0xfc00;
a=~(a-1);
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560*a/512;
e=a;
if(a>2000)
{
zhiling(0x80+0x43);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<20)
{
a=0;
}
for(inti=3;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju('-');
shuju('0');
shuju('.');
shuju(b[0]);
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
}
voidi_5a()
{
zhiling(0x80+0x4b);
shuju('');
shuju('');
shuju('5');
inte;
charb[6]="";
a=ADCL;
a+=(ADCH<<8);
if((a>>9)==0)
{
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560/512*a/6;
e=a;
if(a>500)
{
zhiling(0x80+0x43);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<5)
{
a=0;
}
for(inti=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju('');
shuju('');
shuju(b[0]);
shuju('.');
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
else
{
a|=0xfc00;
a=~(a-1);
junzhi+=a;
cishu++;
if(cishu==CHONGFU)
{
a=junzhi/CHONGFU;
junzhi=0;
cishu=0;
a=2560/512*a/6;
e=a;
if(a>500)
{
zhiling(0x80+0x43);
shuju('M');
shuju('a');
shuju('x');
shuju('!
');
shuju('!
');
shuju('!
');
}
else
{
if(a<5)
{
a=0;
}
for(inti=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju('');
shuju('-');
shuju(b[0]);
shuju('.');
shuju(b[1]);
shuju(b[2]);
shuju('');
}
xuanze=-xuanze;
}
}
}
intmain(void)
{
sei();
lcd_chushihua();
adc_chushihua();
while
(1)
{
}
}