LTlib LaurTec Library
4.0.1
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
Main Page
Data Structures
Files
File List
Globals
All
Data Structures
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Macros
main.c
Go to the documentation of this file.
1
#include <xc.h>
2
3
#include "
LTlib.h
"
4
5
#include "
module_IO.h
"
6
#include "
module_IO.c
"
7
8
#include "
module_I2C.h
"
9
#include "
module_I2C.c
"
10
11
#include "
delay.h
"
12
#include "
delay.c
"
13
14
#include "
MCP7940.h
"
15
#include "
MCP7940.c
"
16
17
#include "
MCP2300x.h
"
18
#include "
MCP2300x.c
"
19
20
#include "
LCD_44780_I2C.h
"
21
#include "
LCD_44780_I2C.c
"
22
23
//******************************************
24
// Program Definitions
25
//******************************************
26
27
#define BT1 0b11100000
28
#define BT2 0b11010000
29
#define BT3 0b10110000
30
#define BT4 0b01110000
31
32
#define BEEP IO_BIT0
33
34
#define STATE_CHANGE_DONE 0x00
35
#define STATE_CHANGE_MINUTES 0x01
36
#define STATE_CHANGE_HOURS 0x02
37
#define STATE_CHANGE_YEARS 0x03
38
#define STATE_CHANGE_MONTHS 0x04
39
#define STATE_CHANGE_DAYS 0x05
40
41
volatile
unsigned
char
clock_state
=
STATE_CHANGE_DONE
;
42
43
44
//******************************************
45
// Interrupt Handling
46
//******************************************
47
48
__interrupt
(high_priority) void ISR_alta (
void
) {
49
50
unsigned
char
button;
51
52
// Check the interrupt source
53
if
(INTCONbits.RBIF == 1 ) {
54
55
INTCONbits.RBIF = 0;
56
57
button = PORTB;
58
button = button & 0xF0;
59
60
// Check the button that got pressed
61
switch
(button) {
62
63
case
BT1
&
BT3
:
64
clock_state
=
STATE_CHANGE_MINUTES
;
65
break
;
66
67
case
BT1
&
BT4
:
68
clock_state
=
STATE_CHANGE_HOURS
;
69
break
;
70
case
BT2
:
71
clock_state
=
STATE_CHANGE_YEARS
;
72
break
;
73
case
BT3
:
74
clock_state
=
STATE_CHANGE_MONTHS
;
75
break
;
76
case
BT4
:
77
clock_state
=
STATE_CHANGE_DAYS
;
78
break
;
79
}
80
}
81
}
82
83
84
//******************************************
85
// Main Program
86
//******************************************
87
int
main
(
void
) {
88
89
unsigned
char
button_still_pressed;
90
91
IO_set_all_ports_as_inputs
();
92
93
IO_set_port_direction
(
IO_PORTB
,
IO_ALL_PORT_INPUT
);
94
IO_set_port_direction
(
IO_PORTC
, 0xFE);
95
96
IO_enable_pull_up_resistors
(
IO_PORTB
, 0xF0);
97
98
// RTCC initialization at 100KHz
99
RTCC_initialize
(100);
100
101
// Small delay to enable 32KHz crystal to get stabilized
102
delay_ms
(500);
103
104
LCD_initialize
(20);
105
LCD_backlight
(
LCD_TURN_ON_LED
);
106
107
// Initialize date
108
RTCC_set_days
(0x30);
109
RTCC_set_months
(0x12);
110
RTCC_set_years
(0x08);
111
112
// Initialize Time
113
RTCC_set_hours
(0x02);
114
RTCC_set_minutes
(0x56);
115
RTCC_set_seconds
(0x33);
116
117
118
// Clear Interrupt Flag
119
INTCONbits.RBIF = 0;
120
121
// Interrupt on PORTB
122
IOCB = 0xF0;
123
INTCONbits.RBIE = 1;
124
125
// Compatible Interrupt Mode
126
RCONbits.IPEN = 0;
127
128
// Global Interrupt
129
INTCONbits.GIE = 1;
130
131
// Peripheral Interrupt
132
INTCONbits.PEIE = 1 ;
133
134
135
while
(1) {
136
137
LCD_write_message
(
"Time : "
);
138
139
LCD_write_string
(
RTCC_get_time_seconds
());
140
141
LCD_goto_line
(2);
142
143
LCD_write_message
(
"Date : "
);
144
145
LCD_write_string
(
RTCC_get_date
());
146
147
LCD_home
();
148
149
if
(
clock_state
!=
STATE_CHANGE_DONE
){
150
151
//Anti Spikes filter
152
delay_ms
(30);
153
button_still_pressed = PORTB & 0xF0;
154
155
//Check if any button is still pressed
156
if
(button_still_pressed != 0xE0){
157
158
IO_write_port
(
IO_PORTC
, 0x01);
159
delay_ms
(100);
160
IO_write_port
(
IO_PORTC
, 0x00);
161
162
//check which button was pressed
163
switch
(
clock_state
) {
164
165
case
STATE_CHANGE_MINUTES
:
RTCC_increment_minutes
();
166
break
;
167
168
case
STATE_CHANGE_HOURS
:
RTCC_increment_hours
();
169
break
;
170
case
STATE_CHANGE_YEARS
:
RTCC_increment_years
();
171
break
;
172
173
case
STATE_CHANGE_MONTHS
:
RTCC_increment_months
();
174
break
;
175
176
case
STATE_CHANGE_DAYS
:
RTCC_increment_days
();
177
break
;
178
}
179
180
}
181
182
clock_state
=
STATE_CHANGE_DONE
;
183
184
}
185
}
186
}
LTlib_v_4.0.1
ex
PIC18
MCP7940
03 - MCP7940_Clock_with_time_and_date_regulations.X
main.c
Generated on Sat Mar 12 2016 11:01:40 for LTlib LaurTec Library by
1.8.3.1