[菜农助学交流] 第五批 之3 中断法AD+查询法AD

[复制链接]
3706|0
 楼主| cnjxlxp 发表于 2012-4-15 22:25 | 显示全部楼层 |阅读模式
AD, IO, ADC, dc, UART
中断法:


  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "Driver\DrvGPIO.h"
  4. #include "Driver\DrvSYS.h"
  5. #include "Driver\DrvUART.h"
  6. #include "Driver\DrvADC.h"

  7. #define  Run_Led  2     //2----LED1  3----LED2   4----LED3  5----LED4
  8. uint16_t i32ConversionData;
  9. void Init_System(void)
  10. {
  11.     UNLOCKREG(x);
  12. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);  
  13. DrvSYS_Delay(5000);
  14. LOCKREG(x);
  15. }
  16. void delay_ms(uint32_t count)
  17. {
  18.     uint32_t i,j;
  19. for(i=count;i>0;i--)
  20.     for(j=2395;j>0;j--);
  21. }
  22. void AdcIntCallback(uint32_t u32UserData)
  23. {
  24. i32ConversionData = DrvADC_GetConversionData(1);
  25. printf("%d\n",i32ConversionData);
  26. }
  27. void Init_Uart(void)
  28. {
  29. STR_UART_T param;
  30. DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
  31. DrvGPIO_InitFunction(E_FUNC_UART0);
  32. param.u32BaudRate        = 9600;     
  33. param.u8cDataBits        = DRVUART_DATABITS_8;  
  34. param.u8cStopBits        = DRVUART_STOPBITS_1;   
  35. param.u8cParity          = DRVUART_PARITY_NONE;   
  36. param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;   
  37. param.u8TimeOut          = 0;
  38. DrvUART_Open(UART_PORT0,&param);      
  39. }
  40. int main (void)
  41. {
  42. uint8_t IsFirst = 1;

  43. Init_System();

  44. Init_Uart();
  45. DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);
  46. DrvGPIO_ClrBit(E_GPA,Run_Led);
  47.     DrvGPIO_InitFunction(E_FUNC_ADC1);
  48.     DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);
  49. printf("\n");
  50. printf("Driver version: %x\n", DrvADC_GetVersion());
  51. DrvADC_EnableSelfCalibration();     
  52. while(DrvADC_IsCalibrationDone()==0);  
  53. DrvADC_DisableSelfCalibration();
  54. DrvSYS_Delay(5000);
  55. DrvADC_EnableADCInt(AdcIntCallback, 0);
  56.     while(1)
  57.     {
  58.   DrvADC_StartConvert();           
  59.     }
  60. }


查询法:


  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "Driver\DrvGPIO.h"
  4. #include "Driver\DrvSYS.h"
  5. #include "Driver\DrvUART.h"
  6. #include "Driver\DrvADC.h"

  7. #define  Run_Led  2     //2----LED1  3----LED2   4----LED3  5----LED4
  8. void Init_System(void)
  9. {
  10.     UNLOCKREG(x);
  11. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);  
  12. DrvSYS_Delay(5000);
  13. LOCKREG(x);
  14. }
  15. void delay_ms(uint32_t count)
  16. {
  17.     uint32_t i,j;
  18. for(i=count;i>0;i--)
  19.     for(j=2395;j>0;j--);
  20. }
  21. void Init_Uart(void)
  22. {
  23. STR_UART_T param;
  24. DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC,0);
  25. DrvGPIO_InitFunction(E_FUNC_UART0);
  26. param.u32BaudRate        = 9600;     
  27. param.u8cDataBits        = DRVUART_DATABITS_8;  
  28. param.u8cStopBits        = DRVUART_STOPBITS_1;   
  29. param.u8cParity          = DRVUART_PARITY_NONE;   
  30. param.u8cRxTriggerLevel  = DRVUART_FIFO_1BYTES;   
  31. param.u8TimeOut          = 0;
  32. DrvUART_Open(UART_PORT0,&param);      
  33. }
  34. int main (void)
  35. {
  36. uint16_t i32ConversionData;
  37. // uint32_t i32ConversionData_Sum;
  38. uint8_t IsFirst = 1;

  39. Init_System();

  40. Init_Uart();
  41. DrvGPIO_Open(E_GPA,Run_Led, E_IO_OUTPUT);
  42. DrvGPIO_ClrBit(E_GPA,Run_Led);
  43.     DrvGPIO_InitFunction(E_FUNC_ADC1);
  44.     DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x02, EXTERNAL_12MHZ, 3);
  45. printf("\n");
  46. printf("Driver version: %x\n", DrvADC_GetVersion());
  47. DrvADC_EnableSelfCalibration();     
  48. while(DrvADC_IsCalibrationDone()==0);  
  49. DrvADC_DisableSelfCalibration();
  50. DrvSYS_Delay(5000);
  51.     while(1)
  52.     {
  53.        DrvADC_StartConvert();           

  54.     while(DrvADC_IsConversionDone()==0);  
  55.     i32ConversionData = DrvADC_GetConversionData(1);
  56.     printf("%d\n",i32ConversionData);
  57.     DrvSYS_Delay(5000);
  58.     DrvSYS_Delay(5000);
  59.     delay_ms(1000);
  60.     // DrvADC_Close();
  61.      }
  62.     }

本帖子中包含更多资源

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

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

0

主题

46

帖子

2

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