目录
- API (机翻)
- 上机实战
- 引脚配置
- ADC引脚配置
- 串口引脚配置
- 指示工作状态的LED1引脚配置
- 代码部分
- ADC初始化和读取函数
- myADC.c
- myADC.h
- 获取数据并通过串口发送
- main.c
- main.h
- 任务管理函数
- myTask.c
- myTask.h
- 串口代码
- myUart.c
- myUart.h
- 实验结果
平台:Code Composer Studio 10.4.0
MSP432P401R SimpleLink™ 微控制器 LaunchPad™ 开发套件
(MSP-EXP432P401R)
API (机翻)
ADC API 官方手册
void ADC_close (ADC_Handle handle)
关闭ADC驱动实例int_fast16_t ADC_control (ADC_Handle handle, uint_fast16_t cmd, void *arg)
在驱动程序实例上执行特定的实现特性int_fast16_t ADC_convert (ADC_Handle handle, uint16_t *value)
执行ADC转换uint32_t ADC_convertToMicroVolts (ADC_Handle handle, uint16_t adcValue)
将原始ADC数据转换为以微伏为单位的数据void ADC_init (void)
初始化ADC驱动程序ADC_Handle ADC_open (uint_least8_t index, ADC_Params *params)
初始化ADC外设void ADC_Params_init (ADC_Params *params)
将ADC_Params结构初始化为其默认值
上机实战
引脚配置
ADC引脚配置
串口引脚配置
指示工作状态的LED1引脚配置
代码部分
ADC初始化和读取函数
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_ */
获取数据并通过串口发送
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_ */
任务管理函数
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_ */
实验结果
接上电位器,打开上位机,转动电位器,得到如下波形
数据单位为微伏。