Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 /* Name : RTC1.c * Purpose : RTC based 3 phase motor on off controller * Date : 31/10/2014. */ #include <stdio.h> #include <string.h> #include <conio.h> #include <stdlib.h> #include <xc.h> #include <htc.h> #include "p18f46k22.h" // Pin assignments #define LCDRW LATDbits.LATD5 /* PORT for RW */ #define TRISD_RW TRISDbits.TRISD5 /* TRIS for RW */ #define LCDRS LATDbits.LATD4 /* PORT for RS */ #define TRISD_RS TRISDbits.TRISD4 /* TRIS for RS */ #define LCDE LATDbits.LATD6 /* PORT for enable */ #define TRISD_E TRISDbits.TRISD6 #define TRISC_GREEN_LED TRISCbits.TRISC0 #define GREEN_LED PORTCbits.RC0 #define TRISD_GREEN_LED1 TRISDbits.TRISD0 #define GREEN_LED1 PORTDbits.RD0 #define CH_P1ACVTG 0X59 // AN22 // RD2 #define CH_P2ACVTG 0X5D // AN23 //RD3 #define CH_P3ACVTG 0X41 // AN16 //RC4 //************DEF for RTC Program starts// #define RTC 0XD0 //************DEF for RTC Program ends// // fn delarations void nop(void); void delay(unsigned char); void fetchstring(const char *); void writestring (unsigned char); void commandLCD(char); void welcomemsg(void); void ownermsg(void); void initialise(void); void init_lcd(void); void I2C_INITIALIZE(void); void I2C_WRITE_WRITE(unsigned char, unsigned char, unsigned char); unsigned char I2C_READ_DATA(unsigned char, unsigned char); //int AVG_ADC(int ADCVALUE); // variable declarations unsigned char sec,min,hour,dd,mm,yy; int DATA_RTC,hexval,hexval1; unsigned char tempregaddr,tempcmd; int MUKESH1,MUKESH2,MUKESH3,MUKESH11,MUKESH22,MUKESH33;//td char temp,temp1,temp2,temp3,Byte,two_d_flag = 0,n=0; char lsb1,msb1,mid1,lsb2,msb2,mid2,lsb3,msb3,mid3; char TEMP_CHSEL; char ACAVGBIT; int PASS_ADCVALUE,ADCVALUE,ADCVALUE1,ADCVALUE2,ADCVALUE3,ADCVALUE4,lsb,msb,mid; int quotient,reminder; int lsbbyte_hexno; int DIVI,QUOT,ACVG,ACVG1,ACVGTEMP,NEWACVGTEMP; int temp_adc1,temp_adc2,temp_adc3=0; int ADRES10BIT,ADRES10BIT1,ADRES10BIT2; int SHIFTED_L8_ADRESH; int AVERAGE1=0,AVERAGE2=0,AVERAGE3=0,AVERAGE4=0,AVERAGE5=0,AVERAGE6=0,ADC_TEMP=0; unsigned char s1[] = "TANKERPACIFIC"; unsigned char s2[] = " "; unsigned char s3[]= " "; unsigned char owner[]= "RTC BASED MOTOR "; unsigned char alliswell[]= " CONTROLLER "; unsigned char agrileaders[]= " CUM 24 HR CLK "; unsigned char technicalfarms[]="PROJECT FOR "; unsigned char MOBILE[]="MOB : "; unsigned char allphok[]= "ALL PHASES R OK "; unsigned char enjoy[]= " ENJOY!!! "; unsigned char phase1vtg[]= "PHASE 1 = VAC"; unsigned char phase2vtg[]= "PHASE 2 = VAC"; unsigned char phase3vtg[]= "PHASE 3 = VAC"; unsigned char freq[]= "FREQUENCY = Hz"; unsigned char p1p2p3vtg[]= "P1 P2 P3 VAC"; unsigned char phase1error[]= "FAULT P1 LOW VTG"; unsigned char phase2error[]= "FAULT P2 LOW VTG"; unsigned char phase3error[]= "FAULT P3 LOW VTG"; unsigned char ph1highfault[]= "FAULT P1 HI VTG "; unsigned char ph2highfault[]= "FAULT P2 HI VTG "; unsigned char ph3highfault[]= "FAULT P3 HI VTG "; unsigned char p1p2p3vtgON[]= "P1 P2 P3 ON"; unsigned char p1p2p3vtgOFF[]= "P1 P2 P3 0FF"; unsigned char VAC[]= "V"; // fn definations void initialise(void) { TRISB = 0X00; //LCD DATA PORT : PORT B AS AN O/P PORT TRISD_RW = 0; TRISD_RS = 0; TRISD_E = 0; TRISC_GREEN_LED = 0; TRISD_GREEN_LED1 = 0; GREEN_LED = 0; GREEN_LED1 = 0; PORTB = 0X00; OSCTUNE = 0X80; OSCCON = 0X76; // 66:- 8 Mhz,76:- 16Mhz INTERNAL OSCILLATOR : CONFIG WORD VALUE - 29 OSCCON2 = 0X00; INTCONbits.PEIE = 0; INTCONbits.GIE = 0; } /************delay***************/ void nop(void) { CLRWDT(); unsigned int i=0; for(;i<100;i++); } void delay(unsigned char i) { for(;i!=0;i--) { nop(); nop(); nop(); } } void init_lcd(void) { CLRWDT(); unsigned char a[5]={0x38,0x0c,0x01,0x06,0x80};//8 bit, sh cur,no bl, cur off,scr on, first line unsigned char i; for(i=0;i<5;i++) { commandLCD(a[i]); delay(40); } } void i2c_initialize() { TRISCbits.TRISC3=1; //SCL TRISCbits.TRISC4=1; //SDA SSP1ADD=0x27;//13;//27; //100Khz @ 16MHz,Clock=Fosc/(4x(SSPADD+1) SSP1STAT=0x80; //Disable Slew Rate SSP1CON1=0x28; //Enable Synchronous Serial Port //I2C Master mode, clock = FOSC / (4 * (SSPADD+1))//21/10/14 by mukesh SSP1CON2=0X00; } void commandLCD(char temp2) { CLRWDT(); TRISD_RW = 0; TRISD_RS = 0; TRISD_E = 0; PORTB = temp2; LCDRS = 0; // cmd mode LCDRW = 0; // LCDRW = 0 : write to LCD LCDE = 1; delay(40); LCDE = 0; } void fetchstring(const char *s) { while(*s) writestring(*s++); } void writestring(unsigned char data) { CLRWDT(); PORTB = data; LCDRS = 1; // char mode LCDRW = 0; // LCDRW = 0 : write to LCD LCDE = 1; delay(40); LCDE = 0; } void welcomemsg(void) { CLRWDT(); commandLCD(0x01); //clear display delay(40); commandLCD(0x80); //1st line delay(40); fetchstring(&s1[0]); delay(500); commandLCD(0xc0); delay(40); fetchstring(&s2[0]); delay(500); } void ownermsg(void) { CLRWDT(); commandLCD(0x01); delay(40); commandLCD(0x80); delay(40); fetchstring(&owner[0]); delay(500); commandLCD(0xc0); delay(40); fetchstring(&alliswell[0]); delay(500); delay(500); delay(500); commandLCD(0xc0); delay(40); fetchstring(&agrileaders[0]); delay(500); delay(500); delay(500); commandLCD(0xc0); delay(40); fetchstring(&technicalfarms[0]); delay(500); delay(500); delay(500); commandLCD(0xc0); delay(40); fetchstring(&MOBILE[0]); delay(500); delay(500); delay(500); } void i2c_start(void) { SSP1CON2bits.SEN = 1;//send a start signal. while(SSP1CON2bits.SEN == 1);//wait to finish // while((SSP1CON2 & 0x1F) | (SSP1STATbits.R_W));//wait until idle while(SSP1CON2bits.RSEN == 1);//wait untill idle while(SSP1CON2bits.PEN == 1);//wait untill idle while(SSP1CON2bits.RCEN == 1);//wait untill idle while(SSP1CON2bits.ACKEN == 1);//wait untill idle while(SSP1STATbits.R_W == 1);//wait untill idle } void i2c_address(unsigned char slaveaddr) { SSP1BUF = slaveaddr; while(SSP1STATbits.BF==1); //wait until all bits are transmitted // while((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle while(SSP1CON2bits.RSEN == 1);//wait untill idle while(SSP1CON2bits.PEN == 1);//wait untill idle while(SSP1CON2bits.RCEN == 1);//wait untill idle while(SSP1CON2bits.ACKEN == 1);//wait untill idle while(SSP1STATbits.R_W == 1);//wait untill idle while(SSP1CON2bits.ACKSTAT==1); } void i2c_write(unsigned char cmd) { tempcmd = cmd; SSP1BUF = tempcmd; while(SSP1STATbits.BF==1); //wait until all bits are transmitted // while((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle while(SSP1CON2bits.RSEN == 1);//wait untill idle while(SSP1CON2bits.PEN == 1);//wait untill idle while(SSP1CON2bits.RCEN == 1);//wait untill idle while(SSP1CON2bits.ACKEN == 1);//wait untill idle while(SSP1STATbits.R_W == 1);//wait untill idle while(SSP1CON2bits.ACKSTAT==1); } void i2c_stop() { SSP1CON2bits.PEN=1; //send STOP signal while(SSP1CON2bits.PEN==1);//wait to finish // while ((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle while(SSP1CON2bits.RSEN == 1);//wait untill idle while(SSP1CON2bits.PEN == 1);//wait untill idle while(SSP1CON2bits.RCEN == 1);//wait untill idle while(SSP1CON2bits.ACKEN == 1);//wait untill idle while(SSP1STATbits.R_W == 1);//wait untill idle } void i2c_restart() { SSP1CON2bits.RSEN=1; //send RESTART signal while(SSP1CON2bits.RSEN==1);//wait to finish // while ((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle while(SSP1CON2bits.PEN == 1);//wait untill idle while(SSP1CON2bits.RCEN == 1);//wait untill idle while(SSP1CON2bits.ACKEN == 1);//wait untill idle while(SSP1STATbits.R_W == 1);//wait untill idle } void i2c_read(unsigned char regaddr) { tempregaddr = regaddr; i2c_start(); i2c_address(0xD0); i2c_write(tempregaddr); i2c_restart(); //led bcomes 0 here if made i2c_address(0xD1); //led bcomes 0 here if made SSP1CON2bits.RCEN=1; //Enables Receive mode for I2C while(SSP1CON2bits.RCEN == 1);//wait untill idle //// while(SSP1STATbits.BF==0); //wait until all bits are received DATA_RTC=SSP1BUF;//copy received data//21/10/14 by mukesh //// while(SSP1STATbits.BF==0); //wait until all bits are received //// DATA_RTC=SSP1BUF;//copy received data//21/10/14 by mukesh ///// GREEN_LED1 = 0; // while ((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle //// while(SSP1CON2bits.RSEN == 1);//wait untill idle //// while(SSP1CON2bits.PEN == 1);//wait untill idle //// while(SSP1CON2bits.RCEN == 1);//wait untill idle //// while(SSP1CON2bits.ACKEN == 1);//wait untill idle //// while(SSP1STATbits.R_W == 1);//wait untill idle SSP1CON2bits.ACKDT=1; //Acknowledge Data bit=1 ie NACK SSP1CON2bits.ACKEN=1;//start NACK signal ///// while(SSP1CON2bits.ACKEN==1);//wait to finish // while ((SSP1CON2 & 0x1F)|(SSP1STATbits.R_W));//wait until idle //// while(SSP1CON2bits.RSEN == 1);//wait untill idle //// while(SSP1CON2bits.PEN == 1);//wait untill idle //// while(SSP1CON2bits.RCEN == 1);//wait untill idle //// while(SSP1CON2bits.ACKEN == 1);//wait untill idle //// while(SSP1STATbits.R_W == 1);//wait untill idle i2c_stop(); } void bcd_hex(unsigned char RTC_DATA) { hexval=((RTC_DATA>>4) & (0x0F)); hexval1=(RTC_DATA & 0x0F); } /************************* MAIN PROGRAM *****************************/ void main(void) { initialise(); init_lcd(); init_lcd(); init_lcd(); init_lcd(); init_lcd(); welcomemsg(); delay(500); delay(500); ownermsg(); delay(500); delay(500); i2c_initialize(); GREEN_LED1 = 1; i2c_start(); i2c_address(0xD0); i2c_write(0x00);//sec reg addr i2c_write(0x00);//sec value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x02);//hr reg addr i2c_write(0x04);//hr value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x01);//min reg addr i2c_write(0x15);//min value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x03);//day reg addr i2c_write(0x01);//day value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x04);//date reg addr i2c_write(0x16);//date value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x05);//mm reg addr i2c_write(0x15);//mm value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x06);//yr reg addr i2c_write(0x89);//yr value i2c_stop(); i2c_start(); i2c_address(0xD0); i2c_write(0x07);//cntrl reg addr i2c_write(0x10);////Disable SQW , select oscc freq=32.768Khz i2c_stop(); delay(500); delay(500); delay(500); delay(500); GREEN_LED1 = 0; delay(500); delay(500); delay(500); delay(500); while(1) { init_lcd(); GREEN_LED1 = 1; i2c_read(0x02); delay(500); delay(500); delay(500); delay(500); // GREEN_LED1 = 0; // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring(hexval); writestring(hexval1); writestring(':'); delay(500); delay(500); delay(500); delay(500); // GREEN_LED1 = 0; i2c_read(0x01); // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring(hexval); writestring(hexval1); writestring(':'); i2c_read(0x00); // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring(hexval); writestring(hexval1); commandLCD(0xC0); //line 2 delay(40); i2c_read(0x04); // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring(hexval); writestring(hexval1); writestring('-'); i2c_read(0x05); // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring('-'); i2c_read(0x06); // bcd_hex(DATA_RTC); // hexval = (hexval+0x30); // hexval1 = (hexval1+0x30); hexval = ((DATA_RTC/10)+0x30); hexval1 = ((DATA_RTC%10)+0x30); writestring('-'); } } void interrupt ISR(void) { }
void writeDS1307()
{
StartI2C1();
WriteI2C1(0xD0);
WriteI2C1(0x00);
WriteI2C1(sec);// sec
WriteI2C1(min);//minutes
WriteI2C1(hr);// hours
WriteI2C1(week_day);//week_day
WriteI2C1(day);//date
WriteI2C1(mn);//month
WriteI2C1(year);//year
WriteI2C1(0x10); // REG 7 - [B]Enable squarewave output pin1hz [/B]
StopI2C1();
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?