Added small changes to UART and ADC
This commit is contained in:
parent
a2203b7cd5
commit
dece559b52
@ -63,9 +63,10 @@ blinkLedWithTimer (void)
|
|||||||
if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */
|
if(Timer_getTick() - g_startMS >= 1000) { /* Wait 1000ms before switching LED1 */
|
||||||
g_startMS=Timer_getTick();
|
g_startMS=Timer_getTick();
|
||||||
|
|
||||||
|
/* Flip-Flop LED to on/off */
|
||||||
if(g_ledStatus % 2 == 0){
|
if(g_ledStatus % 2 == 0){
|
||||||
Led1_On();
|
Led1_On();
|
||||||
} else {
|
} else {
|
||||||
Led1_Off();
|
Led1_Off();
|
||||||
}
|
}
|
||||||
g_ledStatus++;
|
g_ledStatus++;
|
||||||
|
@ -20,16 +20,23 @@ Taster_init (void){
|
|||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
Taster1_get (void){
|
Taster1_get (void){
|
||||||
|
/* Check if pin is low
|
||||||
|
-> low active pin
|
||||||
|
-> button is pressed
|
||||||
|
-> return 1 */
|
||||||
return ((PIND & (1<<PIND7)) == 0);
|
return ((PIND & (1<<PIND7)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
Taster2_get (void){
|
Taster2_get (void){
|
||||||
return ((PIND & (1<<PIND6)) == 0);
|
return ((PIND & (1<<PIND6)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
Taster3_get (void){
|
Taster3_get (void){
|
||||||
return ((PIND & (1<<PIND5)) == 0);
|
return ((PIND & (1<<PIND5)) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t
|
uint8_t
|
||||||
Taster4_get (void){
|
Taster4_get (void){
|
||||||
return ((PINC & (1<<PINC2)) == 0);
|
return ((PINC & (1<<PINC2)) == 0);
|
||||||
|
@ -6,16 +6,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "UART.h"
|
#include "UART.h"
|
||||||
|
CircularBuffer TxBuffer;
|
||||||
|
CircularBuffer* pTxBuffer = &TxBuffer;
|
||||||
|
CircularBuffer RxBuffer;
|
||||||
|
CircularBuffer* pRxBuffer = &RxBuffer;
|
||||||
|
|
||||||
#define SIZE_BUFFER 500
|
|
||||||
struct CircularBuffer{
|
|
||||||
volatile char data[SIZE_BUFFER];
|
|
||||||
volatile uint16_t Readpointer;
|
|
||||||
volatile uint16_t Writepointer;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct CircularBuffer TxBuffer;
|
|
||||||
struct CircularBuffer RxBuffer;
|
|
||||||
volatile uint8_t TxActive;
|
volatile uint8_t TxActive;
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -34,10 +29,10 @@ uart_init(void)
|
|||||||
(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 */
|
||||||
|
|
||||||
TxBuffer.Readpointer = 0;
|
pTxBuffer->Readpointer = 0;
|
||||||
TxBuffer.Writepointer = 0;
|
pTxBuffer->Writepointer = 0;
|
||||||
RxBuffer.Readpointer = 0;
|
pRxBuffer->Readpointer = 0;
|
||||||
RxBuffer.Writepointer = 0;
|
pRxBuffer->Writepointer = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ------------------------ */
|
/* ------------------------ */
|
||||||
@ -57,28 +52,30 @@ uart_send_string(char* string)
|
|||||||
void
|
void
|
||||||
uart_send_byte(char c)
|
uart_send_byte(char c)
|
||||||
{
|
{
|
||||||
UCSR0B&=~(1<<TXCIE0);
|
/* Disable the TX Interrupt */
|
||||||
|
UCSR0B &= ~(1<<TXCIE0);
|
||||||
|
|
||||||
if(TxActive){
|
if(TxActive){
|
||||||
TxBuffer.data[TxBuffer.Writepointer++] = c;
|
pTxBuffer->data[pTxBuffer->Writepointer++] = c;
|
||||||
if (TxBuffer.Writepointer>=SIZE_BUFFER){
|
if (pTxBuffer->Writepointer>=SIZE_BUFFER){
|
||||||
TxBuffer.Writepointer = 0;
|
pTxBuffer->Writepointer = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TxActive = 1;
|
TxActive = 1;
|
||||||
UDR0 = c;
|
UDR0 = c;
|
||||||
}
|
}
|
||||||
|
/* Enable the TX Interrupt again*/
|
||||||
UCSR0B |= (1<<TXCIE0);
|
UCSR0B |= (1<<TXCIE0);
|
||||||
}
|
}
|
||||||
|
|
||||||
ISR(USART0_TX_vect)
|
ISR(USART0_TX_vect)
|
||||||
{
|
{
|
||||||
if(TxBuffer.Readpointer != TxBuffer.Writepointer)
|
if(pTxBuffer->Readpointer != pTxBuffer->Writepointer)
|
||||||
{
|
{
|
||||||
UDR0 = TxBuffer.data[TxBuffer.Readpointer++];
|
UDR0 = pTxBuffer->data[pTxBuffer->Readpointer++];
|
||||||
|
|
||||||
if(TxBuffer.Readpointer >= SIZE_BUFFER){
|
if(pTxBuffer->Readpointer >= SIZE_BUFFER){
|
||||||
TxBuffer.Readpointer = 0;
|
pTxBuffer->Readpointer = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
TxActive = 0;
|
TxActive = 0;
|
||||||
@ -94,7 +91,7 @@ uart_data_available(void)
|
|||||||
{
|
{
|
||||||
uint8_t dataAvailabel = 0;
|
uint8_t dataAvailabel = 0;
|
||||||
UCSR0B &= ~(1<<RXCIE0);
|
UCSR0B &= ~(1<<RXCIE0);
|
||||||
if(RxBuffer.Readpointer != RxBuffer.Writepointer){
|
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer){
|
||||||
dataAvailabel = 1;
|
dataAvailabel = 1;
|
||||||
}
|
}
|
||||||
UCSR0B |= (1<<RXCIE0);
|
UCSR0B |= (1<<RXCIE0);
|
||||||
@ -106,11 +103,11 @@ uart_get_data(void)
|
|||||||
{
|
{
|
||||||
char data = 0;
|
char data = 0;
|
||||||
UCSR0B &= ~(1<<RXCIE0);
|
UCSR0B &= ~(1<<RXCIE0);
|
||||||
if(RxBuffer.Readpointer != RxBuffer.Writepointer)
|
if(pRxBuffer->Readpointer != pRxBuffer->Writepointer)
|
||||||
{
|
{
|
||||||
data = RxBuffer.data[RxBuffer.Readpointer++];
|
data = pRxBuffer->data[pRxBuffer->Readpointer++];
|
||||||
if(RxBuffer.Readpointer >= SIZE_BUFFER){
|
if(pRxBuffer->Readpointer >= SIZE_BUFFER){
|
||||||
RxBuffer.Readpointer = 0;
|
pRxBuffer->Readpointer = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UCSR0B |= (1<<RXCIE0);
|
UCSR0B |= (1<<RXCIE0);
|
||||||
@ -123,9 +120,9 @@ ISR(USART0_RX_vect)
|
|||||||
uint8_t data = UDR0;
|
uint8_t data = UDR0;
|
||||||
|
|
||||||
if((status & ((1<<DOR0) | (1>>FE0))) == 0){
|
if((status & ((1<<DOR0) | (1>>FE0))) == 0){
|
||||||
RxBuffer.data[RxBuffer.Writepointer++] = data;
|
pRxBuffer->data[pRxBuffer->Writepointer++] = data;
|
||||||
if(RxBuffer.Writepointer >= SIZE_BUFFER) {
|
if(pRxBuffer->Writepointer >= SIZE_BUFFER) {
|
||||||
RxBuffer.Writepointer = 0;
|
pRxBuffer->Writepointer = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -11,6 +11,15 @@
|
|||||||
#ifndef UART2_H_
|
#ifndef UART2_H_
|
||||||
#define UART2_H_
|
#define UART2_H_
|
||||||
|
|
||||||
|
#define SIZE_BUFFER 500
|
||||||
|
|
||||||
|
typedef struct CircularBuffer CircularBuffer;
|
||||||
|
struct CircularBuffer{
|
||||||
|
volatile char data[SIZE_BUFFER];
|
||||||
|
volatile uint16_t Readpointer;
|
||||||
|
volatile uint16_t Writepointer;
|
||||||
|
};
|
||||||
|
|
||||||
void uart_init(void);
|
void uart_init(void);
|
||||||
void uart_send_byte(char c);
|
void uart_send_byte(char c);
|
||||||
void uart_send_string(char* string);
|
void uart_send_string(char* string);
|
||||||
|
@ -7,36 +7,6 @@
|
|||||||
|
|
||||||
#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))
|
#define myADC ((ADC_t*)(0x78))
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -46,9 +16,7 @@ adc_init(void)
|
|||||||
myADC->uiREFS0 = 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->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
|
myADC->uiREFS1 = 0; /* Set Voltage reference to 2.56V */
|
||||||
|
|
||||||
myADC->uiADPS0 = 1; /* Set ADC Prescaler to 128 */
|
myADC->uiADPS = 7; /* Set ADC Prescaler to 128 */
|
||||||
myADC->uiADPS1 = 1;
|
|
||||||
myADC->uiADPS2 = 1;
|
|
||||||
myADC->uiADIE = 1; /* Enable the ADC interrupt */
|
myADC->uiADIE = 1; /* Enable the ADC interrupt */
|
||||||
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
|
myADC->uiADATE = 0; /* Disable the ADC auto trigger */
|
||||||
myADC->uiADEN = 1; /* Enable ADC */
|
myADC->uiADEN = 1; /* Enable ADC */
|
||||||
|
@ -11,6 +11,34 @@
|
|||||||
#ifndef ADC_H_
|
#ifndef ADC_H_
|
||||||
#define ADC_H_
|
#define ADC_H_
|
||||||
|
|
||||||
|
typedef struct ADC_t ADC_t;
|
||||||
|
struct ADC_t{
|
||||||
|
/* ADC Data */
|
||||||
|
uint16_t uiADC :16;
|
||||||
|
|
||||||
|
/* ADCSRA */
|
||||||
|
uint8_t uiADPS :3;
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
void adc_init(void);
|
void adc_init(void);
|
||||||
|
|
||||||
uint16_t adc_get_poti(void);
|
uint16_t adc_get_poti(void);
|
||||||
|
Loading…
Reference in New Issue
Block a user