PIC16F877A interface IR sensor to count object pass by.

Thread Starter

BlissEva

Joined Jun 28, 2015
20
anyone can help me see this? my LCD cannot display the sensor count.

Code:
//MAINfile
#include <htc.h>
#include <pic.h>
#include <stdio.h>


__CONFIG(0x3F3A); //(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBOG_OFF);

#define RS RD2        //LCD
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
#define S RE0

#define _XTAL_FREQ 8000000    //set the crystal oscillator frquency
#include "lcd.h"

void main(){

    int count=0;

    PORTD = 0;
    PORTE = 1;
    TRISD = 0x00;        //LCD
    TRISE = 0x01;        //sensor

    EN=1;
    ADCON1 = 0XFF;        //Sets All to Digital.     

    lcd_init();
    lcd_clear();
    __delay_ms(10);
      while(1){

        lcd_cmd(0x00);
        lcd_cmd(0x01);
        WriteString("Count: ");

        if(count==9999){
            count=0;
        }    
        if(S==0){
            count = (count+1);
        }
        WriteString(count);
    }
}


//HEADERfile

#ifndef __LCD

#define D0 RB0
#define D1 RB1
#define D2 RB2
#define D3 RB3
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7

#endif

void lcd_port(char d)    //function to
{
    if(d & 1)            //if w equal 1 (0001)
        D4 = 1;            //set D4 data bit to 1
    else                 //else
        D4 = 0;            //set D4 data bit to 0
 
    if(d & 2)            //if w equal 2 (0010)
        D5 = 1;            //set D5 data bit to 1
    else                 //else
        D5 = 0;            //set D5 data bit to 0
 
    if(d & 4)            //if w equal 4 (0100)
        D6 = 1;            //set D6 data pin to 1
    else                //else
        D6 = 0;            //set D6 data bit to 0
 
    if(d & 8)            //if w equal 8 (1000)
        D7 = 1;            //set D7 data pin to 1
    else                //else
        D7 = 0;            //set D7 data bit to 0
}

void lcd_cmd(char k)    //able the LCD sending command
{
    RS = 0;             //able sending commond
    lcd_port(k);        //get data from lcd_port(a)
    EN  = 0;            //enable it
      __delay_ms(4);        //delay
      EN  = 1;            //disable
}


lcd_clear()                //clear LCD display
{
    lcd_cmd(0);
    lcd_cmd(1);            //Clears all display
                        //and returns the cursor
                        //to the home position
}

void SetCursor(char row, char col)  //set cursor of the LCD
{
    char temp,z,y;                //declare temp, z, y         
    if(row == 1)                //if row equal 1
    {
      temp = 0x80 + col;        //0x80 add col and store to temp
        z = temp>>4;            //shift temp right 4 bit and store in z
        y = (0x80+col) & 0x0F;    // Start Cursor From First Line
        lcd_cmd(z);                //call command function
        lcd_cmd(y);                //call command function
    }
    else if(row == 2)            //if row equal 2
    {
        temp = 0xC0 + col;        //0x80 add col and store to temp
        z = temp>>4;            //shift temp right 4 bit and store in z
        y = (0xC0+col) & 0x0F;    // Start Cursor From Second Line
        lcd_cmd(z);             //call command function
        lcd_cmd(y);                //call command function
    }
}

/* Power initialization to prepare LCD
* to read/write 8 bits as 2 4-bit nibbles */
void lcd_init()            //Intilize LCD in 4-Bit Mode
{
    lcd_port(0x00);     //clear display
    __delay_ms(20);        //delay
    lcd_cmd(0x03);        //Special Sequence:Write Function Set.
    __delay_ms(20);        //delay
    lcd_cmd(0x03);          //Special Sequence:Write Function Set.
    __delay_ms(20);        //delay
    lcd_cmd(0x03);          //Special Sequence:Write Function Set.
                         //write 3 time 0x03 for the LCD change interface to 4 bit

    lcd_cmd(0x02);         //set to 4 bit operation
    lcd_cmd(0x02);        //set the function set for 4 bit
    lcd_cmd(0x08);         //set the function set for 4 bit

    lcd_cmd(0x00);         //Display On
    lcd_cmd(0x0C);       //Display On
    lcd_cmd(0x00);        //Set entry mode
    lcd_cmd(0x06);       //Set entry mode
}




void WriteChar(char c) //write character data to LCD
{
   char temp1,y1;            //declare temp and y
   temp1 = c&0x0F;         //Mask higher 4 bits
   y1 = c&0xF0;            //Mask Lower 4 Bits
   RS = 1;                //wan data input
   lcd_port(y1>>4);      //Data transfer, shift 4 bit right
     EN = 0;         
     __delay_ms(5);        //Send Enable Signal to LCD
     EN = 1;
     lcd_port(temp1);    //data transfer to function port 
     EN = 0;         
     __delay_ms(5);        //Send Enable Signal to LCD
     EN = 1;         
}


void WriteString(char *str)    //convert string to character
{
    for(int i=0; str!='\0'; i++) //loop the string last character
     WriteChar(str);        //call write char function
}
 

ErnieM

Joined Apr 24, 2011
7,995
Did you triple check your schematic? You better because no one here can help you do the check as you chose to keep it a secret.

There are some very specific places where these displays need delays. I see none of them in your code.
 
Top