初学者:救救我啊,谁来帮我看看我的程序有什么问题,怎么得不到正确结果?
先多谢你的帮助,我是从串口发过去这样的代码(hex):0a 31 0d,我希望单片机能返回
来A/D检测的值,但结果是无论我怎么改变A/D端口的输入电压返回来的值都无法改变,我
怀疑是A/D转换根本没有修改数据寄存器的值,但我检查了程序,发现不了问题出在那里
啊,我的程序如下,麻烦各位帮帮忙,多谢!!!
/*********************************************
Chip type : AT90S8535
Clock frequency : 11.059200 MHz
Memory model : Small
Internal SRAM size : 512
External SRAM size : 0
Data Stack size : 128
*********************************************/
#include<90s8535.h>
#include<delay.h>
#define UDRE USR.5
#define ADSC ADCSR.6
typedef unsigned char uchar;
typedef unsigned int uint;
uchar K,adc_h,adc_l;
bit FirstRceFlag,FinishRceFlag;
#define ADC_VREF_TYPE 0x00
// ADC interrupt service routine
#pragma savereg-
interrupt [ADC_INT] void adc_isr(void)
{
adc_l=ADCL;
adc_h=ADCH;
}
#pragma savereg+
// Read the ADC conversion result
// with noise canceling
void UART_send(uchar d)
{
UDR=d;
while(UDRE==0);
}
interrupt [UART_RXC] void UART_receive(void)
{
uchar j;
K=UDR;
if(K==0x0a)
{
FirstRceFlag=1;
}
else if(K==0x0d)
{
FirstRceFlag=0;
FinishRceFlag=1;
}
else if(FirstRceFlag==1)
{
switch(K)
{
case 0x30:j=0;break;
case 0x31:j=1;break;
case 0x32:j=2;break;
case 0x33:j=3;break;
case 0x34:j=4;break;
case 0x35:j=5;break;
case 0x36:j=6;break;
case 0x37:j=7;break;
}
ADMUX=j|ADC_VREF_TYPE;
ADSC=1;
UART_send(0x0A);
UART_send(K);
UART_send(adc_h);
UART_send(adc_l);
UART_send(0x0D);
}
}
void init(void)
{
// Input/Output Ports initialization
// Port A
PORTA=0x00;
DDRA=0x00;
// Port B
PORTB=0x00;
DDRB=0x00;
// Port C
PORTC=0x00;
DDRC=0xFF;
// Port D
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Output Compare
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Output Compare
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Output Compare
// OC2 output: Disconnected
TCCR2=0x00;
ASSR=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
GIMSK=0x00;
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// UART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// UART Receiver: On
// UART Transmitter: On
// UART Baud rate: 9600
UCR=0x98;
UBRR=0x47;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
// ADC initialization
// ADC Clock frequency: 5529.600 kHz
ADCSR=0x89;
// Global enable interrupts
#asm("sei")
}
void main(void)
{
init();
while (1)
{
delay_ms(500);
PORTC=~PORTC;
};
}
发表时间:2002年9月4日12:37:00