lab04 code ready

This commit is contained in:
Arti Zirk 2016-11-16 00:15:07 +02:00
parent 0fc8ce42ab
commit eb02997c80
5 changed files with 114 additions and 111 deletions

View File

@ -6,13 +6,14 @@
#define LIBC_VERSION "avr-libc version: %S\n" #define LIBC_VERSION "avr-libc version: %S\n"
#define STUD_NAME "Arti Zirk" #define STUD_NAME "Arti Zirk"
#define GET_MONTH_MSG "Enter Month name first letter >" #define GET_MONTH_MSG "Enter Month name first letter >"
#define UPTIME "Uptime: %lu s"
const char m1[] PROGMEM = "January"; const char m1[] PROGMEM = "January";
const char m2[] PROGMEM = "February"; const char m2[] PROGMEM = "February";
const char m3[] PROGMEM = "March"; const char m3[] PROGMEM = "March";
const char m4[] PROGMEM = "April"; const char m4[] PROGMEM = "April";
const char m5[] PROGMEM = "May"; const char m5[] PROGMEM = "May";
const char m6[] PROGMEM = "June"; const char m6[] PROGMEM = "June";
PGM_P const months[] PROGMEM = {m1,m2,m3,m4,m5,m6}; PGM_P const months[] PROGMEM = {m1,m2,m3,m4,m5,m6};
#endif /* _HMI_MSG_H_ */ #endif /* _HMI_MSG_H_ */

View File

@ -2,32 +2,48 @@
#include <string.h> #include <string.h>
#include <avr/io.h> #include <avr/io.h>
#include <avr/pgmspace.h> #include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <util/delay.h> #include <util/delay.h>
#include "hmi_msg.h" #include "hmi_msg.h"
#include "uart.h" #include "uart-wrapper.h"
#include "print_helper.h" #include "print_helper.h"
#include "../lib/hd44780_111/hd44780.h" #include "../lib/hd44780_111/hd44780.h"
#include "../lib/andygock_avr-uart/uart.h"
#define BAUD 9600
#define BLINK_DELAY_MS 100 #define BLINK_DELAY_MS 100
int main (void) volatile uint32_t time;
static inline void init_system_clock(void)
{ {
/* Init */ TCCR5A = 0; // Clear control register A
DDRA |= _BV(DDA3); TCCR5B = 0; // Clear control register B
uart0_init(); TCCR5B |= _BV(WGM52) | _BV(CS52); // CTC and fCPU/256
uart3_init(); OCR5A = 62549; // 1 s
TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable
}
static inline void hw_init() {
DDRA |= _BV(DDA3); // setup Arduino pin 25 as output
uart0_init(UART_BAUD_SELECT(BAUD, F_CPU));
uart3_init(UART_BAUD_SELECT(BAUD, F_CPU));
init_system_clock();
sei(); // Enable interupts
stdout = stdin = &uart0_io; stdout = stdin = &uart0_io;
stderr = &uart3_out; stderr = &uart3_out;
lcd_init(); lcd_init();
lcd_clrscr(); lcd_clrscr();
/* End init */ }
/* Print version info to stderr */ static inline void print_prog_version() {
fprintf_P(stderr, PSTR(PROG_VERSION), fprintf_P(stderr, PSTR(PROG_VERSION),
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__)); PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
fprintf_P(stderr, PSTR(LIBC_VERSION), PSTR(__AVR_LIBC_VERSION_STRING__)); fprintf_P(stderr, PSTR(LIBC_VERSION), PSTR(__AVR_LIBC_VERSION_STRING__));
/* End version print */ }
static inline void print_program_startup() {
fprintf_P(stdout, PSTR(STUD_NAME)); fprintf_P(stdout, PSTR(STUD_NAME));
fputc('\n', stdout); /* Add a new line to the uart printout */ fputc('\n', stdout); /* Add a new line to the uart printout */
lcd_puts_P(PSTR(STUD_NAME)); lcd_puts_P(PSTR(STUD_NAME));
@ -40,30 +56,57 @@ int main (void)
} }
print_for_human(stdout, ascii, sizeof(ascii)); print_for_human(stdout, ascii, sizeof(ascii));
while (1) { fprintf_P(stdout, PSTR(GET_MONTH_MSG));
/* set pin 3 high to turn led on */ }
PORTA |= _BV(PORTA3);
_delay_ms(BLINK_DELAY_MS);
/* Month search and print */
char letter; static inline void search_month() {
fprintf_P(stdout, PSTR(GET_MONTH_MSG)); /* Month search and print */
fscanf(stdin, "%c", &letter); char letter;
fprintf(stdout, "%c\n", letter); fscanf(stdin, "%c", &letter);
lcd_goto(0x40); /* Got to the beginning of the next line */ fprintf(stdout, "%c\n", letter);
for (int i = 0; i < 6; i++) { lcd_goto(0x40); /* Got to the beginning of the next line */
if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) { for (int i = 0; i < 6; i++) {
fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i])); if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) {
fputc('\n', stdout); fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i]));
lcd_puts_P((PGM_P)pgm_read_word(&months[i])); fputc('\n', stdout);
lcd_putc(' '); lcd_puts_P((PGM_P)pgm_read_word(&months[i]));
} lcd_putc(' ');
} }
}
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
lcd_puts_P(PSTR(" ")); /* Clear the end of the line */
}
lcd_puts_P(PSTR(" ")); /* Clear the end of the line */ static inline void heartbeat() {
static uint32_t last_time;
uint32_t cur_time = time;
if ((last_time - cur_time) > 0) {
// Toggle led on arduino pin 25
PORTA ^= _BV(PORTA3);
fprintf_P(stderr, PSTR(UPTIME "\n"), cur_time);
}
last_time = cur_time;
}
/* set pin 3 low to turn led off */ int main (void)
PORTA &= ~_BV(PORTA3); {
_delay_ms(BLINK_DELAY_MS);
hw_init();
print_prog_version();
print_program_startup();
while (1) {
heartbeat();
if(uart0_available()) {
search_month();
}
} }
} }
/* System clock ISR */
ISR(TIMER5_COMPA_vect)
{
time++;
}

33
src/uart-wrapper.c Normal file
View File

@ -0,0 +1,33 @@
#include <avr/io.h>
#include <stdio.h>
#include "../lib/andygock_avr-uart/uart.h"
int uart0_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart0_putchar('\r', stream);
}
uart0_putc(c);
return 0;
}
int uart3_putchar(char c, FILE *stream)
{
(void) stream;
if (c == '\n') {
uart3_putchar('\r', stream);
}
uart3_putc(c);
return 0;
}
int uart0_getchar(FILE *stream)
{
(void) stream;
return (unsigned char)uart0_getc();
}

View File

@ -1,13 +1,11 @@
#ifndef _UART_H_ #ifndef _UART_WRAPPER_H_
#define _UART_H_ #define _UART_WRAPPER_H_
int uart0_putchar(char c, FILE *stream); int uart0_putchar(char c, FILE *stream);
int uart0_getchar(FILE *stream); int uart0_getchar(FILE *stream);
int uart3_putchar(char c, FILE *stream); int uart3_putchar(char c, FILE *stream);
void uart0_init(void);
void uart3_init(void);
/* http://www.ermicro.com/blog/?p=325 */ /* http://www.ermicro.com/blog/?p=325 */

View File

@ -1,72 +0,0 @@
#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;
}