|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2013-3-17 11:21:57
|
显示全部楼层
本帖最后由 wxwdxx 于 2013-3-17 11:24 编辑
这个是我折中的办法,可以正常运行了。
把200ms的AD采样放在了timer0的中断里。
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.7e Professional
Automatic Program Generator
?Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:office@hpinfotech.com
Project :
Version :
Date : 2013/3/14
Author : F4CG
Company : F4CG
Comments:
Chip type : ATmega8L
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
flash char led_7[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
flash char position[4]={0xf7,0xfb,0xfd,0xfe};
unsigned char dis_buff[4];
unsigned char posit;
unsigned int adc_data;
unsigned int adc_v;
int i;
bit irrigation=0;
void adc_to_disbuffer(unsigned int adc)
{char i;
for(i=0;i<=3;i++)
{dis_buff=adc % 10;
adc /= 10;
}
}
void display(void)
{PORTB |= 0x0f;
PORTD = led_7[dis_buff[posit]];
if(posit==0&&(irrigation||PINB.5))
PORTD |= 0x80;
if(posit==3)
PORTD |= 0x80;
PORTB &= position[posit];
if(++posit>=4)
posit=0;
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x83;
// Place your code here
display();
if(++i>=100)
{i=0;
ADCSRA|=0x40;
}
if(irrigation||PINB.5)
PORTB.4=1;
else
PORTB.4=0;
}
#define ADC_VREF_TYPE 0x40
// ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void)
{
// Read the AD conversion result
adc_data=ADCW;
// Place your code here
adc_v=(unsigned long)adc_data*5000/1024;
adc_to_disbuffer(adc_v);
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
int j;
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=T State4=0 State3=1 State2=1 State1=1 State0=1
PORTB=0x0F;
DDRB=0x1F;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz
TCCR0=0x03;
TCNT0=0x83;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x8A;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
for(j=0;j<600;j++)
delay_ms(1000);
if(adc_v>=2500)
irrigation=1;
if(irrigation)
{for(j=0;j<40;j++)
delay_ms(1000);
irrigation=0;
}
};
}
|
|