PIC16F877A interface IR sensor to count object pass by.

Discussion in 'Embedded Systems and Microcontrollers' started by BlissEva, Jul 5, 2015.

  1. BlissEva

    Thread Starter New Member

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

    Code (Text):
    1. //MAINfile
    2. #include <htc.h>
    3. #include <pic.h>
    4. #include <stdio.h>
    5.  
    6.  
    7. __CONFIG(0x3F3A); //(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBOG_OFF);
    8.  
    9. #define RS RD2        //LCD
    10. #define EN RD3
    11. #define D4 RD4
    12. #define D5 RD5
    13. #define D6 RD6
    14. #define D7 RD7
    15. #define S RE0
    16.  
    17. #define _XTAL_FREQ 8000000    //set the crystal oscillator frquency
    18. #include "lcd.h"
    19.  
    20. void main(){
    21.  
    22.     int count=0;
    23.  
    24.     PORTD = 0;
    25.     PORTE = 1;
    26.     TRISD = 0x00;        //LCD
    27.     TRISE = 0x01;        //sensor
    28.  
    29.     EN=1;
    30.     ADCON1 = 0XFF;        //Sets All to Digital.    
    31.  
    32.     lcd_init();
    33.     lcd_clear();
    34.     __delay_ms(10);
    35.       while(1){
    36.  
    37.         lcd_cmd(0x00);
    38.         lcd_cmd(0x01);
    39.         WriteString("Count: ");
    40.  
    41.         if(count==9999){
    42.             count=0;
    43.         }    
    44.         if(S==0){
    45.             count = (count+1);
    46.         }
    47.         WriteString(count);
    48.     }
    49. }
    50.  
    51.  
    52. //HEADERfile
    53.  
    54. #ifndef __LCD
    55.  
    56. #define D0 RB0
    57. #define D1 RB1
    58. #define D2 RB2
    59. #define D3 RB3
    60. #define RS RD2
    61. #define EN RD3
    62. #define D4 RD4
    63. #define D5 RD5
    64. #define D6 RD6
    65. #define D7 RD7
    66.  
    67. #endif
    68.  
    69. void lcd_port(char d)    //function to
    70. {
    71.     if(d & 1)            //if w equal 1 (0001)
    72.         D4 = 1;            //set D4 data bit to 1
    73.     else                 //else
    74.         D4 = 0;            //set D4 data bit to 0
    75.  
    76.     if(d & 2)            //if w equal 2 (0010)
    77.         D5 = 1;            //set D5 data bit to 1
    78.     else                 //else
    79.         D5 = 0;            //set D5 data bit to 0
    80.  
    81.     if(d & 4)            //if w equal 4 (0100)
    82.         D6 = 1;            //set D6 data pin to 1
    83.     else                //else
    84.         D6 = 0;            //set D6 data bit to 0
    85.  
    86.     if(d & 8)            //if w equal 8 (1000)
    87.         D7 = 1;            //set D7 data pin to 1
    88.     else                //else
    89.         D7 = 0;            //set D7 data bit to 0
    90. }
    91.  
    92. void lcd_cmd(char k)    //able the LCD sending command
    93. {
    94.     RS = 0;             //able sending commond
    95.     lcd_port(k);        //get data from lcd_port(a)
    96.     EN  = 0;            //enable it
    97.       __delay_ms(4);        //delay
    98.       EN  = 1;            //disable
    99. }
    100.  
    101.  
    102. lcd_clear()                //clear LCD display
    103. {
    104.     lcd_cmd(0);
    105.     lcd_cmd(1);            //Clears all display
    106.                         //and returns the cursor
    107.                         //to the home position
    108. }
    109.  
    110. void SetCursor(char row, char col)  //set cursor of the LCD
    111. {
    112.     char temp,z,y;                //declare temp, z, y        
    113.     if(row == 1)                //if row equal 1
    114.     {
    115.       temp = 0x80 + col;        //0x80 add col and store to temp
    116.         z = temp>>4;            //shift temp right 4 bit and store in z
    117.         y = (0x80+col) & 0x0F;    // Start Cursor From First Line
    118.         lcd_cmd(z);                //call command function
    119.         lcd_cmd(y);                //call command function
    120.     }
    121.     else if(row == 2)            //if row equal 2
    122.     {
    123.         temp = 0xC0 + col;        //0x80 add col and store to temp
    124.         z = temp>>4;            //shift temp right 4 bit and store in z
    125.         y = (0xC0+col) & 0x0F;    // Start Cursor From Second Line
    126.         lcd_cmd(z);             //call command function
    127.         lcd_cmd(y);                //call command function
    128.     }
    129. }
    130.  
    131. /* Power initialization to prepare LCD
    132. * to read/write 8 bits as 2 4-bit nibbles */
    133. void lcd_init()            //Intilize LCD in 4-Bit Mode
    134. {
    135.     lcd_port(0x00);     //clear display
    136.     __delay_ms(20);        //delay
    137.     lcd_cmd(0x03);        //Special Sequence:Write Function Set.
    138.     __delay_ms(20);        //delay
    139.     lcd_cmd(0x03);          //Special Sequence:Write Function Set.
    140.     __delay_ms(20);        //delay
    141.     lcd_cmd(0x03);          //Special Sequence:Write Function Set.
    142.                          //write 3 time 0x03 for the LCD change interface to 4 bit
    143.  
    144.     lcd_cmd(0x02);         //set to 4 bit operation
    145.     lcd_cmd(0x02);        //set the function set for 4 bit
    146.     lcd_cmd(0x08);         //set the function set for 4 bit
    147.  
    148.     lcd_cmd(0x00);         //Display On
    149.     lcd_cmd(0x0C);       //Display On
    150.     lcd_cmd(0x00);        //Set entry mode
    151.     lcd_cmd(0x06);       //Set entry mode
    152. }
    153.  
    154.  
    155.  
    156.  
    157. void WriteChar(char c) //write character data to LCD
    158. {
    159.    char temp1,y1;            //declare temp and y
    160.    temp1 = c&0x0F;         //Mask higher 4 bits
    161.    y1 = c&0xF0;            //Mask Lower 4 Bits
    162.    RS = 1;                //wan data input
    163.    lcd_port(y1>>4);      //Data transfer, shift 4 bit right
    164.      EN = 0;        
    165.      __delay_ms(5);        //Send Enable Signal to LCD
    166.      EN = 1;
    167.      lcd_port(temp1);    //data transfer to function port
    168.      EN = 0;        
    169.      __delay_ms(5);        //Send Enable Signal to LCD
    170.      EN = 1;        
    171. }
    172.  
    173.  
    174. void WriteString(char *str)    //convert string to character
    175. {
    176.     for(int i=0; str!='\0'; i++) //loop the string last character
    177.      WriteChar(str);        //call write char function
    178. }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
Loading...