Added a serial0_flush_rxbuf() function
This commit is contained in:
parent
3554cb60f2
commit
6c9a11828b
63
avr_usart.c
63
avr_usart.c
|
@ -7,6 +7,7 @@
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
#include <avr/interrupt.h>
|
#include <avr/interrupt.h>
|
||||||
#include <avr/sleep.h>
|
#include <avr/sleep.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <util/delay.h>
|
#include <util/delay.h>
|
||||||
#include "avr_usart.h"
|
#include "avr_usart.h"
|
||||||
|
|
||||||
|
@ -30,14 +31,17 @@ int main() {
|
||||||
|
|
||||||
uint8_t buf_len = 12;
|
uint8_t buf_len = 12;
|
||||||
//unsigned char data[12] = "snd 12chars\0";
|
//unsigned char data[12] = "snd 12chars\0";
|
||||||
unsigned char data[1] = "A";
|
//unsigned char data[1] = "A";
|
||||||
|
unsigned char data = '0';
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
serial0_write(data, buf_len);
|
//serial0_write(data, buf_len);
|
||||||
_delay_ms(1000);
|
tx_usart0(data);
|
||||||
serial0_read(data, buf_len);
|
//serial0_read(data, buf_len);
|
||||||
led_blink();
|
//data = rx_usart0();
|
||||||
|
serial0_flush_rxbuf();
|
||||||
|
serial0_read(data, 1);
|
||||||
|
//led_blink();
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -66,8 +70,6 @@ void init_usart0(void) {
|
||||||
UBRR0H |= (uint8_t) (0x33>>8);
|
UBRR0H |= (uint8_t) (0x33>>8);
|
||||||
UBRR0L |= (uint8_t) 0x33;
|
UBRR0L |= (uint8_t) 0x33;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//Enable recv and Transmit pins, overrides other uses.
|
//Enable recv and Transmit pins, overrides other uses.
|
||||||
//IN the usart control and status register 0B
|
//IN the usart control and status register 0B
|
||||||
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
|
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
|
||||||
|
@ -103,12 +105,18 @@ void tx_usart0(unsigned char data) {
|
||||||
*/
|
*/
|
||||||
unsigned char rx_usart0(void) {
|
unsigned char rx_usart0(void) {
|
||||||
//first wait for the data to be received.
|
//first wait for the data to be received.
|
||||||
while(!(UCSR0A & (1<<RXC0))) {
|
while( !(UCSR0A & (1<<RXC0)) ) {
|
||||||
;//Equiv of continue
|
;//Equiv of continue
|
||||||
}
|
}
|
||||||
|
|
||||||
//now return the data
|
//now return the data
|
||||||
return (unsigned char) UDR0;
|
return UDR0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void serial0_flush_rxbuf(void) {
|
||||||
|
uint8_t none;
|
||||||
|
while( UCSR0A & (1<<RXC0)) none = UDR0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -139,13 +147,46 @@ void serial0_write(unsigned char* buffer, uint8_t write_length) {
|
||||||
*/
|
*/
|
||||||
void serial0_read(unsigned char* buffer, uint8_t buf_length) {
|
void serial0_read(unsigned char* buffer, uint8_t buf_length) {
|
||||||
//clear buffer out?
|
//clear buffer out?
|
||||||
buffer = 0;
|
//buffer = 0;
|
||||||
for(uint8_t i = 0; i < buf_length; i++) {
|
for(uint8_t i = 0; i < buf_length; i++) {
|
||||||
buffer[i] = rx_usart0();
|
buffer[i] = rx_usart0();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Inupt: A serialbuffer and length
|
||||||
|
* Output: None
|
||||||
|
* Description: Reads the serial data into a buffer of x length,
|
||||||
|
* it does error checking from the register first.
|
||||||
|
*/
|
||||||
|
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length) {
|
||||||
|
//first wait for the data to be received.
|
||||||
|
while( !(UCSR0A & (1<<RXC0)) ) {
|
||||||
|
;//Equiv of continue
|
||||||
|
}
|
||||||
|
|
||||||
|
//check for errors in the register.
|
||||||
|
//Check for frame error.
|
||||||
|
if(UCSR0A & (1<<FE0)) {
|
||||||
|
led_blink();
|
||||||
|
}
|
||||||
|
//check for data over-run
|
||||||
|
else if(UCSR0A & (1<<DOR0)) {
|
||||||
|
led_blink();
|
||||||
|
led_blink();
|
||||||
|
}
|
||||||
|
//check for parity error
|
||||||
|
else if(UCSR0A & (1<<UPE0)) {
|
||||||
|
led_blink();
|
||||||
|
led_blink();
|
||||||
|
led_blink();
|
||||||
|
}
|
||||||
|
|
||||||
|
//now return the data
|
||||||
|
buffer[0] = UDR0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Inupt: BUAD RATE as uint8_t
|
* Inupt: BUAD RATE as uint8_t
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
//#############################
|
//#############################
|
||||||
//PIN DEFINES AND CONSTS
|
//PIN DEFINES AND CONSTS
|
||||||
|
@ -48,9 +49,10 @@ unsigned char rx_usart0(void);
|
||||||
|
|
||||||
void serial0_write(unsigned char* buffer, uint8_t write_length);
|
void serial0_write(unsigned char* buffer, uint8_t write_length);
|
||||||
void serial0_read(unsigned char* buffer, uint8_t buf_length);
|
void serial0_read(unsigned char* buffer, uint8_t buf_length);
|
||||||
|
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length);
|
||||||
void serial0_set_baud(uint8_t baud);
|
void serial0_set_baud(uint8_t baud);
|
||||||
void seiral0_enable_timeouts(uint8_t ms);
|
void seiral0_enable_timeouts(uint8_t ms);
|
||||||
|
void serial0_flush_rxbuf(void);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue