#include <iom16v.h>
#define uchar unsigned char
#define uint unsigned int
uchar const
SEG7[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar const ACT[4]={0b00001000,0b00000100,0b00000010,0b00000001};
uint adc_val,dis_val;
uchar i,cnt;
/************************************************/
void port_init(void)
{
PORTA = 0x00;
DDRA
= 0x00;
PORTB = 0xFF;
DDRB
= 0xFF;
PORTC = 0x00;
DDRC
= 0xff;
//PORTD = 0xff;
//DDRD = 0xFF;
}
/************************************************/
void adc_init(void)
{
ADCSRA = 0xE3;
ADMUX=0b11100101 ; //ADMUX = 0x46;
}
//***************************
void timer0_init(void)
{
TCNT0 = 0x83;
TCCR0 = 0x03;
TIMSK = 0x01;
}
/*********************************************/
void init_devices(void)
{
port_init();
timer0_init();
adc_init();
SREG =0x80;
}
//***************************
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x83;
cnt++;
}
//=========================
uint ADC_Convert(void)
{uint temp1,temp2;
adc_init();
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<2)+(temp1>>6);//temp2=(temp2<<8)+temp1;
return(temp2);
}
/**************************/
uint conv(uint i)
{
long x;
uint y;
x=(2560*(long)i)/1023; //x=(5000*(long)i)/1023;
y=(uint)x;
return y;
}
void delay(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<1141;j++);
}
}
/***********************/
void main(void)
{
init_devices();
/*delay(400);
DDRA=0X7F;PORTA=0X7F;
DDRC=0XFF;PORTC=0X00;
DDRB=0XFF;PORTB=0X00; */
while(1)
{
if(cnt>100)
{
adc_val=ADC_Convert();
dis_val=conv(adc_val);
cnt=0;
}
delay(10);
for(i=0;i<100;i++)
{
PORTC=ACT[0];
PORTB=SEG7[dis_val%10];
delay(1);
PORTC=ACT[1];
PORTB=SEG7[dis_val/10%10];
delay(1);
PORTC=ACT[2];
PORTB=SEG7[dis_val/100%10];
delay(1);
PORTC=ACT[3];
PORTB=SEG7[dis_val/1000];
PORTB&=0B01111111 ;
delay(1);
}
}
}
仿真图