1

I am working on pic microcontroller programming and went lately to lcd made 2 codes, one in mplab and the other in mikroc actually both worked in isis but when I tried it in real only mikroc code worked in the microcontroller I don't know actually what is the problem/why did that happen I don't guess this is a hardware or software code because it is same circuit and both codes worked in isis so If anyone wanted to view the code so here it is:

mikroC:

    // Lcd pinout settings
    sbit LCD_RS at RC0_bit;
    sbit LCD_EN at RC1_bit;
    sbit LCD_D7 at RB7_bit;
    sbit LCD_D6 at RB6_bit;
    sbit LCD_D5 at RB5_bit;
    sbit LCD_D4 at RB4_bit;

    // Pin direction
    sbit LCD_RS_Direction at TRISC0_bit;
    sbit LCD_EN_Direction at TRISC1_bit;
    sbit LCD_D7_Direction at TRISB7_bit;
    sbit LCD_D6_Direction at TRISB6_bit;
    sbit LCD_D5_Direction at TRISB5_bit;
    sbit LCD_D4_Direction at TRISB4_bit;

    void main() {
         Lcd_Init();
         Lcd_Cmd(_LCD_CLEAR);               // Clear display
         Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

         Lcd_Out(1, 3, "Hello!");

    }

mplab:

main.c:

       #include "config.h"

        unsigned char CurvyObject[8] = { 0x01,0x02,0x04,0x08,0x10,0x11,0x1f,0x00 };

        void main(){
            TRISB = 0;
            TRISC = 0;
            send_command(0x38);
            __delay_us(40);
            send_command(0x01);
            __delay_ms(1.75);
            send_command(0x0C);
            __delay_us(40);
            moveto(0, 0, 0, 0, 0, 0, 0);
            __delay_us(40);
            write_character(70);
            __delay_us(50);
            write_character(97);
            __delay_us(50);
            write_character(100);
            __delay_us(50);
            write_character(121);
            __delay_us(50);

            while(1);


        }

config.h:

/* 
             * File:   config.h
             * Author: Fady
             *
             * Created on August 14, 2014, 6:19 PM
             */


            // PIC16F877A Configuration Bit Settings

            // 'C' source line config statements

            #include <xc.h>

            // #pragma config statements should precede project file includes.
            // Use project enums instead of #define for ON and OFF.

            // CONFIG
            #pragma config FOSC = XT        
            #pragma config WDTE = OFF       
            #pragma config PWRTE = OFF      
            #pragma config BOREN = ON       
            #pragma config LVP = ON         
            #pragma config CPD = OFF        
            #pragma config WRT = OFF        
            #pragma config CP = OFF         

            #define _XTAL_FREQ 4000000

            unsigned char i;

            //Library Declaration:
            void send_command(int command);
            void write_character(int character);
            void enable_blink();
            void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0);

            void send_command(int command){
                PORTB = command;
                PORTCbits.RC0 = 0;
                enable_blink();

            }

            void write_character(int character){
                PORTB = character;
                PORTCbits.RC0 = 1;
                enable_blink();

            }

            void enable_blink(){
                PORTCbits.RC1 = 1;
                __delay_ms(10);
                PORTCbits.RC1 = 0;
                __delay_ms(10);

            }

            void moveto(char b6, char b5, char b4, char b3, char b2, char b1, char b0){
                PORTCbits.RC0 = 0;
                PORTBbits.RB7 = 1;

                PORTBbits.RB6 = b6;
                PORTBbits.RB5 = b5;
                PORTBbits.RB4 = b4;
                PORTBbits.RB3 = b3;
                PORTBbits.RB2 = b2;
                PORTBbits.RB1 = b1;
                PORTBbits.RB0 = b0;

                enable_blink();

            }
user3674628
  • 29
  • 12
  • 1
    Before setting the "command" into PORTB, be sure RS and RW lines are properly set. You're doing it after putting the value in the PORT value, so it doesn't recognize whether it's a command, a read, or anything else. I saw from your other post that you've added the enableBLing function like I said. If you have access to a logic analyzer or Scope, post a picture of the signals it gives you. – Jean-francois Aug 19 '14 at 14:03

1 Answers1

1

It is a good practice to make sure all peripherals and outputs are in a known state before starting your program. As mentioned in the comment, you never made sure the LCD has the R/W pin correctly configured.

PORTCbits.RC2=0; // The Microcontroller will always write to the LCD   
                 // Replace RC2 with your R/W pin

In addition to that I compared your LCD library to the one I made and you didn't configure the entry mode, try adding this:

send_command(6);      //Cursor Move direction and Display Shift

Check out this generic datasheet to see if you configured the module as desired: http://www.lcd-module.com/eng/pdf/doma/dip162-de.pdf

On a side note, I see that the moveto(); function is used to position the cursor around, may I suggest my approach:

// L1=128,L2=192,L3=144,L4=208 
send_command(L1+6);    // Go to the 6th character of the first line
Grossu Iulian
  • 275
  • 1
  • 2
  • 10