workaround for a pointer to bit

  • 1
  • 2
  • 3
  • Seite 2 von 3
Avra
Schreiberling
Avatar
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 00:33 Uhr  ·  #9
In my case board already has defined pins like this:
Code
const
  DI_1_TAGNUM: word = 14;
  DI_2_TAGNUM: word = 15;

var
{$PDATA}
  DI_1[@PinA, 0]: bit;
  DI_2[@PinA, 1]: bit; 


and since AvrCo doesn't have a bit pointer I will not be able to simply loop through an array of records like in my first post, but to manually code something like this:
Code
  case TagNum of
    ...
      DI_1_TAGNUM:
         if Command = cmdRead  then Result := DI_1; endif;
         if Command = cmdWrite then DI_1 := NewValue; endif;|
      DI_2_TAGNUM:
         if Command = cmdRead  then Result := DI_2; endif;
         if Command = cmdWrite then DI_2 := NewValue; endif;|
    ...
  endcase;


Of course this is over simplified example of what I have, but you can see from it that using bit setting and getting functions would complicate things, make code less readable, and make me maintain bits pin list at two places: var section and code section.
Merlin
Administrator
Avatar
Geschlecht:
Alter: 25
Beiträge: 1474
Dabei seit: 03 / 2005
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 10:23 Uhr  ·  #10
Hi Avra.

Here is an adaptation of your original suggestion that shows what I had in mind.

Code

type 
  PByte = pointer to byte; 
  p = pointer; 
  TTagStruct = record 
    No : word; 
    Tag: PByte;
    Offset : byte; 
  end; 

var 
{$PDATA} 
  DI_1[@PinA, 0]: bit; 
  DI_2[@PinA, 1]: bit; 

const // or structconst 
  MX: array[1..2] of TTagStruct = ((No: 8; Tag: @PinA; Offset: 0), 
                                   (No: 23; Tag: @PinA; Offset: 1)); 


then

Code

with MX[1] do
  if Bit(Tag^, Offset),  then 
    WriteLn(Serout, 'that bit is on'); 
  else 
    WriteLn(Serout, 'that bit is off'); 
  endif; 
endwith;


I don't know if it will work but...
Thomas.AC
Benutzer
Avatar
Geschlecht: keine Angabe
Alter: 44
Beiträge: 308
Dabei seit: 07 / 2013
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 10:37 Uhr  ·  #11
some suggestions to handle port bits ...


Example 1: not working, compiler error, why?
Code
const
    MYBIT_REG : word = @PINA;
    MYBIT_NR : byte = 4;

var
    MYBIT[MYBIT_REG,MYBIT_NR] : bit; // compile error: address $0608 is below iDataStart $2000

begin
    // accessing the BIT //
    MYBIT := 1;
end.


Example2: not working, Compiler error, why?
Code
const
    MYBIT_REG : pointer = @PINA;
    MYBIT_NR : byte = 4;

var
    MYBIT[MYBIT_REG,MYBIT_NR] : bit; // compile error: $0608 is below iDataStart $2000

begin
    // accessing the BIT //
    MYBIT := 1;
end.


Example3: not working, compiler error, why?
Code
const
    MYBIT_NR : byte = 4;
    
alias
    MYBIT_REG = PINA;

var
    MYBIT[@MYBIT_REG,MYBIT_NR] : bit; // compile error: Identifier expected

begin
    // accessing the BIT //
    MYBIT := 1;
end.


Example4: working
Code
// a variable for register + const for bitnumber
const
    MYBIT_NR : byte = 4;

var
    MYBIT_REG[@PINA] : byte;
    MYBIT[@MYBIT_REG,MYBIT_NR] : bit;

begin
    // accessing the bit //
    MYBIT := 1;
    // or //
    MYBIT_REG.MYBIT_NR := 1;
end.


Example5:
Code
// an alias for the register + const for bitnumber
alias
    MYBIT_REG = PINA;

const
    MYBIT_NR : byte = 4;


var
    // // it is not possible to define a bit with an alias //
    // MYBIT[@MYBIT_REG,MYBIT_NR] : bit; // compiler error: Identifier expected

begin
    // accessing the bit //
    MYBIT_REG.MYBIT_NR := 1;
end.


