我正在尝试使用 pic18f14k50 控制器读取一种鼠标的模拟信号。这里是简单的电路:http ://dl.dropbox.com/u/14663091/schematiconew.pdf 。我必须从 AN9 电路端口读取模拟信号。Main 函数从端口读取,如果达到阈值,则闪烁 30 次:
void main(void) {
InitializeSystem();
#if defined(USB_INTERRUPT)
USBDeviceAttach();
#endif
while(1) {
if((USBDeviceState < CONFIGURED_STATE)||(USBSuspendControl==1)) continue;
if(!HIDTxHandleBusy(lastTransmission))
{
int readed = myReadADC2(); //Here i tried both myReadADC2() or myReadADC1()
if(readed>40) { //If read threshold > 40, blink led 30 times
int i;
for(i=0; i<30; i++) {
Delay1KTCYx(0);
mLED_1_On();
Delay1KTCYx(0);
mLED_1_Off();
}
}
lastTransmission = HIDTxPacket(HID_EP, (BYTE*)hid_report_in, 0x03);
}//end while
}//end main
我使用两种方法从 AN9 端口读取,myReadADC() 使用 OpenADC() API 方法:
int myReadADC(void) {
#define ADC_REF_VDD_VDD_X 0b11110011
OpenADC(ADC_FOSC_RC & ADC_RIGHT_JUST & ADC_12_TAD, ADC_CH9 & ADC_INT_OFF, ADC_REF_VDD_VDD_X & ADC_REF_VDD_VSS, 0b00000010); // channel 9
SetChanADC(ADC_CH9);
ConvertADC(); // Start conversion
while(BusyADC()); // Wait for completion
return ReadADC(); // Read result
}
和 myReadADC2(),实现从端口手动读取。
int myReadADC2() {
int iRet;
OSCCON=0x70; // Select 16 MHz internal clock
ANSEL = 0b00000010; // Set PORT AN9 to analog input
ANSELH = 0; // Set other PORTS as Digital I/O
/* Init ADC */
ADCON0=0b00100101; // ADC port channel 9 (AN9), Enable ADC
ADCON1=0b00000000; // Use Internal Voltage Reference (Vdd and Vss)
ADCON2=0b10101011; // Right justify result, 12 TAD, Select the FRC for 16 MHz
iRet=100;
ADCON0bits.GO=1;
while (ADCON0bits.GO); // Wait conversion done
iRet=ADRESL; // Get the 8 bit LSB result
iRet += (ADRESH << 8); // Get the 2 bit MSB result
return iDelay;
}
两种情况都不起作用,我触摸(发送模拟信号)端口 AN9,但是当我设置高阈值(~50)时 LED 不闪烁,低阈值(~0)当我为 PIC 供电时它会立即闪烁。也许我使用了错误的端口?我实际上是通过 AN9 作为读取端口?或者阈值是错误的?我怎样才能找到正确的价值?谢谢
这里是 MPLAB C18 API http://dl.dropbox.com/u/14663091/API%20microchip%20C18.pdf。