PIC basic programming tutorial: In the following PIC tutorial i used books to learn fundamental knowledge about Embedded system. After that i install MikroC compiler and Proteus simulator tool to implement following basic PIC programming tutorial:
Interfacing of External RTC with PIC: In this tutorial External RTC is interfaced with PIC(PIC18F452) controller. You need to enter password "1234#", you can use this tutorial for security checking projects. If you want to learn more kindly see tutorial on youtube as well.
>> External RTC interface with PIC C Code: /*************** RTC TEST.c file***************/ /* lcd data port c control port b rtc portd */
#include<lcd.h> #include<rtc.h>
void main(void) { unsigned char hrs1=17,min1=22,sec1=10,disp[5],set; int i,a,b;
void write_rtc(char dat,char add) // eeprom write function at24c256 eeprom is used and is having 16 bits of address { Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xD0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2 Soft_I2C_Write(dat); // Start from address 2 Soft_I2C_Stop(); // Issue stop signal }
unsigned char read_rtc(char add) { char read; Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xD0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal Soft_I2C_Write(0xD1); // Address PCF8583 for reading R/W=1 read = Soft_I2C_Read(0); // Read seconds byte Soft_I2C_Stop(); // Issue stop signal return read; } void set_time() { set = bcdtime(sec1)&0x7F; write_rtc(set,0);
set = bcdtime(min1)&0x7F; write_rtc(set,1);
set = bcdtime(hrs1)&0x3F; write_rtc(set,2); } void init_rtc() { Soft_I2C_Init(); // initialize I2C communication delay_ms(100); } /*************** lcd.h file***************/ #define LCD PORTC // lcd output data PORT #define RS PORTB.F0 #define E PORTB.F1
void cmd_lcd (unsigned char dat) // function to write command at lcd port { LCD=dat; RS=0; //clear RS (ie. RS=0) to write command E=1; // send H-L pulse at E pin delay_us(300); E=0; delay_us(500); }
void data_lcd (unsigned char dat) // function to write data at lcd port { LCD=dat; RS=1; // set RS=1 to write DATA E=1; // send H-L pulse at E pin delay_us(300); E=0; delay_us(500); } void init_lcd() // function to initialize the LCD at power on time { TRISC = 0x00; TRISB.F0 = 0; TRISB.F1 = 0; cmd_lcd (0x38); // 2x16 display select cmd_lcd (0x06); // display on cursor off command cmd_lcd (0x0e); // automatic cursor movement to right cmd_lcd (0x01); // lcd clear command cmd_lcd (0x80); // first row first column select command }
void string_lcd(unsigned char *str) // function to display string to lcd { while(*str!='\0') // '\0' is null char as last char of pointer is null { data_lcd(*str); str++; } } void number_lcd(unsigned int dat, unsigned int size) // function to display any digit decimal value to lcd { unsigned int temp,temparr[10];