Added ADC and rotary encoder functions
parent
4173c08c95
commit
2e6026421e
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* adc.c
|
||||
*
|
||||
* Created: 09/12/2021 16:31:16
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "adc.h"
|
||||
|
||||
void
|
||||
adc_init(void)
|
||||
{
|
||||
ADMUX = (1<<REFS1)|
|
||||
(0<<REFS0)| /* Set Voltage reference to 2.56V */
|
||||
(0<<ADLAR) /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
|
||||
;
|
||||
|
||||
ADCSRA = (1<<ADEN)| /* Enable ADC */
|
||||
(0<<ADATE)| /* Disable the ADC auto trigger */
|
||||
(1<<ADIE)| /* Enable the ADC interrupt */
|
||||
(1<<ADPS2)|
|
||||
(1<<ADPS1)|
|
||||
(1<<ADPS0) /* Set ADC Prescaler to 128 */
|
||||
;
|
||||
}
|
||||
|
||||
volatile int done = 1;
|
||||
|
||||
/* ADC 1 */
|
||||
uint16_t
|
||||
adc_get_poti(void)
|
||||
{
|
||||
uint16_t adc;
|
||||
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
|
||||
|
||||
ADMUX |= (1<<MUX0); /* Set ADMUX to access ADC channel 1 */
|
||||
ADCSRA |= (1<<ADSC); /* Start the conversion */
|
||||
|
||||
done = 0; /* reset the done flag to false */
|
||||
|
||||
while(done == 0); /* Wait till conversion completes */
|
||||
|
||||
adc = ADC;
|
||||
|
||||
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */
|
||||
|
||||
return adc;
|
||||
}
|
||||
|
||||
|
||||
/* ADC 0 */
|
||||
uint16_t
|
||||
adc_get_LM35(void)
|
||||
{
|
||||
uint16_t adc;
|
||||
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
|
||||
|
||||
ADMUX &= (0<<MUX0); /* Set ADMUX to access ADC channel 0 */
|
||||
ADCSRA |= (1<<ADSC); /* Start the conversion */
|
||||
|
||||
done = 0; /* reset the done flag to false */
|
||||
|
||||
while(done == 0); /* Wait till conversion completes */
|
||||
|
||||
adc = ADC;
|
||||
|
||||
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */
|
||||
|
||||
return adc;
|
||||
}
|
||||
|
||||
ISR(ADC_vect){
|
||||
done = 1; /* set the done flag to true */
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* adc.h
|
||||
*
|
||||
* Created: 09/12/2021 16:31:06
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#ifndef ADC_H_
|
||||
#define ADC_H_
|
||||
|
||||
void adc_init(void);
|
||||
|
||||
uint16_t adc_get_poti(void);
|
||||
uint16_t adc_get_LM35(void);
|
||||
|
||||
#endif /* ADC_H_ */
|
@ -0,0 +1,148 @@
|
||||
/*
|
||||
* rotaryEncoder.c
|
||||
*
|
||||
* Created: 16/12/2021 16:34:05
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include "rotaryEncoder.h"
|
||||
|
||||
#define ROTA PORTC7
|
||||
#define ROTB PORTC6
|
||||
#define ROTBUTTON PORTC5
|
||||
|
||||
/* enums */
|
||||
enum{
|
||||
S2, S3, S4, S5, S6, S7,
|
||||
INIT, WAIT
|
||||
} RotaryState = INIT; /* Emus for the stoplight task (task 3) */
|
||||
|
||||
int16_t count;
|
||||
|
||||
void
|
||||
drehgeber_init(void)
|
||||
{
|
||||
DDRC &= ~(7<<DDC5); /* Set bit 5-7 of Data Direction Register C as input */
|
||||
PORTC |= (7<<PORTC5); /* Initialize bit 5-7 of PORTC as for pull up resistor */
|
||||
|
||||
/* With Timer */
|
||||
// TCCR2A |= (1<<WGM21);
|
||||
// TCCR2B |= (3<<CS20);
|
||||
// TIMSK2 |= (1<<OCIE2A);
|
||||
// OCR2A = 250 - 1;
|
||||
|
||||
/* With external interrupt */
|
||||
PCICR |= (1<<PCIE2);
|
||||
PCMSK2 |= (1<<PCINT23)|(1<<PCINT22);
|
||||
}
|
||||
|
||||
// ISR(TIMER2_COMPA_vect)
|
||||
// {
|
||||
// drehgeber_process();
|
||||
// }
|
||||
|
||||
ISR(PCINT2_vect)
|
||||
{
|
||||
drehgeber_process();
|
||||
}
|
||||
|
||||
void
|
||||
drehgeber_process(void)
|
||||
{
|
||||
uint8_t a,b, enc;
|
||||
a = ((PINC & (1<<ROTA)) == 0);
|
||||
b = ((PINC & (1<<ROTB)) == 0)<<1;
|
||||
enc=a+b;
|
||||
|
||||
switch(RotaryState)
|
||||
{
|
||||
case INIT:
|
||||
RotaryState = WAIT;
|
||||
break;
|
||||
|
||||
case WAIT:
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = S2;
|
||||
}
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = S5;
|
||||
}
|
||||
break;
|
||||
|
||||
case S2:
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = S3;
|
||||
}
|
||||
if(enc == 0)
|
||||
{
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case S3:
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = S4;
|
||||
}
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = S2;
|
||||
}
|
||||
break;
|
||||
|
||||
case S4:
|
||||
if(enc == 0)
|
||||
{
|
||||
count++;
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = S3;
|
||||
}
|
||||
break;
|
||||
|
||||
case S5:
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = S6;
|
||||
}
|
||||
if(enc == 0)
|
||||
{
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
break;
|
||||
|
||||
case S6:
|
||||
if(enc == 1)
|
||||
{
|
||||
RotaryState = S7;
|
||||
}
|
||||
if(enc == 2)
|
||||
{
|
||||
RotaryState = S5;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case S7:
|
||||
if(enc == 0)
|
||||
{
|
||||
count--;
|
||||
RotaryState = WAIT;
|
||||
}
|
||||
if(enc == 3)
|
||||
{
|
||||
RotaryState = S6;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
int16_t drehgeber_get(void)
|
||||
{
|
||||
return count;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
/*
|
||||
* rotaryEncoder.h
|
||||
*
|
||||
* Created: 16/12/2021 16:33:57
|
||||
* Author: n0x
|
||||
*/
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
|
||||
#ifndef ROTARYENCODER_H_
|
||||
#define ROTARYENCODER_H_
|
||||
|
||||
void drehgeber_init(void);
|
||||
void drehgeber_process(void);
|
||||
int16_t drehgeber_get(void);
|
||||
|
||||
|
||||
#endif /* ROTARYENCODER_H_ */
|
Loading…
Reference in New Issue