123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
-
- #include <avr/io.h>
- #include <stdint.h>
-
- #include "adc.h"
-
- void adcInit(void) {
- DDRC &= ~(3);
- ADMUX = 0;
- ADMUX |= (1 << REFS0);
- ADCSRA |= (1 << ADPS2) | (1 << ADPS0);
- ADCSRA |= (1 << ADEN);
- adcStartConversion(0);
- adcGetResult();
- }
-
- void adcStartConversion(uint8_t channel) {
- ADMUX &= 0xF0;
- ADMUX |= (channel & 0x0F);
- ADCSRA |= (1 << ADSC);
- }
-
- uint8_t adcIsFinished(void) {
-
- if (ADCSRA & (1 << ADSC)) {
- return 0;
- } else {
- return 1;
- }
- }
-
- uint16_t adcGetResult(void) {
- while (adcIsFinished() == 0);
- ADCSRA &= ~(1 << ADSC);
- return ADCW;
- }
-
- uint8_t adcGetByte(void) {
- uint16_t tmp = adcGetResult();
- tmp = tmp >> 2;
- return tmp;
- }
|