有一點很重要的是你要翻翻datasheet看ADC的取樣極限(TAD)
The time to complete one bit conversion is defined as TAD.
這邊以PIC16F1619為例:
datasheet中已經把各個頻率應該對應到的設定畫成表格了(陰影超出推薦範圍)
另外在這個表格中也有TAD的最大值與最小值
如果TAD超出推薦範圍,讀取出來的值將會是錯誤的
如果你讀取出來的值是異常的不妨回去看看是不是TAD的設定出問題
這邊的實作範例mcc設定如下
其中要注意的是ADC的結果靠右還是靠左(Result Alignment)
這顆PIC的ADC精度為10bits,因為資料有10bits所以必須使用2Bytes來裝
而資料得靠左或靠右的差別如下
這會影響到你後續要如何處理這筆資料
假設你今天只用到8bits的精度,以靠左為例你可以很直觀的直接把資料右移8bits來保留前面的8bits資料
如果資料是靠右的,就必須考慮到你的PIC精度是幾bits來計算要右移幾位(右移2bits)
這次示範的硬體為Curiosity實驗版 上面的PIC為PIC16F1619
AN4訊號源是一顆可變電阻
以下是我的main.c程式碼
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//The file is used in https://wyp8711.blogspot.com/2018/03/pic-xide-mcc-adc-pic16f1619-curiosity.html | |
//design by W.Y.Peng | |
//date:2018/3/26 | |
#include "mcc_generated_files/mcc.h" | |
void main(void) | |
{ | |
SYSTEM_Initialize(); | |
uint8_t data; | |
while (1) | |
{ | |
data=ADC_GetConversion(AN4)>>12; | |
LED0_LAT=data >> 3; | |
LED1_LAT=data >> 2; | |
LED2_LAT=data >> 1; | |
LED3_LAT=data; | |
} | |
} |
範例成果:
沒有留言:
張貼留言