Problem: "switch()" C Code for 8051

8051, AVR, ARM, ISP programmer, multi processor communication, embedded C...

Moderators: ikalogic, cotto88

Problem: "switch()" C Code for 8051

Postby Arvie » Sat Sep 10, 2011 2:34 am

I use MIDE51 Editor and Compiler(SDCC)
http://www.opcube.com/home.html#MIDE51

here's my code:
1 | #include <AT89X52.h>
2 |
3 | char BUF = 0b11111111;
4 |
5 | main()
6 | { while(1)
7 | { if (BUF % 2 == 0)
8 | { switch (BUF)
9 | { case 0b11111111 : P0_0 = 0; break;
10| } } } }


here's the error:
C:\DOCUME~1\Arvie\Desktop\NEWFOL~1\ask.c(8) : warning 94: comparison is always true resp. false due to limited range of data type
C:\DOCUME~1\Arvie\Desktop\NEWFOL~1\ask.c(8) : warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG
C:\DOCUME~1\Arvie\Desktop\NEWFOL~1\ask.c(9) : warning 126: unreachable code
C:\DOCUME~1\Arvie\Desktop\NEWFOL~1\ask.c(7) : warning 110: conditional flow changed by optimizer: so said EVELYN the modified DOG


I dont know why there's an error.
It says that "false due to limited range of data type".

the error only occurs when the 0bxxxxxxxx
in the line case 0b11111111 : P0_0 = 0; break; above
is equal to 0b10000000 to 0b11111111.
when it is 0b00000000 to 0b01111111, there's no error.

I fixed the problem by replacing
char BUF = 0b11111111; to int BUF = 0b11111111;

char 8bit 1byte 0 to 255
int 16bit 2bytes 0 to 65535

but I still want to know why the error occurs. :|
Arvie
 
Posts: 8
Joined: Sat Apr 30, 2011 5:41 pm

Re: Problem: "switch()" C Code for 8051

Postby ikalogic » Sat Sep 10, 2011 10:00 am

let me first rewrite it this way, so that i or other can follow

Code: Select all
#include &lt;AT89X52.h]

char BUF = 0b11111111;
main()
{
    while(1)
    {
        if (BUF % 2 == 0)
        {
            switch (BUF)
            {
                case 0b11111111 : P0_0 = 0; break;
            }
        }
    }
}


now. i don't see error, just warnings..

anyway, could you try to define your BUF this way and tell me how it goes2687:
Code: Select all
volatile char BUF = 0b11111111;
True wisdom is knowing what you don't know -- Confucius
User avatar
ikalogic
Site Admin
 
Posts: 2214
Joined: Wed Jan 02, 2008 2:00 pm
Location: Limoges, France

Re: Problem: "switch()" C Code for 8051

Postby Arvie » Sat Sep 10, 2011 1:13 pm

can i just ignore those warnings even if it says "unreachable code"2687

volatile char BUF = 0b11111111; shows same warnings.

i replace the switch (BUF) to switch (P0) and no error occurs.

I believe P0 (Port0) is a char (1byte 8bit 0-255) so why using char BUF shows warnings/error.
Arvie
 
Posts: 8
Joined: Sat Apr 30, 2011 5:41 pm

Re: Problem: "switch()" C Code for 8051

Postby ikalogic » Sat Sep 10, 2011 1:17 pm

can you try that
Code: Select all
#include <AT89X52.h>

char BUF = 0b11111111;
char temp = = 0b11111111;;
main()
{
    while(1)
    {
        if (BUF % 2 == 0)
        {
            switch (temp)
            {
                case 0b11111111 : P0_0 = 0; break;
            }
        }
    }
}


i think the problem is that your compiler tries to optimize the code, and concludes that the 2 nested conditions can never occure at the same time (the IF and the SWITCH) . what i mean is that (0b11111111 % 2) will never be == 0, so why bother entring in this block of code anyway..2687
True wisdom is knowing what you don't know -- Confucius
User avatar
ikalogic
Site Admin
 
Posts: 2214
Joined: Wed Jan 02, 2008 2:00 pm
Location: Limoges, France

Re: Problem: "switch()" C Code for 8051

Postby Arvie » Sat Sep 10, 2011 2:30 pm

i tried your code and replace 0b11111111 to 0b11111110 .. but the same error appears.
I also changed (BUF % 2 == 0) to (BUF % 2 == 1).. same result.

I checked the parameters for the sdcc.. it doesnt have any code optimization.
Can you please try it sir using a different compiler. :|

I'm not convinced to use int BUF as solution.
It should work in char BUF. :roll:
Arvie
 
Posts: 8
Joined: Sat Apr 30, 2011 5:41 pm

Re: Problem: "switch()" C Code for 8051

Postby ikalogic » Sat Sep 10, 2011 2:33 pm

let me ask please, what are you trying to do.. i mean the logic behind that..2687
True wisdom is knowing what you don't know -- Confucius
User avatar
ikalogic
Site Admin
 
Posts: 2214
Joined: Wed Jan 02, 2008 2:00 pm
Location: Limoges, France

Re: Problem: "switch()" C Code for 8051

Postby Arvie » Mon Sep 19, 2011 1:29 am

ikalogic wrote:let me ask please, what are you trying to do.. i mean the logic behind that..2687


i will store the value of PORT(P1) to BUF.

then if the value is an odd number.. it will be ignored.

if it is an even number then there will be a routine for each even number(00000000 - 11111110) combination.
Arvie
 
Posts: 8
Joined: Sat Apr 30, 2011 5:41 pm


Return to Micro controllers

Who is online

Users browsing this forum: No registered users and 0 guests