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 #include <stdio.h> #include <stdbool.h> #define LCD_DATA PORTB #define LCD_CTRL PORTA sbit LCD_EN at PORTA.B0; // LCD Latch pin (Enable) sbit LCD_RS at PORTA.B1; // Command/Data select PIN | Command(RS=0) | DATA(RS=1) sbit LCD_RW at PORTA.B2; // READ/WRTIE pin | Read (1) | Write (0) #define LCD_DATA_DIRECTION TRISB sbit LCD_EN_Direction at TRISA0_bit; sbit LCD_RS_Direction at TRISA1_bit; sbit LCD_RW_Direction at TRISA2_bit; #define CHECK_BIT(var,pos) (((var)>>(pos)) & 1) // ========================= LCD FUNCTIONS ========================= // LCD Busy-Check function void lcd_ready(){ LCD_DATA_DIRECTION=0xff; // Data ports input LCD_RS=0; // Command register LCD_RW=1; // Read from LCD LCD_EN=0;Delay_ms(1);LCD_EN=1; // Low-to-High signal for latching //while(LCD_DATA && 0x08 == 0x08); // Checking the MSB (B7) for busy indication while(CHECK_BIT(LCD_DATA,7)); // Stay here if the d7 bit is high (i.e. LCD is busy) } // LCD Command write function void lcd_Command_write(char value){ LCD_DATA_DIRECTION=0x00; LCD_RS=0;LCD_RW=0; LCD_DATA=value; LCD_EN=1; delay_ms(1); LCD_EN=0; // High to Low signal for latching } // LCD Display function void lcd_Data_write(char value){ LCD_DATA_DIRECTION=0x00; LCD_RS=1;LCD_RW=0; LCD_DATA=value; LCD_EN=1;delay_ms(1);LCD_EN=0; // High to Low signal for latching } // LCD_POWER(ON/OFF) | ON=1 void LCD_POWER(bool flag){ if(flag){ Delay_ms(250); // Power up delay lcd_ready(); // Is LCD BUSY ? wait here lcd_Command_write(0x38); // LCD 2 Lines, 5x7 characters lcd_ready(); lcd_Command_write(0x0E); // Display ON, Cursor ON lcd_ready(); lcd_Command_write(0x01); // Clear LCD lcd_ready(); lcd_Command_write(0x06); // Shift Cursor Right lcd_ready(); lcd_Command_write(0x80); // Cursor at line 1 position 1 } else{ lcd_ready(); // Is LCD BUSY ? wait here LCD_RS=0; // Select Command Register LCD_RW=0; // Write operation LCD_DATA=0x10; // Cursor and Display off Delay_us(1);LCD_EN=1;Delay_us(1);LCD_EN=0; // Latch } } void main() { LCD_EN_Direction=LCD_RS_Direction=LCD_RW_Direction=0x00; LCD_DATA_DIRECTION=0x00; LCD_POWER(1); LCD_ready(); LCD_DATA_write('1'); LCD_ready(); LCD_DATA_write('2'); LCD_ready(); LCD_DATA_write('3'); LCD_ready(); LCD_DATA_write('4'); // Last character doens't shows up! }
void lcd_Data_write(char value){
LCD_DATA_DIRECTION=0x00;
LCD_RS=1;LCD_RW=0;
LCD_DATA=value;
LCD_EN=1;delay_ms(1);LCD_EN=0;[COLOR="#FF0000"]delay_ms(1);[/COLOR] // High to Low signal for latching
}
Code written pretty dirty, but just guessing, may be strobe have to be delayed simmetrically?
Code:void lcd_Data_write(char value){ LCD_DATA_DIRECTION=0x00; LCD_RS=1;LCD_RW=0; LCD_DATA=value; LCD_EN=1;delay_ms(1);LCD_EN=0;[COLOR="#FF0000"]delay_ms(1);[/COLOR] // High to Low signal for latching }
Do not mix the time-units, mikroC really does not have nanosecond (ns) delay libraries but it has microsecond (us) delays so use Delay_us(1) instead of every Delay_ms(1) which is unnecessarily long.... I had just read the timing diagram of LATCH cycle and its shows 140ns delay b/w LCD R/W. the timing diagram shows about 450us delay (minimum). Somehow i had used 1ms delay (because MicroC pro hasn't builtin function for nano-seconds delay)
I don't see where you are printing out a string
My idea (not tried yet, sorry):... the 1st problem still remains i.e. The last character doesn't show up on the LCD. Any idea's about that ?
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 //8-bit LCD with BF, written by zuisti sbit LCD_EN at PORTA.B0; // LCD Latch pin (Enable) sbit LCD_RS at PORTA.B1; // Command/Data select PIN | Command(RS=0) | DATA(RS=1) sbit LCD_RW at PORTA.B2; // READ/WRTIE pin | Read (1) | Write (0) char sfr LCD_DATA at PORTB; char sfr LCD_DATA_DIRECTION at TRISB; sbit LCD_EN_Direction at TRISA0_bit; sbit LCD_RS_Direction at TRISA1_bit; sbit LCD_RW_Direction at TRISA2_bit; // ========================= LCD FUNCTIONS ========================= void lcd_ready(){ LCD_DATA_DIRECTION=0xFF; // Data ports input LCD_RS=0; // Command register LCD_RW=1; // Read from LCD while (1) { Delay_us(1); LCD_EN=1; Delay_us(1); if (!LCD_DATA.B7) // Busy Flag break; LCD_EN=0; } LCD_EN=0; LCD_DATA_DIRECTION=0; // Data ports output } void lcd_Command_write(char value){ lcd_ready(); LCD_RS=0;LCD_RW=0; LCD_DATA=value; Delay_us(1);LCD_EN=1;Delay_us(1);LCD_EN=0; } void lcd_Data_write(char value){ lcd_ready(); LCD_RS=1;LCD_RW=0; LCD_DATA=value; Delay_us(1);LCD_EN=1;Delay_us(1);LCD_EN=0; } void LCD_POWER(void){ Delay_ms(250); // Power up delay lcd_Command_write(0x38); // LCD 2 Lines, 5x7 characters lcd_Command_write(0x0E); // Display ON, Cursor ON lcd_Command_write(0x01); // Clear LCD lcd_Command_write(0x06); // Shift Cursor Right lcd_Command_write(0x80); // Cursor home } //------- void main() { ANSELA = 0; // !!! this was the main problem ANSELB = 0; // this does not necessarily need // PORTA = 0; // TRISA = 0; // PORTB = 0; // TRISB =0; // or: LCD_DATA_DIRECTION=0; LCD_EN_Direction=0; LCD_RS_Direction=0; LCD_RW_Direction=0; // but DO NOT use this form in mikroC (much larger code): //LCD_DATA_DIRECTION=LCD_EN_Direction=LCD_RS_Direction=LCD_RW_Direction=0x00; LCD_POWER(); lcd_Data_write('1'); lcd_Data_write('2'); lcd_Data_write('3'); lcd_Data_write('4'); // not '?' }
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?