Compare commits
42 Commits
Author | SHA1 | Date | |
---|---|---|---|
04031523d4 | |||
f3676e9408 | |||
256059d358 | |||
55ed13b805 | |||
861f0c57d8 | |||
30bd26d471 | |||
22461eaff4 | |||
6635f07e8d | |||
079a8ff367 | |||
9e46b92744 | |||
500c36b81b | |||
9d5fff9fa0 | |||
f72a7b33e0 | |||
1fad6de2e1 | |||
14a6e4ff91 | |||
281d4d72c5 | |||
89b7339775 | |||
adc3ad6a4a | |||
f4c572a2e6 | |||
25b1feb71d | |||
39720c676e | |||
a836b3a624 | |||
51fc127b48 | |||
be366ed50e | |||
8b64aca736 | |||
aa4e954f76 | |||
275cf9fa28 | |||
bdd27a6bea | |||
a882534979 | |||
a7a915faf4 | |||
f1f06f056a | |||
5b78b7da02 | |||
713cc80417 | |||
7d422c5494 | |||
8436ba76b0 | |||
d827bbe427 | |||
85dca87ff3 | |||
ae55fbac04 | |||
8c5f723619 | |||
90a1b412df | |||
dca26ac547 | |||
cc4321ae40 |
41
README.md
Normal file
41
README.md
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# I237 Hardware Programming
|
||||||
|
|
||||||
|
This here is my repository for hardware programming course at
|
||||||
|
Estonian IT College that is based around a
|
||||||
|
[Arduino Mega 2560](https://www.arduino.cc/en/Main/ArduinoBoardMega2560).
|
||||||
|
|
||||||
|
This code us usualy updated once every two weeks when a new excersice is put up
|
||||||
|
by Silver Kits.
|
||||||
|
|
||||||
|
# How to install
|
||||||
|
|
||||||
|
Currently to test this code you have to clone the repository
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git clone https://git.wut.ee/arti/i237.git hardware
|
||||||
|
cd hardware
|
||||||
|
```
|
||||||
|
|
||||||
|
And export a enviroment variable that points to a Arduino Mega board. The
|
||||||
|
real path will depend on the OS and Computer that you are using. For me it is
|
||||||
|
usualy `/dev/ttyACM0`.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
export ARDUINO=/dev/ttyACM0
|
||||||
|
```
|
||||||
|
|
||||||
|
After that you can `make` the project.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
make clean
|
||||||
|
make
|
||||||
|
make install
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also chekcout previous labs using `git checkout <lab name>`. For example
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git checkout lab02
|
||||||
|
```
|
||||||
|
|
||||||
|
Don't forget to `make clean` after each checkout!
|
BIN
doc/arduino-mega-lcd1602-keypad-shield-placement.png
Normal file
BIN
doc/arduino-mega-lcd1602-keypad-shield-placement.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 159 KiB |
@ -10,9 +10,21 @@ This shield consists of three logical parts:
|
|||||||
|
|
||||||
<div class=pagebreak></div>
|
<div class=pagebreak></div>
|
||||||
|
|
||||||
|
## Connection to Arduino Mega
|
||||||
|
|
||||||
|
Following illustration shows how shield is connected to Arduino mega board.
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
Image source: [sainsmart.com](http://www.sainsmart.com/media/catalog/product/2/_/2_16_6.jpg)
|
||||||
|
|
||||||
|
Additional installation details and tutorials can be found with help of keywords [Arduino 1602 lcd keypad shield tutorial](https://www.google.ee/webhp?q=Arduino+1602+lcd+keypad+shield+tutorial).
|
||||||
|
|
||||||
|
<div class=pagebreak></div>
|
||||||
|
|
||||||
## Wiring illustration
|
## Wiring illustration
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Author: [Lauri Võsandi](http://lauri.võsandi.com/arduino/lcd1602-key-shield.html#hd44780)
|
Author: [Lauri Võsandi](http://lauri.võsandi.com/arduino/lcd1602-key-shield.html#hd44780)
|
||||||
|
|
Before Width: | Height: | Size: 119 KiB After Width: | Height: | Size: 119 KiB |
@ -6,12 +6,12 @@ This wiring schema uses only Tx from Arduino and is suitable to be used as stand
|
|||||||
|
|
||||||
## Wiring illustration
|
## Wiring illustration
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Wiring table
|
## Wiring table
|
||||||
|
|
||||||
| Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | USB UART converter pin |
|
| Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | USB UART converter pin |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Ground (GND) | GND | GND | GND |
|
| Ground (GND) | GND | GND | GND |
|
||||||
| Transmit data from Arduino (TxD) | PORTJ pin 1 (TXD3) | Digital pin 14 (TX3) | TxD |
|
| Transmit data from Arduino (TxD) | PORTJ pin 1 (TXD3) | Digital pin 14 (TX3) | RxD |
|
||||||
|
|
Before Width: | Height: | Size: 133 KiB After Width: | Height: | Size: 133 KiB |
0
lib/hd44780_111/hd44780.h
Executable file → Normal file
0
lib/hd44780_111/hd44780.h
Executable file → Normal file
0
lib/hd44780_111/hd44780.txt
Executable file → Normal file
0
lib/hd44780_111/hd44780.txt
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings_example.h
Executable file → Normal file
0
lib/hd44780_111/hd44780_settings_example.h
Executable file → Normal file
19
src/hmi_msg.h
Normal file
19
src/hmi_msg.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#ifndef _HMI_MSG_H_
|
||||||
|
#define _HMI_MSG_H_
|
||||||
|
|
||||||
|
#define PROG_VERSION "Version: %S built on: %S %S"
|
||||||
|
#define LIBC_VERSION "avr-libc version: %S"
|
||||||
|
#define STUD_NAME "Arti Zirk"
|
||||||
|
#define GET_MONTH_MSG "Enter Month name first letter >"
|
||||||
|
#define UPTIME_MSG "Uptime: %lu s"
|
||||||
|
|
||||||
|
const char m1[] PROGMEM = "January";
|
||||||
|
const char m2[] PROGMEM = "February";
|
||||||
|
const char m3[] PROGMEM = "March";
|
||||||
|
const char m4[] PROGMEM = "April";
|
||||||
|
const char m5[] PROGMEM = "May";
|
||||||
|
const char m6[] PROGMEM = "June";
|
||||||
|
|
||||||
|
PGM_P const months[] PROGMEM = {m1,m2,m3,m4,m5,m6};
|
||||||
|
#endif /* _HMI_MSG_H_ */
|
128
src/main.c
128
src/main.c
@ -0,0 +1,128 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include <avr/pgmspace.h>
|
||||||
|
#include <util/delay.h>
|
||||||
|
#include "hmi_msg.h"
|
||||||
|
#include "../lib/andygock_avr-uart/uart.h"
|
||||||
|
#include "uart_wrap.h"
|
||||||
|
#include "print_helper.h"
|
||||||
|
#include "../lib/hd44780_111/hd44780.h"
|
||||||
|
|
||||||
|
#define BAUDRATE 9600
|
||||||
|
|
||||||
|
// For configuring arduino mega pin 25
|
||||||
|
#define LED_INIT DDRA |= _BV(DDA3);
|
||||||
|
#define LED_TOGGLE PORTA ^= _BV(PORTA3)
|
||||||
|
|
||||||
|
static inline void init_system_clock(void)
|
||||||
|
{
|
||||||
|
TCCR5A = 0; // Clear control register A
|
||||||
|
TCCR5B = 0; // Clear control register B
|
||||||
|
TCCR5B |= _BV(WGM52) | _BV(CS52); // CTC and fCPU/256
|
||||||
|
OCR5A = 62549; // 1 s
|
||||||
|
TIMSK5 |= _BV(OCIE5A); // Output Compare A Match Interrupt Enable
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void init_hw (void)
|
||||||
|
{
|
||||||
|
// IO init
|
||||||
|
/// Set arduino pin 25 as output
|
||||||
|
LED_INIT;
|
||||||
|
|
||||||
|
// System clock
|
||||||
|
init_system_clock();
|
||||||
|
|
||||||
|
// UART init
|
||||||
|
uart0_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
|
||||||
|
uart3_init(UART_BAUD_SELECT(BAUDRATE, F_CPU));
|
||||||
|
stdout = stdin = &uart0_io;
|
||||||
|
stderr = &uart3_out;
|
||||||
|
|
||||||
|
// LCD init
|
||||||
|
lcd_init();
|
||||||
|
lcd_clrscr();
|
||||||
|
|
||||||
|
// Enable interupts
|
||||||
|
sei();
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void start_ui (void)
|
||||||
|
{
|
||||||
|
// Print program and libc versions
|
||||||
|
fprintf_P(stderr, PSTR(PROG_VERSION "\n"),
|
||||||
|
PSTR(GIT_DESCR), PSTR(__DATE__), PSTR(__TIME__));
|
||||||
|
fprintf_P(stderr, PSTR(LIBC_VERSION "\n"), PSTR(__AVR_LIBC_VERSION_STRING__));
|
||||||
|
|
||||||
|
// print student name
|
||||||
|
fprintf_P(stdout, PSTR(STUD_NAME));
|
||||||
|
fputc('\n', stdout); // Add a new line to the uart printout
|
||||||
|
lcd_puts_P(PSTR(STUD_NAME));
|
||||||
|
|
||||||
|
// ASCII table print
|
||||||
|
print_ascii_tbl(stdout);
|
||||||
|
unsigned char ascii[128];
|
||||||
|
for (unsigned char i = 0; i < sizeof(ascii); i++) {
|
||||||
|
ascii[i] = i;
|
||||||
|
}
|
||||||
|
print_for_human(stdout, ascii, sizeof(ascii));
|
||||||
|
|
||||||
|
// Bootstrap search_month message
|
||||||
|
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void search_month (void)
|
||||||
|
{
|
||||||
|
char letter;
|
||||||
|
|
||||||
|
fscanf(stdin, "%c", &letter);
|
||||||
|
fprintf(stdout, "%c\n", letter);
|
||||||
|
lcd_goto(0x40); // Got to the beginning of the next line
|
||||||
|
for (int i = 0; i < 6; i++) {
|
||||||
|
if (!strncmp_P(&letter, (PGM_P)pgm_read_word(&months[i]), 1)) {
|
||||||
|
fprintf_P(stdout, (PGM_P)pgm_read_word(&months[i]));
|
||||||
|
fputc('\n', stdout);
|
||||||
|
lcd_puts_P((PGM_P)pgm_read_word(&months[i]));
|
||||||
|
lcd_putc(' ');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// this is fine because even when the hd44780 address counter goes over 0xf4
|
||||||
|
// we still have quite a few addresses left until address counter overflow
|
||||||
|
// and we also dont care about the data that is at the end of the ddram
|
||||||
|
lcd_puts_P(PSTR(" ")); // Clear the end of the line
|
||||||
|
fprintf_P(stdout, PSTR(GET_MONTH_MSG));
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline void heartbeat (void)
|
||||||
|
{
|
||||||
|
static time_t time_prev;
|
||||||
|
time_t time_cur = time(NULL);
|
||||||
|
if (time_cur <= time_prev) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
time_prev = time_cur;
|
||||||
|
fprintf_P(stderr, PSTR(UPTIME_MSG "\n"), time_cur);
|
||||||
|
LED_TOGGLE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main (void)
|
||||||
|
{
|
||||||
|
init_hw();
|
||||||
|
start_ui();
|
||||||
|
|
||||||
|
while (1) {
|
||||||
|
heartbeat();
|
||||||
|
if (uart0_available()) {
|
||||||
|
search_month();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// System clock
|
||||||
|
ISR(TIMER5_COMPA_vect)
|
||||||
|
{
|
||||||
|
system_tick();
|
||||||
|
}
|
||||||
|
33
src/print_helper.c
Normal file
33
src/print_helper.c
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <avr/pgmspace.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++) {
|
||||||
|
unsigned char c = array[i];
|
||||||
|
if (c >= ' ' && c <= '~') {
|
||||||
|
if (!fprintf(stream, "%c", c)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!fprintf(stream, "\"0x%02x\"", c)) {
|
||||||
|
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_ */
|
37
src/uart_wrap.c
Normal file
37
src/uart_wrap.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "../lib/andygock_avr-uart/uart.h"
|
||||||
|
|
||||||
|
int uart0_putc_wrap(char c, FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
uart0_putc_wrap('\r', stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
uart0_putc(c);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart0_getc_wrap(FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
// Probabbly should add some error checking in here but because
|
||||||
|
// this function is only called out when there is at least one character
|
||||||
|
// available in the input buffer (see main.c line 114) then error checking
|
||||||
|
// is not currently necessary.
|
||||||
|
return (char)uart0_getc();
|
||||||
|
}
|
||||||
|
|
||||||
|
int uart3_putc_wrap(char c, FILE *stream)
|
||||||
|
{
|
||||||
|
(void) stream;
|
||||||
|
|
||||||
|
if (c == '\n') {
|
||||||
|
uart3_putc_wrap('\r', stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
uart3_putc(c);
|
||||||
|
return 0;
|
||||||
|
}
|
15
src/uart_wrap.h
Normal file
15
src/uart_wrap.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef _UART_WRAP_H_
|
||||||
|
#define _UART_WRAP_H_
|
||||||
|
|
||||||
|
int uart0_putc_wrap(char c, FILE *stream);
|
||||||
|
int uart0_getc_wrap(FILE *stream);
|
||||||
|
|
||||||
|
int uart3_putc_wrap(char c, FILE *stream);
|
||||||
|
|
||||||
|
|
||||||
|
/* http://www.ermicro.com/blog/?p=325 */
|
||||||
|
|
||||||
|
FILE uart0_io = FDEV_SETUP_STREAM(uart0_putc_wrap, uart0_getc_wrap, _FDEV_SETUP_RW);
|
||||||
|
FILE uart3_out = FDEV_SETUP_STREAM(uart3_putc_wrap, NULL, _FDEV_SETUP_WRITE);
|
||||||
|
|
||||||
|
#endif /* _UART_WRAP_H_ */
|
@ -13,15 +13,13 @@ then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#
|
#
|
||||||
for FILE in "$@"
|
for FILE in "$@"
|
||||||
do
|
do
|
||||||
RESULT="$(astyle --style=1tbs \
|
RESULT="$(astyle --style=1tbs \
|
||||||
--indent-col1-comments \
|
--indent-col1-comments \
|
||||||
--break-blocks \
|
|
||||||
--pad-oper \
|
--pad-oper \
|
||||||
--pad-header \
|
--pad-header \
|
||||||
--delete-empty-lines \
|
|
||||||
--add-brackets \
|
--add-brackets \
|
||||||
--convert-tabs \
|
--convert-tabs \
|
||||||
--max-code-length=80 \
|
--max-code-length=80 \
|
||||||
|
Reference in New Issue
Block a user