[开发工具] 【PIC-IoT WA开发板】+ADC采集光亮

[复制链接]
1336|1
 楼主| qjp1988113 发表于 2020-11-2 10:31 | 显示全部楼层 |阅读模式
本帖最后由 qjp1988113 于 2020-11-2 10:38 编辑

今天有空,试一下PIC-IoT WA开发板上PIC24FJ128的ADC功能,我们测试板上的光敏传感器在不同亮度下的分压。
电路如下:

PIC24FJ128的ADC支持10BIt/12BIT的采样模式。我们这里用12BIT,软件触发非扫描的形式采样,毕竟就用了一个通道么。
采样速度最高200K/S,普通也够用了。

下面进行代码的配置:


生成的ADC的程序:
C文件:
  1. /**
  2.   Section: Included Files
  3. */
  4. #include "adc1.h"

  5. /**
  6.   Section: File Specific Functions
  7. */

  8. // ADC1 Default Interrupt Handler
  9. static void (*ADC1_DefaultInterruptHandler)(void) = NULL;

  10. /**
  11.   Section: Driver Interface
  12. */

  13. void ADC1_Initialize (void)
  14. {
  15.     // ASAM enabled; DMABM disabled; ADSIDL disabled; DONE disabled; DMAEN disabled; FORM Absolute decimal result, unsigned, right-justified; SAMP disabled; SSRC Internal counter ends sampling and starts conversion; MODE12 12-bit; ADON enabled;
  16.     AD1CON1 = 0x8474;
  17.     // CSCNA disabled; NVCFG0 AVSS; PVCFG AVDD; ALTS disabled; BUFM disabled; SMPI Generates interrupt after completion of every sample/conversion operation; BUFREGEN disabled;
  18.     AD1CON2 = 0x00;
  19.     // SAMC 31; EXTSAM disabled; PUMPEN disabled; ADRC RC clock; ADCS 0;
  20.     AD1CON3 = 0x9F00;
  21.     // CH0SA AN0; CH0SB AN0; CH0NB AVSS; CH0NA AVSS;
  22.     AD1CHS = 0x00;
  23.     // CSS30 disabled; CSS29 disabled; CSS28 disabled;
  24.     AD1CSSH = 0x00;
  25.     // CSS9 disabled; CSS8 disabled; CSS7 disabled; CSS6 disabled; CSS5 disabled; CSS4 disabled; CSS3 disabled; CSS2 disabled; CSS15 disabled; CSS1 disabled; CSS14 disabled; CSS0 disabled; CSS13 disabled; CSS12 disabled; CSS11 disabled; CSS10 disabled;
  26.     AD1CSSL = 0x00;
  27.     // DMABL Allocates 1 word of buffer to each analog input;
  28.     AD1CON4 = 0x00;
  29.     // ASEN disabled; WM Legacy operation; ASINT No interrupt; CM Less Than mode; BGREQ disabled; CTMREQ disabled; LPEN disabled;
  30.     AD1CON5 = 0x00;
  31.     // CHH9 disabled; CHH8 disabled; CHH7 disabled; CHH6 disabled; CHH5 disabled; CHH4 disabled; CHH3 disabled; CHH2 disabled; CHH1 disabled; CHH0 disabled; CHH11 disabled; CHH10 disabled; CHH13 disabled; CHH12 disabled;
  32.     AD1CHITL = 0x00;
  33.     // CTMEN30 disabled; CTMEN29 disabled; CTMEN28 disabled;
  34.     AD1CTMENH = 0x00;
  35.     // CTMEN5 disabled; CTMEN6 disabled; CTMEN7 disabled; CTMEN8 disabled; CTMEN9 disabled; CTMEN12 disabled; CTMEN13 disabled; CTMEN10 disabled; CTMEN0 disabled; CTMEN11 disabled; CTMEN1 disabled; CTMEN2 disabled; CTMEN3 disabled; CTMEN4 disabled; CTMEN14 disabled; CTMEN15 disabled;
  36.     AD1CTMENL = 0x00;
  37.     // AD1RESDMA 0;
  38.     AD1RESDMA = 0x00;
  39.     // VBGEN3 disabled; VBGEN2 disabled; VBGEN1 disabled;
  40.     ANCFG = 0x00;
  41.    
  42.     //Assign Default Callbacks
  43.     ADC1_SetInterruptHandler(&ADC1_CallBack);
  44.    

  45. }

  46. void __attribute__ ((weak)) ADC1_CallBack(void)
  47. {

  48. }

  49. void ADC1_SetInterruptHandler(void* handler)
  50. {
  51.     ADC1_DefaultInterruptHandler = handler;
  52. }

  53. void __attribute__ ((weak)) ADC1_Tasks ( void )
  54. {
  55.     if(IFS0bits.AD1IF)
  56.     {
  57.         if(ADC1_DefaultInterruptHandler)
  58.         {
  59.             ADC1_DefaultInterruptHandler();
  60.         }

  61.         // clear the ADC interrupt flag
  62.         IFS0bits.AD1IF = false;
  63.     }
  64. }

  65. /*******************************************************************************

  66.   !!! Deprecated Definitions and APIs !!!
  67.   !!! These functions will not be supported in future releases !!!

  68. *******************************************************************************/
  69. void ADC1_Start(void)
  70. {
  71.    AD1CON1bits.SAMP = 1;
  72. }

  73. void ADC1_Stop(void)
  74. {
  75.    AD1CON1bits.SAMP = 0;
  76. }

  77. uint16_t ADC1_ConversionResultBufferGet(uint16_t *buffer)
  78. {
  79.     int count;
  80.     uint16_t *ADC16Ptr;
  81.     uint16_t sampleCount = AD1CON2bits.SMPI;

  82.     ADC16Ptr = (uint16_t *)&(ADC1BUF0);

  83.     for(count=0;count<=sampleCount;count++)
  84.     {
  85.         buffer[count] = (uint16_t)*ADC16Ptr;
  86.         ADC16Ptr++;
  87.     }
  88.     return count;
  89. }


  90. /**
  91.   End of File
  92. */
