MSP432P401R TI Drivers 库函数学习笔记(八)ADC
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                MSP432P401R TI Drivers 库函数学习笔记(八)ADC
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                目錄
- API (機(jī)翻)
 - 上機(jī)實(shí)戰(zhàn)
 - 引腳配置
 - ADC引腳配置
 - 串口引腳配置
 - 指示工作狀態(tài)的LED1引腳配置
 
- 代碼部分
 - ADC初始化和讀取函數(shù)
 - myADC.c
 - myADC.h
 
- 獲取數(shù)據(jù)并通過串口發(fā)送
 - main.c
 - main.h
 
- 任務(wù)管理函數(shù)
 - myTask.c
 - myTask.h
 
- 串口代碼
 - myUart.c
 - myUart.h
 
- 實(shí)驗(yàn)結(jié)果
 
平臺:Code Composer Studio 10.4.0
  MSP432P401R SimpleLink? 微控制器 LaunchPad? 開發(fā)套件
  (MSP-EXP432P401R)
API (機(jī)翻)
ADC API 官方手冊
void ADC_close (ADC_Handle handle) 關(guān)閉ADC驅(qū)動實(shí)例int_fast16_t ADC_control (ADC_Handle handle, uint_fast16_t cmd, void *arg) 在驅(qū)動程序?qū)嵗蠄?zhí)行特定的實(shí)現(xiàn)特性int_fast16_t ADC_convert (ADC_Handle handle, uint16_t *value) 執(zhí)行ADC轉(zhuǎn)換uint32_t ADC_convertToMicroVolts (ADC_Handle handle, uint16_t adcValue) 將原始ADC數(shù)據(jù)轉(zhuǎn)換為以微伏為單位的數(shù)據(jù)void ADC_init (void) 初始化ADC驅(qū)動程序ADC_Handle ADC_open (uint_least8_t index, ADC_Params *params) 初始化ADC外設(shè)void ADC_Params_init (ADC_Params *params) 將ADC_Params結(jié)構(gòu)初始化為其默認(rèn)值上機(jī)實(shí)戰(zhàn)
引腳配置
ADC引腳配置
串口引腳配置
指示工作狀態(tài)的LED1引腳配置
代碼部分
ADC初始化和讀取函數(shù)
myADC.c
/** myADC.c** Created on: 2021年8月4日* Author: Royic*/#include "./inc/myADC.h"ADC_Handle hadc1;void My_ADC_Init(ADC_Handle *adcHandle, uint_least8_t index) {// One-time init of ADC driverADC_init();// initialize optional ADC parametersADC_Params params;ADC_Params_init(¶ms);params.isProtected = true;// Open ADC channels for usage*adcHandle = ADC_open(index, ¶ms); }uint32_t Get_Micro_Volts(ADC_Handle *adcHandle) {uint16_t AdcRaw = 0;// Sample the analog output from the ThermocoupleADC_convert(*adcHandle, &AdcRaw);// Convert the sample to microvoltsreturn ADC_convertToMicroVolts(*adcHandle, AdcRaw); }myADC.h
/** myADC.h** Created on: 2021年8月4日* Author: Royic*/#ifndef INC_MYADC_H_ #define INC_MYADC_H_#include "./inc/main.h"// Import ADC Driver definitions #include <ti/drivers/ADC.h>void My_ADC_Init(ADC_Handle *adcHandle, uint_least8_t index); uint32_t Get_Micro_Volts(ADC_Handle *adcHandle);extern ADC_Handle hadc1;#endif /* INC_MYADC_H_ */獲取數(shù)據(jù)并通過串口發(fā)送
main.c
/** ======== main_tirtos.c ========*/#include "./inc/main.h"/* POSIX Header files */ #include <pthread.h>/* RTOS header files */ #include <ti/sysbios/BIOS.h>/* Driver configuration */ #include <ti/drivers/Board.h> #include <ti/drivers/GPIO.h>#include "./inc/myTask.h" #include "./inc/myADC.h" #include "./inc/myUart.h"/** ======== main ========*/ int main(void) {/* Call driver init functions */Board_init();GPIO_init();My_Task_Init(mainThread, 1, 1024);BIOS_start();return (0); }/** ======== mainThread ========*/ void *mainThread(void *arg0) {My_Task_Init(LED_Task, 1, 1024);My_Uart_Init(&huart1, USB_UART, 115200);My_ADC_Init(&hadc1, ADC1);while(1){UART_printf(huart1, "%d\r\n", Get_Micro_Volts(&hadc1));usleep(1000);} }main.h
/** main.h** Created on: 2021年8月2日* Author: Royic*/#ifndef INC_MAIN_H_ #define INC_MAIN_H_/* For usleep() */ #include <unistd.h> #include <stdint.h> #include <stddef.h>/* Driver configuration */ #include "ti_drivers_config.h"#endif /* INC_MAIN_H_ */任務(wù)管理函數(shù)
myTask.c
/** myTask.c** Created on: 2021年8月2日* Author: Royic*//* POSIX Header files */ #include <pthread.h>/* RTOS header files */ #include <ti/sysbios/BIOS.h>#include "./inc/myTask.h"/* Driver Header files */ #include <ti/drivers/GPIO.h>void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize) {pthread_t thread;pthread_attr_t attrs;struct sched_param priParam;int retc;/* Initialize the attributes structure with default values */pthread_attr_init(&attrs);/* Set priority, detach state, and stack size attributes */priParam.sched_priority = priority;retc = pthread_attr_setschedparam(&attrs, &priParam);retc |= pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);retc |= pthread_attr_setstacksize(&attrs, stacksize);if (retc != 0){/* failed to set attributes */while (1){}}retc = pthread_create(&thread, &attrs, startroutine, NULL);if (retc != 0){/* pthread_create() failed */while (1){}} }void *LED_Task(void *arg0) {while(1){GPIO_toggle(LED1);sleep(1);} }myTask.h
/** myTask.h** Created on: 2021年8月2日* Author: Royic*/#ifndef INC_MYTASK_H_ #define INC_MYTASK_H_#include "./inc/main.h"void *mainThread(void *arg0); void My_Task_Init(void *(*startroutine)(void *), int priority, size_t stacksize);void *LED_Task(void *arg0);#endif /* INC_MYTASK_H_ */串口代碼
myUart.c
/** myUart.c** Created on: 2021年8月3日* Author: Royic*/#include "./inc/myUart.h"#include <ti/drivers/GPIO.h>UART_Handle huart1;char Uart_Rx_Buffer[Uart_Rx_Buffer_Size] = {0};void Uart_TxCallback_Func(UART_Handle handle, void *buf, size_t count) {}void Uart_RxCallback_Func(UART_Handle handle, void *buf, size_t count) {UART_read(huart1, Uart_Rx_Buffer, 32); }void My_Uart_Init(UART_Handle *huart, uint_least8_t index, uint32_t BaudRate) {UART_Params uartParams;// Initialize the UART driver. UART_init() must be called before// calling any other UART APIs.UART_init();// Create a UART with data processing off.UART_Params_init(&uartParams);uartParams.readMode = UART_MODE_CALLBACK; // uartParams.writeMode = UART_MODE_CALLBACK;uartParams.writeMode = UART_MODE_BLOCKING;uartParams.readCallback = Uart_RxCallback_Func;uartParams.writeCallback = Uart_TxCallback_Func;uartParams.writeDataMode = UART_DATA_TEXT;uartParams.readDataMode = UART_DATA_TEXT;uartParams.readReturnMode = UART_RETURN_NEWLINE;uartParams.readEcho = UART_ECHO_OFF;uartParams.baudRate = BaudRate;// Open an instance of the UART drivers*huart = UART_open(index, &uartParams);if (*huart == NULL){// UART_open() failedwhile (1);}UART_read(*huart, Uart_Rx_Buffer, 32); }#include <string.h> #include <stdarg.h> #include <stdio.h> void UART_printf(UART_Handle handle, const char *format,...) {uint32_t length;va_list args;char TxBuffer[32] = {0};va_start(args, format);length = vsnprintf((char*)TxBuffer, sizeof(TxBuffer)+1, (char*)format, args);va_end(args);UART_write(handle, TxBuffer, length); }myUart.h
/** myUart.h** Created on: 2021年8月3日* Author: Royic*/#ifndef INC_MYUART_H_ #define INC_MYUART_H_#include "./inc/main.h"// Import the UART driver definitions #include <ti/drivers/UART.h>#define Uart_Rx_Buffer_Size 32extern char Uart_Rx_Buffer[Uart_Rx_Buffer_Size];void My_Uart_Init(UART_Handle *huart, uint_least8_t index, uint32_t BaudRate); void UART_printf(UART_Handle handle, const char *format,...);//Example //My_Uart_Init(&huart1, USB_UART, 115200); //UART_write(huart1, "OK\r\n", 5);extern UART_Handle huart1;#endif /* INC_MYUART_H_ */實(shí)驗(yàn)結(jié)果
接上電位器,打開上位機(jī),轉(zhuǎn)動電位器,得到如下波形
 
 數(shù)據(jù)單位為微伏。
總結(jié)
以上是生活随笔為你收集整理的MSP432P401R TI Drivers 库函数学习笔记(八)ADC的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Html中如何让超链接a、图片img居中
 - 下一篇: html 中加载字体太慢,css字体文件