Compare commits
2 Commits
400c751961
...
2a3de0d746
| Author | SHA1 | Date |
|---|---|---|
|
|
2a3de0d746 | 3 years ago |
|
|
4a42b81529 | 3 years ago |
@ -1,52 +0,0 @@
|
||||
#include <avr/io.h>
|
||||
#include "uart.h"
|
||||
|
||||
static int uart_putchar(char c, FILE *stream);
|
||||
|
||||
FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
void uart_init()
|
||||
{
|
||||
DDRD |= 1 << 1; // TX
|
||||
UART_BAUD_REGH = (BAUDRATE>>8);
|
||||
UART_BAUD_REGL = BAUDRATE; // set baud rate
|
||||
|
||||
UART_CTRL_REGB |= (1<<UART_TXEN_BM)
|
||||
//|(1<<UART_RXEN_BM)
|
||||
|(1<<UART_RXCIE_BM); // enable receiver and transmitter
|
||||
|
||||
UART_CTRL_REGC |= (1<<UART_URSEL_BM)
|
||||
|(1<<UART_UCSZ0_BM)
|
||||
|(1<<UART_UCSZ1_BM); // 8bit data format
|
||||
|
||||
stdout = &uart_output;
|
||||
}
|
||||
|
||||
static int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
loop_until_bit_is_set(UART_CTRL_REGA, UART_UDRE_BM);
|
||||
UART_DATA_REG = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex){
|
||||
for(int i=0; i < buffersize; i++){
|
||||
buffer[i]=0;
|
||||
}
|
||||
*bufferindex=0;
|
||||
|
||||
char input;
|
||||
do{
|
||||
while ((UART_CTRL_REGA & (1 << UART_RXC_BM)) == 0);
|
||||
input = UART_DATA_REG;
|
||||
|
||||
putchar(input); //echo
|
||||
buffer[*bufferindex]=input;
|
||||
*bufferindex=*bufferindex+1;
|
||||
}while(!(input == '\n' || input == '\r'));
|
||||
buffer[*bufferindex]=0;
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define BAUD 9600
|
||||
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1)
|
||||
|
||||
#define UART_BAUD_REGH UBRR0H
|
||||
#define UART_BAUD_REGL UBRR0L
|
||||
|
||||
#define UART_CTRL_REGA UCSR0A
|
||||
#define UART_CTRL_REGB UCSR0B
|
||||
#define UART_CTRL_REGC UCSR0C
|
||||
|
||||
// UCSRA
|
||||
#define UART_UDRE_BM UDRE0
|
||||
#define UART_RXC_BM RXC0
|
||||
|
||||
// UCSRB
|
||||
#define UART_TXEN_BM TXEN0
|
||||
#define UART_RXEN_BM RXEN0
|
||||
#define UART_RXCIE_BM RXCIE0
|
||||
|
||||
// UCSRC
|
||||
#define UART_URSEL_BM 0 /* only for old atmega */
|
||||
#define UART_UCSZ0_BM UCSZ00
|
||||
#define UART_UCSZ1_BM UCSZ01
|
||||
|
||||
#define UART_DATA_REG UDR0
|
||||
|
||||
void uart_init (void);
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue