|
发表于 2009-6-22 13:50:32
|
显示全部楼层
临时搭了一个,大概0.xS测一次,使用串口助手查看。
拿灯照着大概800+。
平常400到800左右不固定。
拿手捂了一会,怎么会出现800+和0交替出现呢?(AD采样不对?二极管特性?)
一下是俺的程序:
//===========================================================
/*
//By: SlowHand
*/
#include "iom16v.h"
#include <macros.h>
#define AD_FLAG (1 << ADIF)
#define AD_SC (1 << ADSC)
#define AD_EN (1 << ADEN)
void UartInit(void){
UCSRB = 0x00; //Close usart
UCSRA = 0x02; //Document page151
UCSRC = 0x06; //Write UCSRC,set 8 bit
UBRRH = 0x00;
UBRRL = 0x0C; //BAUD 9600(1M->0C;6M->4D)
UCSRB = 0x98; //open usart ,enable transmit and receive
}
void SetUpPort(void){
DDRA = 0x00;//(0->input; 1->output)
PORTA = 0x00;//low voltage
DDRB = 0xFF;//output
PORTB = 0xFF;//high
DDRC = 0xFF;
PORTC = 0xFF;
DDRD = 0xFF;
PORTD = 0xFF;
}
unsigned int GetAD(void) {
unsigned int tmp_AD;
ADMUX = 0xC6; // 1100 0xxx AREF(INNER); align(right); AD port(6)
ADCSRA = 0xC4; // 1100 0100 frequency( 1M / 16 = 62.5K )
while(!(ADCSRA & AD_FLAG)) _NOP(); //Wait for AD ready flag set(when ADIF Bit = 0)
for(tmp_AD=0x20; tmp_AD; tmp_AD--) _NOP(); //Delay time
tmp_AD = ADC;
ADCSRA &= ~AD_FLAG; //clear flag(set ADIF Bit = 1)
ADCSRA &= ~AD_SC; //Close A-D convert
return tmp_AD;
}
void Uart_Transmit(unsigned char i) {
while (!(UCSRA & (1<<UDRE)));//wait till null
UDR = i;//send data
}
void main(void){
unsigned int i;
char str[8];
SetUpPort();
UartInit();
while(1){
for(i=0; i<0xFFFF; i++){ _NOP(); }
Uart_Transmit( 'A' );
Uart_Transmit( 'D' );
Uart_Transmit( ':' );
itoa( str, GetAD(), 10 );
for(i=0; i<8; i++){
if( str == '\0' ) break;
Uart_Transmit( str );
}
Uart_Transmit( '\n' );
}
} |
|