1 2 3 Previous Next 87 Replies Latest reply on Sep 19, 2012 3:54 PM by occupy@hnspi.edu.cn.chou

    comparing inputs

    cathal.ferry

      Hi Guys,

         

      Ive been playing around with my PSoC eval1 board and  I was trying to write a code that would look at two inputs and if one was high but the other wasnt then an led would come on but if both were on or none or any other condition other than outlined then the led would come on,

         

      I was thinking i might work something like this:

         

      if (PRT1DR |= 0X01 && PRT1DR !=0X02)  // port 1.0 High, port 1.1 low

         

      {

         

      ///LED on

         

      }

         

      Else

         

      {

         

      // No led on

         

      }

         

      I am not 100% on how to do this but ill look it up if i can find some useful info on the subject.

         

      Thanks guys

        • 1. Re: comparing inputs
          user_14586677

          Basically -

             

           

             

          if (  ( PRT1DR == 0x01 ) || ( PRT1DR == 0x02 ) ) {

             

          ////LED on

             

          } else {

             

          ////LED off

             

          }

             

           

             

          If upper 6 bits in port used mask off with -

             

          unsigned char portstate = PRT1DR & 0x03;

             

          if ( ( portstate == 0X01 ) || ( portstate==0X02 ) ) {

             

           

             

          Regards, Dana.

          • 2. Re: comparing inputs
            cathal.ferry

            Hi Dana,

               

            Thanks so much for your help,

               

            Basically I have:

               

            if ((PRT0DR == 0x01) || ( PRT0DR == 0x02)){  //inputs are port 0.0 and 0.1

               

            //LED ON

               

            }

               

            else {

               

            //LED OFF

               

            }

               

            I tried this and made both p0.0 and p0.1 high and the LED on p1.0 remains illuminated. I must be missing something

               

            Similarly can i do this if (PRT0DR == 0X01) || (PRT0DR != 0X02)) \\ If i want the LED on when P0.0 is high and P0.1 is low?

               

            Thanks

            • 3. Re: comparing inputs
              ki.leung

              If you want the LED on only when ONE and ONLY ONE of the inputs is high

                 

               

                 

              if ((PRT1DR == 0X01) || (PRT1DR == 0X02))  //only when one of the two bits is high

                 

              {

                 

                      //LED on

                 

              }

                 

              Else

                 

              {

                 

                     // No led on

                 

              }

              • 4. Re: comparing inputs
                user_14586677

                What are you using for LED on code, and how is the led connected ?

                   

                 

                   

                You might consider posting project, or an extraction of it.

                   

                 

                   

                Regards, Dana.

                • 5. Re: comparing inputs
                  cathal.ferry

                  Hi guys,

                     

                  The LED is put on using the following (I am using shadow registers on port 0 and port 1):

                     

                  if(condition etc.)

                     

                  {

                     

                  Port_1_Data_SHADE &= ~0x01;  //led on p1.0

                     

                  PRT1DR = Port_1_Data_SHADE;

                     

                  }

                     

                  else

                     

                  {

                     

                  Port_1_Data_SHADE |= 0x01;

                     

                  PRT1DR = Port_1_Data_SHADE;

                     

                  }

                     

                  this code was working fine for me if i was only looking at the value of 1 input, but doesnt seem to be working when Im comparing 1 or more inputs.

                     

                  Thanks

                  • 6. Re: comparing inputs
                    ki.leung

                    looks like you reply when i was typing my reply

                    • 7. Re: comparing inputs
                      cathal.ferry

                      whoops sorry

                      • 8. Re: comparing inputs
                        ki.leung

                         How is your hardware setup? 

                           

                        The following is a bit long, but this should give you the correct result. If this doesn't work, there must be some other issues.

                           

                         

                           

                        if (PRT0DR == 0) 

                           

                        {

                           

                          shardow register = 0;

                           

                        }

                           

                        else if  (PRT0DR == 1) 

                           

                        {

                           

                           shardow register = 1;

                           

                         

                           

                        }

                           

                         

                           

                        else if  (PRT0DR == 2) 

                           

                        {

                           

                           shardow register = 1;

                           

                         

                           

                        }

                           

                         

                           

                        else if  (PRT0DR == 3) 

                           

                        {

                           

                           shardow register = 0;

                           

                         

                           

                        }

                           

                         

                           

                         

                           

                         

                           

                        /* output the shadow register here */

                        • 9. Re: comparing inputs
                          cathal.ferry

                          Hi lleung,

                             

                          The port pins are set up like so,

                             

                          Output Led is stdCPU and drive mode is strong,

                             

                          I have a port pin set high all the time to enable me to make the input pin High or Low using a wire, the port pin is driven strong,

                             

                          I have the input pin set up as stdCPU with the drive mode being pull down

                          • 10. Re: comparing inputs
                            user_14586677

                            If LED is tied  pin > LED > R > Vdd, then LED turns on with a logic 0. This is preferred

                               

                            method for driving an LED, eg. available drive current is greater.

                               

                             

                               

                            Your code looks correct, is LED wired as above or pin > LED > R > Vss ?

                               

                            In this case it runs on with logic 1.

                               

                             

                               

                            if(condition etc.)

                               

                            {

                               

                            Port_1_Data_SHADE &= ~0x01; //led on p1.0

                               

                            PRT1DR = Port_1_Data_SHADE;

                               

                            }

                               

                            else

                               

                            {

                               

                            Port_1_Data_SHADE |= 0x01;

                               

                            PRT1DR = Port_1_Data_SHADE;

                               

                            }

                               

                             

                               

                            Regards, Dana.

                            • 11. Re: comparing inputs
                              user_14586677

                              Ap note that may be useful -

                                 

                               

                                 

                                    http://www.cypress.com/?docID=38040

                                 

                               

                                 

                              Regards, Dana.

                              • 12. Re: comparing inputs
                                cathal.ferry

                                Hi Dana,

                                   

                                The LED im using is one of the 4 available on the Eval1 prototyping board, so its Port pin Vcc > LED > R > GND by the looks of things

                                • 13. Re: comparing inputs
                                  ki.leung

                                  Is your software works now? If not, have you try the LONG routine I proposed?

                                  • 14. Re: comparing inputs
                                    user_1377889

                                    In PSoC 1 there is -just to make things easier- an LED-component which may be adapted for active high (as on your board) and active low leds. Just tell the component to which port and pin your LED is connected and that's it. That easy!

                                       

                                    There are some APIs for turning the LED on or off and as I recently saw the component itself is using a shadow-register internally, so that you are not aware of and do not have to care for. Simply turn your LED on and off at the right places within your program.

                                       

                                     

                                       

                                    Happy lighting

                                       

                                    Bob

                                    1 2 3 Previous Next