实现助学板上电位器控制的AD采集
main函数- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2011 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: main
- ** Descriptions: usart
- ** input parameters: 无
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- uint32_t AD_Value;
- uint8_t buffer[6] = {0,46,0,0,0,86};
- uint8_t buffer1[2] = {0x0D,0x0A};
- int main (void)
- {
- Set_System(); //封装一些初始化模块
- while(1)
- {
- DrvADC_StartConvert(); // 开启ADC转换
- while(DrvADC_IsConversionDone()==FALSE); // 判断ADC是否转换结束 并送串口显示
- AD_Value = DrvADC_GetConversionData(1);
- AD_Value = AD_Value*3300/4095;
- buffer[0] = AD_Value/1000 +48;
- buffer[2] = AD_Value/100%10 +48 ;
- buffer[3] = AD_Value/10%10 +48;
- buffer[4] = AD_Value%10+48;
- DrvUART_Write(UART_PORT0,buffer,6) ;
- delay_ms(300);
- DrvUART_Write(UART_PORT0,buffer1,2) ;
- delay_ms(6000);
- }
- }
hw_config.c函数- #include "includes.h" //包含所需的头文件
- /*************************************************************************************
- ** Function name: Set_System
- ** Descriptions: 封装一些初始化模块
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void Set_System(void)
- {
- RCC_Configuration(); //配置系统时钟
- GPIO_Configuration(); //配置GPIO
- USART_Configuration(); //配置USART
- ADC_Configuration(); //配置ADC
- }
- /*************************************************************************************
- ** Function name: RCC_Configuration
- ** Descriptions: 系统时钟源设置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void RCC_Configuration(void)
- {
- UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88,
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);//与其 SYSCLK->PWRCON.XTL12M_EN = 1; 等同
- // PWRCON寄存器(这些寄存器在上电复位到用户解锁定之前是锁定的)除了 BIT[6]位其他位都受写保护
- // 解除这些需要向 0x50000 0100写入 0x59,0x16,0x88,
- // 令PWRCON寄存器的BITP[0]为1(即设定12M外部晶振)
- delay_ms(100); //while (DrvSYS_GetChipClockSourceStatus(E_SYS_XTL12M) != 1);//等待外部12MHZ晶振就绪
- LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
- }
- /*************************************************************************************
- ** Function name: GPIO_Configuration
- ** Descriptions: GPIO配置
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void GPIO_Configuration()
- {
- DrvGPIO_Open( E_GPA, 5, E_IO_OUTPUT );
- DrvGPIO_Open( E_GPB, 0, E_IO_INPUT ); //RX
- DrvGPIO_Open( E_GPB, 1, E_IO_OUTPUT ); //TX
- DrvGPIO_Open( E_GPA, 1, E_IO_INPUT ); //AD1
- }
- /*************************************************************************************
- ** Function name: USART_Configuration
- ** Descriptions: 配置USART
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void USART_Configuration()
- {
- STR_UART_T param;
- DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0); // 使能UART时钟
- DrvGPIO_InitFunction(E_FUNC_UART0); // 复用功能引脚设置
- param.u32BaudRate = 9600; // 波特率 9600
- param.u8cDataBits = DRVUART_DATABITS_8; // 数据位
- param.u8cStopBits = DRVUART_STOPBITS_1; // 停止位
- param.u8cParity = DRVUART_PARITY_NONE; // 校验位
- param.u8cRxTriggerLevel = DRVUART_FIFO_62BYTES; // FIFO存储深度 6 字节
- param.u8TimeOut = 0; // FIFO超时设定
- DrvUART_Open(UART_PORT0, ¶m); // 串口usart0开启、结构体整体
- 赋值
- }
- /*************************************************************************************
- ** Function name: ADC_Configuration
- ** Descriptions: 配置ADC
- ** input parameters: none
- ** output parameters: none
- ** Returned value: none
- *************************************************************************************/
- void ADC_Configuration()
- {
- DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0, EXTERNAL_12MHZ, 3); // ADC_SINGLE_END AD为单端输入模式
- // ADC_SINGLE_OP 单一转换
- // 1 GA0作为输入 模式输入通道使能
- // EXTERNAL_12MHZ ADC时钟为 外部12MHZ
- // 3 AD时钟频率 = ADC时钟/(3+1) = 3MHZ
- DrvADC_SetADCChannel(0x02,ADC_SINGLE_END); // 2 GA1作为输入 模式输入通
- 道使能
- // ADC_SINGLE_END AD为单端输入模式
- ADC->ADCR.DMOF = 0; //转化结果
- 无符号
- }
- /*************************************************************************************
- ** Function name: delay_ms
- ** Descriptions: 1ms(晶振为12MHZ)延时子程序
- ** input parameters: count
- ** output parameters: 无
- ** Returned value: 无
- *************************************************************************************/
- void delay_ms(uint32_t count)
- {
- uint32_t i,j;
- for(i=count;i>0;i--)
- for(j=2395;j>0;j--);
- }
hw_config.h头文件- void Set_System(void);
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void USART_Configuration(void);
- void ADC_Configuration(void);
- void delay_ms(uint32_t count);
includes.h头文件- #include <stdio.h>
- #include "NUC1xx.h"
- #include "variables.h"
- #include "hw_config.h"
- #include "Driver\DrvSYS.h"
- #include "Driver\DrvGPIO.h"
- #include "Driver\Drvuart.h"
- #include "Driver\DrvADC.h"
截图
工程
|