MSP432P401R TI Drivers 库函数学习笔记(八)ADC

平台: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 driver
	ADC_init();
	// initialize optional ADC parameters
	ADC_Params params;
	ADC_Params_init(&params);
	params.isProtected = true;
	// Open ADC channels for usage
	*adcHandle = ADC_open(index, &params);
}


uint32_t Get_Micro_Volts(ADC_Handle *adcHandle)
{
	uint16_t AdcRaw = 0;
	// Sample the analog output from the Thermocouple
	ADC_convert(*adcHandle, &AdcRaw);
	// Convert the sample to microvolts
	return 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() failed
	    while (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 32

extern 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_ */

实验结果

接上电位器,打开上位机,转动电位器,得到如下波形
在这里插入图片描述
数据单位为微伏。

  • 9
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乙酸氧铍

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值