Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 13 of 13

Thread: Read signals

  1. #1
    Senior Member
    Join Date
    Aug 2016
    Posts
    149

    Read signals

    I want to read the signals from a transmitter,so i wrote the following code and read channel 1 correctly:

    Code:
    volatile byte stick_channel_1;
    volatile int transmitter_channel_1;
    volatile unsigned long timer_channel_1;
    
    void transmitter_signals();
    void print_signals();
    
    void setup() {
    
      while (!Serial);
      Serial.begin(9600);
    
      pinMode(19, INPUT_PULLUP); 
    
      attachInterrupt(digitalPinToInterrupt(19), transmitter_signals, CHANGE);
    
    }
    
    void loop() {
    
     delay(250);
     Serial.println(transmitter_channel_1);
    
    }
    
    void transmitter_signals() {
    
      current_time = micros();
    
      if (stick_channel_1 == 0 && digitalRead(19) == 1 ) {                   
        stick_channel_1 = 1;                                                
        timer_channel_1 = current_time;                                     
      }
      else if (stick_channel_1 == 1 && digitalRead(19) == 0 ) {                  
        stick_channel_1 = 0;                                                 
        transmitter_channel_1 = current_time - timer_channel_1;             
      }
    }
    Then ι wanted to read channel 1 using PDIR register with the following code:

    Code:
    volatile byte stick_channel_1;
    volatile int transmitter_channel_1;
    volatile unsigned long timer_channel_1;
    
    void transmitter_signals();
    void print_signals();
    
    void setup() {
    
      while (!Serial);
      Serial.begin(9600);
    
      PORTB_PCR2 =  PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);  // input_pullup                                                   
     
      GPIOB_PDDR &= ~(1 << 2); //input
    
      attachInterrupt(digitalPinToInterrupt(19), transmitter_signals, CHANGE);
    
    }
    
    void loop() {
    
     delay(250);
     Serial.println(transmitter_channel_1);
    
    }
    
    void transmitter_signals() {
    
      current_time = micros();
    
      if (stick_channel_1 == 0 && GPIOB_PDIR == (1 << 2) ) {                   
        stick_channel_1 = 1;                                                
        timer_channel_1 = current_time;                                     
      }
      else if (stick_channel_1 == 1 && GPIOB_PDIR == (0 << 2)) {                  
        stick_channel_1 = 0;                                                 
        transmitter_channel_1 = current_time - timer_channel_1;             
      }
    }
    but i can not read the channel 1,i have made some mistake in the register pdir??

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,534
    Why not simply use digitalReadFast(19) ?

  3. #3
    Senior Member
    Join Date
    Aug 2016
    Posts
    149
    You are right,i just want to know what is wrong.

  4. #4
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,710
    Something about shifting a ZERO seems odd here :: else if (stick_channel_1 == 1 && GPIOB_PDIR == (0 << 2))

  5. #5
    Senior Member
    Join Date
    Aug 2016
    Posts
    149
    I tried to activate a led with a button with this code and works properly.

    Code:
    void setup() {
    
     PORTC_PCR5 =  PORT_PCR_SRE | PORT_PCR_DSE | PORT_PCR_MUX(1); 
     PORTB_PCR2 = PORT_PCR_PE | PORT_PCR_PS | PORT_PCR_MUX(1);    
    
     GPIOB_PDDR &= ~(1 << 2); // input
     GPIOC_PDDR |= (1 << 5);  // output
    
    }
    
    void loop(){
    
      if (GPIOB_PDIR == (0 << 2)) {  // if pin ptb2 = 0 turn LED on:    
       
        GPIOC_PSOR |= (1 << 5);  
      } 
      else {
      
        GPIOC_PCOR |= (1 << 5);
      }
    
    }

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,534
    if (GPIOB_PDIR == (0 << 2)) { // if pin ptb2 = 0 turn LED on:
    erroneous! This condition will never be true even when ptb2 is low but when any other pin on port B is high. Think!

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,155
    Code:
      if (GPIOB_PDIR == (0 << 2)) {  // if pin ptb2 = 0 turn LED on
    This is wrong. It should be:
    Code:
      if ((GPIOB_PDIR & (1 << 2)) == 0) {  // if pin ptb2 = 0 turn LED on
    You have a similar problem in the code in your first message.

    Pete

  8. #8
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,710
    Quote Originally Posted by Theremingenieur View Post
    erroneous! This condition will never be true even when ptb2 is low but when any other pin on port B is high. Think!
    That is what I thought

  9. #9
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,576
    Want to emphasize again using the easy digitalRead, digitalWrite, pinMode functions avoid all the extra bug-prone complexity of direct register access.

    They also will make your program compatible with most other boards. When we have Teensy 4 next year (with ~5X better performance), the GPIO registers will be different. This sort of register access code will be tied tightly to the Teensy 3 hardware.

  10. #10
    Senior Member
    Join Date
    Aug 2016
    Posts
    149
    Paul i will not disagree with what you said,but it is the only solution.
    On my board i have some pins which are not registered,you can help to register them??
    On my thread "Teensy 3.6 diy board" (post 56) in the archives "core_pins.h" and "pins_tensy.c"
    i made some changes and added, but the new pin does not work.
    You can help me?

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,710
    Confirm your understanding of the base logic and testing refining the above code to work on known good pins. Perhaps test with digital Read/Write - then move to the direct pin manipulation on those same pins. Logic errors like above could be hiding usable functionality.

    Perhaps one first test would be to use the direct pin manipulation to set all pins INPUT_PULLUP as that would be easy to observe.

  12. #12
    Senior Member
    Join Date
    Dec 2016
    Location
    Montreal, Canada
    Posts
    3,070
    thats usually what i did for spi_mst when finding the port register for the spi cs pin, i set the pin as output asserted and checked the ports to see which had the bit set. in his case he could set all the ports to input pullup then ground the pin, then check the ports to see which bit is not set. as a safety measure if you’re not comfortable with grounding a pin when manipulating ports or stuff like this, always use a series resistor to prevent possible shorts

  13. #13
    Senior Member
    Join Date
    Aug 2016
    Posts
    149
    The pcb board is already ready and i can not make any changes to the pins.
    The addition of new pins it can not be done?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •