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] help me in programming :) detecting the input LOW bits..

Status
Not open for further replies.
You said in a previous post that you want to check which bits become 0, you can go to the basic form of doing that


Code C - [expand]
1
2
3
4
5
6
7
8
if !CHECKBIT(PORTX,0) ...;   // true if bit0 is 0
if !CHECKBIT(PORTX,1) ...;
if !CHECKBIT(PORTX,2) ...;
if !CHECKBIT(PORTX,3) ...;
if !CHECKBIT(PORTX,4) ...;
if !CHECKBIT(PORTX,5) ...;
if !CHECKBIT(PORTX,6) ...;
if !CHECKBIT(PORTX,7) ...;



But what do you want to do with the result of each bit?

Alex
 

hi alexan,,


actually this is what i want.. this is my finished code..
i also attached the protues file and the hex. now its totally done.. hehe :)




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
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))
void main()
{
  char i;
  P3=0x00;
  P1=0xFF;
 
  while(1)
  {
 
      if (CHECKBIT(~P1,0))
      {
         P3= 1;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,1))
      {
         P3= 2;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,2))
      {
         P3= 3;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,3))
      {
         P3= 4;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,4))
      {
         P3= 5;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,5))
      {
         P3= 6;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,6))
      {
         P3= 7;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,7))
      {
         P3= 8;
      }
      Delay_ms(300);
      if (CHECKBIT(~P1,8))
      {
         P3= 9;
      }
      else P3=0;
      Delay_ms(300);
      
 
     }
  }

 

Attachments

  • Desktop.rar
    14.4 KB · Views: 100

Tell me how the code above and this code behave differently??
Code:
void main()
{
  P3=0x00;
  P1=0xFF;

  while(1)
  {
    for(i = 0; i<8; i++)
    {
      if(~P1 & (0x01 << i))
        P3 = i + 1;
     else
       P3 = 0;
     Delay_ms(300);
    }
  }
}
 

Actually your code is different from what he does because for each i value you either set the output to a value or turn off everything,
in his code he only assigns value to the output after the check of each bit and he only clears the output port using if/else for bit 7 (you use it for all bits).
I know that you can easily change it to have exactly the same behavior, other than that the for loop is a more refined way.
That being said i think that both codes don't behave as they should because the output should be 0 only if all the input bits are 1 so it would reasonable to use a check after the loop ends and if the port is 0xff then turn everything off.

Alex
 
Thanks alexan_e.

So, this code is what the OP really wanted,

Code:
void main()
{
  P3=0x00;
  P1=0xFF;

  while(1)
  {
    for(i = 0; i<8; i++)
    {
      if(~P1 & (0x01 << i))
        P3 += i + 1;
      if(~P1 == 0)
        P3 = 0;
     Delay_ms(300);
    }
  }
}
 
I'm not sure what he wants but this resembles the functionality of the code he wrote
with the addition of the clear output when input is 0xff
as i think it should be but maybe he doesn't want it this way.

The only other change i would make would be the delay,
I don't understand the purpose of a delay between each step (but that is what he did),
i would probably use the delay out of the loop and then add if(P1 == 0xff) P3 = 0; before the loop executes again.

Alex
 
Last edited:

hi raco, your last posted code is what i am using now.... it's very nice and efficient.. :) :)

hi alexan, I added delay because i just want to delay and see the simulation properly....
as what i have said, my programming skill is not good enough to create good algorithm for my programs. I only knew basic style of codding.. as of now, it is okay to me if i see my code working as it should be but i dont know hot to write a code efficiently like what raco did to my code..

yesterday I am tired of using a very long switch cases just to get all of the combination of 8bits and my code exceeds 2kb of memory which is not enough if i will use AT89c2051.


it is so funny to think about yesterday this was my code of getting the low bits... :-D:-D

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void main()
{
 while(1)
 {
   switch(P1)
   {
       case 0:
       case 1:
       .
       .  
       . 
       .
       case 255:
 
 
   }
 
 }
}




and then after now the equivalent to my long cases of switch is just like this.. very short compared to my style... :-D:-D Im very thankful guys... thanks for helping :p:p



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
void main()
{
  char i;
  P3=0x00;
  P1=0xFF;
 
  while(1)
  {
    for(i = 0; i<8; i++)
    {
      if(~P1 & (0x01 << i))
       {
        Delay_ms(100);
        P3 += i + 1;
        Delay_ms(100);
       }
      if(~P1 == 0)
        Delay_ms(100);
        P3 = 0;
 
    }
 
  }
}



---------- Post added at 01:51 ---------- Previous post was at 01:44 ----------

thanks all of you guys for contributing my learning :)
 
Last edited:

hi alexan, I added delay because i just want to delay and see the simulation properly....

actually i didn't say to remove the delay, i said to reposition it.
The delay is used in the above code in each step of the loop, this means that if you provide a 0 to bit7 you will have to wait for 7*200ms for that to be detected.
What i propose is this

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
void main()
{
  char i;
  P3=0x00;
  P1=0xFF;
 
  while(1)
  {
    for(i = 0; i<8; i++)
    {
      if(~P1 & (0x01 << i))
       {
         P3 = i + 1;
        // break;  // you can also add a break here to exit the loop when an input is detected
       }
    }
    
    if (P3 != 0)  // if the output is 0 no reason to delay or clear
    {
        Delay_ms(500);   //delay
        P3 = 0;  // and then clear everything
    }
  }
}



using this code you will read immediately the input when the output is off
and the output will hold the value for the delay time

I think the line P3 += i + 1; should be P3 = i + 1; as it was in the previous post so that the output is 1 to 8 depending on the input, id depends what you want.

Alex
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top