Evo kod koji konacno radi
Code:
#define _LCD_FIRST_ROW 0x80 //Move cursor to the 1st row
#define _LCD_SECOND_ROW 0xC0 //Move cursor to the 2nd row
#define _LCD_THIRD_ROW 0x94 //Move cursor to the 3rd row
#define _LCD_FOURTH_ROW 0xD4 //Move cursor to the 4th row
#define _LCD_CLEAR 0x01 //Clear display
#define _LCD_RETURN_HOME 0x02 //Return cursor to home position, returns a shifted display to
//its original position. Display data RAM is unaffected.
#define _LCD_CURSOR_OFF 0x0C //Turn off cursor
#define _LCD_UNDERLINE_ON 0x0E //Underline cursor on
#define _LCD_BLINK_CURSOR_ON 0x0F //Blink cursor on
#define _LCD_MOVE_CURSOR_LEFT 0x10 //Move cursor left without changing display data RAM
#define _LCD_MOVE_CURSOR_RIGHT 0x14 //Move cursor right without changing display data RAM
#define _LCD_TURN_ON 0x0C //Turn Lcd display on
#define _LCD_TURN_OFF 0x08 //Turn Lcd display off
#define _LCD_SHIFT_LEFT 0x18 //Shift display left without changing display data RAM
#define _LCD_SHIFT_RIGHT 0x1E //Shift display right without changing display data RAM
#define Waddr 0x4E
// Software I2C connections
sbit Soft_I2C_Scl at RA3_bit;
sbit Soft_I2C_Sda at RA4_bit;
sbit Soft_I2C_Scl_Direction at TRISA3_bit;
sbit Soft_I2C_Sda_Direction at TRISA4_bit;
/* LCD_I2C found with PCF8574
P7,P6,P5,P4 of PCF8574 = MSbits DB7,DB6,DB5,DB4 of LCD Display.
P3 is hardware display bit (BackLight : 1 = on / 0 = off)
P2 is hardware E display bit clock : E = 1 -> E = 0 active
P1 is R/W hardware Read/Write bit hardware : Read = 1 / Write = 0
P0 is RS hardware Register Select : CmdReg = 0 / DataReg = 1
*/
char msg[21];
char message1[] = "Software I2C LCD";
char message2[] = "16x2, 20x2, 20x4";
const char message3[] = "PIC12F683";
const char message4[] = "mikroC PRO PIC";
void Soft_I2C_LCD_Cmd(char out_char) {
char hi_n, lo_n;
char rs = 0x00;
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
Soft_I2C_Start();
Soft_I2C_Write(Waddr);
Soft_I2C_Write(hi_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(hi_n | rs | 0x00 | 0x08);
Delay_us(100);
Soft_I2C_Write(lo_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(lo_n | rs | 0x00 | 0x08);
Soft_I2C_Stop();
if(out_char == 0x01)Delay_ms(2);
}
void Soft_I2C_LCD_Chr(char row, char column, char out_char) {
char hi_n, lo_n;
char rs = 0x01;
switch(row){
case 1:
Soft_I2C_LCD_Cmd(0x80 + (column - 1));
break;
case 2:
Soft_I2C_LCD_Cmd(0xC0 + (column - 1));
break;
case 3:
Soft_I2C_LCD_Cmd(0x94 + (column - 1));
break;
case 4:
Soft_I2C_LCD_Cmd(0xD4 + (column - 1));
break;
};
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
Soft_I2C_Start();
Soft_I2C_Write(Waddr);
Soft_I2C_Write(hi_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(hi_n | rs | 0x00 | 0x08);
Delay_us(100);
Soft_I2C_Write(lo_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(lo_n | rs | 0x00 | 0x08);
Soft_I2C_Stop();
}
void Soft_I2C_LCD_Chr_Cp(char out_char) {
char hi_n, lo_n;
char rs = 0x01;
hi_n = out_char & 0xF0;
lo_n = (out_char << 4) & 0xF0;
Soft_I2C_Start();
Soft_I2C_Write(Waddr);
Soft_I2C_Write(hi_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(hi_n | rs | 0x00 | 0x08);
Delay_us(100);
Soft_I2C_Write(lo_n | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(lo_n | rs | 0x00 | 0x08);
Soft_I2C_Stop();
}
void Soft_I2C_LCD_Init() {
char rs = 0x00;
Soft_I2C_Start();
Soft_I2C_Write(Waddr);
Delay_ms(30);
Soft_I2C_Write(0x30 | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(0x30 | rs | 0x00 | 0x08);
Delay_ms(10);
Soft_I2C_Write(0x30 | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(0x30 | rs | 0x00 | 0x08);
Delay_ms(10);
Soft_I2C_Write(0x30 | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(0x30 | rs | 0x00 | 0x08);
Delay_ms(10);
Soft_I2C_Write(0x20 | rs | 0x04 | 0x08);
Delay_us(50);
Soft_I2C_Write(0x20 | rs | 0x00 | 0x08);
Soft_I2C_Stop();
Delay_ms(10);
Soft_I2C_LCD_Cmd(0x28);
Soft_I2C_LCD_Cmd(0x06);
}
void Soft_I2C_LCD_Out(char row, char col, char *text) {
while(*text)
Soft_I2C_LCD_Chr(row, col++, *text++);
}
void Soft_I2C_LCD_Out_Cp(char *text) {
while(*text)
Soft_I2C_LCD_Chr_Cp(*text++);
}
void main() {
void Soft_I2C_Init();
CMCON |= 7; // Disable CompaRAtors
TRISB = 0x00; // Set PORTB7 input all other direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA = 0b00000000; // RA5-6-7 is input only
delay_ms(200);
Soft_I2C_LCD_Init();
Soft_I2C_LCD_Cmd(_LCD_CURSOR_OFF);
Soft_I2C_LCD_Cmd(_LCD_CLEAR);
Soft_I2C_LCD_Out(1,1,message1);
Soft_I2C_LCD_Out(2,1,message2);
while(1) {
}
}
Problem je bio u ovoj liniji koda
Code:
void Soft_I2C_Init();
A adresa za modul je 0x4E