|
我这两天调一个mega48单片机,其中要用到AD采样。在调试中遇到AD只能在初始化完后采样一次,然后ADSL寄存器里面的值就不变了。我不管设自动采样,还是触发采样,都一样的结果。调了两天也没找到原因。请帮忙指教啊!附原程序
#include <iom48v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define PWM_PIND 0x01
#define LED0_PINB 0x80
#define LED1_PINB 0x40
#define LED2_PIND 0x10
#define Show_DATA_B 0x01 //有效位
#define Show_DATA_D 0xee //有效位
#define Show_IO_CLR_B (~(LED0_PINB|LED1_PINB|Show_DATA_B) ) //清除有效显示数据
#define Show_IO_CLR_D (~(LED2_PIND|Show_DATA_D)) //清除有效显示数据
#define KEY1_PINB 0x20 //按键
#define KEY2_PINB 0x04
#define BF_PINB 0x02 //
#define moto_wait 0x00 //等待
#define moto_run 0x01 //运行
#define moto_haf 0x02 //运行中
#define moto_ach 0x03 //完成
#define set_max 340 //最大值
#define set_min 80 //最小值
uchar Port_B,Port_D;
uchar point_save;
uchar led_7[10]={0x6f,0x42,0xab,0xea,0xc6,0xec,0xed,0x4a,0xef,0xee}; //0-9
uchar led_end[3]={0xad,0xc1,0xe3};
uchar add_datas[40]={15,40,35,30,26,23,20,17,15,12,9,9,8,8,7,7,6,6,5,5,5,4,4,4,4,4,3,3,3,2,2,2,2,2,2,2};
uchar show_led[5]; //显存
uchar Time1s_end;
uchar moto_states; //状态
uint setup_kf; //设置
uint run_kf; //实际运行
uint gun_on;
uint time_delay; //延时
uchar FIR_10=0; //滤波结果
//-------------------------------------------------------------
void show_data(uint a);
//-------------------------------------------------------------
void timer1_init(void) //time1 setup
{
TCCR1B = 0x00; //stop
TCNT1H = 0xC6; //setup
TCNT1L = 0x67;
OCR1AH = 0x39;
OCR1AL = 0x99;
OCR1BH = 0x39;
OCR1BL = 0x99;
ICR1H = 0x39;
ICR1L = 0x99;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer
}
void ADC_init(void) //启动AD采样
{
ADCSRA = 0x00; //disable adc
ADMUX = 0x07; //select adc input 0
ACSR = 0x80;
ADCSRB = 0x00;
ADCSRA = 0xef;
}
#pragma interrupt_handler Read_ad:22
void Read_ad(void)
{
FIR_10=ADCL; //读取AD值
}
//---------------------------数据显示转换--------------------------------
void show_data(uint a)
{
show_led[0]=a/100;
a%=100;
show_led[1]=a/10;
show_led[2]=a%10;
show_led[3]=0;
show_led[4]=0;
}
void show_end(void)
{
show_led[0]='e';
show_led[1]='n';
show_led[2]='d';
show_led[3]=6;
show_led[4]=0;
}
//---------------------------开启关闭MOSE--------------------------------
void close_mosft(void)
{
Port_D=Port_D&(~PWM_PIND);
//Port_D=Port_D|PWM_PIND;
}
void open_mosft(void)
{
Port_D=Port_D|PWM_PIND;
//Port_D=Port_D&(~PWM_PIND);
}
//--------------------------中断显示数据---------------------------------
void sent_show_led(void)
{
static uchar i=0;
uchar a=0;
Port_B=Port_B&Show_IO_CLR_B;
Port_D=Port_D&Show_IO_CLR_D;
if(show_led[3]|show_led[4])
{
if(show_led[4]>0)--show_led[4];
else
{
--show_led[3];
show_led[4]=200;
}
}
if(!(show_led[3]%2))
{
if(++i>4)
{
i=0;
}
if(i==0)Port_D|=LED2_PIND;
else if(i==1)Port_B|=LED1_PINB;
else if(i==2)Port_B|=LED0_PINB;
if(show_led<10)
{
a=led_7[show_led];
}
else if(show_led==' ')
{
a=0;
}
else
{
a=led_end;
}
Port_B=Port_B|(a&Show_DATA_B);
Port_D=Port_D|(a&Show_DATA_D);
}
PORTB=~Port_B;
PORTD=~Port_D;
}
//------------------------1ms interrupt-------------------------------
#pragma interrupt_handler timer1_ovf_isr:14
void timer1_ovf_isr(void)
{
//CLI();
TCNT1H = 0xf8;
TCNT1L = 0x2f;
//SEI();
Time1s_end=1;
sent_show_led();
}
void main()
{
uchar start_read=0;
uint a;
int i;
CLI();
PORTC=0;
DDRC=0;
DDRB = 0xC1; //IO设置
DDRD=0XFF;
timer1_init();
ADC_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI();
while (1)
{
if(Time1s_end)
{
Time1s_end=0;
show_data(FIR_10); //ADCL 显示采样结果,结果一直都不变啊
}
}
} |
|