device đo dung lượng pin là LM4041
Em đã thử viết nhưng kết quả trả về đều là giá trị 0. Có lẽ em chưa hiểu nguyên lý nó hoạt động thế nào. Và vấn đề hình như có liên quan đến Vref.
sau đây là đoạn code:
Code:
#include <p18f2620.h>
#include <delays.h>
#include <usart.h>
#include <adc.h>
#include <stdio.h>
#pragma config OSC = HS, WDT=OFF, LVP=ON
void main (void)
{
int result,i;
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD,
ADC_CH9 & ADC_INT_OFF & ADC_VREFPLUS_VDD,3);
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON &
USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_CONT_RX & USART_BRGH_HIGH, 8); //8 bits, 57600
Delay10TCYx(5); //delay for 50T cycle
TRISA=0x2F;
TRISB=0xFF;
while(1){
ConvertADC(); //start conversion
while(BusyADC()); //wait for completion
result = ReadADC(); //read result
Delay1KTCYx(100000); //delay
Delay1KTCYx(100000);
PORTAbits.RA4=0;
printf("%x,",result);
Delay1KTCYx(100000);
Delay1KTCYx(100000);
PORTAbits.RA4=1;
}
}