Aguijón Software Libraries  1.0
Documentation for the included Libraries
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
init.c
Go to the documentation of this file.
1 /********************************************************************
2  FileName: init.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 initialization functions.
15 
16 
17  Change History:
18  Rev Description
19  ---- -----------------------------------------
20  1.0 Initial release
21 ********************************************************************/
22 
23 #include <p24FJ128GB106.h>
24 #include "i2c.h"
25 #include "definitions.h"
26 #include "init.h"
27 
39 void configTimer1(void)
40 {
41  T1CON = 0x00; //Stops the Timer1 and reset control reg.
42  TMR1 = 0x00; //Clear contents of the timer register
43  PR1 = 0x1F6F; //Load the Period register with 1mSec preset
44  IPC0bits.T1IP = 0x01; //Setup Timer2 interrupt for desired priority level
45 
46  IFS0bits.T1IF = 0; //Clear the Timer1 interrupt status flag
47  IEC0bits.T1IE = 1; //Enable Timer1 interrupts
48  T1CONbits.TON = 1; //Start Timer1 with prescaler settings at 1:1 and
49  //clock source set to the internal instruction cycle
50 }
51 
52 #ifdef I2C_HW //estamos usando I2C por hardware?
53 //I2C por hardware
54 
55 
67 void InitI2C(void)
68 {
69  unsigned int config2, config1;
70 
71  SCL_TRIS = 0;
72  SDA_TRIS = 0;
73  RST_TRIS = 0;
74 
75 
76  /* Baud rate is set for 100 Khz */
77  config2 = 0x27;
78  /* Configure I2C for 7 bit address mode */
79  config1 = (I2C_ON & I2C_IDLE_CON & I2C_CLK_HLD & I2C_IPMI_DIS & I2C_7BIT_ADD
80  & I2C_SLW_DIS & I2C_SM_DIS & I2C_GCALL_DIS & I2C_STR_DIS & I2C_NACK
81  & I2C_ACK_DIS & I2C_RCV_DIS & I2C_STOP_DIS & I2C_RESTART_DIS & I2C_START_DIS);
82 
83  OpenI2C1(config1,config2);
84  IdleI2C1();
85 }
86 
98 void I2C_Start(void)
99 {
100  StartI2C1(); \
101  while(I2C1CONbits.SEN); /* Wait till start sequence is completed */
102 }
103 
115 void I2C_Stop(void)
116 {
117  StopI2C1();
118  while(I2C1CONbits.PEN); /* Wait till stop sequence is completed */
119 }
120 
132 void I2CsendByte(unsigned char j)
133 {
134  MasterWriteI2C1(j); // Write Slave address and set master for transmission
135  /* Wait till address is transmitted */
136  while(I2C1STATbits.TBF); // 8 clock cycles
137  while(!IFS1bits.MI2C1IF); // Wait for 9th clock cycle
138  IFS1bits.MI2C1IF = 0; // Clear interrupt flag
139 }
140 
152 unsigned char I2C_getByte(char do_ack)
153 {
154  unsigned char data=0;
155 
156  I2C1CONbits.RCEN = 1;
157  while(I2C1CONbits.RCEN);
158  I2C1CONbits.RCEN = 0;
159  I2C1STATbits.I2COV = 0;
160  data = I2C1RCV;
161 
162  if(do_ack){
163  AckI2C1();
164  while(I2C1CONbits.ACKEN);
165  }
166  return data;
167 }
168 
169 #else //si no, hacerlo por software
170 
171 //I2C por software
172 
173 void I2C_Stop(void)
174 {
175  SDA=0;
176  SCL=0;
177  SCL=1;
178  SDA=1;
179 }
180 
181 void I2C_Start(void)
182 {
183  SCL=1;
184  SDA=1;
185  SDA=0;
186  SCL=0;
187 }
188 
189 void I2CsendByte(unsigned char j) //I2C Output
190 {
191  int n;
192  volatile char SDA_VAR;// = 1;
193  unsigned char d;
194  int retries=200;
195 
196 
197  d=j;
198  for(n=0;n<8;n++){
199  if((d&0x80)==0x80)
200  SDA=1;
201  else
202  SDA=0;
203  d=(d<<1);
204  SCL = 0;
205  SCL = 1;
206  SCL = 0;
207  }
208 
209  SDA_TRIS = 1;
210 
211  SDA_VAR = SDA_READ;
212  SCL = 1;
213  while(SDA_VAR==1 && retries){
214  SDA_VAR = SDA_READ;
215  SCL=0;
216  SCL=1;
217  retries--;
218  }
219 
220  SDA_TRIS = 0;
221  SCL=0;
222 }
223 #endif
224 
236 void LCDinit(void)
237 {
238  RST = 1;
239 
240  LCDDelayms(80);
241 
242  I2C_Start();
243  I2CsendByte(0x78);//Slave=0x78
244  I2CsendByte(0x00);//Comsend = 0x00
245  I2CsendByte(0x38);
246  LCDDelayms(10);
247  I2CsendByte(0x39);
248  LCDDelayms(10);
249  I2CsendByte(0x14);
250  I2CsendByte(0x70);
251  I2CsendByte(0x5E);
252  I2CsendByte(0x6D);
253  I2CsendByte(0x0C);
254  I2CsendByte(0x01);
255  I2CsendByte(0x06);
256  LCDDelayms(10);
257  I2C_Stop();
258 }
259 
271 void LCDinit2(void)
272 {
273  RST = 1;
274 
275  LCDDelayms(10);
276 
277  I2C_Start();
278  I2CsendByte(0x78);//Slave=0x78
279  I2CsendByte(0x00);//Comsend = 0x00
280  I2CsendByte(0x34);
281  LCDDelayms(10);
282  I2CsendByte(0x34);
283  LCDDelayms(10);
284  I2CsendByte(0x14);
285  I2CsendByte(0x70);
286  I2CsendByte(0x5E);
287  I2CsendByte(0x6D);
288 
289  LCDDelayms(10);
290  I2C_Stop();
291 }