Reading a pulses

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

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); 

0 Likes
1 Solution
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given
0 Likes
41 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Duplicate post, thread being handled here - http://www.cypress.com/forum/psoc-3-device-programming/reading-pulses

0 Likes
Anonymous
Not applicable

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?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Anonymous
Not applicable

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. */
    }
 
}

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
Anonymous
Not applicable

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Ok. I have attached the file. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You need to enable interrupts before starting your ADC.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

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

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

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.

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

        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.

Anonymous
Not applicable

OK thanks: 

   

One more question. If I want to read 6 bytes of ACSII values like " 42AAT1" from a UART block . How can I do this?

   

My idea was stored the data in char variabel like char data[] = UART_GetChar();  but I get an error when doing this. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

What error are you getting ? Consider posting project.

   

 

   

“File”                                                             Creator

   

“Create Workspace Bundle”

   

 

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

This my code for sending 6 bytes and receiving the 6 bytes.. I have bunch of errors.. 😉

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

You have this

   

 

   

void handel( uint8 addr1_, uint8 addr2_, uint8 addr3, uint8 addr4, uint8 addr5, uint8 addr6 )

   

 

   

and this

   

 

   

uint8 adrr1_ = 0;
uint8 adrr2_ = 0;
uint8 adrr3_ = 0;
uint8 adrr4_ = 0;
uint8 adrr5_ = 0;
uint8 adrr6_ = 0;

   

 

   

Are you intending all these variable names be typed the same ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Ups sorry.. It should of course be. 

   

uint8 addr1_ = 0;
uint8 addr2_ = 0;
uint8 addr3_ = 0;
uint8 addr4_ = 0;
uint8 addr5_ = 0;
uint8 addr6_ = 0;

0 Likes
lock attach
Attachments are accessible only for community members.
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This compiles, attached.

   

 

   

Regards, Dana.

Anonymous
Not applicable

Okay. Thanks. 

   

Though is seems that this part does not work..

   


void handel( uint8 addr1_, uint8 addr2_, uint8 addr3_, uint8 addr4_, uint8 addr5_, uint8 addr6_ )

   

{
    
    if( ( addr1_ = '4' ) && ( addr2_ = '5' ) && ( addr3_ = 'A' ) &&
        ( addr4_ = 'A' ) && ( addr5_ = 'R' ) && ( addr6_ = '1' ) )
    {
         UART_1_PutString(" Send 45AAR.1\0");
    } 
    else 
  

   

The UART throws out " Send 45AAR.1\0" on whatever I transmit... 😉

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

When you execute this

   

 

   

UART_1_PutString(" Send 45AAR.1\0");

   

 

   

You are telling the UART to send the string inside your quote marks, so it is doing

   

what you told it to ?

   

 

   

What is it you want to send ?

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

Yes. But UART_1_PutString(" Send 45AAR.1\0"), should only be executed if I send 45AAR1. But if I send 222222 it still executes the if -function  sending "Send 45AAR.1\0"

   

 😉

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I see the problem, this -

   

 

   

    if( ( addr1_ = '4' ) && ( addr2_ = '5' ) && ( addr3_ = 'A' ) &&
        ( addr4_ = 'A' ) && ( addr5_ = 'R' ) && ( addr6_ = '1' ) )
    {
         UART_1_PutString(" Send 45AAR.1\0");
    } 
    else 

   

 

   

should be typed as this -

   

 

   

    if( ( addr1_ == '4'  ) && ( addr2_ == '5'  ) && ( addr3_ == 'A' ) &&
         ( addr4_ == 'A' ) && ( addr5_ == 'R' ) && ( addr6_ == '1'  ) )
    {
         UART_1_PutString(" Send 45AAR.1\0");
    } 
    else 

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi Dana

   

I still have problems with receiving the correct value.

   

When I send 45AAR01, the function handel should send 45AAR.1 through the UART but what happens is that  the code goes to my else statement.

   

I have no clue why it does this.  

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You make the whole job within your interrupt routine, which could be done better, define an Rx buffer of size 30 and poll in your main-loop for RxBufferSize() >= 7 (look at the APIs for UART) and then read off the bytes. Your current approach assumes that the UART keeps your 7 'A's, but it is (without the buffer) only able to keep 4 bytes.

   

Do not re-start the interrupt handler in your main-loop

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi, thanks for the reply.

   

I am not sure, I am understanding you correct.  

   

Ok, the UART Rx Buffersize is only four bytes, and I am sending 7 bytes.

   

I tried to look up RxBuffersize() under API, and I couldn't find such a function. Under defines I found this UART_RXBUFFERSIZE.

   

If I make an Rxbuffersize , How can I read it?

   

char RxBuffer[30] = {0}; ¨

   

if(UART_1_GetRxBufferSize() != 0){   // Returns the number of received bytes remaining in the RX buffer
                RxBufferi++] = UART_1_GetChar();         
                 if (strstr(resp, "<AAAAAAA>") != NULL)    
                  {                
                  UART_1_PutString("BBBBBBB");

   

}

0 Likes
Anonymous
Not applicable

I have change my code to this. 

   

do  {
    if(UART_1_GetRxBufferSize() != 0){   // Returns the number of received bytes remaining in the RX buffer
                Rxbuffersize[i++] = UART_1_GetChar();         
                 if (strstr(Rxbuffersize, "45AAR01~") != NULL)    
                  {                
                  UART_1_PutString("45AAR1");  // the problem is here!!
                  ans = 1;  
                UART_1_ClearRxBuffer();
}
    } 
}
  while(ans == 0); }

   

