Compare commits
23 Commits
lab03.2
...
lab03.2-al
Author | SHA1 | Date | |
---|---|---|---|
22968ef6a4 | |||
09f82d5258 | |||
a61348e1c7 | |||
5d4b4a1e6d | |||
8b7377dff3 | |||
51e4a8bc9b | |||
2ea46d4402 | |||
e6888bd251 | |||
bdd27a6bea | |||
a882534979 | |||
a7a915faf4 | |||
f1f06f056a | |||
5b78b7da02 | |||
713cc80417 | |||
7d422c5494 | |||
8436ba76b0 | |||
d827bbe427 | |||
85dca87ff3 | |||
ae55fbac04 | |||
8c5f723619 | |||
90a1b412df | |||
dca26ac547 | |||
cc4321ae40 |
10
Makefile
10
Makefile
@ -43,13 +43,19 @@ CFLAGS = -Wall \
|
||||
-Werror \
|
||||
-Wfatal-errors \
|
||||
-Os \
|
||||
-flto \
|
||||
-fdata-sections \
|
||||
-ffunction-sections \
|
||||
-mmcu=$(BOARD) \
|
||||
-DF_CPU=16000000UL \
|
||||
-DGIT_DESCR=\"$(shell git describe --abbrev=6 --dirty --always --tags --long)\" \
|
||||
-ffreestanding \
|
||||
-std=c11
|
||||
|
||||
# Linker flags
|
||||
LDFLAGS = -mmcu=$(BOARD)
|
||||
LDFLAGS = -mmcu=$(BOARD) \
|
||||
-flto \
|
||||
-Wl,-gc-sections
|
||||
|
||||
OBJCOPYARGS = -O ihex \
|
||||
-R .eeprom
|
||||
@ -87,7 +93,7 @@ install:
|
||||
$(AVRDUDE) $(AVRDUDEARGS) -U flash:w:$(TARGET)
|
||||
|
||||
format:
|
||||
$(CODE_FORMATTER) $(SRC)
|
||||
$(CODE_FORMATTER) $(SRCDIR)/*.c
|
||||
|
||||
size:
|
||||
$(AVRSIZE) $(AVRSIZEARGS) $(ELF)
|
||||
|
26
src/hmi_msg.h
Normal file
26
src/hmi_msg.h
Normal file
@ -0,0 +1,26 @@
|
||||
#ifndef _HMI_MSG_H_
|
||||
#define _HMI_MSG_H_
|
||||
|
||||
#define VER_FW "Version: %S built on: %S %S\n"
|
||||
#define VER_LIBC "avr-libc version: %S\n"
|
||||
#define UI_GET_MONTH_LETTER "Enter Month name first letter >"
|
||||
|
||||
#define STUD_NAME "Arti Zirk"
|
||||
const char string_1[] PROGMEM = "January";
|
||||
const char string_2[] PROGMEM = "February";
|
||||
const char string_3[] PROGMEM = "March";
|
||||
const char string_4[] PROGMEM = "April";
|
||||
const char string_5[] PROGMEM = "May";
|
||||
const char string_6[] PROGMEM = "June";
|
||||
|
||||
PGM_P const months_table[] PROGMEM =
|
||||
{
|
||||
string_1,
|
||||
string_2,
|
||||
string_3,
|
||||
string_4,
|
||||
string_5,
|
||||
string_6
|
||||
};
|
||||
|
||||
#endif /* _HMI_MSG_H_ */
|
66
src/main.c
66
src/main.c
@ -0,0 +1,66 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include "hmi_msg.h"
|
||||
#include "uart.h"
|
||||
#include "print_helper.h"
|
||||
#include "../lib/hd44780_111/hd44780.h"
|
||||
|
||||
#define BLINK_DELAY_MS 100
|
||||
|
||||
int main (void)
|
||||
{
|
||||
/* set pin 3 of PORTA for output*/
|
||||
DDRA |= _BV(DDA3);
|
||||
/* Init stdio on UART0 and UART3 and print user code info */
|
||||
uart0_init();
|
||||
uart3_init();
|
||||
lcd_init();
|
||||
lcd_clrscr();
|
||||
stdout = stdin = &uart0_io;
|
||||
stderr = &uart3_out;
|
||||
fprintf_P(stderr, PSTR(VER_FW),
|
||||
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
|
||||
fprintf_P(stderr, PSTR(VER_LIBC), PSTR(__AVR_LIBC_VERSION_STRING__));
|
||||
/* End stdio init and info print */
|
||||
fprintf_P(stdout, PSTR(STUD_NAME "\n"));
|
||||
lcd_puts_P(PSTR(STUD_NAME));
|
||||
/* ASCII table print */
|
||||
print_ascii_tbl(stdout);
|
||||
unsigned char ascii[128] = {0};
|
||||
|
||||
for (unsigned char i = 0; i < sizeof(ascii); i++) {
|
||||
ascii[i] = i;
|
||||
}
|
||||
|
||||
print_for_human(stdout, ascii, sizeof(ascii));
|
||||
|
||||
while (1) {
|
||||
char month_first_leter;
|
||||
fprintf_P(stdout, PSTR(UI_GET_MONTH_LETTER));
|
||||
fscanf(stdin, "%c", &month_first_leter);
|
||||
fprintf(stdout, "%c\n", month_first_leter);
|
||||
lcd_goto(0x40);
|
||||
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (!strncmp_P(&month_first_leter, (PGM_P)pgm_read_word(&months_table[i]), 1)) {
|
||||
fprintf_P(stdout, PSTR("%S\n"), (PGM_P)pgm_read_word(&months_table[i]));
|
||||
lcd_puts_P((PGM_P)pgm_read_word(&months_table[i]));
|
||||
lcd_putc(' ');
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < 16; i++) {
|
||||
lcd_putc(' ');
|
||||
}
|
||||
|
||||
/* set pin 3 high to turn led on */
|
||||
PORTA |= _BV(PORTA3);
|
||||
_delay_ms(BLINK_DELAY_MS);
|
||||
/* set pin 3 low to turn led off */
|
||||
PORTA &= ~_BV(PORTA3);
|
||||
_delay_ms(BLINK_DELAY_MS);
|
||||
}
|
||||
}
|
||||
|
31
src/print_helper.c
Normal file
31
src/print_helper.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
#include "print_helper.h"
|
||||
|
||||
int print_ascii_tbl (FILE *stream)
|
||||
{
|
||||
for (char c = ' '; c <= '~'; c++) {
|
||||
if (!fprintf(stream, "%c ", c)) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return fprintf(stream, "\n");
|
||||
}
|
||||
|
||||
|
||||
int print_for_human (FILE *stream, const unsigned char *array, const int len)
|
||||
{
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (array[i] >= ' ' && array[i] <= '~') {
|
||||
if (!fprintf(stream, "%c", array[i])) {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
if (!fprintf(stream, "\"0x%02x\"", array[i])) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fprintf(stream, "\n");;
|
||||
}
|
7
src/print_helper.h
Normal file
7
src/print_helper.h
Normal file
@ -0,0 +1,7 @@
|
||||
#ifndef _PRINT_HELPER_H_
|
||||
#define _PRINT_HELPER_H_
|
||||
|
||||
int print_ascii_tbl (FILE *stream);
|
||||
int print_for_human (FILE *stream, const unsigned char *array, const int len);
|
||||
|
||||
#endif /* _PRINT_HELPER_H_ */
|
72
src/uart.c
Normal file
72
src/uart.c
Normal file
@ -0,0 +1,72 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef F_CPU
|
||||
#define F_CPU 16000000UL
|
||||
#endif
|
||||
|
||||
#ifndef BAUD
|
||||
#define BAUD 9600
|
||||
#endif
|
||||
#include <util/setbaud.h>
|
||||
|
||||
/* http://www.cs.mun.ca/~rod/Winter2007/4723/notes/serial/serial.html */
|
||||
|
||||
void uart0_init(void)
|
||||
{
|
||||
UBRR0H = UBRRH_VALUE;
|
||||
UBRR0L = UBRRL_VALUE;
|
||||
#if USE_2X
|
||||
UCSR0A |= _BV(U2X0);
|
||||
#else
|
||||
UCSR0A &= ~(_BV(U2X0));
|
||||
#endif
|
||||
UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
|
||||
UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
|
||||
}
|
||||
|
||||
void uart3_init(void)
|
||||
{
|
||||
UBRR3H = UBRRH_VALUE;
|
||||
UBRR3L = UBRRL_VALUE;
|
||||
#if USE_2X
|
||||
UCSR3A |= _BV(U2X3);
|
||||
#else
|
||||
UCSR3A &= ~(_BV(U2X3));
|
||||
#endif
|
||||
UCSR3C = _BV(UCSZ31) | _BV(UCSZ30); /* 8-bit data */
|
||||
UCSR3B = _BV(TXEN3); /* Enable TX */
|
||||
}
|
||||
|
||||
int uart0_putchar(char c, FILE *stream)
|
||||
{
|
||||
(void) stream;
|
||||
|
||||
if (c == '\n') {
|
||||
uart0_putchar('\r', stream);
|
||||
}
|
||||
|
||||
loop_until_bit_is_set(UCSR0A, UDRE0);
|
||||
UDR0 = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart3_putchar(char c, FILE *stream)
|
||||
{
|
||||
(void) stream;
|
||||
|
||||
if (c == '\n') {
|
||||
uart3_putchar('\r', stream);
|
||||
}
|
||||
|
||||
loop_until_bit_is_set(UCSR3A, UDRE3);
|
||||
UDR3 = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int uart0_getchar(FILE *stream)
|
||||
{
|
||||
(void) stream;
|
||||
loop_until_bit_is_set(UCSR0A, RXC0);
|
||||
return UDR0;
|
||||
}
|
17
src/uart.h
Normal file
17
src/uart.h
Normal file
@ -0,0 +1,17 @@
|
||||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
int uart0_putchar(char c, FILE *stream);
|
||||
int uart0_getchar(FILE *stream);
|
||||
|
||||
int uart3_putchar(char c, FILE *stream);
|
||||
|
||||
void uart0_init(void);
|
||||
void uart3_init(void);
|
||||
|
||||
/* http://www.ermicro.com/blog/?p=325 */
|
||||
|
||||
FILE uart0_io = FDEV_SETUP_STREAM(uart0_putchar, uart0_getchar, _FDEV_SETUP_RW);
|
||||
FILE uart3_out = FDEV_SETUP_STREAM(uart3_putchar, NULL, _FDEV_SETUP_WRITE);
|
||||
|
||||
#endif /* _UART_H_ */
|
Reference in New Issue
Block a user