h文件:
  1. #ifndef _ADC1_H
  2. #define _ADC1_H

  3. /**
  4.   Section: Included Files
  5. */

  6. #include <xc.h>
  7. #include <stdbool.h>
  8. #include <stdint.h>
  9. #include <stdlib.h>

  10. #ifdef __cplusplus  // Provide C++ Compatibility

  11.     extern "C" {

  12. #endif

  13. /**
  14.   Section: Data Types
  15. */

  16. /** Scan Selected Macro Definition

  17. [url=home.php?mod=space&uid=267864]@summary[/url]
  18.    Defines the scan option selection done for the shared channels.

  19. @Description
  20.    This macro defines the scan option selection done for the shared channels.

  21. Remarks:
  22.    None
  23. */
  24. #define ADC1_SCAN_MODE_SELECTED false

  25. /** ADC1 Channel Definition

  26. [url=home.php?mod=space&uid=267864]@summary[/url]
  27.    Defines the channels selected.

  28. @Description
  29.    This routine defines the channels that are available for the module to use.

  30. Remarks:
  31.    None
  32. */
  33. typedef enum
  34. {
  35.     Light_In,//Channel Name:AN8   Assigned to:Shared Channel
  36.     CHANNEL_CTMU_temperature_sensor_input,//Channel Name:CTMU temperature sensor input   Assigned to:Shared Channel
  37.     CHANNEL_CTMU,//Channel Name:CTMU   Assigned to:Shared Channel
  38.     CHANNEL_VBG,//Channel Name:VBG   Assigned to:Shared Channel
  39.     CHANNEL_AVSS,//Channel Name:AVSS   Assigned to:Shared Channel
  40.     CHANNEL_AVDD,//Channel Name:AVDD   Assigned to:Shared Channel
  41. } ADC1_CHANNEL;

  42. /**
  43.   Section: Interface Routines
  44. */

  45. /**
  46.   @Summary
  47.     Initializes ADC1 module.

  48.   @Description
  49.     This routine initializes ADC1 module, using the given initialization data.
  50.     This routine must be called before any other ADC routine is called.

  51.   @Preconditions
  52.     None.

  53.   @Param
  54.     None.

  55.   @Returns
  56.     None

  57.   @Example
  58.     <code>
  59.         int conversion,i=0;
  60.         ADC1_Initialize();

  61.         ADC1_Enable();
  62.         ADC1_ChannelSelect(channel);
  63.         ADC1_SoftwareTriggerEnable();
  64.         //Provide Delay
  65.         for(i=0;i <1000;i++)
  66.         {
  67.         }
  68.         ADC1_SoftwareTriggerDisable();
  69.         while(!ADC1_IsConversionComplete(channel));
  70.         conversion = ADC1_ConversionResultGet(channel);
  71.         ADC1_Disable();
  72.     </code>
  73. */
  74. void ADC1_Initialize (void);

  75. /**
  76.   @Summary
  77.     Enables the ADC1 module.

  78.   @Description
  79.     This routine is used to enable the ADC1 module.

  80.   @Preconditions
  81.     ADC1_Initialize function should have been called
  82.     before calling this function.

  83.   @Param
  84.     None.

  85.   @Returns
  86.     None.

  87.   @Example
  88.     <code>
  89.         int conversion,i=0;
  90.         ADC1_Initialize();

  91.         ADC1_Enable();
  92.         ADC1_ChannelSelect(channel);
  93.         ADC1_SoftwareTriggerEnable();
  94.         //Provide Delay
  95.         for(i=0;i <1000;i++)
  96.         {
  97.         }
  98.         ADC1_SoftwareTriggerDisable();
  99.         while(!ADC1_IsConversionComplete(channel));
  100.         conversion = ADC1_ConversionResultGet(channel);
  101.         ADC1_Disable();
  102.     </code>
  103. */
  104. inline static void ADC1_Enable(void)
  105. {
  106.    AD1CON1bits.ADON = 1;
  107. }

  108. /**
  109.   @Summary
  110.     Disables the ADC1 module.

  111.   @Description
  112.     This routine is used to disable the ADC1 module.

  113.   @Preconditions
  114.     ADC1_Initialize function should have been called
  115.     before calling this function.

  116.   @Param
  117.     None.

  118.   @Returns
  119.     None.

  120.   @Example
  121.     <code>
  122.         int conversion,i=0;
  123.         ADC1_Initialize();

  124.         ADC1_Enable();
  125.         ADC1_ChannelSelect(channel);
  126.         ADC1_SoftwareTriggerEnable();
  127.         //Provide Delay
  128.         for(i=0;i <1000;i++)
  129.         {
  130.         }
  131.         ADC1_SoftwareTriggerDisable();
  132.         while(!ADC1_IsConversionComplete(channel));
  133.         conversion = ADC1_ConversionResultGet(channel);
  134.         ADC1_Disable();
  135.     </code>
  136. */
  137. inline static void ADC1_Disable(void)
  138. {
  139.    AD1CON1bits.ADON = 0;
  140. }

  141. /**
  142.   @Summary
  143.     Starts sampling manually.

  144.   @Description
  145.     This routine is used to start sampling manually.

  146.   @Preconditions
  147.     ADC1_Initialize function should have been called
  148.     before calling this function.

  149.   @Param
  150.     None.

  151.   @Returns
  152.     None.

  153.   @Example
  154.     <code>
  155.         int conversion,i=0;
  156.         ADC1_Initialize();

  157.         ADC1_Enable();
  158.         ADC1_ChannelSelect(channel);
  159.         ADC1_SoftwareTriggerEnable();
  160.         //Provide Delay
  161.         for(i=0;i <1000;i++)
  162.         {
  163.         }
  164.         ADC1_SoftwareTriggerDisable();
  165.         while(!ADC1_IsConversionComplete(channel));
  166.         conversion = ADC1_ConversionResultGet(channel);
  167.         ADC1_Disable();
  168.     </code>
  169. */

  170. inline static void ADC1_SoftwareTriggerEnable(void)
  171. {
  172.    AD1CON1bits.SAMP = 1;
  173. }

  174. /**
  175.   @Summary
  176.     Stops sampling manually.

  177.   @Description
  178.     This routine is used to stop the sampling manually.

  179.   @Preconditions
  180.     ADC1_Initialize() function should have been
  181.     called before calling this function.

  182.   @Param
  183.     None.

  184.   @Returns
  185.     None.

  186.   @Example
  187.     <code>
  188.         int conversion,i=0;
  189.         ADC1_Initialize();

  190.         ADC1_Enable();
  191.         ADC1_ChannelSelect(channel);
  192.         ADC1_SoftwareTriggerEnable();
  193.         //Provide Delay
  194.         for(i=0;i <1000;i++)
  195.         {
  196.         }
  197.         ADC1_SoftwareTriggerDisable();
  198.         while(!ADC1_IsConversionComplete(channel));
  199.         conversion = ADC1_ConversionResultGet(channel);
  200.         ADC1_Disable();
  201.     </code>
  202. */
  203. inline static void ADC1_SoftwareTriggerDisable(void)
  204. {
  205.    AD1CON1bits.SAMP = 0;
  206. }

  207. /**
  208.   @Summary
  209.     Allows selection of a channel for conversion.

  210.   @Description
  211.     This routine is used to select desired channel for conversion.
  212.   
  213.   @Preconditions
  214.     ADC1_Initialize() function should have been
  215.     called before calling this function.

  216.   @Param
  217.     channel - Channel for conversion

  218.   @Returns
  219.     None
  220.   
  221.   @Example
  222.     <code>
  223.         int conversion,i=0;
  224.         ADC1_Initialize();

  225.         ADC1_Enable();
  226.         ADC1_ChannelSelect(channel);
  227.         ADC1_SoftwareTriggerEnable();
  228.         //Provide Delay
  229.         for(i=0;i <1000;i++)
  230.         {
  231.         }
  232.         ADC1_SoftwareTriggerDisable();
  233.         while(!ADC1_IsConversionComplete(channel));
  234.         conversion = ADC1_ConversionResultGet(channel);
  235.         ADC1_Disable();
  236.     </code>
  237. */
  238. inline static void ADC1_ChannelSelect( ADC1_CHANNEL channel )
  239. {
  240.     switch(channel)
  241.     {
  242.         case Light_In:
  243.                 AD1CHSbits.CH0SA= 0x8;
  244.                 break;
  245.         case CHANNEL_CTMU_temperature_sensor_input:
  246.                 AD1CHSbits.CH0SA= 0xE;
  247.                 break;
  248.         case CHANNEL_CTMU:
  249.                 AD1CHSbits.CH0SA= 0xF;
  250.                 break;
  251.         case CHANNEL_VBG:
  252.                 AD1CHSbits.CH0SA= 0x1C;
  253.                 break;
  254.         case CHANNEL_AVSS:
  255.                 AD1CHSbits.CH0SA= 0x1D;
  256.                 break;
  257.         case CHANNEL_AVDD:
  258.                 AD1CHSbits.CH0SA= 0x1E;
  259.                 break;
  260.         default:
  261.                 break;
  262.     }
  263. }

  264. /**
  265.   @Summary
  266.     Returns the conversion value for the channel selected.

  267.   @Description
  268.     This routine is used to get the analog to digital converted value for a
  269.     specific channel.

  270.   @Preconditions
  271.     This routine returns the conversion value only after the conversion is complete.
  272.     Conversion completion status can be checked using ADC1_IsConversionComplete(channel)
  273.     routine.

  274.   @Param
  275.     channel - Selected channel
  276.    
  277.   @Returns
  278.    Returns the analog to digital converted value
  279.   
  280.   @Example
  281.     <code>
  282.         int conversion,i=0;
  283.         ADC1_Initialize();

  284.         ADC1_Enable();
  285.         ADC1_ChannelSelect(channel);
  286.         ADC1_SoftwareTriggerEnable();
  287.         //Provide Delay
  288.         for(i=0;i <1000;i++)
  289.         {
  290.         }
  291.         ADC1_SoftwareTriggerDisable();
  292.         while(!ADC1_IsConversionComplete(channel));
  293.         conversion = ADC1_ConversionResultGet(channel);
  294.         ADC1_Disable();
  295.     </code>
  296. */
  297. inline static uint16_t ADC1_ConversionResultGet( ADC1_CHANNEL channel )
  298. {
  299.     uint16_t result;

  300.     result = ADC1BUF0;

  301.     return result;
  302. }

  303. /**
  304.   @Summary
  305.     Returns the status of conversion.

  306.   @Description
  307.     This routine is used to determine if conversion is completed. When conversion
  308.     is complete the routine returns true otherwise false.

  309.   @Preconditions
  310.     ADC1_Initialize() function should have been
  311.     called before calling this function.

  312.   @Param
  313.     channel - Selected channel

  314.   @Returns
  315.     true - Conversion is complete.
  316.     false - Conversion is not complete.
  317.   
  318.   @Example
  319.     <code>
  320.         int conversion,i=0;
  321.         ADC1_Initialize();

  322.         ADC1_Enable();
  323.         ADC1_ChannelSelect(channel);
  324.         ADC1_SoftwareTriggerEnable();
  325.         //Provide Delay
  326.         for(i=0;i <1000;i++)
  327.         {
  328.         }
  329.         ADC1_SoftwareTriggerDisable();
  330.         while(!ADC1_IsConversionComplete(channel));
  331.         conversion = ADC1_ConversionResultGet(channel);
  332.         ADC1_Disable();
  333.     </code>
  334. */
  335. inline static bool ADC1_IsConversionComplete(ADC1_CHANNEL channel)
  336. {
  337.     bool status;

  338.     status = AD1CON1bits.DONE;

  339.     return status;
  340. }

  341. /**
  342.   @Summary
  343.     Enables interrupts.

  344.   @Description
  345.     This routine is used to enable the ADC1 interrupt.

  346.   @Preconditions
  347.     None.

  348.   @Param
  349.     None.

  350.   @Returns
  351.     None.

  352.   @Example
  353.     <code>
  354.         ADC1_InterruptEnable();
  355.     </code>
  356. */
  357. inline static void ADC1_InterruptEnable(void)
  358. {
  359.     IEC0bits.AD1IE = 1;
  360. }

  361. /**
  362.   @Summary
  363.     Disables interrupts.

  364.   @Description
  365.     This routine is used to disable the ADC1 interrupt.

  366.   @Preconditions
  367.     None.

  368.   @Param
  369.     None.

  370.   @Returns
  371.     None.

  372.   @Example
  373.     <code>
  374.         ADC1_InterruptDisable();
  375.     </code>
  376. */
  377. inline static void ADC1_InterruptDisable(void)
  378. {
  379.     IEC0bits.AD1IE = 0;
  380. }

  381. /**
  382.   @Summary
  383.     Clears interrupt flag

  384.   @Description
  385.     This routine is used to clear the interrupt flag manually.

  386.   @Preconditions
  387.     None.

  388.   @Param
  389.     None.

  390.   @Returns
  391.     None.

  392.   @Example
  393.     <code>
  394.         ADC1_InterruptFlagClear();
  395.     </code>
  396. */
  397. inline static void ADC1_InterruptFlagClear(void)
  398. {
  399.     IFS0bits.AD1IF = 0;
  400. }

  401. /**
  402.   @Summary
  403.     Allows selection of priority for interrupt.

  404.   @Description
  405.     This routine is used to select desired priority for interrupt.
  406.   
  407.   @Preconditions
  408.     None.

  409.   @Param
  410.     None.

  411.   @Returns
  412.     None.

  413.   @Example
  414.     <code>
  415.         uint16_t priorityValue;
  416.         priorityValue = 0x002;

  417.         ADC1_InterruptPrioritySet(priorityValue);
  418.     </code>
  419. */
  420. inline static void ADC1_InterruptPrioritySet( uint16_t priorityValue )
  421. {
  422.     IPC3bits.AD1IP = 0x7 & priorityValue;
  423. }

  424. /**
  425.   @Summary
  426.     Callback for ADC1.

  427.   @Description
  428.     This routine is callback for ADC1
  429.   
  430.   @Preconditions
  431.     None.

  432.   @Param
  433.     None.

  434.   @Returns
  435.     None

  436.   [url=home.php?mod=space&uid=389923]@example[/url]
  437.     <code>   
  438.         ADC1_CallBack();
  439.     </code>
  440. */
  441. void ADC1_CallBack(void);

  442. /**
  443.   @Summary
  444.     Assigns a function pointer with a callback address.

  445.   @Description
  446.     This routine assigns a function pointer with a callback address.

  447.   @Preconditions
  448.     None.

  449.   @Param
  450.     Address of the callback routine.

  451.   @Returns
  452.     None

  453.   [url=home.php?mod=space&uid=389923]@example[/url]
  454.     <code>
  455.         ADC1_SetInterruptHandler(&ADC1_CallBack);
  456.     </code>
  457. */
  458. void ADC1_SetInterruptHandler(void* handler);


  459. /**
  460.   [url=home.php?mod=space&uid=267864]@summary[/url]               
  461.     Polled implementation

  462.   @Description
  463.     This routine is used to implement the tasks for polled implementations.
  464.   
  465.   @Preconditions
  466.     ADC1_Initialize() function should have been
  467.     called before calling this function.

  468.   @Param
  469.     None

  470.   @Returns
  471.     None

  472.   @Example
  473.     <code>   
  474.         ADC1_Tasks();
  475.     </code>
  476. */
  477. void ADC1_Tasks(void);

  478. /*******************************************************************************

  479.   !!! Deprecated Definitions and APIs !!!
  480.   !!! These functions will not be supported in future releases !!!

  481. *******************************************************************************/
  482. /**
  483.   @Summary
  484.     Starts sampling manually.

  485.   @Description
  486.     This routine is used to start the sampling manually.

  487.   @Preconditions
  488.     ADC1_Initialize function should have been called
  489.     before calling this function.

  490.   @Param
  491.     None.

  492.   @Returns
  493.     None.

  494.   @Example
  495.     None.
  496. */
  497. void __attribute__((deprecated("\nThis will be removed in future MCC releases. \nUse ADC1_SoftwareTriggerEnable instead."))) ADC1_Start(void);

  498. /**
  499.   @Summary
  500.     Stops sampling manually.

  501.   @Description
  502.     This routine is used to stop the sampling manually before conversion
  503.     is triggered.

  504.   @Preconditions
  505.     ADC1_Initialize() function should have been
  506.     called before calling this function.

  507.   @Param
  508.     None.

  509.   @Returns
  510.     None.

  511.   @Example
  512.     None.
  513. */
  514. void __attribute__((deprecated("\nThis will be removed in future MCC releases. \nUse ADC1_SoftwareTriggerDisable instead."))) ADC1_Stop(void);

  515. /**
  516.   @Summary
  517.     Gets the buffer loaded with conversion results.

  518.   @Description
  519.     This routine is used to get the analog to digital converted values in a
  520.     buffer. This routine gets converted values from multiple channels.

  521.   @Preconditions
  522.     This routine returns the buffer containing the conversion values only after
  523.     the conversion is complete. Completion status conversion can be checked using
  524.     ADC1_IsConversionComplete() routine.

  525.   @Param
  526.     None.

  527.   @Returns
  528.     Returns the count of the buffer containing the conversion values.

  529.   @Example
  530.     None.
  531. */
  532. uint16_t __attribute__((deprecated("\nThis will be removed in future MCC releases. \nUse ADC1_ConversionResultGet instead."))) ADC1_ConversionResultBufferGet(uint16_t *buffer);

  533. #ifdef __cplusplus  // Provide C++ Compatibility

  534.     }

  535. #endif

  536. #endif //_ADC1_H
  537.    
  538. /**
  539. End of File
  540. */
