Question

I am writing a very simple application which allows one to change the temperature. The temperature is displayed using LEDS (BCD format)

I wrote the following code in Keil C51:

#include< REG51.h>

sbit select = P1^7;
sbit up = P1^0;
sbit down = P1^1;
int data room = 24;

void main()
{   int prog;
    P2 &=0x00;
    P1 |=0x83;
    prog = 0;
    while (1)
    {
        if(select == 1)
            prog++;
        if(prog == 1)
        {
            if(up == 1)
                room++;
            if(down == 1)
                room--;

            P2 = room;
        }
    }
}

I then complied this and obtained the Intel hex file which i then tried to simulate using the Edsim.

According to the C code the temp should change when prog=1 and when either up(p1.0) or down(p1.1) is pressed, but in the simulation it only changes when both select(p1.7) and up/down is pressed!

Why is this happening?

Was it helpful?

Solution

prog++ means that the value of prog increments by 1 every time that the condition select == 1 is true. This means that the condition prog == 1 is true only on the first iteration that it's incremented.

Try changing prog++ to prog = 1.

Edit: As per discussion in comments, if you wish to track the number of times select has gone up, you need to wait for it to be 0 again before you let prog be incremented again. For example:

int prev = select;
…
if (select != prev) {
    // select has changed from its previous state

    prev = select;
    if (prev) {
        // select went from 0 to 1
        ++prog;

        if (prog == 1) {
           // code to be executed once only on the first press
        } else if (prog == 2) {
           // code to be executed once only on the second press
        } else if (prog >= 3) {
           // code to be executed once on every subsequent press
        }
    } else {
        // select went from 1 to 0
    }
}

if (prev) {
   // select is being pressed

   if (prog == 1) {
       // code to be executed _continuously_ while select is held down
       // (after first press only)
   }
   // ...
}

OTHER TIPS

I think your code does not reflect what you want it to do.

if(select == 1)
            prog++;
        if(prog == 1)
        {

prog is initially 0, so prog==1 will be true only for the first time you press select.

at that time it goes into the IF and check

if(up == 1)
                room++;
            if(down == 1)
                room--;

            P2 = room;

change the room current temp to +1 or -1 if up or down is pressed. since u r simulating with buttons, this means that either of the button should be pressed when you press the select.

and then it outputs the data to P2

what you need to do is:

while (1)
{
    if(select== 1)
    {   
        P2 = room;
    }


   if(up == 1)
       room++;
   if(down == 1)
       room--; 
}

you still will need to press select to update the temperature, and up/down to change temperature. If you can make yourself more clear as to how it is to work, maybe i can help more.

You should Try changing prog++ to prog = 1. Let me know if that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top