Reading a 5 bit encoder on a Port with 0ne instruction

Note that I'm not familiar with the Due so can't quite help but forget about decimal and think binary.

1111 1111 1111 1111 1111 1111 1111 1101

A wild guess is that that number sets the output direction of the second pin of a port.
Similar, 0x00000002 equals

0000 0000 0000 0000 0000 0000 0000 0010

Note the position of the bit that is 1 and compare it with the bit that is 0 inthe previous number.

Hi Sterretjie,
Thank you for your email. I changed 0000 0000 0000 0000 0000 0000 0000 0011 in the loop () and nothing changed on the Due! and nothing changed. The same LED was still flashing! I realise that my knowledge is almost zero on this chip and I managed to find the datasheet. Did not have enough time to study it carefully yet. So like some guys say RTFM, I can see there are not much available in sample format to at least read a port with 1 instruction, so let me get into the manual!!!!!!

Hi Railroader,
I am still basically where I started knowledge wise after spending a few hours on port reading and writing and managed to get a whole port working and 1 pin, but could not make enough sense to make progress. So today I am going to try your shift example out.
Regards
Cobus

You need to read the inputs like
bit1 = digitalRead(bit1pin);
bit2 = digitalRead(bit2pin);
.
.
before You shift and add them.

Thank you, fortunately I do that already!
But thanks for taking the time to check!
Rgds Cobus

Hi Railroader,
I used your code for bit reading and this is the only way I could get bitcount to be correct. The reading is 0 to 32.

void loop()
{
BIT1state = digitalRead(Bt1);
BIT2state = digitalRead(Bt2);
BIT3state = digitalRead(Bt3);
BIT4state = digitalRead(Bt4);
BIT5state = digitalRead(Bt5);
count1 = BIT1state;
count2 = BIT2state << 1;
count3 = BIT3state << 2;
count4 = BIT4state << 3;
count5 = BIT5state << 4;
bitcount = count1 + count2 + count3 + count4 + count5;
}

Yes, that's my idea. 0 - 31....
If You want You can drop the intermediate variables and compact it to
sum = digitalRead(pin0) + (digitalRead(pin1) << 1) +.......
This ought to be rather quick. I don't know how to perform a port read of all 5 bits at the same time. However this code should work for any controller.

Thanks, will try that!!!

Hi Guys,
I managed to get the one write and change 4 ouputs done like this. Thank you for your patience with me. Reading a port with only 5 inputs of the 32 without having to gnd all the others is still a challenge.

void setup() 
{
  //PC0 is the erase pin
  pinMode(33, OUTPUT); 
  pinMode(34, OUTPUT); 
  pinMode(35, OUTPUT); 
  pinMode(36, OUTPUT); 
  REG_PIOC_OWER = B11110;// 4 lasb bits enabled and last one not because PC0 is erase pin.
  REG_PIOC_OWDR = 0XFFFFFFE0; //The lsb 6 bits = 0`s and the first 26 bits = 1`s ie disabled
}

void loop() 
{
  REG_PIOC_ODSR = B10000; //0x00000036/35/34/33/; 
  delay(100);             
  REG_PIOC_ODSR = 0x00000000;    
  delay(1000); 
  REG_PIOC_ODSR = B01000; //0x00000036/35/34/33/; 
  delay(100);             
  REG_PIOC_ODSR = 0x00000000;    
  delay(100); 
  REG_PIOC_ODSR = B00100; //0x00000036/35/34/33/; 
  delay(100);             
  REG_PIOC_ODSR = 0x00000000;    
  delay(100); 
  REG_PIOC_ODSR = B00010; //0x00000036/35/34/33/; 
  delay(1000);             
  REG_PIOC_ODSR = 0x00000000;    
  delay(1000);
     
}

Regards
Cobus

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.