0

By using the following draft i decode the signals from an RF receiver

#define CH1 2
#define CH2 3
#define CH3 4
#define CH4 5
#define CH5 6
#define CH6 7

int ch1Time, ch2Time, ch3Time, ch4Time, ch5Time, ch6Time = 0;
void channels()
{
  TCNT1 = 0;
  ch1Time = 0; ch2Time = 0; ch3Time = 0; ch4Time = 0; ch5Time = 0; ch6Time = 0;

  while ((PIND & ((1 << CH1) | (1 << CH2) | (1 << CH3) | (1 << CH4) | (1 << CH5) | (1 << CH6))))
  {
    if (PIND & (1 << CH1))
      ch1Time = TCNT1;

    if (PIND & (1 << CH2))
      ch2Time = TCNT1;

    if (PIND & (1 << CH3))
      ch3Time = TCNT1;

    if (PIND & (1 << CH4))
      ch4Time = TCNT1;

    if (PIND & (1 << CH5))
      ch5Time = TCNT1;

    if (PIND & (1 << CH6))
      ch6Time = TCNT1;
  }
}

The issue is that i need also to use the 6 PWM's of the nano board. So i thought of moving the channels 1-6 to the analog pins like so..

#define CH1 A0
#define CH2 A1
#define CH3 A2
#define CH4 A3
#define CH5 A4
#define CH6 A5


while ((PINC & ((1 << CH1) | (1 << CH2) | (1 << CH3) | (1 << CH4) | (1 << CH5) | (1 << CH6))))
  {
    if (PINC & (1 << CH1))
      ch1Time = TCNT1;
etc...

but it simply does not work. Any thoughts please?

0

I see two immediate problems:

  1. The macros A0-A5 are the numbers 14 to 19, not the numbers 0 to 5 - so you're looking at (1 << 14) for CH1 not (1 << 0).
  2. The analog pins are port C not port B, so you need to use PINC not PINB.
  3. Your ch?Time variables should be volatile.
|improve this answer|||||
  • Oups. I am actually using PORTC and that is a typo. I will edit it asap. The channels method is called by... attachInterrupt(0, channels, RISING); – OrElse Feb 24 at 19:50
  • Actually i do not get it. (PINC & (1 << 0)) for CH1 does not work also, with pinMode(A0, INPUT); – OrElse Feb 24 at 20:04
  • Added the volatile option but the issue remains. – OrElse Feb 24 at 20:16
  • @OrElse Are you sure you want to be clearing TCNT1 just before you read from it? – Majenko Feb 24 at 20:20
  • Actually yes. This way by counting the time, i decode the RF receiver signal which is from 1ms to 2ms. Plus this works great on digital inputs. TCCR1A = 0; TIMSK1 = 0; TIFR1 = 0; TCNT1 = 0; TCCR1B = (1 << CS10); Thank you for all your effort – OrElse Feb 24 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.