:::

論壇索引


Board index » All Posts (FRED0)




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


使用的是PIC16F1825

發表於: 2024/7/11 16:26
頂部


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


#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
}
}

發表於: 2024/7/9 10:13
頂部






:::

Microchip連結

https://www.linkedin.com/company/microchiptechnologytaiwan
https://www.facebook.com/microchiptechnologytaiwan/?locale=zh_TW
https://mkpage.microchip.com/l/1082123/2025-03-12/2stnv5
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
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... ]

教育訓練中心

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