中断法:
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvADC.h"
#define Run_Led 2 //2----LED1 3----LED2 4----LED3 5----LED4
uint16_t i32ConversionData;
void Init_System(void)
{
UNLOCKREG(x);
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
DrvSYS_Delay(5000);
LOCKREG(x);
}
void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
for(j=2395;j>0;j--);
}
void AdcIntCallback(uint32_t u32UserData)
{
i32ConversionData = DrvADC_GetConversionData(1);
printf("%d\n",i32ConversionData);
}
void Init_Uart(void)
{
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
DrvGPIO_InitFunction(E_FUNC_UART0);
param.u32BaudRate = 9600;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT0,¶m);
}
int main (void)
{
uint8_t IsFirst = 1;
Init_System();
Init_Uart();
DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA,Run_Led);
DrvGPIO_InitFunction(E_FUNC_ADC1);
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);
printf("\n");
printf("Driver version: %x\n", DrvADC_GetVersion());
DrvADC_EnableSelfCalibration();
while(DrvADC_IsCalibrationDone()==0);
DrvADC_DisableSelfCalibration();
DrvSYS_Delay(5000);
DrvADC_EnableADCInt(AdcIntCallback, 0);
while(1)
{
DrvADC_StartConvert();
}
}
查询法:
#include <stdio.h>
#include "NUC1xx.h"
#include "Driver\DrvGPIO.h"
#include "Driver\DrvSYS.h"
#include "Driver\DrvUART.h"
#include "Driver\DrvADC.h"
#define Run_Led 2 //2----LED1 3----LED2 4----LED3 5----LED4
void Init_System(void)
{
UNLOCKREG(x);
DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
DrvSYS_Delay(5000);
LOCKREG(x);
}
void delay_ms(uint32_t count)
{
uint32_t i,j;
for(i=count;i>0;i--)
for(j=2395;j>0;j--);
}
void Init_Uart(void)
{
STR_UART_T param;
DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
DrvGPIO_InitFunction(E_FUNC_UART0);
param.u32BaudRate = 9600;
param.u8cDataBits = DRVUART_DATABITS_8;
param.u8cStopBits = DRVUART_STOPBITS_1;
param.u8cParity = DRVUART_PARITY_NONE;
param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES;
param.u8TimeOut = 0;
DrvUART_Open(UART_PORT0,¶m);
}
int main (void)
{
uint16_t i32ConversionData;
// uint32_t i32ConversionData_Sum;
uint8_t IsFirst = 1;
Init_System();
Init_Uart();
DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);
DrvGPIO_ClrBit(E_GPA,Run_Led);
DrvGPIO_InitFunction(E_FUNC_ADC1);
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);
printf("\n");
printf("Driver version: %x\n", DrvADC_GetVersion());
DrvADC_EnableSelfCalibration();
while(DrvADC_IsCalibrationDone()==0);
DrvADC_DisableSelfCalibration();
DrvSYS_Delay(5000);
while(1)
{
DrvADC_StartConvert();
while(DrvADC_IsConversionDone()==0);
i32ConversionData = DrvADC_GetConversionData(1);
printf("%d\n",i32ConversionData);
DrvSYS_Delay(5000);
DrvSYS_Delay(5000);
delay_ms(1000);
// DrvADC_Close();
}
}
|