1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
-
- #include <avr/io.h>
- #include <stdint.h>
-
- #include "adc.h"
-
- void adcInit(void) {
- 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;
- }
|