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 EEPROM with PIC: In this tutorial External EEPROMis 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 EEPROM interface with PIC C Code: /*************** eeprom.c file***************/ lcd data port c control port b eeprom portd */
#include"lcd.h" #include"eeprom.h"
void main(void) { char disp; ADCON1=0X0F; init_lcd(); cmd_lcd(0x80); string_lcd("EEPROM Test"); write_eeprom(20,0); while(1) { disp = read_eeprom(0); cmd_lcd(0xC0); number_lcd(disp,3); } } /*************** eeprom.h file***************/ sbit Soft_I2C_Scl at RD1_bit; sbit Soft_I2C_Sda at RD0_bit; sbit Soft_I2C_Scl_Direction at TRISD1_bit; sbit Soft_I2C_Sda_Direction at TRISD0_bit;
void write_eeprom(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(0xa0); // 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_eeprom(char add) { char read; Soft_I2C_Start(); // Issue start signal Soft_I2C_Write(0xa0); // Address PCF8583, see PCF8583 datasheet Soft_I2C_Write(add); // Start from address 2 Soft_I2C_Start(); // Issue repeated start signal Soft_I2C_Write(0xa1); // Address PCF8583 for reading R/W=1 read = Soft_I2C_Read(0); // Read seconds byte Soft_I2C_Stop(); // Issue stop signal return read; }
void init_eeprom() { 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_ms(10); E=0; }
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_ms(10); E=0; } 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];