2023-01-14 05:13:45 +00:00
|
|
|
/*
|
|
|
|
* Author: Jake Goodwin
|
|
|
|
* Date: 2023
|
|
|
|
* Description: Small library to communicate with the AT-09 bluetooth module.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/sleep.h>
|
|
|
|
#include <util/delay.h>
|
2023-01-14 06:47:43 +00:00
|
|
|
#include "avr_usart.h"
|
2023-01-14 05:13:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
//#############################
|
|
|
|
//Globals
|
|
|
|
//#############################
|
|
|
|
|
2023-01-17 04:30:36 +00:00
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
int main() {
|
2023-01-14 21:45:21 +00:00
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
led_blink();
|
|
|
|
led_blink();
|
|
|
|
led_blink();
|
|
|
|
_delay_ms(1000);
|
|
|
|
|
|
|
|
init_usart0();
|
|
|
|
|
2023-01-17 04:54:15 +00:00
|
|
|
//enable parity, because the internal
|
|
|
|
//crystal is garbage
|
|
|
|
serial0_enable_parity_bit(2);
|
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
//enable interrupts
|
|
|
|
sei();
|
2023-01-17 03:43:28 +00:00
|
|
|
unsigned char data[8] = "AT+BAUD3";
|
|
|
|
unsigned char data_cmd[2] = "AT";
|
|
|
|
uint8_t data_len = 8;
|
|
|
|
uint8_t data_cmd_len = 2;
|
2023-01-14 06:47:43 +00:00
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
while(1) {
|
2023-01-17 03:43:28 +00:00
|
|
|
led_blink();
|
|
|
|
|
|
|
|
serial0_write(data, data_len);
|
|
|
|
|
|
|
|
//_delay_ms(1000);
|
|
|
|
_delay_ms(500);
|
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//#############################
|
|
|
|
//FUNCTIONS
|
|
|
|
//#############################
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input: None
|
|
|
|
* Output: None
|
2023-01-17 04:30:36 +00:00
|
|
|
* Description: init usart0 hardware in async mode,
|
|
|
|
* This sets up the connection with default params 8-n-1
|
2023-01-14 05:13:45 +00:00
|
|
|
*/
|
|
|
|
void init_usart0(void) {
|
|
|
|
//setup stuff for usart communications.
|
|
|
|
|
2023-01-17 03:43:28 +00:00
|
|
|
UBRR0H |= (uint8_t) (BT_UBRR>>8);
|
|
|
|
UBRR0L |= (uint8_t) BT_UBRR;
|
2023-01-14 06:47:43 +00:00
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
//Enable recv and Transmit pins, overrides other uses.
|
|
|
|
//IN the usart control and status register 0B
|
|
|
|
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
|
2023-01-17 03:43:28 +00:00
|
|
|
|
|
|
|
#if USE_U2X
|
|
|
|
UCSR0A |= (1<<U2X0);
|
|
|
|
#else
|
|
|
|
UCSR0A &= ~(1<<U2X0);
|
|
|
|
#endif
|
2023-01-17 04:10:53 +00:00
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
//setting the data frame format
|
2023-01-17 03:43:28 +00:00
|
|
|
UCSR0C = (0<<UMSEL01)|(0<<UMSEL00)| //Async
|
|
|
|
(0<<UPM01)|(0<<UPM00)| //Parity bits
|
|
|
|
(1<<USBS0)| //Stop bits
|
|
|
|
(0<<UCSZ02)|(1<<UCSZ01)|(1<<UCSZ00)| //Set the number of bits.
|
|
|
|
(0<<UCPOL0); //Clock polarity, 0=falling 1=rising
|
|
|
|
|
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input: a 8bit char
|
|
|
|
* Output: None
|
|
|
|
* Description: Sends a char over usart0
|
|
|
|
*/
|
|
|
|
void tx_usart0(unsigned char data) {
|
|
|
|
//first wait for the transmit buffer to be empty
|
|
|
|
while(!(UCSR0A & (1<<UDRE0))) {
|
|
|
|
;//Equiv of continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//now that it's empty, fill buffer with TX data.
|
|
|
|
UDR0 = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input:None
|
|
|
|
* Output: a 8bit char
|
|
|
|
* Description: Recvs a char over usart0
|
|
|
|
*/
|
|
|
|
unsigned char rx_usart0(void) {
|
|
|
|
//first wait for the data to be received.
|
2023-01-14 20:59:34 +00:00
|
|
|
while( !(UCSR0A & (1<<RXC0)) ) {
|
2023-01-14 05:13:45 +00:00
|
|
|
;//Equiv of continue
|
|
|
|
}
|
|
|
|
|
|
|
|
//now return the data
|
2023-01-14 20:59:34 +00:00
|
|
|
return UDR0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void serial0_flush_rxbuf(void) {
|
|
|
|
uint8_t none;
|
2023-01-14 21:26:11 +00:00
|
|
|
while( UCSR0A & (1<<RXC0)) {
|
|
|
|
none = UDR0;
|
|
|
|
}
|
2023-01-14 05:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-14 19:36:13 +00:00
|
|
|
/*
|
|
|
|
* Inupt: None
|
|
|
|
* Output: None
|
2023-01-14 19:46:29 +00:00
|
|
|
* Description: Nothing right now.
|
2023-01-14 19:36:13 +00:00
|
|
|
*/
|
2023-01-14 19:46:29 +00:00
|
|
|
void serial0_enable_timeouts(uint8_t ms) {
|
|
|
|
;
|
|
|
|
}
|
2023-01-14 19:36:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Inupt: None
|
|
|
|
* Output: None
|
|
|
|
* Description:
|
|
|
|
*/
|
2023-01-14 20:03:16 +00:00
|
|
|
void serial0_write(unsigned char* buffer, uint8_t write_length) {
|
2023-01-14 19:44:48 +00:00
|
|
|
for(uint8_t i = 0; i < write_length; i++) {
|
|
|
|
tx_usart0(buffer[i]);
|
|
|
|
}
|
2023-01-17 03:50:56 +00:00
|
|
|
|
|
|
|
#if CARRIAGE_RETURN
|
|
|
|
tx_usart0(0x0d);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if LINE_FEED
|
|
|
|
tx_usart0(0x0a);
|
|
|
|
#endif
|
|
|
|
|
2023-01-14 19:44:48 +00:00
|
|
|
}
|
2023-01-14 19:36:13 +00:00
|
|
|
|
2023-01-17 04:30:36 +00:00
|
|
|
/*
|
|
|
|
* Input: none
|
|
|
|
* Output: none
|
|
|
|
* Description:
|
|
|
|
*/
|
|
|
|
void serial0_enable_line_feeds(void) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input: none
|
|
|
|
* Output: none
|
|
|
|
* Description:
|
|
|
|
*/
|
|
|
|
void serial0_enable_carriage_returns(void) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input: A parity setting as a 8bit uint
|
|
|
|
* Output: none
|
|
|
|
* Description: 0val clears for no parity
|
|
|
|
* 1val sets positive parity
|
|
|
|
* 2val sets negative parity
|
|
|
|
*/
|
|
|
|
void serial0_enable_parity_bit(uint8_t pos) {
|
|
|
|
switch(pos) {
|
|
|
|
case 0:
|
|
|
|
UCSR0C &= ~((1<<UPM01)|(1<<UPM00)); //Clear all.
|
|
|
|
case 1:
|
|
|
|
UCSR0C &= ~(1<<UPM00); //Make sure that the negative parity if off.
|
|
|
|
UCSR0C |= (1<<UPM01); //Set the positive parity
|
|
|
|
case 2:
|
|
|
|
UCSR0C |= (1<<UPM01)|(1<<UPM00); //Set both bits for neg parity
|
|
|
|
default:
|
|
|
|
;//Do nothing, just put here because
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Input: number of stop bits
|
|
|
|
* Output: none
|
|
|
|
* Description:
|
|
|
|
*/
|
|
|
|
void serial0_set_stop_bits(uint8_t number) {
|
|
|
|
//clear all the set bits to start off.
|
|
|
|
UCSR0C &= ~(1<<USBS0);
|
|
|
|
switch(number) {
|
|
|
|
case 0:
|
|
|
|
;
|
|
|
|
case 1:
|
|
|
|
;
|
|
|
|
case 2:
|
|
|
|
UCSR0C |= (1<<USBS0);
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
|
2023-01-17 04:54:15 +00:00
|
|
|
}
|
|
|
|
}
|
2023-01-17 04:30:36 +00:00
|
|
|
|
|
|
|
|
2023-01-14 19:36:13 +00:00
|
|
|
/*
|
2023-01-14 19:41:11 +00:00
|
|
|
* Inupt: A serialbuffer and length
|
2023-01-14 19:36:13 +00:00
|
|
|
* Output: None
|
2023-01-14 19:41:11 +00:00
|
|
|
* Description: Reads the serial data into a buffer of x length.
|
2023-01-14 19:36:13 +00:00
|
|
|
*/
|
2023-01-14 20:03:16 +00:00
|
|
|
void serial0_read(unsigned char* buffer, uint8_t buf_length) {
|
|
|
|
//clear buffer out?
|
2023-01-14 20:59:34 +00:00
|
|
|
//buffer = 0;
|
2023-01-14 19:41:11 +00:00
|
|
|
for(uint8_t i = 0; i < buf_length; i++) {
|
|
|
|
buffer[i] = rx_usart0();
|
|
|
|
}
|
|
|
|
}
|
2023-01-14 19:36:13 +00:00
|
|
|
|
|
|
|
|
2023-01-14 20:59:34 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2023-01-14 19:36:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Inupt: BUAD RATE as uint8_t
|
|
|
|
* Output: None
|
|
|
|
* Description: Sets the baud rate of the serial usart0
|
|
|
|
* VALUES @ 1Mhz:
|
|
|
|
* 207 --> 300bps
|
|
|
|
* 103 --> 600bps
|
|
|
|
* 51 --> 1200bps
|
|
|
|
* 25 --> 2400bps
|
|
|
|
* 12 --> 4800bps
|
|
|
|
* 6 --> 9600bps !!! 7% error rate
|
|
|
|
*/
|
|
|
|
|
2023-01-14 20:03:16 +00:00
|
|
|
void serial0_set_baud(uint8_t baud) {
|
2023-01-14 19:36:13 +00:00
|
|
|
UBRR0H |= (uint8_t) (baud>>8);
|
|
|
|
UBRR0L |= (uint8_t) baud;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-14 05:13:45 +00:00
|
|
|
/*
|
|
|
|
* Input: None
|
|
|
|
* Output: None
|
|
|
|
* Description: Toggles the pin for the LED indicator.
|
|
|
|
*/
|
|
|
|
static void led_blink(void) {
|
|
|
|
//Set the DDR for output.
|
|
|
|
DDRC |= (1<<LED_PIN);
|
|
|
|
|
|
|
|
PORTC ^= (1<<LED_PIN);
|
|
|
|
_delay_ms(250);
|
|
|
|
PORTC ^= (1<<LED_PIN);
|
|
|
|
_delay_ms(250);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|