Got analog input working.

This commit is contained in:
Marvin Blum
2017-03-21 22:50:17 +01:00
parent fbf8bd8a4f
commit 3c4451e1bb
3 changed files with 39 additions and 22 deletions

20
main.c
View File

@@ -1,5 +1,7 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include "serial.h"
#include "pins.h"
@@ -15,14 +17,21 @@ int main(){
}
void prepare(){
// enable global interrupts and serial port
sei();
serial_init(9600);
// digital input/output example
/*pin_mode(11, OUTPUT);
pin_mode(10, INPUT);
pin_mode(8, INPUT);*/
// anlog input example
pin_mode(A4, INPUT);
}
void loop(){
// digital input/output example
/*if(digital_read(10) && digital_read(8)){
digital_write(11, HIGH);
}
@@ -30,12 +39,11 @@ void loop(){
digital_write(11, LOW);
}*/
if(analog_read(A4) > 0){
serial_write("ok", 3);
}
else{
serial_write("nope", 5);
}
// anlog input example
int analog = analog_read(A4);
char out[15];
sprintf(out, "%d", analog);
serial_write(out, 15);
_delay_ms(25);
}

35
pins.c
View File

@@ -1,5 +1,6 @@
#include "pins.h"
#include <avr/io.h>
#include <avr/interrupt.h>
const unsigned char LOW = 0x00;
const unsigned char HIGH = 0x01;
@@ -16,6 +17,9 @@ const unsigned char A5 = 0x13;
const unsigned char A6 = 0x14;
const unsigned char A7 = 0x15;
double dutyCycle = 0; // TODO more than one???
void analog_read_duty_cycle();
unsigned char map_analog_pin(unsigned char);
void pin_mode(unsigned char pin, unsigned char mode){
@@ -61,8 +65,6 @@ int digital_read(unsigned char pin){
return 0;
}
// TODO turn off pwm
if(pin < 8){
// pin 0-7
return PIND&_BV(pin) ? 1 : 0;
@@ -78,8 +80,6 @@ void digital_write(unsigned char pin, unsigned char value){
return;
}
// TODO turn off pwm
if(pin < 8){
// pin 0-7
if(value == HIGH){
@@ -102,31 +102,35 @@ void digital_write(unsigned char pin, unsigned char value){
}
}
int analog_read(unsigned char pin){
unsigned int analog_read(unsigned char pin){
pin = map_analog_pin(pin);
if(pin > 7){
return 0;
}
// TODO use ADC
ADMUX = 0xf0|pin;
ADCSRA |= _BV(pin);
while(ADCSRA&_BV(pin));
// enable ADC (128 bit ADPS scaling factor 0x07),
// disable digital input buffer (DIDR0)
ADCSRA = _BV(ADEN)|_BV(ADIE)|0x07;
ADMUX = _BV(REFS0)|pin;
DIDR0 |= _BV(pin);
// while(!(ADCSRA&_BV(ADIF)));
analog_read_duty_cycle();
return ADC;
}
void analog_write(unsigned char pin, int value){
void analog_read_duty_cycle(){
ADCSRA |= _BV(ADSC);
}
void analog_write(unsigned char pin, unsigned int value){
pin = map_analog_pin(pin);
if(pin > 7){
return;
}
// TODO turn off pwm
if(value == HIGH){
PORTC |= _BV(pin);
}
@@ -143,3 +147,8 @@ unsigned char map_analog_pin(unsigned char pin){
return pin;
}
ISR(ADC_vect){
dutyCycle = ADC;
analog_read_duty_cycle();
}

4
pins.h
View File

@@ -18,7 +18,7 @@ extern const unsigned char A7;
void pin_mode(unsigned char, unsigned char);
int digital_read(unsigned char);
void digital_write(unsigned char, unsigned char);
int analog_read(unsigned char);
void analog_write(unsigned char, int);
unsigned int analog_read(unsigned char);
void analog_write(unsigned char, unsigned int);
#endif