STM8S系列基于IAR开发单通道ADC连续采样示例
生活随笔
收集整理的這篇文章主要介紹了
STM8S系列基于IAR开发单通道ADC连续采样示例
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
STM8S系列基于IAR開(kāi)發(fā)單通道ADC連續(xù)采樣示例
- 📌相關(guān)篇《STM8S系列基于STVD開(kāi)發(fā)ADC采樣示例》
?本示例基于STM8S9003K3,使用通道6(PD6),作為ADC采集引腳。使用10K可調(diào)定位器作為輸入端測(cè)試。這里直接讀取ADC數(shù)據(jù)寄存器數(shù)據(jù)進(jìn)行輸出,如果需要添加數(shù)據(jù)平移算法,可以直接采用多次數(shù)據(jù)累加然后取平均值即可。
- 📓通道和引腳映射關(guān)系
📖相關(guān)寄存器介紹
-
🌿ADC 配置寄存器 1 (ADC_CR1)
-
🌿ADC 配置寄存器 2 (ADC_CR2)
-
🌿ADC控制/狀態(tài)寄存器(ADC_CSR)
-
🌿ADC 施密特觸發(fā)器禁止寄存器低位 (ADC_TDRL)
-
🌿ADC 數(shù)據(jù)低位寄存器(ADC_DRL)
📑ADC模塊初始化
/*************************************************************************** 函數(shù)名:ADC_conf* 描述 :ADC模塊初始化* ADC通道 :PD2(AIN3),PD3(AIN4),* 輸出 :無(wú)* 返回 :無(wú) * 調(diào)用 :外部調(diào)用 *************************************************************************/ void ADC_conf(unsigned char ch ) {ADC_CR1 = (0<<4)|(1<<1)|(0<<0); //ADC時(shí)鐘輸入頻率為16MHz 這里設(shè)置分頻系數(shù)為2 連續(xù)轉(zhuǎn)換模式 先禁止ADC轉(zhuǎn)換 // ADC_CR2 = (1<<3)|(0<<1); //設(shè)置數(shù)據(jù)右對(duì)齊 禁止掃描模式ADC_CR2 = 0x08; // 同上,右對(duì)齊ADC_CSR =(0<<5)|(ch + 1); //不用外部觸發(fā) 禁止轉(zhuǎn)換結(jié)束中斷 設(shè)置轉(zhuǎn)換通道為AIN4(PD3)ADC_TDRL = ( 1 << (ch + 1 )); //禁止相應(yīng)通道 施密特觸發(fā)功能 1左移ch+1位// ADC_TDRH = 4; //禁止AIN4施密特觸發(fā)器功能 ADC_CR1 |= 1; //第一次寫(xiě)1是從低功耗模式喚醒 ADC_CR1 |= 1; //在這一位是1的情況下再次寫(xiě)1啟動(dòng)ADC轉(zhuǎn)換 }📝右對(duì)齊方式數(shù)據(jù)轉(zhuǎn)換實(shí)現(xiàn)
/*************************************************************************** 函數(shù)名:ADC_GetConversionValue* 描述 :獲取ADC轉(zhuǎn)換結(jié)果* 輸入 :無(wú)* 輸出 :無(wú)* 返回 :無(wú) * 調(diào)用 :內(nèi)部調(diào)用 *************************************************************************/ uint16_t ADC_GetConversionValue(void) {uint16_t value=0; uint8_t templ,temph ; // 定義templ存儲(chǔ)低8位數(shù)據(jù) temph存儲(chǔ)高8位數(shù)據(jù)while(!(ADC_CSR & 0x80)); //等待轉(zhuǎn)換完成templ = ADC_DRL;temph = ADC_DRH; //讀取ADC轉(zhuǎn)換 在左對(duì)齊和右對(duì)齊模式下 讀取數(shù)據(jù)的順序不同 value = (uint16_t)(templ + (uint16_t)(temph << (uint8_t)8)) ; //得到十位精度的數(shù)據(jù) 0--1024//注意是10位的轉(zhuǎn)換精度 value、temph應(yīng)為unsigned int 變量return (uint16_t)value; }📝數(shù)據(jù)左右對(duì)齊數(shù)據(jù)轉(zhuǎn)換綜合實(shí)現(xiàn)
*************************************************************************** 函數(shù)名:ADC_GetConversionValue* 描述 :獲取ADC轉(zhuǎn)換結(jié)果* 輸入 :無(wú)* 輸出 :無(wú)* 返回 :無(wú) * 調(diào)用 :內(nèi)部調(diào)用 *************************************************************************/ uint16_t ADC_GetConversionValue(void) {uint16_t value=0; uint8_t templ,temph ; // 定義templ存儲(chǔ)低8位數(shù)據(jù) temph存儲(chǔ)高8位數(shù)據(jù)while(!(ADC_CSR & 0x80)); //等待轉(zhuǎn)換完成if ((ADC_CR2 & 0x08) != 0) /* Right alignment */{templ = ADC_DRL;temph = ADC_DRH; //讀取ADC轉(zhuǎn)換 在左對(duì)齊和右對(duì)齊模式下 讀取數(shù)據(jù)的順序不同 value = (uint16_t)(templ + (uint16_t)(temph << (uint8_t)8)) ; //得到十位精度的數(shù)據(jù) 0--1024}else {//注意是10位的轉(zhuǎn)換精度 value、temph應(yīng)為unsigned int 變量temph = ADC_DRH;templ = ADC_DRL; value = ((uint16_t)temph << 2 ) + (uint16_t)templ ; //得到十位精度的數(shù)據(jù) 0--1024 }return (uint16_t)value; }🛠IAR Option Bytes配置說(shuō)明
🔰由于stm8s903k3,AIN6通道在PD6引腳上,需要將串口功能映射到其他地方。否則,串口無(wú)法打印。
- 🔨點(diǎn)擊下面的Save保存格式后綴名位.obc格式。
- 📍在配置選項(xiàng)中的ST-LINK中勾選下面的選項(xiàng)并加載上面保存的.obc格式文件。
📑主程序代碼
/***************************************** 文件名 :main.c* 描述 :AD轉(zhuǎn)換實(shí)驗(yàn) * 程序現(xiàn)象:本程序通過(guò)初始化ADC的AIN6通道,將傳感器的ADC值通過(guò)串口進(jìn)行打印* 寄存器版本 *****************************************//* Includes ------------------------------------------------------------------*/ #include "clk_conf.h" #include "uart.h" #include "adc.h"/* Private defines -----------------------------------------------------------*/ /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/void delay(u32 nCount) {/* Decrement nCount value */while (nCount != 0){nCount--;}}int main(void) {/* Infinite loop *//*設(shè)置內(nèi)部高速時(shí)鐘16M為主時(shí)鐘*/ Clk_conf(); /* 串口初始化 */uart_conf();/* ADC模塊初始化AIN0->PB6, AIN1->PB5,AIN2->PB4,AIN3->PD3,AIN4->PD5,AIN5->PD6*/ADC_conf(5);printf("\r\n:%s\r\n","Using STM8s903k3");while(1){printf("ADC_value=%u\r\n",ADC_GetConversionValue());delay(0xffff);delay(0xffff);delay(0xffff); } }#ifdef USE_FULL_ASSERT/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval : None*/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* Infinite loop */while (1){} } #endif/******************* (C) COPYRIGHT 風(fēng)馳iCreate嵌入式開(kāi)發(fā)工作室 *****END OF FILE****/📚程序源碼
鏈接:https://pan.baidu.com/s/1vdxOTbBcCYHYgUGTV41-lg 提取碼:nzap總結(jié)
以上是生活随笔為你收集整理的STM8S系列基于IAR开发单通道ADC连续采样示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: CGI接口介绍
- 下一篇: 视频演示SHAtter越狱iOS 4.1