MAX176的驱动源码:
#include <mega16.h>
#include <delay.h>
#define STAT PORTB.0//开始控制位
#define CLK PORTB.1 //时钟位
#define DATA PINB.2 //数据位
#define KEY1 PINC.0
#define KEY2 PINC.1
#define KEY3 PINC.2
#define KEY4 PINC.3
/*----------------ad转换程序-------------------
参数:无
返回值:得到的AD值
计算公式:Vin=(D_return/4096)*5
Vin:输入的模拟量(范围:-4096~4096)
D_return:返回的数字量
-----------------------------------------------*/
int Get_Max176_Adc(void)
{
uchar t;
int result;
uchar ADCH=0,ADCL=0;
bit jud=0;
STAT=1; //上升沿开始转换
delay_us(4);//确保转换结束
STAT=0; //下降沿准备数据传输
CLK=1;CLK=0;//除去前导位
CLK=1;CLK=0;
jud=DATA;//获得符号值
for(t=0;t<4;t++)//获得高四位值
{
CLK=1;CLK=0;
ADCH<<=1;
ADCH+=DATA;
}
for(t=0;t<8;t++)//获得低八位值
{
CLK=1;CLK=0;
ADCL<<=1;
ADCL+=DATA;
}
result=(int)ADCH*256+ADCL;
if(jud)return -result;
else return result;
}
void display(unsigned char number,unsigned char position)//digtial show function
{
DDRA=0Xff;//set PORTA output
 ORTA=0XFF;//set PORTA output in high level
switch(position)//choice the position
{
case 1:{PORTA&=0xf8;break;}
case 2:{PORTA&=0xf9;break;}
case 3:{PORTA&=0Xfa;break;}
case 4:{PORTA&=0Xfb;break;}
case 5:{PORTA&=0Xfc;break;}
case 6:{PORTA&=0Xfd;break;}
case 7:{PORTA&=0Xfe;break;}
case 8:{PORTA&=0Xff;break;}
}
switch(number)//show number
{
case 0:{PORTA&=0x87;break;} //0
case 1:{PORTA&=0x8f;break;} //1
case 2:{PORTA&=0x97;break;} //2
case 3:{PORTA&=0x9f;break;} //3
case 4:{PORTA&=0xa7;break;} //4
case 5:{PORTA&=0xaf;break;} //5
case 6:{PORTA&=0xb7;break;} //6
case 7:{PORTA&=0xbf;break;} //7
case 8:{PORTA&=0xc7;break;} //8
case 9:{PORTA&=0xcf;break;} //9
default:{PORTA=0xff;break;} //null
}
}
void main(void)
{
uchar t=0;
int res1=0;
PORTB=0XFF;
DDRB=0XFB;
PORTD=0XFF;
DDRD=0XFF;
PORTB=0X00;
while(1)
{
for(t=0;t<5;t++)
{
res1+=Get_Max176_Adc();
}
res1=res1/5;
if(res1<0){res1=-res1ORTD=0X00;} //负值
display(res1%10,8);delay_us(1000);
display((res1/10)%10,7);delay_us(1000);
display((res1/100)%10,6);delay_us(1000);
display((res1/1000)%10,5);delay_us(1000);
res1=0;
PORTD=0XFF;
}
}
|