-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
132 lines (109 loc) · 2.88 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/**
* Driver library for HD44780-compatible LCD displays.
* https://github.com/armandas/Yet-Another-LCD-Library
*
* Author: Armandas Jarusauskas (http://projects.armandas.lt)
*
* Contributors:
* Sylvain Prat (https://github.com/sprat)
*
* This work is licensed under a
* Creative Commons Attribution 3.0 Unported License.
* http://creativecommons.org/licenses/by/3.0/
*
*/
#include <p18cxxx.h>
#include <stdio.h>
#include <delays.h>
#include "HD44780.h"
#define MENU_LENGTH 5
#pragma config WDT = OFF
#pragma config PLLDIV = 4
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HSPLL_HS
#pragma config BOR = OFF
#pragma config PBADEN = OFF
#pragma config PWRT = ON
#pragma config MCLRE = ON
#pragma config XINST = OFF
#pragma code
/******************************************************************************/
unsigned char menu(void);
const rom char * menu_items[MENU_LENGTH] = {
/* v----LENGTH-----v */
"Start Process ",
"Info ",
"Options ",
"About ",
"RTFM "
};
void main(void)
{
char buffer[LCD_CHARACTERS];
char item;
unsigned char degree[8] = {
0b01100,
0b10010,
0b10010,
0b01100,
0b00000,
0b00000,
0b00000,
0b00000
};
unsigned char arrow[8] = {
0b00000,
0b01000,
0b01100,
0b01110,
0b01100,
0b01000,
0b00000,
0b00000
};
// set RE2 as digital I/O
ADCON1 = 0x08;
// set ports as outputs
TRISC = 0x00;
LCD_DATA_DDR = 0x00;
// tactile switch
TRISE = 0x04;
lcd_initialize();
lcd_add_character(1, degree);
lcd_add_character(2, arrow);
while (1) {
item = menu();
sprintf(buffer, "Selected item: %d", item);
lcd_goto(2, 1);
lcd_write(buffer);
while (PORTEbits.RE2 == 1);
Delay10KTCYx(255);
}
}
unsigned char menu(void)
{
static unsigned char position = 0;
unsigned char item = 0;
unsigned char i;
if (position == MENU_LENGTH)
// reset position if the end of menu is reached
position = 0;
for (i = 0; i < LCD_LINES; i++) {
// set the line for item display
lcd_goto(i + 1, 1);
// enable continuous rotation of items
// i.e. item 1 follows the last item in the list
item = (position + i > MENU_LENGTH - 1) ? 0 : position + i;
if (i == 0)
// add an arrow to indicate current menu item
lcd_write_pgm("\002");
else
// add a space to keep items aligned
lcd_write_pgm(" ");
// display menu item
lcd_write_pgm(menu_items[item]);
}
// return current position and increment counter for the next call
return position++;
}