1 2 3 Previous Next 41 Replies Latest reply on Sep 27, 2015 2:57 PM by DaKn_263916

    Reading a pulses

      Hi 

         

      Hi I am new to PSoC. 

         

      I want to read 3 pulses in a row and then set a pin High.  

         

      This is my code it does not work. 

         

       

         

      int main()
      {
         CyGlobalIntEnable;
         UART_1_Start();
            /* Place your initialization/startup code here (e.g. MyInst_Start()) */
       
          /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
      for(;;) {
       

         

        high = Pin_1_Read(); // reading the pin
         
          
          while(count < 3)    
          {
          count = count+high;   
              
          }
         
          if(count > 3)
          {
              Setpin_Write(1); 
              
          }
          else 
          Setpin_Write(0); 

        • 1. Re: Reading a pulses
          DaKn_263916

          Are you trying to read 3 pulses in a row or a pulse that is 3 x wider than

             

          some reference pulse ?

             

           

             

          Normally you would use a counter to count the pulses if former, or a

             

          timer to measure pulse width for latter case. If former case do the 3 pulses

             

          have to occur within some time limit window ?

             

           

             

          Regards, Dana.

          • 3. Re: Reading a pulses

            I am trying to read three pulses in a row, where there are ex 50 us between them. Can't I use a pin to read the pulses and then stored them in variable?

            • 5. Re: Reading a pulses
              DaKn_263916

              The problem is your code defines when they will get detected, versus

                 

              notifying you with an interrupt that 3 have occured.

                 

               

                 

              I would use a down counter, set its period to 2, and use an ISR on its Tc, terminal

                 

              count. In ISR you set a flag, and reset counter, in main() process the flag.

                 

               

                 

              Regards, Dana.

              • 6. Re: Reading a pulses

                Okay thanks. One more question. I have also problems starting the ADC. 

                   

                 

                   

                #include <project.h>
                uint8 variabel = 0;
                uint8 buffer = 0; 
                uint8 a = 0; 
                uint8 b = 0; 

                   

                int main()
                {
                ADC_Start();
                //ADC_DelSig_1_SetCoherency(ADC_DelSig_1_COHER_LOW);
                ADC_StartConvert();
                    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

                   

                    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
                    for(;;)
                    {
                         variabel = ADC_GetResult8();
                         buffer =ADC_CountsTo_Volts(ADC_GetResult8());
                        /* Place your application code here. */
                    }
                 
                }

                • 7. Re: Reading a pulses
                  DaKn_263916

                  Is your A to D named "ADC" or ADC_DelSig_1" ? If latter

                     

                  you have several f()'s you are calling with the wrong name

                     

                  associated with the A to D.

                     

                   

                     

                  If former then

                     

                   

                     

                  ADC_Start();
                  //ADC_SetCoherency(ADC_COHER_LOW);
                  ADC_StartConvert();

                     

                   

                     

                  If latter then

                     

                   

                     

                  ADC_DelSig_1_Start();
                  //ADC_DelSig_1_SetCoherency(ADC_DelSig_1_COHER_LOW);
                  ADC_DelSig_1_StartConvert();

                     

                   

                     

                  and the others used below these.

                     

                   

                     

                  Regards, Dana.

                  • 8. Re: Reading a pulses

                    The ADC is named ADC. I think it might be something else? 

                    • 9. Re: Reading a pulses
                      DaKn_263916

                      Keep in mind you can always open an example project, add to your workspace, and

                         

                      look at it, deleting it from workspace when done.

                         

                       

                         

                      Consider posting your project, makes life easier to troubleshoot.

                         

                       

                         

                      “File”                                                             Creator

                         

                      “Create Workspace Bundle”

                         

                       

                         

                       

                         

                      Regards, Dana.

                      • 10. Re: Reading a pulses

                        Ok. I have attached the file. 

                        • 11. Re: Reading a pulses
                          JoMe_264151

                          You need to enable interrupts before starting your ADC.

                             

                           

                             

                          Bob

                          • 12. Re: Reading a pulses

                            CyGlobalIntEnable ?  I am testing by putting the VDD pin into the ADC pin.. Still not working

                            • 13. Re: Reading a pulses
                              DaKn_263916

                              Odd, the datasheet only implies single sample mode needs interrupts

                                 

                              enabled, and TRM only seems to confirm if you want to use an interrupt

                                 

                              you can, otherwise poll.

                                 

                               

                                 

                              Regards, Dana.

                              • 14. Re: Reading a pulses
                                DaKn_263916

                                        variabel = ADC_GetResult8();
                                        buffer =ADC_CountsTo_Volts(variabel);
                                        UART_1_WriteTxData(variabel);

                                   

                                 

                                   

                                You should use GetResult16, and declare variable as int16 -

                                   

                                 

                                   

                                int8 ADC_GetResult8(void)
                                Description: Returns a signed 8-bit value. The largest positive signed 8-bit value that can be represented
                                is 127, but in single-ended 8-bit mode, the maximum positive value is 255. Hence, for 8-bit
                                single-ended mode, use the ADC_GetResult16() function instead
                                . Note that if the ADC
                                resolution is set greater than 8 bits, the LSB of the result is returned.

                                   

                                 

                                   

                                        buffer =ADC_CountsTo_Volts(variabel);

                                   

                                 

                                   

                                float ADC_CountsTo_Volts(int32 adcCounts)
                                Description: Converts the ADC output to volts as a floating point number. For example, if the ADC
                                measures a voltage of 1.2345 V, the returned result would be +1.2345 V.

                                   

                                 

                                   

                                buffer now has a cast of the float in it back to uint8, not a char string which is what you need to send

                                   

                                via UART. Note when you call CountsTo_Volts cast variable to a int32 in the call. Note you need

                                   

                                the receiving variable to be a float.

                                   

                                 

                                   

                                now use sprintf() to fill a new char buffer which you should create with the chars that represent the float,

                                   

                                and use the send string api in UART API set.

                                   

                                 

                                   

                                Attached the formatting chars to use for sprintf().

                                   

                                 

                                   

                                Regards, Dana.

                                1 of 1 people found this helpful
                                1 2 3 Previous Next