h文件里老是插入一些函数的写法,又不要define,起码看起来归类好些。但是用肯定是正常的。
新建light_in.c和light_in.h
light_in.c:
  1. #include "light_in.h"


  2. //NEW ADD
  3. uint16_t ADC1_GetConversion(ADC1_CHANNEL channel)
  4. {
  5.     int conversion,i=0;

  6.     ADC1_Initialize();
  7.    
  8.     //ADC1_Enable();
  9.     ADC1_ChannelSelect(channel);
  10.     ADC1_SoftwareTriggerEnable();
  11.    
  12.     //Provide Delay
  13.     for(i=0;i <1000;i++)
  14.     {
  15.     }
  16.     ADC1_SoftwareTriggerDisable();
  17.     while (!ADC1_IsConversionComplete(channel))
  18.     {
  19.     }
  20.     conversion = ADC1_ConversionResultGet(channel);
  21.     //ADC1_Disable();
  22.     return conversion;   
  23. }

  24. uint16_t SENSORS_getLightValue(void)
  25. {
  26.     return ADC1_GetConversion(LIGHT_SENSOR_ADC_CHANNEL);
  27. }
light_in.h:
  1. /*
  2. * File:   light_in.h
  3. * Author: Administrator
  4. *
  5. * Created on November 2, 2020, 8:50 AM
  6. */

  7. #ifndef LIGHT_IN_H
  8. #define        LIGHT_IN_H

  9. #ifdef        __cplusplus
  10. extern "C" {
  11. #endif

  12. #include "../mcc_generated_files/adc1.h"

  13. #define LIGHT_SENSOR_ADC_CHANNEL        Light_In

  14. uint16_t ADC1_GetConversion(ADC1_CHANNEL channel);
  15. uint16_t SENSORS_getLightValue(void);

  16. #ifdef        __cplusplus
  17. }
  18. #endif

  19. #endif        /* LIGHT_IN_H */

在mian函数while循环里调用:
  1. int nowLight=0;
  2. nowLight= SENSORS_getLightValue();
  3. printf("the light is : %d\r\n",nowLight);
编译下载,查看串口输出:

有±20个最小单位的跳到,效果还是不错的。
本次实验比较简单,但ADC配置那块还得注意些,ADC就到这。



  

本帖子中包含更多资源

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

×
FYLZLXD 发表于 2020-11-2 19:24 | 显示全部楼层
测试数据又交叉, 不能分开, 楼主解决了吗? 什么问题导致的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

111

主题

627

帖子

2

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