main.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. ChibiOS - Copyright (C) 2016 Theodore Ateba
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "chprintf.h"
  16. /*
  17. * In this demo we use a single channel to sample voltage across
  18. * a potentiometer.
  19. */
  20. #define MY_NUM_CH 1
  21. #define MY_SAMPLING_NUMBER 10
  22. /**
  23. * @brief Global variables
  24. */
  25. BaseSequentialStream * chp = (BaseSequentialStream *) &SD1;
  26. static int32_t adcConvA0 = 0; /**< A0 adc conversion. */
  27. static float voltageA0 = 0; /**< A0 voltage. */
  28. static adcsample_t sample_buff[MY_NUM_CH * MY_SAMPLING_NUMBER];
  29. /*
  30. * ADC conversion group.
  31. * Mode: Linear buffer, 10 samples of 1 channel, SW triggered.
  32. * Channels: IN0 (Arduino Pin A0).
  33. */
  34. static const ADCConversionGroup my_conversion_group = {
  35. FALSE, /* Not circular buffer. */
  36. MY_NUM_CH, /* Number of channels. */
  37. NULL, /* No ADC callback function. */
  38. 1, /* Channel mask. */
  39. };
  40. static const ADCConfig adcConfig = {
  41. ANALOG_REFERENCE_AVCC, /* Analog reference. */
  42. };
  43. static THD_WORKING_AREA(waThd1, 32);
  44. static THD_FUNCTION(Thd1, arg) {
  45. (void) arg;
  46. chRegSetThreadName("LED Thread");
  47. while (true) {
  48. palTogglePad(IOPORT2, PORTB_LED1);
  49. chThdSleepMilliseconds(1000);
  50. }
  51. }
  52. static THD_WORKING_AREA(waThd2, 512);
  53. static THD_FUNCTION(Thd2, arg) {
  54. unsigned i;
  55. (void) arg;
  56. chRegSetThreadName("ADC1 Thread");
  57. /* Activates the ADC1 driver. */
  58. adcStart(&ADCD1, &adcConfig);
  59. while (TRUE) {
  60. /* Make ADC conversion of the voltage on A0. */
  61. adcConvert(&ADCD1, &my_conversion_group, sample_buff, MY_SAMPLING_NUMBER);
  62. /* Making mean of sampled values.*/
  63. for (i = 0; i < MY_NUM_CH * MY_SAMPLING_NUMBER; i++) {
  64. adcConvA0 += sample_buff[i];
  65. }
  66. adcConvA0 /= (MY_NUM_CH * MY_SAMPLING_NUMBER);
  67. voltageA0 = (((float)adcConvA0 * 5) / 1024);
  68. }
  69. }
  70. /**
  71. * Application entry point.
  72. */
  73. int main(void) {
  74. /*
  75. * System initializations.
  76. * - HAL initialization, this also initializes the configured device drivers
  77. * and performs the board-specific initializations.
  78. * - Kernel initialization, the main() function becomes a thread and the
  79. * RTOS is active.
  80. */
  81. halInit();
  82. chSysInit();
  83. /*
  84. * Initialized the serial driver an turn off the onboard debug led.
  85. */
  86. sdStart(&SD1, NULL);
  87. palClearPad(IOPORT2, PORTB_LED1);
  88. /*
  89. * Starts the LED blinker thread.
  90. */
  91. chThdCreateStatic(waThd1, sizeof(waThd1), NORMALPRIO + 1, Thd1, NULL);
  92. chThdCreateStatic(waThd2, sizeof(waThd2), NORMALPRIO + 1, Thd2, NULL);
  93. chprintf(chp, "AVR ADC program example... \r\n");
  94. while (TRUE) {
  95. chprintf(chp, "Measure the voltage on A0 pin: \r\n");
  96. chprintf(chp, " %.3fv \r\n", voltageA0);
  97. chThdSleepMilliseconds(100);
  98. chprintf(chp, "\033[2J\033[1;1H");
  99. }
  100. }