/*
* dianyadianliubiao.c
*
* Created: 2015/1/4 19:09:40
* Author: Administrator
*/
//////////////////////////////////////////////////////////////////////////
// 端口说明// // PA0 u+,PA1 u-,PA3 I+,PA4 I- //
// PB0~PB7 LCD数据// // PC0 Um=5V,PC1 Um=20V,PC2 Im=2A,PC3 Im=20A // // PD0 E,PD1 RW,PD2 RS // //////////////////////////////////////////////////////////////////////////
#include
#include
#define BIT(x) 1<
//////////////////////////////////////////////////////////////////////////
// 液晶宏定义// //////////////////////////////////////////////////////////////////////////
#define RS_H PORTD|=BIT(2)
#define RS_L PORTD&=~(BIT(2))
#define RW_H PORTD|=BIT(1)
#define RW_L PORTD&=~(BIT(1))
#define E_H PORTD|=BIT(0)
#define E_L PORTD&=~(BIT(0))
#define SHUJU PORTB
//////////////////////////////////////////////////////////////////////////
#define CHONGFU 100
int a=0,umax=1,imax=1,xuanze=1;
unsigned int junzhi=0;
char cishu=0;
void lcd_chushihua()
{
char u[]="U V V",
i[]="I A A";
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 (int j=0;j<16;j++)
{
shuju(u[j]);
}
zhiling(0x80+0x40);
for (int j=0;j<16;j++)
{
shuju(i[j]);
}
}
void adc_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);
}
void jianpan_chushihua()
{
DDRC&=0xf0;
PORTC|=0x0f;
}
int jiance()
{
int i;
i=PINC|0xf0;
if (i==0xff)
{
return 0;
}
else
{
return 1;
}
}
void shibie()
{
int i;
i=(PINC|0xf0);
switch(i)
{
case 0xfd:umax=2;break;
case 0xfe:umax=1;break;
case 0xfb:imax=1;break;
case 0xf7:imax=2;break;
default:;
}
}
void zhiling(int com)
{
RS_L;
RW_L;
SHUJU=com;
E_H;
_delay_us(100);
E_L;
_delay_ms(1);
}
void shuju(int data)
{
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();
}
}
}
void u_20v()//x10倍
{
zhiling(0x80+12);
shuju('2');
shuju('0');
int e;
char b[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 (int i=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 (int i=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;
}
}
}
void u_5v()//x2倍
{
zhiling(0x80+12);
shuju(' ');
shuju('5');
int e;
char b[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('!');
}
else
{
if (a<5)
{
a=0;
}
for (int i=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('!');
shuju('!');
shuju('!');
}
else
{
if (a<5)
{
a=0;
}
for (int i=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;
}
}
}
void i_200ma()
{
zhiling(0x80+0x4b);
shuju('0');
shuju('.');
shuju('2');
int e;
char b[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 (int i=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 (int i=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;
}
}
}
void i_5a()
{
zhiling(0x80+0x4b);
shuju(' ');
shuju(' ');
shuju('5');
int e;
char b[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 (int i=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju(' ');
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/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 (int i=2;i>=0;i--)
{
b[i]=((a%10)+'0');
a/=10;
}
zhiling(0x80+0x42);
shuju(' ');
shuju(b[0]);
shuju('.');
shuju(b[1]);
shuju(b[2]);
shuju(' ');
}
xuanze=-xuanze;
}
}
}
int main(void)
{
sei();
lcd_chushihua();
adc_chushihua();
while(1)
{
}
}