Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
demo.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: demo.c
3  Dependencies: See INCLUDES section
4  Hardware: Aguijón rev3.0
5  Complier: Microchip XC16(for PIC24), C30(for PIC24)
6  Company: Vinagrón Digital
7 
8  Software License Agreement:
9 
10  blah blah blah
11 
12 ********************************************************************
13  File Description:
14  Includes demonstration functions.
15 
16 
17  Change History:
18  Rev Description
19  ---- -----------------------------------------
20  1.0 Initial release
21 ********************************************************************/
22 
23 #include <p24FJ128GB106.h>
24 #include "GenericTypeDefs.h"
25 #include "definitions.h"
26 #include "inputs.h"
27 #include "outputs.h"
28 #include "LCD.h"
29 #include "init.h"
30 #include "analog.h"
31 #include "EEPROM.h"
32 #include "demo.h"
33 
34 BOOL toggle = TRUE;
35 BOOL toggle2 = TRUE;
36 unsigned char backlight_index=4;
37 
38 
39 unsigned char message[] = " ";
40 
41 extern unsigned char minutes;
42 extern unsigned char seconds;
43 
44 typedef enum __MAIN_STATES{
45  TIME,
49 } MAIN_STATES;
50 
51 static MAIN_STATES demo_state = 0;
52 
53 
54 /******** Private function protorypes ************/
55 void TimeDemo(void);
56 void InputDemo(void);
57 void OptoDemo(void);
58 void EEPROM_demo(void);
59 /********* Ends prototypes ***********************/
60 
61 static unsigned char keyboard = 0;
62 
74 void keyPressHandler(void)
75 {
76  char i;
77 
78  keyboard = readSW();
79 
80  //prender los cuatro relays cuando el SW1 sea presionado.
81  if(keyboard == 1 && demo_state != EEPROM){
82  if(toggle){
83  for(i=0 ; i<4 ; i++){
84  delayms(150);
85  RLYport(i+1,ON);
86  }
87  toggle = FALSE;
88  }else{
89  for(i=0 ; i<4 ; i++){
90  delayms(150);
91  RLYport(i+1,OFF);
92  }
93  toggle = TRUE;
94  }
95  }
96 
97  //cada vez que el SW2 sea presionado, cambiar el brillo del backlight
98  if(keyboard == 2){
99  delayms(200);
100  buzzer(400,50);
101  switch(backlight_index){
102  case 1: backlight(BLI1); backlight_index++; break;
103  case 2: backlight(BLI2); backlight_index++; break;
104  case 3: backlight(BLI3); backlight_index++; break;
105  case 4: backlight(BLI4); backlight_index++; break;
106  case 5: backlight(0); backlight_index=1; break;
107  }
108  delayms(200);
109  Buzzer_OFF();
110  }
111 
112  //prender todo el OCport con una secuencia LSB -> MSB
113  if(keyboard == 3){
114  buzzer(450,50);
115  delayms(300);
116  Buzzer_OFF();
117  if(toggle2){
118  for(i=0 ; i<4 ; i++){
119  delayms(150);
120  OCport(i+1,ON);
121  }
122  toggle2 = FALSE;
123  }else{
124  for(i=0 ; i<4 ; i++){
125  delayms(150);
126  OCport(4-i,OFF);
127  }
128  toggle2 = TRUE;
129  }
130  }
131 
132  //cambiar de estado y, hacer un beep
133  if(keyboard == 4){
134  buzzer(500,50);
135  delayms(400);
136  Buzzer_OFF();
137 
138  if(demo_state < STATE_COUNT){ demo_state++; }
139  else { demo_state = 0;}
140 
141  }
142 }
143 
155 unsigned char get_state(void)
156 {
157  return demo_state;
158 }
159 
171 void set_state(unsigned char value)
172 {
173  if(value <= STATE_COUNT){
174  demo_state = value;
175  }else{
176  demo_state = 1;
177  }
178 }
179 
191 void DemoMenu(void) //main state machine for main program demo
192 {
193  switch(demo_state){
194  case TIME: TimeDemo(); break;
195  case INPUT: InputDemo(); break;
196  case OPTO: OptoDemo(); break;
197  case EEPROM: EEPROM_demo(); break;
198  }
199 }
200 
212 void TimeDemo(void)
213 {
214  unsigned char msg1[] = {" 00:00 "};
215  unsigned char msg2[] = {" Realtime Clock "};
216 
217  msg1[7] = (minutes / 10) | 0x30; //obtener minutos y convertirlos a ASCII
218  msg1[8] = (minutes % 10) | 0x30;
219 
220  msg1[10] = (seconds / 10) | 0x30; //igual con los segundos
221  msg1[11] = (seconds % 10) | 0x30;
222 
223  DisplayString(1,0,msg1,FALSE);
224  DisplayString(2,0,msg2,FALSE);
225 }
226 
238 void InputDemo(void) //escribe en la LCD al valor ADC y del DIPSW
239 {
240  unsigned char msg1[] = {" ->DIP SW "};
241  unsigned char msg2[] = {" ->ADC "};
242  unsigned char value = 0;
243  unsigned int adcval = 0;
244 
245  value = readDIP(); //leer el DIP switch
246  adcval = readPOT(); //leer la entrada AN1
247 
248 
249 
250  DisplayString(1,0,msg1,FALSE);
251  DisplayString(2,0,msg2,FALSE);
252 
253  LATE = value; //asignar el valor de los leds al del dip
254  LATE ^= 0xFF;
255 
256  if(value > 0){
257  itoa1(value,message,16); //convertir el valor a ASCII y en hexadecimal
259  }
260 
261  if(adcval > 0){
262  itoa1(adcval,message,16);
264  }
265 }
266 
278 void OptoDemo(void) //escribe en la LCD cual entrada se activo
279 {
280  unsigned char msg1[] = {" Opto IN1 IN2 "};
281  unsigned char msg2[] = {"Acopladas IN3 IN4 "};
282 
283  if(PORTE != 0){
284  LATE = 0x00FF; //apagar todos los leds
285  }
286 
287  if(!OPTO1){
288  msg1[10] = '['; //ponerle [ ] para señalar cual se activo
289  msg1[14] = ']';
290  }
291  if(!OPTO2){
292  msg1[15] = '[';
293  msg1[19] = ']';
294  }
295  if(!OPTO3){
296  msg2[10] = '[';
297  msg2[14] = ']';
298  }
299  if(!OPTO4){
300  msg2[15] = '[';
301  msg2[19] = ']';
302  }
303 
304  DisplayString(1,0,msg1,FALSE);
305  DisplayString(2,0,msg2,FALSE);
306 }
307 
319 void EEPROM_demo(void)
320 {
321  int i2c = 0;
322  BOOL test_done = FALSE;
323  unsigned char temp_tecla;
324 
325  unsigned char msg1[] = {"Test EEPROM -> SW1 "};
326  unsigned char msg2[] = {"Press SW1 "};
327 
328  unsigned char msgTEST[] = {"Testing EEPROM... "};
329  unsigned char msgOK[] = {"No R/W errors "};
330  unsigned char msgBAD[] = {" R/W errors "};
331 
332  DisplayString(1,0,msg1,FALSE);
333 
334  if(read_EEPROM(0x22) == 0xCC){
335  DisplayString(2,0,msgOK,FALSE);
336  }else if(!test_done){
337  DisplayString(2,0,msg2,FALSE);
338  }
339 
340  if(!SW1){
341  DisplayString(2,0,msgTEST,FALSE);
342  delayms(1000);
343 
344  i2c = test_EEPROM();
345 
346  if(i2c > 0){
347  itoa1(i2c, message, 10);
348  DisplayString(2,0,msgBAD,FALSE);
350  while(!write_EEPROM(0x22, 0xAA));
351  }else{
352  DisplayString(2,0,msgOK,FALSE);
353  while(!write_EEPROM(0x22, 0xCC));
354  }
355  test_done = TRUE;
356  }
357 }