AVR與VB通信模擬量輸入簡單程序
//----------------------------------------AVR代碼------------------------
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar seg[10]={0x3f,0x06,0x5b, //共陰極數(shù)碼管0~9的字形碼
0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar tab[4]={0xfe,0xfd,0xfb,0xf7};//4位共陰極數(shù)碼管的位選碼
#define UDRE 5
#define RXC 7
uchar datt; //接受到的數(shù)據(jù)比較變量
uchar adc_val; //模擬量轉(zhuǎn)換值
void delay(uint k)
{
while(k--);
}
void port_init()
{
PORTA=0x7f; // 通道7作為模擬量輸入
DDRA=0x7f;
PORTB=0xff;
DDRB=0xff;
PORTC=0xff;
DDRC=0xff;
PORTD=0xff;
DDRD=0xff;
}
void adc_init() //模擬量初始化
{
ADCSRA=0xe3;
ADMUX=0xc7;
}
uint adc_vert()
{
uint temp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return temp2;
}
void time0_init() //定時器0初始化
{
TCNT0=0x83;
TCCR0=0x03;
TIMSK=0x01; //定時器0溢出中斷
}
#pragma vector = 0x24
__interrupt void time0_vef()
{
static uchar i;
TCNT0=0x83;
i++;
if(i>3)i=0;
switch(i)
{
case 0:PORTA=seg[adc_val%10];PORTC=tab[0];break;
case 1:PORTA=seg[adc_val%100/10];PORTC=tab[1];break;
case 2:PORTA=seg[adc_val%1000/100];PORTC=tab[2];break;
case 3:PORTA=seg[adc_val/1000];PORTC=tab[3];break;
default:break;
}
}
關鍵詞:
AVRVB通信模擬量輸
評論