Minor improvement to code, added more commments espeically to UART
This commit is contained in:
parent
dece559b52
commit
6310b91164
@ -26,7 +26,7 @@
|
|||||||
#define CHAR_ESC 0x1B
|
#define CHAR_ESC 0x1B
|
||||||
|
|
||||||
#define NEXT_LINE (char*)"\r\n"
|
#define NEXT_LINE (char*)"\r\n"
|
||||||
#define CURSOR (char*)">>"
|
#define CURSOR (char*)">> "
|
||||||
|
|
||||||
//================================================================================================================================
|
//================================================================================================================================
|
||||||
// Lokale Variablen
|
// Lokale Variablen
|
||||||
@ -65,8 +65,8 @@ const struct {
|
|||||||
} command[] PROGMEM = {
|
} command[] PROGMEM = {
|
||||||
{"HELP", &cmd_help, "Display this Help Menu"},
|
{"HELP", &cmd_help, "Display this Help Menu"},
|
||||||
{"BUTTON", &cmd_taster, "Current State of the Buttons"},
|
{"BUTTON", &cmd_taster, "Current State of the Buttons"},
|
||||||
{"LED_ON $LED", &cmd_led_on, "Turn on the LED on position $LED"},
|
{"LED_ON", &cmd_led_on, "Turn on the LED on position $LED"},
|
||||||
{"LED_OFF $LED", &cmd_led_off, "Turn off the LED on position $LED"},
|
{"LED_OFF", &cmd_led_off, "Turn off the LED on position $LED"},
|
||||||
{"ADC", &cmd_adc, "Read the ADC output"},
|
{"ADC", &cmd_adc, "Read the ADC output"},
|
||||||
{"ROTARY", &cmd_rotary, "Read the rotary encoder"},
|
{"ROTARY", &cmd_rotary, "Read the rotary encoder"},
|
||||||
{"ALERT", &cmd_alert, "RING THE ALTER!!!"},
|
{"ALERT", &cmd_alert, "RING THE ALTER!!!"},
|
||||||
@ -178,7 +178,7 @@ static char* get_next(char *_data){
|
|||||||
// Rückgabewert: Das eingegebene Zeichen als Großbuchstabe.
|
// Rückgabewert: Das eingegebene Zeichen als Großbuchstabe.
|
||||||
//================================================================================================================================
|
//================================================================================================================================
|
||||||
static char to_uppercase (char _char){
|
static char to_uppercase (char _char){
|
||||||
if ( (_char>='a') && (_char<='x') ){
|
if ( (_char>='a') && (_char<='z') ){
|
||||||
_char -= 'a' - 'A';
|
_char -= 'a' - 'A';
|
||||||
}
|
}
|
||||||
return _char;
|
return _char;
|
||||||
@ -192,9 +192,6 @@ static char to_uppercase (char _char){
|
|||||||
// Rückgabewert: keine
|
// Rückgabewert: keine
|
||||||
//================================================================================================================================
|
//================================================================================================================================
|
||||||
static void print_char (char _data){
|
static void print_char (char _data){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (callback_function != NULL){
|
if (callback_function != NULL){
|
||||||
callback_function(_data);
|
callback_function(_data);
|
||||||
}
|
}
|
||||||
@ -349,6 +346,7 @@ static void cmd_adc(const char* _data){
|
|||||||
char potiData[50];
|
char potiData[50];
|
||||||
char lm35Data[50];
|
char lm35Data[50];
|
||||||
int loopInterval = 500;
|
int loopInterval = 500;
|
||||||
|
int lm35;
|
||||||
|
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@ -361,8 +359,9 @@ static void cmd_adc(const char* _data){
|
|||||||
print_string(potiData);
|
print_string(potiData);
|
||||||
print_string(NEXT_LINE);
|
print_string(NEXT_LINE);
|
||||||
|
|
||||||
// Print LM35 temperature
|
// Print LM35 temperature
|
||||||
sprintf(lm35Data, "LM35: %d\xC2\xB0 C", (int)(adc_get_LM35() * (5000 / 1024) / 10));
|
lm35 = (int)(adc_get_LM35() * 5000.0 / 1024 / 10);
|
||||||
|
sprintf(lm35Data, "LM35: %d\xC2\xB0 C", lm35);
|
||||||
//sprintf(lm35Data, "LM35: %f\xC2\xB0 C", (adc_get_LM35() * (5000 / 1024.0) / 10)); // uses floats - disabled for performance reasons.
|
//sprintf(lm35Data, "LM35: %f\xC2\xB0 C", (adc_get_LM35() * (5000 / 1024.0) / 10)); // uses floats - disabled for performance reasons.
|
||||||
print_string(lm35Data);
|
print_string(lm35Data);
|
||||||
print_string(NEXT_LINE);
|
print_string(NEXT_LINE);
|
||||||
|
@ -19,7 +19,13 @@ Timer_init (void){
|
|||||||
TIMSK0 |= (1<<OCIE0A); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
|
TIMSK0 |= (1<<OCIE0A); /* Set OCIE0A to high to rise an interrupt when the counter matches OCR0A */
|
||||||
|
|
||||||
OCR0A = 250 - 1; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
|
OCR0A = 250 - 1; /* Set the Output Compare Register 0 A to 125 to trigger interrupt every 1ms
|
||||||
16MHz / 64 (pre-scaler 3) / (250 -1 ) ==> 1KHz (1ms) */
|
16MHz / 64 (pre-scaler 3) / (250 ) ==> 1KHz (1ms) */
|
||||||
|
|
||||||
|
/*
|
||||||
|
For internal 1MHz clock:
|
||||||
|
1MHz / 8 (pre-scaler 2) ==> 125 KHz
|
||||||
|
1MHz / 8 (pre-scaler 2) / 125 (OCR) ==> 1 KHz (1ms)
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "UART.h"
|
#include "UART.h"
|
||||||
|
|
||||||
|
/* create structs for TX and TX buffer */
|
||||||
CircularBuffer TxBuffer;
|
CircularBuffer TxBuffer;
|
||||||
CircularBuffer* pTxBuffer = &TxBuffer;
|
CircularBuffer* pTxBuffer = &TxBuffer;
|
||||||
CircularBuffer RxBuffer;
|
CircularBuffer RxBuffer;
|
||||||
@ -14,8 +16,7 @@ CircularBuffer* pRxBuffer = &RxBuffer;
|
|||||||
volatile uint8_t TxActive;
|
volatile uint8_t TxActive;
|
||||||
|
|
||||||
void
|
void
|
||||||
uart_init(void)
|
uart_init(void) {
|
||||||
{
|
|
||||||
UBRR0 = 103; /* set BAUD rate to 9600 */
|
UBRR0 = 103; /* set BAUD rate to 9600 */
|
||||||
|
|
||||||
UCSR0C |=
|
UCSR0C |=
|
||||||
@ -24,24 +25,23 @@ uart_init(void)
|
|||||||
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
|
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
|
||||||
(0<<USBS0); /* 1 Stopbit */
|
(0<<USBS0); /* 1 Stopbit */
|
||||||
|
|
||||||
|
|
||||||
UCSR0B |=
|
UCSR0B |=
|
||||||
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
|
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
|
||||||
(1<<RXCIE0); /* enable Receive interrupts */
|
(1<<RXCIE0); /* enable Receive interrupts */
|
||||||
|
|
||||||
|
|
||||||
|
/* Initiate read- and write-pointers */
|
||||||
pTxBuffer->Readpointer = 0;
|
pTxBuffer->Readpointer = 0;
|
||||||
pTxBuffer->Writepointer = 0;
|
pTxBuffer->Writepointer = 0;
|
||||||
pRxBuffer->Readpointer = 0;
|
pRxBuffer->Readpointer = 0;
|
||||||
pRxBuffer->Writepointer = 0;
|
pRxBuffer->Writepointer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------ */
|
/********Sending Data********/
|
||||||
/* Sending Data */
|
|
||||||
/* ------------------------ */
|
|
||||||
|
|
||||||
void
|
void
|
||||||
uart_send_string(char* string)
|
uart_send_string(char* string) {
|
||||||
{
|
/* Take a whole string and transmit every char from it */
|
||||||
int i = 0;
|
int i = 0;
|
||||||
while(string[i] != '\0'){
|
while(string[i] != '\0'){
|
||||||
uart_send_byte(string[i]);
|
uart_send_byte(string[i]);
|
||||||
@ -50,11 +50,14 @@ uart_send_string(char* string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
uart_send_byte(char c)
|
uart_send_byte(char c) {
|
||||||
{
|
/* Disable the TX Interrupt to prevent issues while writing to the send buffer*/
|
||||||
/* Disable the TX Interrupt */
|
|
||||||
UCSR0B &= ~(1<<TXCIE0);
|
UCSR0B &= ~(1<<TXCIE0);
|
||||||
|
|
||||||
|
/* Check if a transmit is in progress
|
||||||
|
if yes -> write the byte to the buffer
|
||||||
|
if no -> write the byte to the UDR0 register and enable the transmit in progress flag
|
||||||
|
*/
|
||||||
if(TxActive){
|
if(TxActive){
|
||||||
pTxBuffer->data[pTxBuffer->Writepointer++] = c;
|
pTxBuffer->data[pTxBuffer->Writepointer++] = c;
|
||||||
if (pTxBuffer->Writepointer>=SIZE_BUFFER){
|
if (pTxBuffer->Writepointer>=SIZE_BUFFER){
|
||||||
@ -68,10 +71,14 @@ uart_send_byte(char c)
|
|||||||
UCSR0B |= (1<<TXCIE0);
|
UCSR0B |= (1<<TXCIE0);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART0_TX_vect)
|
/* when data (a data frame) is to be send */
|
||||||
{
|
ISR(USART0_TX_vect) {
|
||||||
|
/* check if the read and write pointer of the send buffer are not aligned
|
||||||
|
if not aligned -> read the next byte and write it to UDR0 register
|
||||||
|
if aligned -> no more data to send, disable the send in progress flag */
|
||||||
if(pTxBuffer->Readpointer != pTxBuffer->Writepointer)
|
if(pTxBuffer->Readpointer != pTxBuffer->Writepointer)
|
||||||
{
|
{
|
||||||
|
/* writing data to the register withing the interrupt will cause a new interrupt which then will repeat till no more data is to be read from the send buffer */
|
||||||
UDR0 = pTxBuffer->data[pTxBuffer->Readpointer++];
|
UDR0 = pTxBuffer->data[pTxBuffer->Readpointer++];
|
||||||
|
|
||||||
if(pTxBuffer->Readpointer >= SIZE_BUFFER){
|
if(pTxBuffer->Readpointer >= SIZE_BUFFER){
|
||||||
@ -82,44 +89,54 @@ ISR(USART0_TX_vect)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------ */
|
/*******Receiving Data*******/
|
||||||
/* Receiving Data */
|
|
||||||
/* ------------------------ */
|
|
||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
uart_data_available(void)
|
uart_data_available(void) {
|
||||||
{
|
|
||||||
uint8_t dataAvailabel = 0;
|
uint8_t dataAvailabel = 0;
|
||||||
|
/* Disable the RX Interrupt to prevent issues when checking if data is available */
|
||||||
UCSR0B &= ~(1<<RXCIE0);
|
UCSR0B &= ~(1<<RXCIE0);
|
||||||
|
|
||||||
|
/* check if read and write pointer are at different locations
|
||||||
|
if yes -> data is available and can be read */
|
||||||
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){
|
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){
|
||||||
dataAvailabel = 1;
|
dataAvailabel = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Enable the RX Interrupt again*/
|
||||||
UCSR0B |= (1<<RXCIE0);
|
UCSR0B |= (1<<RXCIE0);
|
||||||
return dataAvailabel;
|
return dataAvailabel;
|
||||||
}
|
}
|
||||||
|
|
||||||
char
|
char
|
||||||
uart_get_data(void)
|
uart_get_data(void) {
|
||||||
{
|
|
||||||
char data = 0;
|
char data = 0;
|
||||||
|
/* Disable the RX Interrupt to prevent issues when reading out a char from the buffer */
|
||||||
UCSR0B &= ~(1<<RXCIE0);
|
UCSR0B &= ~(1<<RXCIE0);
|
||||||
|
|
||||||
|
/* check if read and write pointer are at different locations
|
||||||
|
if yes -> data is available and can be read */
|
||||||
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer)
|
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer)
|
||||||
{
|
{
|
||||||
|
/* read a char from the buffer and increment the readpointer */
|
||||||
data = pRxBuffer->data[pRxBuffer->Readpointer++];
|
data = pRxBuffer->data[pRxBuffer->Readpointer++];
|
||||||
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
|
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
|
||||||
pRxBuffer->Readpointer = 0;
|
pRxBuffer->Readpointer = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/* Enable the RX Interrupt again*/
|
||||||
UCSR0B |= (1<<RXCIE0);
|
UCSR0B |= (1<<RXCIE0);
|
||||||
|
/* give back the read in character */
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART0_RX_vect)
|
/* when data (a data frame) is received */
|
||||||
{
|
ISR(USART0_RX_vect) {
|
||||||
uint8_t status = UCSR0A;
|
uint8_t status = UCSR0A;
|
||||||
uint8_t data = UDR0;
|
uint8_t data = UDR0;
|
||||||
|
|
||||||
if((status & ((1<<DOR0) | (1>>FE0))) == 0){
|
/* check status flags if data is correctly accessible */
|
||||||
|
if((status & ((1<<DOR0) | (1<<FE0))) == 0){
|
||||||
|
/* Save data to buffer and increment the write pointer */
|
||||||
pRxBuffer->data[pRxBuffer->Writepointer++] = data;
|
pRxBuffer->data[pRxBuffer->Writepointer++] = data;
|
||||||
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
|
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
|
||||||
pRxBuffer->Writepointer = 0;
|
pRxBuffer->Writepointer = 0;
|
||||||
|
@ -12,9 +12,8 @@
|
|||||||
void
|
void
|
||||||
adc_init(void)
|
adc_init(void)
|
||||||
{
|
{
|
||||||
myADC->uiADLAR = 0;
|
myADC->uiADLAR = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
|
||||||
myADC->uiREFS0 = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
|
myADC->uiREFS = 0; /* Turn off reference Voltage */
|
||||||
myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
|
|
||||||
|
|
||||||
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
|
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
|
||||||
myADC->uiADIE = 1; /* Enable the ADC interrupt */
|
myADC->uiADIE = 1; /* Enable the ADC interrupt */
|
||||||
@ -30,8 +29,6 @@ adc_get_poti(void)
|
|||||||
{
|
{
|
||||||
uint16_t adc;
|
uint16_t adc;
|
||||||
|
|
||||||
//myADC->uiADIE = 0; /* Disable interrupt */
|
|
||||||
|
|
||||||
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
|
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
|
||||||
|
|
||||||
myADC->uiADSC = 1; /* Start the conversion */
|
myADC->uiADSC = 1; /* Start the conversion */
|
||||||
@ -41,8 +38,6 @@ adc_get_poti(void)
|
|||||||
|
|
||||||
adc = myADC->uiADC;
|
adc = myADC->uiADC;
|
||||||
|
|
||||||
//myADC->uiADIE = 1; /* Enable the interrupt again */
|
|
||||||
|
|
||||||
return adc;
|
return adc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,8 +35,7 @@ struct ADC_t{
|
|||||||
/* ADMUX */
|
/* ADMUX */
|
||||||
uint8_t uiMUX :5;
|
uint8_t uiMUX :5;
|
||||||
uint8_t uiADLAR :1;
|
uint8_t uiADLAR :1;
|
||||||
uint8_t uiREFS0 :1;
|
uint8_t uiREFS :2;
|
||||||
uint8_t uiREFS1 :1;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void adc_init(void);
|
void adc_init(void);
|
||||||
|
@ -34,6 +34,9 @@ main (void)
|
|||||||
Timer_init();
|
Timer_init();
|
||||||
Taster_init();
|
Taster_init();
|
||||||
Led_init();
|
Led_init();
|
||||||
|
initTasks();
|
||||||
|
|
||||||
|
|
||||||
uart_init();
|
uart_init();
|
||||||
adc_init();
|
adc_init();
|
||||||
|
|
||||||
@ -44,6 +47,8 @@ main (void)
|
|||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
|
//blinkLedWithTimer();
|
||||||
|
|
||||||
if(uart_data_available()){
|
if(uart_data_available()){
|
||||||
line_interpreter_get_data(uart_get_data());
|
line_interpreter_get_data(uart_get_data());
|
||||||
}
|
}
|
||||||
|
@ -7,8 +7,8 @@
|
|||||||
|
|
||||||
#include "rotaryEncoder.h"
|
#include "rotaryEncoder.h"
|
||||||
|
|
||||||
#define ROTA PORTC7
|
#define ROTA PORTC7 /* define Rotary Encoder Button A */
|
||||||
#define ROTB PORTC6
|
#define ROTB PORTC6 /* define Rotary Encoder Button B */
|
||||||
#define ROTBUTTON PORTC5
|
#define ROTBUTTON PORTC5
|
||||||
|
|
||||||
/* enums */
|
/* enums */
|
||||||
@ -38,96 +38,99 @@ ISR(PCINT2_vect)
|
|||||||
void
|
void
|
||||||
drehgeber_process(void)
|
drehgeber_process(void)
|
||||||
{
|
{
|
||||||
uint8_t a,b, enc;
|
/* calculate the value of Button A and B of rotary encoder */
|
||||||
|
uint8_t a, b, enc;
|
||||||
a = ((PINC & (1<<ROTA)) == 0);
|
a = ((PINC & (1<<ROTA)) == 0);
|
||||||
b = ((PINC & (1<<ROTB)) == 0)<<1;
|
b = ((PINC & (1<<ROTB)) == 0)<<1;
|
||||||
enc=a+b;
|
enc = a+b;
|
||||||
|
|
||||||
switch(RotaryState)
|
switch(RotaryState)
|
||||||
{
|
{
|
||||||
case INIT:
|
case INIT:
|
||||||
RotaryState = WAIT;
|
RotaryState = WAIT;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WAIT:
|
case WAIT:
|
||||||
if(enc == 1)
|
if(enc == 1)
|
||||||
{
|
{
|
||||||
RotaryState = LEFT1;
|
RotaryState = LEFT1;
|
||||||
}
|
}
|
||||||
if(enc == 2)
|
if(enc == 2)
|
||||||
{
|
{
|
||||||
RotaryState = RIGHT1;
|
RotaryState = RIGHT1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LEFT1:
|
case LEFT1:
|
||||||
if(enc == 3)
|
if(enc == 3)
|
||||||
{
|
{
|
||||||
RotaryState = LEFT2;
|
RotaryState = LEFT2;
|
||||||
}
|
}
|
||||||
if(enc == 0)
|
if(enc == 0)
|
||||||
{
|
{
|
||||||
RotaryState = WAIT;
|
RotaryState = WAIT;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LEFT2:
|
case LEFT2:
|
||||||
if(enc == 2)
|
if(enc == 2)
|
||||||
{
|
{
|
||||||
RotaryState = LEFT3;
|
RotaryState = LEFT3;
|
||||||
}
|
}
|
||||||
if(enc == 1)
|
if(enc == 1)
|
||||||
{
|
{
|
||||||
RotaryState = LEFT1;
|
RotaryState = LEFT1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case LEFT3:
|
case LEFT3:
|
||||||
if(enc == 0)
|
if(enc == 0)
|
||||||
{
|
{
|
||||||
count++;
|
/* Increment counter and wait */
|
||||||
RotaryState = WAIT;
|
count++;
|
||||||
}
|
RotaryState = WAIT;
|
||||||
if(enc == 3)
|
}
|
||||||
{
|
if(enc == 3)
|
||||||
RotaryState = LEFT2;
|
{
|
||||||
}
|
RotaryState = LEFT2;
|
||||||
break;
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
case RIGHT1:
|
case RIGHT1:
|
||||||
if(enc == 3)
|
if(enc == 3)
|
||||||
{
|
{
|
||||||
RotaryState = RIGHT2;
|
RotaryState = RIGHT2;
|
||||||
}
|
}
|
||||||
if(enc == 0)
|
if(enc == 0)
|
||||||
{
|
{
|
||||||
RotaryState = WAIT;
|
RotaryState = WAIT;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case RIGHT2:
|
case RIGHT2:
|
||||||
if(enc == 1)
|
if(enc == 1)
|
||||||
{
|
{
|
||||||
RotaryState = RIGHT3;
|
RotaryState = RIGHT3;
|
||||||
}
|
}
|
||||||
if(enc == 2)
|
if(enc == 2)
|
||||||
{
|
{
|
||||||
RotaryState = RIGHT1;
|
RotaryState = RIGHT1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
||||||
case RIGHT3:
|
case RIGHT3:
|
||||||
if(enc == 0)
|
if(enc == 0)
|
||||||
{
|
{
|
||||||
count--;
|
/* Decrement counter and wait */
|
||||||
RotaryState = WAIT;
|
count--;
|
||||||
}
|
RotaryState = WAIT;
|
||||||
if(enc == 3)
|
}
|
||||||
{
|
if(enc == 3)
|
||||||
RotaryState = RIGHT2;
|
{
|
||||||
}
|
RotaryState = RIGHT2;
|
||||||
break;
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user