#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uint x=0;
void delay()
{
unsigned char j,k;
for(j=0;j<100;j++)
for(k=0;k<500;k++);
}
uint mega16_ad()
{
uint addata;
DDRA&=~BIT(PA0);
PORTA&=~BIT(PA0);
ADMUX=0;
ADCSR=0X80;
ADCSR|=BIT(ADSC);
while(!(ADCSR&(BIT(ADIF))));
addata=ADCL;
addata=addata+ADCH*256;
return addata;
}
void time0()
{
uint m;
while(1)
{
if(x>511) break;
for(m=0;m<7;m++)
{
PORTB&=~BIT(m);
delay();
if(x>511) break;
PORTB|=BIT(m);
delay();
}
}
}
void time1()
{
PORTB=0XFF;
}
void main()
{
DDRA=0X00;
PORTA=0XFF;
DDRB=0XFF;
PORTB=0XFF;
while(1)
{
x=mega16_ad();
if(x<511)
time0();
if(x>511)
time1();
}
}
此帖出自
小平头技术问答
一周热门 更多>