• slider image 442
  • slider image 503
  • slider image 504
  • slider image 505
  • slider image 506
  • slider image 507
  • slider image 508
:::


Browsing this Thread:   1 Anonymous Users






Re: 程式是這樣要把ADC的轉換值用USART輸出可是最大值是1024正確最小值卻不是0該怎麼修改
#3
新會員
新會員


查看用戶資訊
使用的是PIC16F1825

發表於: 7/11 16:26
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 程式是這樣要把ADC的轉換值用USART輸出可是最大值是1024正確最小值卻不是0該怎麼修改
#2
管理員
管理員


查看用戶資訊
Hi,

首先方便詢問您提供MCU的名稱嗎?
您貼上來的程式碼包含了ADC的初始化函數,並在main function中循環讀取ADC的數值。需再對照Datasheet上來查看初始化的設定。

建議您可以先檢查以下幾點:
1. ADC的輸入腳位是否有正確接地或是符合您的規劃
2. 參考電壓是否設定正確
3. 腳位是否有雜訊或是其他干擾

發表於: 7/11 10:26
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


程式是這樣要把ADC的轉換值用USART輸出可是最大值是1024正確最小值卻不是0該怎麼修改
#1
新會員
新會員


查看用戶資訊
#include "mcc_generated_files/system/system.h"
#include
#include

#define _XTAL_FREQ 8000000
#define BAUDRATE 9600
#define MY_UBRR ((_XTAL_FREQ / 16 / BAUDRATE) - 1)

// Function to initialize UART
void UART_Init() {
SPBRG = MY_UBRR;
TXSTAbits.TX9 = 0; // 8-bit transmission
TXSTAbits.TXEN = 1; // Enable transmitter
TXSTAbits.SYNC = 0; // Asynchronous mode
TXSTAbits.BRGH = 1; // High-speed mode
RCSTAbits.SPEN = 1; // Enable serial port
RCSTAbits.CREN = 1; // Enable continuous reception

}

// Function to transmit a single character via UART
void uart_transmit(char data) {
while (!TXSTAbits.TRMT); // Wait for buffer to be empty
TXREG = data; // Transmit data
}

// Function to transmit a string via UART
void uart_transmit_string(const char* str) {
while (*str != '\0') {
uart_transmit(*str++);
}
}

// Function to initialize ADC
void initADC(void) {
ADCON0 = 0b00001001; // Select AN2 (CHS = 00010), enable ADC (ADON = 1)
ADCON1 = 0b10010000; // Right justify result, Fosc/8 as ADC clock
ADCON0bits.GO_nDONE = 1; // Start A/D conversion
}

// Function to read ADC value
unsigned int readADC(void) {
ADCON0bits.GO_nDONE = 1; // Start conversion
while (ADCON0bits.GO_nDONE); // Wait for conversion to complete
return ((ADRESH << 8) | ADRESL); // Return 10-bit result
}

// Function to convert integer to string
void itoa(unsigned int value, char* buffer) {
sprintf(buffer, "%u", value);
}

// Main function
int main(void) {
SYSTEM_Initialize();
UART_Init();
initADC();

char adcBuffer[6]; // Buffer to hold ADC value as string

while (1) {


// Read ADC value
unsigned int adcValue = readADC();

// Convert ADC value to string
itoa(adcValue, adcBuffer);

// Transmit ADC value
uart_transmit_string(adcBuffer);
uart_transmit('\n'); // Newline for readability
uart_transmit('\r');
__delay_ms(1000); // Delay before next reading
}
}

發表於: 7/9 10:13
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------