Re: 雙輸入問題
|
||||
---|---|---|---|---|
資深會員
|
這個看來是 PICC Workshop 的範例改的,但不曉得為什麼要改成這樣,一進去就 ADGO ? 如果要作完 AD 之後先切 port 準備下次,減少 delay,也是可以,但程式裡並沒有真正去做?
原來的 A2D 是打算以 A2D(0) 作 AN0, A2D(1) 作 AN1, ... 此外,由於 Sample/Hold 要一點時間,我把 delay 放到切換 channel 後、 ADGO 之前,這樣比較直覺:
void A2D( unsigned char channel )
發表於: 2007/5/16 11:45
|
|||
|
Re: 雙輸入問題
|
||||
---|---|---|---|---|
高級會員
|
我剛剛有測試 我的輸入部分是RA1 此行不改變 A2D(0x00); // Get 10-bit A/D result from CH0 單一個輸入A/D沒有問題 所以應該不是這問題 還是我有錯誤
發表於: 2007/5/15 15:21
|
|||
|
Re: 雙輸入問題
|
||||
---|---|---|---|---|
高級會員
|
A2D(0x00); // Get 10-bit A/D result from CH0
如果是CH1那是要怎改阿 因為我改過好像一直不對ㄟ 可以指導一下ㄇ
發表於: 2007/5/15 15:12
|
|||
|
Re: 雙輸入問題
|
||||
---|---|---|---|---|
版主
|
參照:
while(1) 程式裡只看到 CH0 的轉換並沒有切換啊!
發表於: 2007/5/15 14:16
|
|||
|
雙輸入問題
|
||||
---|---|---|---|---|
高級會員
|
想請問我如果用單一輸入ok但是兩個輸入的話卻只有一個可以動作 是因為沒有加入DELAY還是哪裏問題可以指導一下ㄇ??
#include <pic.h> // processor if/def file #include "cnfig877a.h" #include "mid_lcd.h" unsigned char i ; //*************************** // A/D延遲 //***************************** void ADdelay(unsigned char i) { while(i--); } //********************************** //* Function Prototype Declaration //********************************** void Init_IO(void); void Init_Adc( void ); void Init_Adc_1( void ); void A2D( unsigned char ); void A2D_1( unsigned char ); void LCD_ItoA(unsigned int); void LCD_ItoA_1(unsigned int); unsigned char Set_BCD_ASCII(unsigned char); unsigned char Set_BCD_ASCII_1(unsigned char); // ================================================================ __CONFIG ( HS_OSC & BODEN_ON & WDT_OFF & CP_OFF & LVP_OFF & DEBUG_ON ); // ================================================================= const char LCD_Msg1[]=" voltage: "; const char LCD_Msg2[]=" current: "; unsigned int AD_Temp; unsigned int AD_Temp_1; bit DS_Zero_Flag; bit DS_Zero_Flag_1; union { int AD_10bit; struct { char AD_LSB; char AD_MSB; } AD_Byte; } AD_Int; union { int AD_10bit_1; struct { char AD_LSB_1; char AD_MSB_1; } AD_Byte_1; } AD_Int_1; /***************************** INITIALIZE I/O PORT *****************************/ void Init_IO(void) { ADCON1=0b00000110; // Disable A/D Function TRISD=0x00; // Set PortD for Output PORTD=0x80; // Initila LED display = 0x80 TRISA0=1; // Set RA0 for input TRISA1=1; // Set RA1 for input } /***************************************** Initialize A/D & Comparator Module ******************************************/ void Init_Adc( void ) { ADCON0=0b10001001; // Select CH1, A/D on ADCON1=0b00000010; // AN1 is A/D input ADFM=0; // Right justified } void Init_Adc_1( void ) { ADCON0=0b10000001; // Select CH0 , A/D on ADCON1=0b00001110; // AN0 is A/D input ADFM=1; // Right justified } /*********************************** Convert A/D Channel ************************************/ void A2D( unsigned char channel ) { unsigned char temp; // Temp storage ADGO = 1; // Initiate conversion on selected channel while (ADGO) ; // Poll ADGO bit until it clears, indicating conv. done AD_Int.AD_Byte.AD_MSB=ADRESH; AD_Int.AD_Byte.AD_LSB=ADRESL; temp= ADRESH *256+ ADRESL; temp = channel >>2; // Shift channel value three bits left ADCON0 &= 0b11001111; // Clear channel select bits in ADCON0 ADCON0 |= temp; // Logically OR shifted channel value into place in ADCON0 } void A2D_1( unsigned char channel_1 ) { unsigned char temp_1; // Temp_1 storage ADGO = 1; // Initiate conversion on selected channel while (ADGO) ; // Poll ADGO bit until it clears, indicating conv. done AD_Int_1.AD_Byte_1.AD_MSB_1=ADRESH; AD_Int_1.AD_Byte_1.AD_LSB_1=ADRESL; temp_1= ADRESH *256+ ADRESL; temp_1 = channel_1 <<2; // Shift channel value three bits left ADCON0 &= 0b11000111; // Clear channel select bits in ADCON0 ADCON0 |= temp_1; // Logically OR shifted channel value into place in ADCON0 } //*************************************** //* Program Main ( ) * //*************************************** void main(void) { Init_IO(); Init_Adc(); OpenLCD(); LCD_Set_Cursor(0,0); putrsLCD(LCD_Msg1); Init_Adc_1(); LCD_Set_Cursor(0,1); putrsLCD(LCD_Msg2); while(1) { A2D(0x00); // Get 10-bit A/D result from CH0 LCD_Set_Cursor(10,0); LCD_ItoA(AD_Int.AD_10bit); ADdelay(10); A2D_1(0x00); // Get 10-bit A/D result from CH0 LCD_Set_Cursor(10,1); LCD_ItoA_1(AD_Int_1.AD_10bit_1); } } void LCD_ItoA(unsigned int AD_Data) { AD_Temp=AD_Data; DS_Zero_Flag=1; putcLCD(Set_BCD_ASCII(AD_Data/1000)); // 顯示千位數 AD_Temp=AD_Temp%1000; // 取出百位以後的數 putcLCD(Set_BCD_ASCII(AD_Temp/100)); // 顯示百位數 AD_Temp=AD_Temp%100; putcLCD(Set_BCD_ASCII(AD_Temp/10)); // 顯示十位數 AD_Temp=AD_Temp%10; putcLCD(AD_Temp+='0'); // 顯示個位數 } void LCD_ItoA_1(unsigned int AD_Data_1) { AD_Temp_1=AD_Data_1; DS_Zero_Flag_1=1; putcLCD(Set_BCD_ASCII_1(AD_Data_1/1000)); // 顯示千位數 AD_Temp_1=AD_Temp_1%1000; // 取出百位以後的數 putcLCD(Set_BCD_ASCII_1(AD_Temp_1/100)); // 顯示百位數 AD_Temp_1=AD_Temp_1%100; putcLCD(Set_BCD_ASCII_1(AD_Temp_1/10)); // 顯示十位數 AD_Temp_1=AD_Temp_1%10; putcLCD(AD_Temp_1+='0'); } unsigned char Set_BCD_ASCII(unsigned char BCD_Data) { if (BCD_Data==0) { if (DS_Zero_Flag) return ' '; // 居先零抑制 else return '0'; // 顯示一般的零 } else { DS_Zero_Flag=0; // 取消居先零的抑制 return (BCD_Data +='0'); // 並傳回 ASCII Code } } unsigned char Set_BCD_ASCII_1(unsigned char BCD_Data_1) { if (BCD_Data_1==0) { if (DS_Zero_Flag_1) return ' '; // 居先零抑制 else return '0'; // 顯示一般的零 } else { DS_Zero_Flag_1=0; // 取消居先零的抑制 return (BCD_Data_1 +='0'); // 並傳回 ASCII Code } }
發表於: 2007/5/15 13:13
|
|||
|