The problem is now, the sending part.  When it should print 45AAR1,  on my terminal I can read 45AA1, it cuts off the R.

   

 Regards

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

The Line

   

UART_1_PutString("45AAR1"); will under all circumstances send the string "45AAR1" over the connection. Check that with a logic analyzer. How do you connect your PSoC3 to the PC? Schematic?

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi

   

It executes the code UART_1_PutString("45AAR1"), but what I see on my terminal is something different than what I send. It cuts off one byte. 

   

 I am using a UART to USB converter to plug it in the PC. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I would suggest you to write an UART Test program to prove a working connection between PSoC and PC. I can assure you that sending via UART  (at least in PSoC 5 world, but there's no difference to PSoC3) works flawlessly. Your speed is already low (9600) you may remove the Tx-hardware control and try a different terminal program (PuTTY) or a different PC (operating system).

   

 

   

Bob

0 Likes
Anonymous
Not applicable

The UART test runs fine. 

   

The code runs fine until I UART_1_PutString("45AAR1") line, where it cuts some of the bytes off.  Is it the do while statement or something else? Could you give me a more concrete  response?

   

Best regards. 

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

I have now uploaded i Screenshot of my terminal. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Well, what happens to your program when ans == 1? Your main() ends and the rest of your data to be sent goes into electronic nirwana. A main() for an embedded controller usually does never end executing an infinite loop. This is not the case for your program. Add a

   

while(1); at the end of main() or put your code to repeat (NOT the initialization) into an infinite loop.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Some how my account has ended in spam filter.

   

Thanks for the reply. 

   

doesn't my main only end, when I receive all my bytes like  

   


                 if (strstr(Rxbuffersize, "1011") != NULL)    //compare the two;
                  {                
                  UART_1_PutString("11");                    //print this out;
                  ans = 1;                                                 //all bytes received end the whille loop
                
               
 

0 Likes
Anonymous
Not applicable

I have put a While(1) at the end, but unfortunately it still does not work. 

   


int main()
{
   UART_1_Start();
   CyGlobalIntEnable
 
  
    if(UART_1_GetRxBufferSize() != 0){   // Returns the number of received bytes remaining in the RX buffer
                Rxbuffersize[i++] = UART_1_GetChar();  // Get the bytes and put it in the array
             
                 if (strstr(Rxbuffersize, "1011") != NULL)    //compare the two;
                  {                
                  UART_1_PutString("11");                    //print this out;
                  ans = 1;  
                
               
}
    } 
     while(ans == 0);
    while(1);
}

   

Thank you very much for the replies 😉

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Something like this -

   

 

   

int main( ) {

   

   UART_1_Start();
   CyGlobalIntEnable
   
    while( ans == 0 )  {
    
        if(UART_1_GetRxBufferSize()  !=  0 ) {               // Returns the number of received bytes remaining in the RX buffer

   

                Rxbuffersize[i++] = UART_1_GetChar();   // Get the bytes and put it in the array
             
                if (strstr(Rxbuffersize, "1011") != NULL) { //compare the two;
                                  
                    UART_1_PutString("11");                  //print this out;
                    ans = 1;  
                }
        }
    }

   

    while(1);

   

}

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

For many } if I remove 1, I get an error.  UART Rx? I don't quite understand 😉

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I removed/edited my prior post.

   

 

   

Regards, Dana.

0 Likes