How to Use if else statement in mikroc using pic18f452

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
CHUNJi
Posts: 3
Joined: 04 Feb 2017 07:52

How to Use if else statement in mikroc using pic18f452

#1 Post by CHUNJi » 04 Feb 2017 11:49

Hi,
i am new to pic and I recently started a project. but in that i not getting the correct output using if else statement. I don't know what is wrong.

//LCD INITIALISATION
sbit LCD_RS at RC2_bit;
sbit LCD_EN at RC3_bit;
sbit LCD_D4 at RC4_bit;
sbit LCD_D5 at RC5_bit;
sbit LCD_D6 at RC6_bit;
sbit LCD_D7 at RC7_bit;
sbit LCD_RS_Direction at TRISC2_bit;
sbit LCD_EN_Direction at TRISC3_bit;
sbit LCD_D4_Direction at TRISC4_bit;
sbit LCD_D5_Direction at TRISC5_bit;
sbit LCD_D6_Direction at TRISC6_bit;
sbit LCD_D7_Direction at TRISC7_bit;

void main() {
char txt[14];
int c;
TRISC == 0x00; // set port c as output for lcd
TRISB == 0xff; //set port b as input
LATB.B4 == 0x00; //initially portB B4 PIN IS SET TO LOGIC LOW VALUE
Lcd_Init(); //FOR LCD INITILISATION
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1, "WELCOME");
Delay_ms(1000);


while(1)
{
if(PORTB.B4 == 0xff)
{
c == 1;
Delay_ms(500);
}
else
{
c == 0;
Delay_ms(500);
}
c == 0;
IntToStr(c, txt);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1, txt); //DISPLAYING THE VALUE OF c THROUGH LCD.
Delay_ms(1000);
}
}


The above program checks the codition and displays the value of 'c' in lcd. But as i run the simulation through proteus it displays "0" even if change the value PORTB.B4 pin. The value"0" doesnot changes even if change the value of 'c' from the program. Plz help...

User avatar
dusan.poluga
mikroElektronika team
Posts: 780
Joined: 02 Feb 2017 14:21

Re: How to Use if else statement in mikroc using pic18f452

#2 Post by dusan.poluga » 06 Feb 2017 10:47

Hi,

You need to do it like this if you want to test if the bit is high:

Code: Select all

if(PORTB.B4 == 1)
And if you want to test it if the bit has the value of zero:

Code: Select all

if(PORTB.B4 == 0)
Because you are asking for a value of a single bit with this statement.
And on the top. You ave not assigned any value to the buffer.

Code: Select all

char text[14]
You need to assign it like this

Code: Select all

char text[14]="c"
Regards,
Dusan Poluga.

teprojects2
Posts: 27
Joined: 21 Jan 2017 10:44
Location: Lahore
Contact:

Re: How to Use if else statement in mikroc using pic18f452

#3 Post by teprojects2 » 06 Feb 2017 15:25

while(1)
{
if(PORTB.B4 == 0xff)
{
c == 1;
Delay_ms(500);
}
else
{
c == 0;
Delay_ms(500);
}
c == 0; // remove this because you are again and again initialized the c as 0
IntToStr(c, txt);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1, txt); //DISPLAYING THE VALUE OF c THROUGH LCD.
Delay_ms(1000);
}
}

CHUNJi
Posts: 3
Joined: 04 Feb 2017 07:52

Re: How to Use if else statement in mikroc using pic18f452

#4 Post by CHUNJi » 06 Feb 2017 16:09

Hi,
I think i finally figured out what's wrong. i am using

Code: Select all

c == 0
and

Code: Select all

c == 1
instead of using

Code: Select all

c = 0;
and

Code: Select all

c =1;
. when i changed this i got the correct output.

teprojects2
Posts: 27
Joined: 21 Jan 2017 10:44
Location: Lahore
Contact:

Re: How to Use if else statement in mikroc using pic18f452

#5 Post by teprojects2 » 07 Feb 2017 13:40

that's great keep it up and if any kind of help you need you can just ask :)

CHUNJi
Posts: 3
Joined: 04 Feb 2017 07:52

Re: How to Use if else statement in mikroc using pic18f452

#6 Post by CHUNJi » 07 Feb 2017 13:47

Hi..

Sure. And thankyou for the support..

teprojects2
Posts: 27
Joined: 21 Jan 2017 10:44
Location: Lahore
Contact:

Re: How to Use if else statement in mikroc using pic18f452

#7 Post by teprojects2 » 08 Feb 2017 12:57

It's my pleasure to help you, by the way my job is to help students to program their logic and developed their final year projects.

Post Reply

Return to “User Projects”