workaround for a pointer to bit

  • 1
  • 2
  • 3
  • Page 2 of 3
Avra
Schreiberling
Avatar
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 00:33  ·  #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
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 10:23  ·  #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
Gender: n/a
Age: 43
Posts: 308
Registered: 07 / 2013
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 10:37  ·  #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
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 11:37  ·  #12
Quote

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
Gender: n/a
Age: 43
Posts: 308
Registered: 07 / 2013
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 12:34  ·  #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
Gender:
Age: 24
Posts: 1409
Registered: 03 / 2005
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 13:13  ·  #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
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 24.06.2014 - 14:23  ·  #15
Quote by 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
Gender:
Location: Belgrade, Serbia
Age: 53
Homepage: rs.linkedin.com/in…
Posts: 653
Registered: 07 / 2002
Subject:

Re: workaround for a pointer to bit

 · 
Posted: 26.06.2014 - 13:03  ·  #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
  • Page 2 of 3
Selected quotes for multi-quoting:   0

Registered users in this topic

Currently no registered users in this section

The statistic shows who was online during the last 5 minutes. Updated every 90 seconds.
MySQL Queries: 15 · Cache Hits: 14   126   140 · Page-Gen-Time: 0.022048s · Memory Usage: 2 MB · GZIP: on · Viewport: SMXL-HiDPI