#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "driverlib/adc.h"
#include "driverlib/uart.h"
#include "driverlib/pin_map.h"
#include "utils/uartstdio.h"
//#include "driverlib/debug.h"
/*
* 外围设备驱动程序库运行时参数检查相当粗略因为过度的检查会对循环计数有负面影响
* 在调试过程,你可能以一种错误的方式调用API或者其他的原因,导致一个错误发生。
* 如果驱动库遇到这样的问题,那么下边的代码将会调用。他将会保存文件名和错误发生的位置。
*
*/
/*#ifdef DEBUG
void__error__(char *pcFilename, uint32_t ui32Line)
{
}
#endif*/
void UART_init(unsigned temp)//配置通信串口
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
GPIOPinTypeUART(GPIO_PORTA_BASE,GPIO_PIN_0|GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE,SysCtlClockGet(),9600,
(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
UARTCharPut(UART0_BASE,temp);
}
int main(void)
{
uint32_t ui32ADC0Value[4];
volatile uint32_t ui32TempAvg;
//设置系统时钟
SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_OSC_MAIN|SYSCTL_XTAL_16MHZ);
//ADC0使能
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
//使能要用的GPIO口E
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//ADC序列设置 序列1,处理器触发方式,优先级最高
ADCSequenceConfigure(ADC0_BASE, 1, ADC_TRIGGER_PROCESSOR, 0);
//使能通道0
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);// Enable pin PE4_AIN0
//ADC_CTL_IE 是用来申请中断源
//ADC_CTL_END,则在完成第四个采样动作之后,结束采样序列,即使后面在进行设置是没有用的
ADCSequenceStepConfigure(ADC0_BASE, 1, 0, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 1, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE, 1, 2, ADC_CTL_CH0);
ADCSequenceStepConfigure(ADC0_BASE,1,3,ADC_CTL_CH0|ADC_CTL_IE|ADC_CTL_END);
//使能
ADCSequenceEnable(ADC0_BASE, 1);
while(1)
{
//ADC中断清零清零
ADCIntClear(ADC0_BASE, 1);
//ADC处理触发器
ADCProcessorTrigger(ADC0_BASE, 1);
//等ADC转换结束
while(!ADCIntStatus(ADC0_BASE, 1, false))
{
}
//获取ADC序列数据
ADCSequenceDataGet(ADC0_BASE, 1, ui32ADC0Value);
/*
* 加2的原因:加2是为了舍入,因为2/4=0.5.1.5进位为2.如果0.5增益到1.5,这个将会被舍入到1,因为整数的规则。
*/
ui32TempAvg = (ui32ADC0Value[0] + ui32ADC0Value[1] + ui32ADC0Value[2] + ui32ADC0Value[3] + 2)/4;
//打印出数据在电脑端显示
UART_init(ui32TempAvg);
}
}