LTlib LaurTec Library  4.0.3
Open Source C Library for Microchip Microcontrollers based on XC8 Compiler
 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 "LTlib_delay.h"
12 #include "LTlib_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:
65  break;
66 
67  case BT1 & BT4:
69  break;
70  case BT2:
72  break;
73  case BT3:
75  break;
76  case BT4:
78  break;
79  }
80  }
81 }
82 
83 
84 //******************************************
85 // Main Program
86 //******************************************
87 int main(void) {
88 
89  unsigned char button_still_pressed;
90 
92 
95 
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);
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 
140 
141  LCD_goto_line(2);
142 
143  LCD_write_message ("Date : ");
144 
146 
147  LCD_home ();
148 
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 
166  break;
167 
169  break;
171  break;
172 
174  break;
175 
177  break;
178  }
179 
180  }
181 
183 
184  }
185  }
186 }