最近在耍TI官方送的开发板Tiva LaunchPad,研究了一天的ADC,共享下写的定时器驱动ADC,顺便和各位讨论这个芯片#include
#include
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "driverlib/interrupt.h"
extern void ADC0Sequence3Handler(void);
uint32_t ulADC0_Value[1];
float Vol_Value;
void ADC0Sequence3Handler(void)
{
uint32_t ulADCStatus = 0;
ulADCStatus = ADCIntStatus(ADC0_BASE, 3, false);//读取MAsk标志位
if(ADC_INT_SS3 && ulADCStatus)
{
ADCSequenceDataGet(ADC0_BASE, 3, ulADC0_Value);//读ADC值
Vol_Value = (3.3) * (ulADC0_Value[0]) / 0xFFF;
}
ADCIntClear(ADC0_BASE, 3);//清ADC中断
}
void ADC_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0);
ADCSequenceConfigure(ADC0_BASE, 3, ADC_TRIGGER_TIMER, 0);//配置ADC0,采样序列3,ADC处理器触发,优先级0
ADCSequenceStepConfigure(ADC0_BASE, 3, 0, ADC_CTL_CH3 | ADC_CTL_IE | ADC_CTL_END);//配置ADC采样序列发生器的步进:输入通道0,中断使能,对列结束选择
ADCIntRegister(ADC0_BASE, 3, ADC0Sequence3Handler);//Register an interrupt handler for an ADC interrupt.
ADCIntEnable(ADC0_BASE, 3);//Enable a sample sequence interrupt
ADCSequenceEnable(ADC0_BASE, 3);//使能ADC采样
ADCComparatorIntEnable(ADC0_BASE, 3);//Enable a sample sequence comparator interrupt
IntMasterEnable();
ADCIntClear(ADC0_BASE, 3);//清ADC中断
}
void Time0B_Init(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
TimerConfigure(TIMER0_BASE, TIMER_CFG_SPLIT_PAIR | TIMER_CFG_B_PERIODIC);//Two half-width timers
TimerLoadSet(TIMER0_BASE, TIMER_B, SysCtlClockGet()/1000);//Sets the timer load value
TimerControlTrigger(TIMER0_BASE, TIMER_B, true);
TimerEnable(TIMER0_BASE, TIMER_B);
}
/*
* main.c
*/
int main(void) {
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);
ADC_Init();
Time0B_Init();
while(1)
{
// ADCProcessorTrigger(ADC0_BASE, 3);//触发ADC转换
// while(!ADCIntStatus(ADC0_BASE, 3, false));//等待转换完成
// ADCSequenceDataGet(ADC0_BASE, 3, ulADC0_Value);//读ADC值
// Vol_Value = (3.3) * (ulADC0_Value[0]) / 0xFFF;
//SysCtlDelay(SysCtlClockGet() / 12);
}
//return 0;
} |