rewrote usart init function with some macro stuff.
This commit is contained in:
parent
1791a94f9e
commit
830c1028c0
61
avr_usart.c
61
avr_usart.c
|
@ -7,7 +7,6 @@
|
||||||
#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"
|
||||||
|
|
||||||
|
@ -22,28 +21,30 @@ int main() {
|
||||||
led_blink();
|
led_blink();
|
||||||
led_blink();
|
led_blink();
|
||||||
_delay_ms(1000);
|
_delay_ms(1000);
|
||||||
_delay_ms(1000);
|
|
||||||
|
|
||||||
init_usart0();
|
init_usart0();
|
||||||
|
|
||||||
//enable interrupts
|
//enable interrupts
|
||||||
sei();
|
sei();
|
||||||
|
unsigned char linefeed = 0x0a;
|
||||||
uint8_t buf_len = 12;
|
unsigned char carrigereturn = 0x0d;
|
||||||
//unsigned char data[12] = "snd 12chars\0";
|
//unsigned char data[11] = "snd 11chars";
|
||||||
//unsigned char data[1] = "A";
|
unsigned char data[8] = "AT+BAUD3";
|
||||||
unsigned char data[1] = "B";
|
unsigned char data_cmd[2] = "AT";
|
||||||
|
uint8_t data_len = 8;
|
||||||
|
uint8_t data_cmd_len = 2;
|
||||||
|
|
||||||
while(1) {
|
while(1) {
|
||||||
//serial0_write(data, buf_len);
|
led_blink();
|
||||||
if(data[0] == 0) {
|
|
||||||
data[0] = '!';
|
serial0_write(data, data_len);
|
||||||
}
|
tx_usart0(carrigereturn);
|
||||||
tx_usart0(data[0]);
|
tx_usart0(linefeed);
|
||||||
//serial0_read(data, buf_len);
|
|
||||||
//data = rx_usart0();
|
|
||||||
serial0_flush_rxbuf();
|
//_delay_ms(1000);
|
||||||
serial0_read_with_err_checking(data, 1);
|
_delay_ms(500);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -61,26 +62,30 @@ int main() {
|
||||||
* Description: init usart0 hardware in async mode
|
* Description: init usart0 hardware in async mode
|
||||||
*/
|
*/
|
||||||
void init_usart0(void) {
|
void init_usart0(void) {
|
||||||
//DDRB |= (1<<TX_DDR)|(0<<RX_DDR);
|
|
||||||
//PORTD |= (0<<TX_PIN)|(0<<RX_PIN);
|
|
||||||
|
|
||||||
//setup stuff for usart communications.
|
//setup stuff for usart communications.
|
||||||
|
|
||||||
//set baud rate, this is 9600Baud with a 8Mhz clock
|
UBRR0H |= (uint8_t) (BT_UBRR>>8);
|
||||||
//UBRR0H |= (uint8_t) (BT_UBRR>>8);
|
UBRR0L |= (uint8_t) BT_UBRR;
|
||||||
//UBRR0L |= (uint8_t) BT_UBRR;
|
|
||||||
UBRR0H |= (uint8_t) (0x33>>8);
|
|
||||||
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);
|
||||||
|
|
||||||
|
#if USE_U2X
|
||||||
|
UCSR0A |= (1<<U2X0);
|
||||||
|
#else
|
||||||
|
UCSR0A &= ~(1<<U2X0);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
//setting the data frame format
|
//setting the data frame format
|
||||||
//We leave the 2MSB zero for async serial. And we also don't enable
|
UCSR0C = (0<<UMSEL01)|(0<<UMSEL00)| //Async
|
||||||
//parity bits for now. We set a 2bit stop bit and 8bit char size.
|
(0<<UPM01)|(0<<UPM00)| //Parity bits
|
||||||
//UCSR0C = (1<<USBS0) | (3<<UCSZ00);
|
(1<<USBS0)| //Stop bits
|
||||||
UCSR0C = (1<<USBS0) | (1<<UCSZ00) | (1<<UCSZ01) | (0<<UCSZ02);
|
(0<<UCSZ02)|(1<<UCSZ01)|(1<<UCSZ00)| //Set the number of bits.
|
||||||
|
(0<<UCPOL0); //Clock polarity, 0=falling 1=rising
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue