Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Push button interfacing Problem with pic 16f877a

Status
Not open for further replies.

yanal

Junior Member level 1
Joined
Sep 20, 2015
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
149
Hello dears , hope you are doing well

i interfaced LCD and push button to pic 16f877a بدون عنوان.png

, and below the code i have used ,
i want when the button pressed , it shows the desired text ,but unfortunately the code enterd the loop and showed the text without i pressed the Button !!!

Could any one please help me to solve this problem .


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
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
 
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
 
unsigned short code1  ;
void button_pressed (){
if ( porta.b0=1) {
code1=49;
Lcd_Cmd(_LCD_CLEAR); // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off
  lcd_chr(1,1,code1);
  delay_ms(1000);
  }
  if ( porta.b0=0) {
code1=50;
Lcd_Cmd(_LCD_CLEAR); // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off
  lcd_chr(1,1,code1);
  delay_ms(1000);
  }
}
 
 
void main()
{
    trisb=0; //port B Output
   // trisa.B0=0; // Set A0 as input
   /* ANSEL  = 0;                        // Configure AN pins as digital I/O
    ANSELH = 0;
   C1ON_bit = 0;                      // Disable comparators
   C2ON_bit = 0;
    */
 
   ADCON1 =0x06 ;
  // CMCON = 0x07 ; // Disable analog comparators
 //   CVRCON = 0;
  Lcd_Init(); // Initialize LCD
   Lcd_Cmd(_LCD_CLEAR); // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off
  loop:
 
  //button_pressed();
  if(porta.b0=1)
  {
  Lcd_Cmd(_LCD_CLEAR); // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);// Cursor off
  lcd_out(1,1,"W");
  delay_ms(500);
  }
 
 goto loop ;
}

 

First, if statement have to be used with double equal sign '=='
Second, ==1 is incorrect. For example, b7 bit of port will never be 1. It can be 0x00 or 0x80. So, basicaly, you can compare it to zero or not zero:
if(porta.b0 != 0) or just 'if(porta.b0)'
And the last one. DO NOT TRY TO SIMULATE CRISTAL OSCILLATORS!!!! AGAIN AND AGAIN!!!
Proteus don't care what you trying to connect to OSC pins.
 

Also, debounce the input from the key. It looks like you are using mikroC (which I've never used) but some of the other code written for that compiler has used a built-in function to debounce a key.
Alternatively 'google' debounce and learn what it is, why key bounce is important to understand and how to do it properly.
Susan
 
First, if statement have to be used with double equal sign '=='
Second, ==1 is incorrect. For example, b7 bit of port will never be 1. It can be 0x00 or 0x80. So, basicaly, you can compare it to zero or not zero:
if(porta.b0 != 0) or just 'if(porta.b0)'
And the last one. DO NOT TRY TO SIMULATE CRISTAL OSCILLATORS!!!! AGAIN AND AGAIN!!!
Proteus don't care what you trying to connect to OSC pins.

OK Dear,
Thanks for your notes , i did what u said and modified my code but the problem is still exist .
the text is displaying on the screen while i'm not press on the button , do u have any idea why this happens ?
i have attached the circuit and the code that i used .
 

Attachments

  • project.rar
    55.3 KB · Views: 88

You're not configuring TRISA anywhere, although as default it should be set as input.
 

i had forget to Configure port A as digital input

ADCON1 = 0x06;
 

You did not forget, it's already present on line #46 of the code above. Anyway, at the attached code you did not made any of the changes as suggested above by Easyrider83 and Aussie Susan.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top