Example6:
Code
// an alias for the register + alias for bitnumber
const
    BIT4 : byte = 4; // workaround for compiler error

alias
    MYBIT_REG = PINA;
    //BITNR = 4; // compiler error: Identifier expected
    MYBIT_NR = BIT4;

var
    // // it is not possible to define a bit with an alias //
    // MYBIT[@MYBIT_REG,MYBIT_NR] : bit; // compiler error: Identifier expected
    
begin
    // accessing the bit //
    MYBIT_REG.MYBIT_NR := 1;
end.
Merlin
Administrator
Avatar
Geschlecht:
Alter: 25
Beiträge: 1474
Dabei seit: 03 / 2005
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 11:37 Uhr  ·  #12
Zitat

Example 1: not working, compiler error, why?
Code

const 
    MYBIT_REG : word = @PINA; 
    MYBIT_NR : byte = 4; 

var 
    MYBIT[MYBIT_REG,MYBIT_NR] : bit; // compile error: address $0608 is below iDataStart $2000

begin 
    // accessing the BIT // 
    MYBIT := 1; 
end.




because it is in the wrong data space.

you need

Code

const 
    MYBIT_REG : word = @PINA; 
    MYBIT_NR : byte = 4; 

{$PDATA}
var 
    MYBIT[MYBIT_REG,MYBIT_NR] : bit; // compile error: address $0608 is below iDataStart $2000
{$IDATA}

begin 
    // accessing the BIT // 
    MYBIT := 1; 
end.


Ditto for example 2.
Thomas.AC
Benutzer
Avatar
Geschlecht: keine Angabe
Alter: 44
Beiträge: 308
Dabei seit: 07 / 2013
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 12:34 Uhr  ·  #13
Hello merlin,

thank you very much. I was so confused about example 1 and 2.

Example 3 with alias Statement still does not work, should it?
Merlin
Administrator
Avatar
Geschlecht:
Alter: 25
Beiträge: 1474
Dabei seit: 03 / 2005
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 13:13 Uhr  ·  #14
Hello Thomas.

In my opinion, yes 3 should work, but I can understand why it doesn't. Aliases are relatively new, and bits are so special, it is perhaps no surprise that it is not implemented yet.
Avra
Schreiberling
Avatar
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 24.06.2014 - 14:23 Uhr  ·  #15
Zitat geschrieben von Merlin
with MX[1] do
  if Bit(Tag^, Offset),  then 
    WriteLn(Serout, 'that bit is on'); 
  else 
    WriteLn(Serout, 'that bit is off'); 
  endif; 
endwith;

:-k
Your plastic example made me think again and it seams that Offsets can be constants so that with little code adaptation bit numbers would not have to be maintained in two separate lists, and I can still iterate through an array.

Merlin, thanks a lot for your persistence and help! :gut:
:occasion5:
Avra
Schreiberling
Avatar
Geschlecht:
Herkunft: Belgrade, Serbia
Alter: 54
Homepage: rs.linkedin.com/in…
Beiträge: 653
Dabei seit: 07 / 2002
Betreff:

Re: workaround for a pointer to bit

 · 
Gepostet: 26.06.2014 - 13:03 Uhr  ·  #16
Hello Merlin,

Above construction works with MX[] stored in structconst section. With MX[] stored in const section, I had to adjust a little:

Code
var
  p: pointer;

...

with MX[1] do
  p := Tag;                   // workaround to avoid compile error with MX[] in const
  if Bit(p^, Offset),  then   // workaround to avoid compile error with MX[] in const
    WriteLn(Serout, 'that bit is on'); 
  else 
    WriteLn(Serout, 'that bit is off'); 
  endif; 
endwith; 


I am publishing this info in case someone else bumps into the same problem.

Thanks again,
Avra
  • 1
  • 2
  • 3
  • Seite 2 von 3
Gewählte Zitate für Mehrfachzitierung:   0

Registrierte in diesem Topic

Aktuell kein registrierter in diesem Bereich

Die Statistik zeigt, wer in den letzten 5 Minuten online war. Erneuerung alle 90 Sekunden.
MySQL Queries: 15 · Cache Hits: 14   126   140 · Page-Gen-Time: 0.043318s · Speichernutzung: 2 MB · GZIP: ein · Viewport: SMXL-HiDPI