Read signals

Status
Not open for further replies.

thanos

Well-known member
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 && [COLOR="#FF0000"]GPIOB_PDIR == (1 << 2)[/COLOR] ) {                   
    stick_channel_1 = 1;                                                
    timer_channel_1 = current_time;                                     
  }
  else if (stick_channel_1 == 1 && [COLOR="#FF0000"]GPIOB_PDIR == (0 << 2)[/COLOR]) {                  
    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??
 
Something about shifting a ZERO seems odd here :: else if (stick_channel_1 == 1 && GPIOB_PDIR == (0 << 2))
 
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);
  }

}
 
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
 
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.
 
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?
 
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.
 
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
 
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?
 
Status
Not open for further replies.
Back
Top