Minor improvement to code, added more commments espeically to UART

This commit is contained in:
_N0x 2022-01-26 21:17:40 +01:00
parent dece559b52
commit 6310b91164
7 changed files with 138 additions and 114 deletions

View File

@ -26,7 +26,7 @@
#define CHAR_ESC 0x1B
#define NEXT_LINE (char*)"\r\n"
#define CURSOR (char*)">>"
#define CURSOR (char*)">> "
//================================================================================================================================
// Lokale Variablen
@ -65,8 +65,8 @@ const struct {
} command[] PROGMEM = {
{"HELP", &cmd_help, "Display this Help Menu"},
{"BUTTON", &cmd_taster, "Current State of the Buttons"},
{"LED_ON $LED", &cmd_led_on, "Turn on the LED on position $LED"},
{"LED_OFF $LED", &cmd_led_off, "Turn off the LED on position $LED"},
{"LED_ON", &cmd_led_on, "Turn on the LED on position $LED"},
{"LED_OFF", &cmd_led_off, "Turn off the LED on position $LED"},
{"ADC", &cmd_adc, "Read the ADC output"},
{"ROTARY", &cmd_rotary, "Read the rotary encoder"},
{"ALERT", &cmd_alert, "RING THE ALTER!!!"},
@ -178,7 +178,7 @@ static char* get_next(char *_data){
// Rückgabewert: Das eingegebene Zeichen als Großbuchstabe.
//================================================================================================================================
static char to_uppercase (char _char){
if ( (_char>='a') && (_char<='x') ){
if ( (_char>='a') && (_char<='z') ){
_char -= 'a' - 'A';
}
return _char;
@ -192,9 +192,6 @@ static char to_uppercase (char _char){
// Rückgabewert: keine
//================================================================================================================================
static void print_char (char _data){
if (callback_function != NULL){
callback_function(_data);
}
@ -349,6 +346,7 @@ static void cmd_adc(const char* _data){
char potiData[50];
char lm35Data[50];
int loopInterval = 500;
int lm35;
do
{
@ -362,7 +360,8 @@ static void cmd_adc(const char* _data){
print_string(NEXT_LINE);
// 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.
print_string(lm35Data);
print_string(NEXT_LINE);

View File

@ -19,7 +19,13 @@ Timer_init (void){
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
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)
*/
}

View File

@ -6,6 +6,8 @@
*/
#include "UART.h"
/* create structs for TX and TX buffer */
CircularBuffer TxBuffer;
CircularBuffer* pTxBuffer = &TxBuffer;
CircularBuffer RxBuffer;
@ -14,8 +16,7 @@ CircularBuffer* pRxBuffer = &RxBuffer;
volatile uint8_t TxActive;
void
uart_init(void)
{
uart_init(void) {
UBRR0 = 103; /* set BAUD rate to 9600 */
UCSR0C |=
@ -24,24 +25,23 @@ uart_init(void)
(1<<UCSZ00)|(1<<UCSZ01)| /* 8 Bit */
(0<<USBS0); /* 1 Stopbit */
UCSR0B |=
(1<<TXEN0)|(1<<RXEN0)| /* enable send and receive */
(1<<RXCIE0); /* enable Receive interrupts */
/* Initiate read- and write-pointers */
pTxBuffer->Readpointer = 0;
pTxBuffer->Writepointer = 0;
pRxBuffer->Readpointer = 0;
pRxBuffer->Writepointer = 0;
}
/* ------------------------ */
/* Sending Data */
/* ------------------------ */
/********Sending Data********/
void
uart_send_string(char* string)
{
uart_send_string(char* string) {
/* Take a whole string and transmit every char from it */
int i = 0;
while(string[i] != '\0'){
uart_send_byte(string[i]);
@ -50,11 +50,14 @@ uart_send_string(char* string)
}
void
uart_send_byte(char c)
{
/* Disable the TX Interrupt */
uart_send_byte(char c) {
/* Disable the TX Interrupt to prevent issues while writing to the send buffer*/
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){
pTxBuffer->data[pTxBuffer->Writepointer++] = c;
if (pTxBuffer->Writepointer>=SIZE_BUFFER){
@ -68,10 +71,14 @@ uart_send_byte(char c)
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)
{
/* 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++];
if(pTxBuffer->Readpointer >= SIZE_BUFFER){
@ -82,44 +89,54 @@ ISR(USART0_TX_vect)
}
}
/* ------------------------ */
/* Receiving Data */
/* ------------------------ */
/*******Receiving Data*******/
uint8_t
uart_data_available(void)
{
uart_data_available(void) {
uint8_t dataAvailabel = 0;
/* Disable the RX Interrupt to prevent issues when checking if data is available */
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){
dataAvailabel = 1;
}
/* Enable the RX Interrupt again*/
UCSR0B |= (1<<RXCIE0);
return dataAvailabel;
}
char
uart_get_data(void)
{
uart_get_data(void) {
char data = 0;
/* Disable the RX Interrupt to prevent issues when reading out a char from the buffer */
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)
{
/* read a char from the buffer and increment the readpointer */
data = pRxBuffer->data[pRxBuffer->Readpointer++];
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
pRxBuffer->Readpointer = 0;
}
}
/* Enable the RX Interrupt again*/
UCSR0B |= (1<<RXCIE0);
/* give back the read in character */
return data;
}
ISR(USART0_RX_vect)
{
/* when data (a data frame) is received */
ISR(USART0_RX_vect) {
uint8_t status = UCSR0A;
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;
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
pRxBuffer->Writepointer = 0;

View File

@ -12,9 +12,8 @@
void
adc_init(void)
{
myADC->uiADLAR = 0;
myADC->uiREFS0 = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
myADC->uiADLAR = 0; /* Set ADLAR to 0 to not left adjust the presentation of the conversion result */
myADC->uiREFS = 0; /* Turn off reference Voltage */
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
myADC->uiADIE = 1; /* Enable the ADC interrupt */
@ -30,8 +29,6 @@ adc_get_poti(void)
{
uint16_t adc;
//myADC->uiADIE = 0; /* Disable interrupt */
myADC->uiMUX = 1; /* Set ADMUX to access ADC channel 1 */
myADC->uiADSC = 1; /* Start the conversion */
@ -41,8 +38,6 @@ adc_get_poti(void)
adc = myADC->uiADC;
//myADC->uiADIE = 1; /* Enable the interrupt again */
return adc;
}

View File

@ -35,8 +35,7 @@ struct ADC_t{
/* ADMUX */
uint8_t uiMUX :5;
uint8_t uiADLAR :1;
uint8_t uiREFS0 :1;
uint8_t uiREFS1 :1;
uint8_t uiREFS :2;
};
void adc_init(void);

View File

@ -34,6 +34,9 @@ main (void)
Timer_init();
Taster_init();
Led_init();
initTasks();
uart_init();
adc_init();
@ -44,6 +47,8 @@ main (void)
while (1)
{
//blinkLedWithTimer();
if(uart_data_available()){
line_interpreter_get_data(uart_get_data());
}

View File

@ -7,8 +7,8 @@
#include "rotaryEncoder.h"
#define ROTA PORTC7
#define ROTB PORTC6
#define ROTA PORTC7 /* define Rotary Encoder Button A */
#define ROTB PORTC6 /* define Rotary Encoder Button B */
#define ROTBUTTON PORTC5
/* enums */
@ -38,96 +38,99 @@ ISR(PCINT2_vect)
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);
b = ((PINC & (1<<ROTB)) == 0)<<1;
enc=a+b;
enc = a+b;
switch(RotaryState)
{
case INIT:
RotaryState = WAIT;
break;
RotaryState = WAIT;
break;
case WAIT:
if(enc == 1)
{
RotaryState = LEFT1;
}
if(enc == 2)
{
RotaryState = RIGHT1;
}
break;
if(enc == 1)
{
RotaryState = LEFT1;
}
if(enc == 2)
{
RotaryState = RIGHT1;
}
break;
case LEFT1:
if(enc == 3)
{
RotaryState = LEFT2;
}
if(enc == 0)
{
RotaryState = WAIT;
}
break;
if(enc == 3)
{
RotaryState = LEFT2;
}
if(enc == 0)
{
RotaryState = WAIT;
}
break;
case LEFT2:
if(enc == 2)
{
RotaryState = LEFT3;
}
if(enc == 1)
{
RotaryState = LEFT1;
}
break;
if(enc == 2)
{
RotaryState = LEFT3;
}
if(enc == 1)
{
RotaryState = LEFT1;
}
break;
case LEFT3:
if(enc == 0)
{
count++;
RotaryState = WAIT;
}
if(enc == 3)
{
RotaryState = LEFT2;
}
break;
if(enc == 0)
{
/* Increment counter and wait */
count++;
RotaryState = WAIT;
}
if(enc == 3)
{
RotaryState = LEFT2;
}
break;
case RIGHT1:
if(enc == 3)
{
RotaryState = RIGHT2;
}
if(enc == 0)
{
RotaryState = WAIT;
}
break;
if(enc == 3)
{
RotaryState = RIGHT2;
}
if(enc == 0)
{
RotaryState = WAIT;
}
break;
case RIGHT2:
if(enc == 1)
{
RotaryState = RIGHT3;
}
if(enc == 2)
{
RotaryState = RIGHT1;
}
break;
if(enc == 1)
{
RotaryState = RIGHT3;
}
if(enc == 2)
{
RotaryState = RIGHT1;
}
break;
case RIGHT3:
if(enc == 0)
{
count--;
RotaryState = WAIT;
}
if(enc == 3)
{
RotaryState = RIGHT2;
}
break;
if(enc == 0)
{
/* Decrement counter and wait */
count--;
RotaryState = WAIT;
}
if(enc == 3)
{
RotaryState = RIGHT2;
}
break;
}
}