Simple single-color 8x8x8 LED Cube with AVRs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

adc.c 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * adc.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <avr/io.h>
  22. #include <stdint.h>
  23. #include "adc.h"
  24. void adcInit(void) {
  25. ADMUX = 0;
  26. ADMUX |= (1 << REFS0); // Ref. Voltage: Vcc
  27. ADCSRA |= (1 << ADPS2) | (1 << ADPS0); // Prescaler 64
  28. ADCSRA |= (1 << ADEN); // Enable adc
  29. adcStartConversion(0);
  30. adcGetResult();
  31. }
  32. void adcStartConversion(uint8_t channel) {
  33. ADMUX &= 0xF0; // Clear channel selection bits
  34. ADMUX |= (channel & 0x0F); // Set channel
  35. ADCSRA |= (1 << ADSC); // start conversion
  36. }
  37. uint8_t adcIsFinished(void) {
  38. // Return 1 if ADSC is 0
  39. if (ADCSRA & (1 << ADSC)) {
  40. return 0;
  41. } else {
  42. return 1;
  43. }
  44. }
  45. uint16_t adcGetResult(void) {
  46. while (adcIsFinished() == 0);
  47. ADCSRA &= ~(1 << ADSC);
  48. return ADCW;
  49. }
  50. uint8_t adcGetByte(void) {
  51. uint16_t tmp = adcGetResult();
  52. tmp = tmp >> 2;
  53. return tmp;
  54. }