122 lines
2.3 KiB
C
122 lines
2.3 KiB
C
|
/*
|
||
|
* 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>
|
||
|
#include "avr_at-09.h"
|
||
|
|
||
|
|
||
|
//#############################
|
||
|
//Globals
|
||
|
//#############################
|
||
|
|
||
|
int main() {
|
||
|
unsigned char* tx_buffer[16];
|
||
|
unsigned char* rx_buffer[16];
|
||
|
|
||
|
led_blink();
|
||
|
led_blink();
|
||
|
led_blink();
|
||
|
_delay_ms(1000);
|
||
|
|
||
|
init_usart0();
|
||
|
|
||
|
//enable interrupts
|
||
|
sei();
|
||
|
|
||
|
while(1) {
|
||
|
led_blink();
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//#############################
|
||
|
//FUNCTIONS
|
||
|
//#############################
|
||
|
|
||
|
/*
|
||
|
* Input: None
|
||
|
* Output: None
|
||
|
* Description: init usart0 hardware in async mode
|
||
|
*/
|
||
|
void init_usart0(void) {
|
||
|
DDRB |= (1<<TX_DDR)|(0<<RX_DDR);
|
||
|
PORTD |= (0<<TX_PIN)|(0<<RX_PIN);
|
||
|
|
||
|
//setup stuff for usart communications.
|
||
|
|
||
|
//set baud rate, this is 9600Baud with a 1Mhz clock
|
||
|
//so it should be equal to 6 or 0x006
|
||
|
UBRR0H |= (uint8_t) (BT_UBRR>>8);
|
||
|
UBRR0L |= (uint8_t) BT_UBRR;
|
||
|
|
||
|
//Enable recv and Transmit pins, overrides other uses.
|
||
|
//IN the usart control and status register 0B
|
||
|
UCSR0B = (1<<RXEN0) | (1<<TXEN0);
|
||
|
|
||
|
//setting the data frame format
|
||
|
//We leave the 2MSB zero for async serial. And we also don't enable
|
||
|
//parity bits for now. We set a 2bit stop bit and 8bit char size.
|
||
|
UCSR0C = (1<<USBS0) | (3<<UCSZ00);
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* 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.
|
||
|
while(!(UCSR0A & (1<<RXC0))) {
|
||
|
;//Equiv of continue
|
||
|
}
|
||
|
|
||
|
//now return the data
|
||
|
return (unsigned char) UDR0;
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
* 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);
|
||
|
|
||
|
}
|
||
|
|
||
|
|