Initial USART echoing to LCD example

Signed-off-by: Collin J. Doering <collin.doering@rekahsoft.ca>
This commit is contained in:
Collin J. Doering 2015-10-06 04:23:40 -04:00
parent 85c7948848
commit 208d90f4ef
3 changed files with 105 additions and 11 deletions

55
USART.c Normal file
View File

@ -0,0 +1,55 @@
/**
* (C) Copyright Collin J. Doering 2015
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* File: USART.c
* Author: Collin J. Doering <collin.doering@rekahsoft.ca>
* Date: Oct 6, 2015
*/
#include <avr/io.h>
#include "USART.h"
#include <util/setbaud.h>
#ifndef BAUD /* if not defined in Makefile... */
#define BAUD 9600 /* set a safe default baud rate */
#endif
void initUSART(void) { /* requires BAUD */
UBRR0H = UBRRH_VALUE; /* defined in setbaud.h */
UBRR0L = UBRRL_VALUE;
#if USE_2X
UCSR0A |= (1 << U2X0);
#else
UCSR0A &= ~(1 << U2X0);
#endif
/* Enable USART transmitter/receiver */
UCSR0B = (1 << TXEN0) | (1 << RXEN0);
UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop bit */
}
void transmitByte(uint8_t data) {
/* Wait for empty transmit buffer */
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data; /* send data */
}
uint8_t receiveByte(void) {
loop_until_bit_is_set(UCSR0A, RXC0); /* Wait for incoming data */
return UDR0; /* return register value */
}

28
USART.h Normal file
View File

@ -0,0 +1,28 @@
/**
* (C) Copyright Collin J. Doering 2015
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* File: USART.h
* Author: Collin J. Doering <collin.doering@rekahsoft.ca>
* Date: Oct 6, 2015
*/
void initUSART(void);
void transmitByte(uint8_t data);
uint8_t receiveByte(void);

View File

@ -31,27 +31,38 @@
#include <util/delay.h>
#include "lcdLib.h"
#include "USART.h"
void showSomePrases(void) {
const char* data[4] = { "Hello there friend!!\nIsn't it a nice day.\nAnyways, I must go..\nTo a midnight show;)",
"This is some other text. It should be wrapped appropriately.\nIsn't that neat!",
"Welcome! (line 1)\nThis is line 2.\nAnd here is line 3.\nAnd finally, line 4.",
"Finally, this is the\nend; of the array,\nthat is.\nCheers."};
for (uint8_t i = 0; i < 4; i++) {
writeStringToLCD(data[i]);
_delay_ms(5000);
clearDisplay();
}
_delay_ms(3000);
}
int main(void) {
clock_prescale_set(clock_div_1);
STATUS_LED_DDR |= 1 << STATUS_LED; // DEBUG
const char* data[4] = { "Hello there friend!!\nIsn't it a nice day.\nAnyways, I must go..\nTo a midnight show;)",
"This is some other text. It should be wrapped appropriately.\nIsn't that neat!",
"Welcome! (line 1)\nThis is line 2.\nAnd here is line 3.\nAnd finally, line 4.",
"Finally, this is the\nend; of the array,\nthat is.\nCheers."};
initUSART();
char serialChar;
initLCD();
//initLCDByInternalReset();
showSomePrases();
while (1) {
for (uint8_t i = 0; i < 4; i++) {
writeStringToLCD(data[i]);
_delay_ms(10000);
clearDisplay();
}
_delay_ms(3000);
serialChar = receiveByte();
writeCharToLCD(serialChar);
}
return 0;