[MINI51] Mini51第三帖——AD测试

[复制链接]
3316|5
 楼主| lixiaoxu2meng 发表于 2012-1-14 15:01 | 显示全部楼层 |阅读模式
本帖最后由 hotpower 于 2012-9-22 12:38 编辑

本例程实现AD采集并实时显示AD值
main.c文件
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include "includes.h" //包含所需的头文件
  7. /*************************************************************************************
  8. ** Function name: main
  9. ** Descriptions: AD测试
  10. ** input parameters: 无
  11. ** output parameters: 无
  12. ** Returned value: 无
  13. *************************************************************************************/
  14. int main (void)
  15. {
  16. uint32_t AD_Value = 0;
  17. Set_System(); //调用系统初始化函数
  18. printf("AD--Test......\n");
  19. while(1)
  20. {
  21. DrvADC_StartConvert(); //清除ADC转换完成标志并启动下一次转换.
  22. while(DrvADC_IsConversionDone()==FALSE); //等待转换完成 (TURE:转换完成 FALSE:正在转换)
  23. AD_Value =DrvADC_GetConversionData(); //获取ADC转换的结果数据 转换结果为32位 10bit有效,其它bit都是0
  24. AD_Value = (float)AD_Value/1024*3300; //将采集到的值转换为电压值
  25. printf("AD采样值:%dMV\n",AD_Value); //输出AD值
  26. delay_ms(1000);
  27. }
  28. }
hw_config.c文件
  1. #include "includes.h" //包含所需的头文件
  2. /*************************************************************************************
  3. ** Function name: Set_System
  4. ** Descriptions: 封装一些初始化模块
  5. ** input parameters: none
  6. ** output parameters: none
  7. ** Returned value: none
  8. *************************************************************************************/
  9. void Set_System(void)
  10. {
  11. RCC_Configuration(); //配置系统时钟

  12. GPIO_Configuration(); //配置GPIO

  13. //USART_Configuration(); //配置USART

  14. ADC_Configuration(); //配置ADC
  15. }
  16. /*************************************************************************************
  17. ** Function name: RCC_Configuration
  18. ** Descriptions: 系统时钟源设置
  19. ** input parameters: none
  20. ** output parameters: none
  21. ** Returned value: none
  22. *************************************************************************************/
  23. void RCC_Configuration(void)
  24. {
  25. UNLOCKREG(); // 对写保护位操作时 需要一次向0x50000 0100写入 0x59,0x16,0x88
  26. DrvSYS_Open(XTL_CLK); // Enable high external clock and use it as system clock (HCLK)
  27. while (DrvSYS_GetChipClockSourceStatus(XTL_CLK) != 1); //等待外部12MHZ晶振就绪
  28. LOCKREG(); // 向“0x5000_0100”写入任何值,就可以重锁保护寄存器
  29. }
  30. /*************************************************************************************
  31. ** Function name: GPIO_Configuration
  32. ** Descriptions: GPIO配置
  33. ** input parameters: none
  34. ** output parameters: none
  35. ** Returned value: none
  36. *************************************************************************************/
  37. void GPIO_Configuration()
  38. {
  39. DrvGPIO_Open(E_PORT3, E_PIN1, E_IO_OUTPUT);//LED0
  40. DrvGPIO_Open(E_PORT3, E_PIN6, E_IO_OUTPUT);//LED1
  41. DrvGPIO_Open(E_PORT5, E_PIN2, E_IO_OUTPUT);//LED2
  42. DrvGPIO_Open(E_PORT2, E_PIN6, E_IO_OUTPUT);//LED3
  43. }
  44. /*************************************************************************************
  45. ** Function name: USART_Configuration
  46. ** Descriptions: 配置USART
  47. ** input parameters: none
  48. ** output parameters: none
  49. ** Returned value: none
  50. *************************************************************************************/
  51. void USART_Configuration()
  52. {
  53. STR_UART_T param;
  54. DrvSYS_SelectIPClockSource(UART_CLK_SET, 0); //使用外设时注意必须设置该外设的时钟 设置USART的时钟源为外部12MHZ
  55. DrvGPIO_InitFunction(FUNC_UART); //复用功能引脚设置
  56. param.u32BaudRate = 9600; //波特率 9600
  57. param.u8cDataBits = DRVUART_DATABITS_8; //数据位 8
  58. param.u8cStopBits = DRVUART_STOPBITS_1; //停止位 1
  59. param.u8cParity = DRVUART_PARITY_NONE; //校验位 无
  60. param.u8cRxTriggerLevel = DRVUART_FIFO_1BYTES; //FIFO存储深度 1 字节
  61. param.u8TimeOut = 0;
  62. DrvUART_Open(UART_PORT0, ¶m); //串口usart0开启、结构体整体赋值
  63. }
  64. /*************************************************************************************
  65. ** Function name: ADC_Configuration
  66. ** Descriptions: 配置ADC
  67. ** input parameters: none
  68. ** output parameters: none
  69. ** Returned value: none
  70. *************************************************************************************/
  71. void ADC_Configuration()
  72. {
  73. DrvGPIO_InitFunction(FUNC_ADC0);//配置引脚为AD功能
  74. DrvADC_Open(0, EXTERNAL_CLOCK, 3);//0:指定采样通道为ADC0; EXTERNAL_CLOCK: ADC时钟源为外部晶振 3:ADC clokc = 12/(3+1) = 3MHz
  75. }
  76. /*************************************************************************************
  77. ** Function name: delay_ms
  78. ** Descriptions: 1ms(晶振为12MHZ)延时子程序
  79. ** input parameters: count
  80. ** output parameters: none
  81. ** Returned value: none
  82. *************************************************************************************/
  83. void delay_ms(uint32_t count)
  84. {
  85. uint32_t i,j;
  86. for(i=count;i>0;i--)
  87. for(j=2395;j>0;j--);
  88. }
hw_config.h文件
  1. #ifndef __HW_CONFIG_H__
  2. #define __HW_CONFIG_H__
  3. void Set_System(void);
  4. void RCC_Configuration(void);
  5. void GPIO_Configuration(void);
  6. void USART_Configuration(void);
  7. void ADC_Configuration(void);
  8. void delay_ms(uint32_t count);
  9. #endif
includes.h文件
  1. #ifndef __INCLUDES_H__
  2. #define __INCLUDES_H__
  3. //库文件
  4. #include <stdio.h>
  5. #include "Mini51.h"
  6. #include "DrvGPIO.h"
  7. #include "DrvUART.h"
  8. #include "DrvSYS.h"
  9. #include "DrvADC.h"
  10. //自己定义的文件
  11. #include "variables.h"
  12. #include "hw_config.h"
  13. #endif

运行截图

工程

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
Swallow_0322 发表于 2012-1-15 07:54 | 显示全部楼层
做沙发!
plc_avr 发表于 2012-1-15 10:34 | 显示全部楼层
板凳。。。
hotpower 发表于 2012-1-15 10:35 | 显示全部楼层
已满三篇,谢谢!!!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
mcs8098 发表于 2012-1-15 10:36 | 显示全部楼层
 楼主| lixiaoxu2meng 发表于 2012-1-15 14:38 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:淡定,不以物喜,不以己悲。

0

主题

1679

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部