Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
main.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: main.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 http://www.vinagrondigital.com
24 ********************************************************************/
25 
26 
27 #include <p24FJ128GB106.h> //PIC24 header
28 #include "EEPROM.h" //EEPROM library
29 #include "analog.h" //analog library
30 #include "LCD.h" //LCD library
31 #include "outputs.h" //output library
32 #include "inputs.h" //input library
33 #include "serial.h" //RS232 Library
34 #include "PPS.h" //remappable pins
35 #include "realtime.h" //RTCC
36 #include "outcompare.h" //PWM
37 #include "init.h" //init library
38 #include "tester.h" //RS232 Tester
39 #include "demo.h" //Demo Code
40 #include "definitions.h" //Global definitions
41 
42 
43 
44 BOOL TESTMODE = FALSE;
46 
47 extern unsigned char cmdBuffer[];
48 unsigned char string[20] = " ";
49 
50 unsigned char minutes = 0;
51 unsigned char seconds = 0;
52 unsigned char sweeppos = 0;
53 
54 
55 unsigned char testmsg1[] = {" test mode 1.0 "};
56 unsigned char testmsg2[] = {"Waiting for COMMands"};
57 
58 
59 
60 _CONFIG1(JTAGEN_OFF & FWDTEN_OFF & ICS_PGx2)
61 _CONFIG2(FNOSC_PRIPLL & POSCMOD_HS& PLL_96MHZ_ON & PLLDIV_DIV2 & FCKSM_CSECMD)
62 
63 
74 void Aguijon3Init(void)
75 {
76  //initialize ext crystal
77  CLKDIVbits.DOZEN = 1;
78  CLKDIVbits.DOZE0 = 0;
79  CLKDIVbits.DOZE1 = 1;
80  CLKDIVbits.DOZE2 = 0;
81 
82  CLKDIVbits.CPDIV0 = 1;
83  CLKDIVbits.CPDIV1 = 0;
84  //end of OSC config
85 
86  //initialize I/O ports
87  TRISB = 0xBDCF; //configure PORTB
88  LATB = 0x0000;
89 
90  TRISD = 0x0000; //configure PORTD as outputs
91  LATD = 0x0000;
92 
93  TRISE = 0x0000; //configure PORTE as outputs
94  LATE = 0x0000;
95 
96  TRISG = 0x0000; //configure PORTG as outputs
97  LATG = 0x0000;
98 
99  TRISF = 0x0000; //configure PORTF as outputs
100  LATF = 0x0000;
101  //done
102 
103  //initialize ADC(CH15 POT) & display
104  initADC();
105 
106  #ifdef I2C_HW
107  InitI2C();
108  #endif
109  LCDinit();
110  //done
111 
112  //disable DIP Switch
113  DIPconfig(DDIS);
114 
115 
116  PPSUnLock;
117  //initialize pins for RS232 Rx & Tx
118  iPPSInput(IN_FN_PPS_U2RX,IN_PIN_PPS_RP8); //Assing U2RX to pin RP8
119  iPPSOutput(OUT_PIN_PPS_RP9,OUT_FN_PPS_U2TX); //Assing U2TX to pin RP9
120  initUARTcommunication(); //init RS232C driver
121  //done
122 
123  //initialize PWM for buzzer & display
124  PPSOutput(PPS_RP21, PPS_OC1); //RP21 selected as OC1 output pin for backlight
125  PPSOutput(PPS_RP22, PPS_OC2); //RP26 selectes as OC2 output pin for buzzer
126  CloseOC1();
127  CloseOC2();
128  ConfigIntOC1(OC_INT_OFF);
129  ConfigIntOC2(OC_INT_OFF);
130  PPSLock;
131  //done?
132 
133  backlight(BLI3); //prender el backlight en la intensidad mas baja
134  LCDIntro(); //intro con el LCD
135 
136  LCDinit2(); //letras grandes en el display
137  LATE = 0x00FF; //apagar el puerto de leds
138 
139  configTimer1(); //prender el timer 1
140  initRTCC(); //prender el RTCC
141 
142 }
143 
155 void _ISR _T1Interrupt(void)
156 {
157  static unsigned int t1subsec = 0;
158 
159  t1subsec++;
160 
161  if(t1subsec == 99){ //cada 100 ms
163  t1subsec = 0;
164 
165  if(get_state() == 0){
166  //secuencia de los leds
167  if(sweeppos < 7){ sweeppos++; }
168  else{ sweeppos = 0; }
169 
170  LATE = (0x0080>>sweeppos);
171  LATE ^= 0xFF;
172  }
173 
174  }
175 
176  IFS0bits.T1IF = 0; //Reset Timer1 interrupt flag and Return from ISR
177 }
178 
190 void _ISR _RTCCInterrupt (void)
191 {
192  if(seconds<59){
193  seconds++;
194  }else{ //the 60th second
195  seconds=0;
196  if(minutes<99){ //the 100th second
197  minutes++;
198  }else{
199  minutes = 0;
200  }
201  }
202 
203  mRtcc_Clear_Intr_Status_Bit;//clear the interrupt status
204 }
205 
217 int main(void)
218 {
219  Aguijon3Init(); //inicializa el board
220 
221  if(readDIP() == 0x81){ //entrar al modo prueba del board
222  TESTMODE = TRUE;
223  T1CON = 0x00; //Stops the Timer1 and reset control reg.
224  TMR1 = 0x00; //Clear contents of the timer register+
227  WriteUART2(0xFF); //un FF para indicar que estamos listos
228  }
229 
230  while(TESTMODE){ //test mode 1.0
231  /* recibe comandos y los interpreta
232  * para probar los modulos de la
233  * tablilla. los comandos son recibidos
234  * por RS232.
235  */
236  switch(cmdBuffer[0]){
237  case RELAY: RLY_serial(); break;
238  case OCOUT: OC_serial(); break;
239  case LEDPT: LEDPort_serial(); break;
240  case BUZZ: /*unimplemented*/ break;
241  case OII: /*unimplemented*/ break;
242  case DIPSW: /*unimplemented*/ break;
243  case PT10K: /*unimplemented*/ break;
244  }
245  }
246 
247  for(;;)
248  {
249  //escribir la LCD cada vez que refresh_display sea TRUE
250  if(refresh_display){
251  DemoMenu();
252  refresh_display = FALSE; //el timer se encarga de lo demas
253  }
254 
255  keyPressHandler();
256  }
257 }