Minor changes to cli, adc and rotary encoder

This commit is contained in:
_N0x 2022-01-10 18:37:14 +01:00
parent 2e6026421e
commit a2203b7cd5
3 changed files with 82 additions and 63 deletions

View File

@ -281,6 +281,7 @@ static void cmd_led_on(const char* _data){
} }
} }
//================================================================================================================================ //================================================================================================================================
// Funktion: cmd_led_off // Funktion: cmd_led_off
// Kann verwendet werden, um eine LED auszuschalten // Kann verwendet werden, um eine LED auszuschalten
@ -349,8 +350,6 @@ static void cmd_adc(const char* _data){
char lm35Data[50]; char lm35Data[50];
int loopInterval = 500; int loopInterval = 500;
int repeat = strcmp(_data, "R") == 0 ? 1 : 0; /* Check if repeat flag is set*/
do do
{ {
if(Timer_getTick() - g_startMS >= loopInterval){ if(Timer_getTick() - g_startMS >= loopInterval){
@ -372,14 +371,13 @@ static void cmd_adc(const char* _data){
print_string("----------"); print_string("----------");
print_string(NEXT_LINE); print_string(NEXT_LINE);
} }
} while (repeat); // repeat if repeat-flag is given } while (strcmp(_data, "R") == 0 ? 1 : 0); /* Check if repeat flag is set*/
} }
//================================================================================================================================ //================================================================================================================================
// Funktion: cmd_rotary // Funktion: cmd_rotary
// Wird verwendet um die Daten von Poti und LM35 zu lesen // Wird verwendet um die Bewegung von Dregeber zu lesen
// Parameter: _data: Zeichenkette mit Parametern des Befehls. // Parameter: _data: Zeichenkette mit Parametern des Befehls.
// //
// Rückgabewert: keine // Rückgabewert: keine

View File

@ -7,42 +7,73 @@
#include "adc.h" #include "adc.h"
typedef struct ADC_t ADC_t;
struct ADC_t{
/* ADC Data */
uint16_t uiADC :16;
/* ADCSRA */
uint8_t uiADPS0 :1;
uint8_t uiADPS1 :1;
uint8_t uiADPS2 :1;
uint8_t uiADIE :1;
uint8_t uiADIF :1;
uint8_t uiADATE :1;
uint8_t uiADSC :1;
uint8_t uiADEN :1;
/* ADCSRB */
uint8_t uiADTS0 :1;
uint8_t uiADTS1 :1;
uint8_t uiADTS2 :1;
uint8_t nui2 :3;
uint8_t uiACME :1;
uint8_t nui1 :1;
/* ADMUX */
uint8_t uiMUX :5;
uint8_t uiADLAR :1;
uint8_t uiREFS0 :1;
uint8_t uiREFS1 :1;
};
#define myADC ((ADC_t*)(0x78))
void void
adc_init(void) adc_init(void)
{ {
ADMUX = (1<<REFS1)| myADC->uiADLAR = 0;
(0<<REFS0)| /* Set Voltage reference to 2.56V */ myADC->uiREFS0 = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
(0<<ADLAR) /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */ myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
;
ADCSRA = (1<<ADEN)| /* Enable ADC */ myADC->uiADPS0 = 1; /* Set ADC Prescaler to 128 */
(0<<ADATE)| /* Disable the ADC auto trigger */ myADC->uiADPS1 = 1;
(1<<ADIE)| /* Enable the ADC interrupt */ myADC->uiADPS2 = 1;
(1<<ADPS2)| myADC->uiADIE = 1; /* Enable the ADC interrupt */
(1<<ADPS1)| myADC->uiADATE = 0; /* Disable the ADC auto trigger */
(1<<ADPS0) /* Set ADC Prescaler to 128 */ myADC->uiADEN = 1; /* Enable ADC */
;
} }
volatile int done = 1; volatile int done = 1;
/* ADC 1 */ /* ADC 1 */
uint16_t uint16_t
adc_get_poti(void) adc_get_poti(void)
{ {
uint16_t adc; uint16_t adc;
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
//myADC->uiADIE = 0; /* Disable interrupt */
ADMUX |= (1<<MUX0); /* Set ADMUX to access ADC channel 1 */
ADCSRA |= (1<<ADSC); /* Start the conversion */ myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
done = 0; /* reset the done flag to false */ done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */ while(done == 0); /* Wait till conversion completes */
adc = ADC; adc = myADC->uiADC;
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */ //myADC->uiADIE = 1; /* Enable the interrupt again */
return adc; return adc;
} }
@ -53,18 +84,19 @@ uint16_t
adc_get_LM35(void) adc_get_LM35(void)
{ {
uint16_t adc; uint16_t adc;
ADCSRA &= ~(1<<ADIE); /* Disable interrupt */
//myADC->uiADIE = 0; /* Disable interrupt */
ADMUX &= (0<<MUX0); /* Set ADMUX to access ADC channel 0 */
ADCSRA |= (1<<ADSC); /* Start the conversion */ myADC->uiMUX = 0; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
done = 0; /* reset the done flag to false */ done = 0; /* reset the done flag to false */
while(done == 0); /* Wait till conversion completes */ while(done == 0); /* Wait till conversion completes */
adc = ADC; adc = myADC->uiADC;
ADCSRA |= (1<<ADIE); /* Enable the interrupt again */ //myADC->uiADIE = 1; /* Enable the interrupt again */
return adc; return adc;
} }

View File

@ -13,9 +13,9 @@
/* enums */ /* enums */
enum{ enum{
S2, S3, S4, S5, S6, S7, LEFT1, LEFT2, LEFT3, RIGHT1, RIGHT2, RIGHT3,
INIT, WAIT INIT, WAIT
} RotaryState = INIT; /* Emus for the stoplight task (task 3) */ } RotaryState = INIT; /* Enum for the rotary enc */
int16_t count; int16_t count;
@ -24,23 +24,12 @@ drehgeber_init(void)
{ {
DDRC &= ~(7<<DDC5); /* Set bit 5-7 of Data Direction Register C as input */ 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 */ 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 */ /* With external interrupt */
PCICR |= (1<<PCIE2); PCICR |= (1<<PCIE2);
PCMSK2 |= (1<<PCINT23)|(1<<PCINT22); PCMSK2 |= (1<<PCINT23)|(1<<PCINT22);
} }
// ISR(TIMER2_COMPA_vect)
// {
// drehgeber_process();
// }
ISR(PCINT2_vect) ISR(PCINT2_vect)
{ {
drehgeber_process(); drehgeber_process();
@ -63,18 +52,18 @@ drehgeber_process(void)
case WAIT: case WAIT:
if(enc == 1) if(enc == 1)
{ {
RotaryState = S2; RotaryState = LEFT1;
} }
if(enc == 2) if(enc == 2)
{ {
RotaryState = S5; RotaryState = RIGHT1;
} }
break; break;
case S2: case LEFT1:
if(enc == 3) if(enc == 3)
{ {
RotaryState = S3; RotaryState = LEFT2;
} }
if(enc == 0) if(enc == 0)
{ {
@ -82,18 +71,18 @@ drehgeber_process(void)
} }
break; break;
case S3: case LEFT2:
if(enc == 2) if(enc == 2)
{ {
RotaryState = S4; RotaryState = LEFT3;
} }
if(enc == 1) if(enc == 1)
{ {
RotaryState = S2; RotaryState = LEFT1;
} }
break; break;
case S4: case LEFT3:
if(enc == 0) if(enc == 0)
{ {
count++; count++;
@ -101,14 +90,14 @@ drehgeber_process(void)
} }
if(enc == 3) if(enc == 3)
{ {
RotaryState = S3; RotaryState = LEFT2;
} }
break; break;
case S5: case RIGHT1:
if(enc == 3) if(enc == 3)
{ {
RotaryState = S6; RotaryState = RIGHT2;
} }
if(enc == 0) if(enc == 0)
{ {
@ -116,19 +105,19 @@ drehgeber_process(void)
} }
break; break;
case S6: case RIGHT2:
if(enc == 1) if(enc == 1)
{ {
RotaryState = S7; RotaryState = RIGHT3;
} }
if(enc == 2) if(enc == 2)
{ {
RotaryState = S5; RotaryState = RIGHT1;
} }
break; break;
case S7: case RIGHT3:
if(enc == 0) if(enc == 0)
{ {
count--; count--;
@ -136,7 +125,7 @@ drehgeber_process(void)
} }
if(enc == 3) if(enc == 3)
{ {
RotaryState = S6; RotaryState = RIGHT2;
} }
break; break;
} }