EPROM like look up data

vk3gjm

New Member
Hi All,

Please excuse my simply request for information. I am very new to PICAXE and programming, but I consider myself a fast learner.

I am trying to operate 2 sets of 4 relays that control 2 RF attenuators that will give me from zero to 121 dB off attenuation in 1dB steps. I have worked out the relay sequence table and manually programmed an EPROM that delivers the required output from a set of input thumbwheel switches, this works.

However I am keen to explore the use of a 20x2 and use only 2 up/down switches which will be units and tents, this will get me to 121. I an going to drive a basic Serial LCD display to show attenuation, simple and over the top.

My question is, how does one add basic pre-defined values (fixed look up table) which is an attenuation value based on the specific attenuator values used. The attenuator values are 1, 2, 4, 4 and 10, 20, 40, 40 dB, total 121 dB.

Regards

Gerald

moved to Active Forum
 
Last edited by a moderator:

AllyCat

Senior Member
Hi Gerald,

The lookup could probably be done as easily with some Basic code such as (not complete or tested):
Code:
symbol flagbyte = b0
symbol atten = b1
symbol lowatten = b2
symbol highatten = b3

lowatten = atten // 10       ; needs a modification for 120 and 121
highatten = atten / 10 max 11 ; max is one way to handle 120 and 121 correctly
flagbyte = 0
bit0 = lowatten // 2
bit1 = lowatten / 2 // 2
if lowatten > 3 then 
	bit2 = 1
endif
if lowatten > 7 then
	bit3 = 1
endif
;  etc..
but if you want to do it with a lookup table, then the following code which stores the data into the EPROM should work:

Code:
data 0,(%00000000,%00000001,%00000010,%00000011,%00000100 , , , ,%00001101)   ; etc
data (%00010000,%00010001,,,,,,)  ; etc
read atten,flagbyte
Cheers, Alan.
 

westaust55

Moderator
The X2 parts give you the option of a table in EEPROM memory or in TABLE memory (Table memory is embedded within your BASIC program at time of program download and cannot be changed).
Then use a byte variable (0 to 255 range) as a pointer to fetch the next data value and increment or decrement the pointer/address with each corresponding switch press.
 

hippy

Ex-Staff (retired)
I am keen to explore the use of a 20x2 and use only 2 up/down switches which will be units and tents, this will get me to 121.
That should be simple enough ...

For +10 : attenuation = attenuation + 10 Max 121
For -10 : attenuation = attenuation Min 10 - 10
For +1 : attenuation = attenuation + 1 Max 121
For -1 : attenuation = attenuation Min 1 - 1

The attenuator values are 1, 2, 4, 4 and 10, 20, 40, 40 dB, total 121 dB.
It might be easier to do it programmatically rather than through look-up table. Look-up is quicker to execute and you can also do the same to create the EEPROM look-up table to save calculating it by hand ...

Code:
[color=Navy]#Picaxe [/color][color=Black]20X2[/color]
[color=Navy]#Terminal 9600[/color]

[color=Blue]Eeprom [/color][color=Navy]121[/color][color=Black], [/color][color=Blue]([/color][color=Navy]0[/color][color=Blue])

Symbol [/color][color=Purple]relaybits   [/color][color=DarkCyan]= [/color][color=Purple]b0[/color]
[color=Blue]Symbol [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]

[color=Blue]Pause [/color][color=Navy]2000 [/color][color=Green]; To allow Terminal display to appear[/color]

[color=Blue]Gosub [/color][color=Black]InitialiseEeprom[/color]

[color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Navy]121[/color]
[color=Blue]Gosub [/color][color=Black]SetAttentuationRelays[/color]

[color=Blue]Do

  [/color][color=Green]; +10 button
  [/color][color=Blue]If [/color][color=Purple]pinC.0 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
    [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]attenuation [/color][color=DarkCyan]+ [/color][color=Navy]10 [/color][color=DarkCyan]Max [/color][color=Navy]121
    [/color][color=Blue]Gosub [/color][color=Black]SetAttentuationRelays
    [/color][color=Blue]Do [/color][color=Black]: [/color][color=Blue]Loop While [/color][color=Purple]pinC.0 [/color][color=DarkCyan]= [/color][color=Navy]1
  [/color][color=Blue]End If

  [/color][color=Green]; -10 button
  [/color][color=Blue]If [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
    [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]attenuation [/color][color=DarkCyan]Min [/color][color=Navy]10 [/color][color=DarkCyan]- [/color][color=Navy]10
    [/color][color=Blue]Gosub [/color][color=Black]SetAttentuationRelays
    [/color][color=Blue]Do [/color][color=Black]: [/color][color=Blue]Loop While [/color][color=Purple]pinC.1 [/color][color=DarkCyan]= [/color][color=Navy]1
  [/color][color=Blue]End If

  [/color][color=Green]; +1 button
  [/color][color=Blue]If [/color][color=Purple]pinC.2 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
    [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]attenuation [/color][color=DarkCyan]+ [/color][color=Navy]1 [/color][color=DarkCyan]Max [/color][color=Navy]121
    [/color][color=Blue]Gosub [/color][color=Black]SetAttentuationRelays
    [/color][color=Blue]Do [/color][color=Black]: [/color][color=Blue]Loop While [/color][color=Purple]pinC.2 [/color][color=DarkCyan]= [/color][color=Navy]1
  [/color][color=Blue]End If

  [/color][color=Green]; -1 button
  [/color][color=Blue]If [/color][color=Purple]pinC.3 [/color][color=DarkCyan]= [/color][color=Navy]1 [/color][color=Blue]Then
    [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]attenuation [/color][color=DarkCyan]Min [/color][color=Navy]1 [/color][color=DarkCyan]- [/color][color=Navy]1
    [/color][color=Blue]Gosub [/color][color=Black]SetAttentuationRelays
    [/color][color=Blue]Do [/color][color=Black]: [/color][color=Blue]Loop While [/color][color=Purple]pinC.3 [/color][color=DarkCyan]= [/color][color=Navy]1
  [/color][color=Blue]End If

Loop[/color]

[color=Black]InitialiseEeprom:
  [/color][color=Blue]Read [/color][color=Navy]121[/color][color=Black], [/color][color=Purple]relaybits
  [/color][color=Blue]If [/color][color=Purple]relaybits [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]Then
    SerTxd( [/color][color=Red]"Initialising Eeprom"[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
    For [/color][color=Purple]b2 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]121
      [/color][color=Purple]attenuation [/color][color=DarkCyan]= [/color][color=Purple]b2
      relaybits [/color][color=DarkCyan]= [/color][color=Navy]0
      [/color][color=Blue]If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]40 [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit0[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]40 [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]40 [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit1[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]40 [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]20 [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit2[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]20 [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]10 [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit3[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]10 [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]4  [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit4[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]4  [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]4  [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit5[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]4  [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]2  [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit6[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]2  [/color][color=Black]: [/color][color=Blue]End If  
      If [/color][color=Purple]attenuation [/color][color=DarkCyan]>= [/color][color=Navy]1  [/color][color=Blue]Then [/color][color=Black]: [/color][color=Purple]bit7[/color][color=DarkCyan]=[/color][color=Navy]1 [/color][color=Black]: [/color][color=Purple]attenuation[/color][color=DarkCyan]=[/color][color=Purple]attenuation[/color][color=DarkCyan]-[/color][color=Navy]1  [/color][color=Black]: [/color][color=Blue]End If  
      Write [/color][color=Purple]b2[/color][color=Black], [/color][color=Purple]relaybits
    [/color][color=Blue]Next
    SerTxd( [/color][color=Red]"Eeprom initialised"[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
  Else
    SerTxd( [/color][color=Red]"Eeprom already initialised"[/color][color=Black], [/color][color=Blue]CR[/color][color=Black], [/color][color=Blue]LF )
  End If
  Return[/color]

[color=Black]SetAttentuationRelays:
  [/color][color=Blue]Read [/color][color=Purple]attenuation[/color][color=Black], [/color][color=Purple]relaybits
  pinsB [/color][color=DarkCyan]= [/color][color=Purple]relaybits
  dirsB [/color][color=DarkCyan]= [/color][color=Navy]$FF
  [/color][color=Blue]SerTxd( [/color][color=Red]"Attenuation="[/color][color=Black], #[/color][color=Purple]attenuation[/color][color=Black], [/color][color=Red]"dB"[/color][color=Black], [/color][color=Navy]9[/color][color=Black], [/color][color=Red]"Relays=" [/color][color=Blue])
  SerTxd( [/color][color=Black]#[/color][color=Purple]bit7[/color][color=Black],#[/color][color=Purple]bit6[/color][color=Black],#[/color][color=Purple]bit5[/color][color=Black],#[/color][color=Purple]bit4[/color][color=Black], [/color][color=Red]" "[/color][color=Black], #[/color][color=Purple]bit3[/color][color=Black],#[/color][color=Purple]bit2[/color][color=Black],#[/color][color=Purple]bit1[/color][color=Black],#[/color][color=Purple]bit0[/color][color=Black], [/color][color=Blue]CR[/color][color=Black],[/color][color=Blue]LF )
  Return[/color]
 

vk3gjm

New Member
Hi Gents,

Wow, some great stuff here from your replies. to be honest, I may have overstepped my capability hippy. Over the weekend I flowed out my requirements on paper and realised there is more to it than realised. I managed to get a basic look up table working with 122 hex values and use the offset to find a value, but that's about as far I got.

Hippy, the code you provided is certainly comprehensive and complete. However, I would like to study it and understand the limits.

The feedback is wonderful, makes it look so simple. I see there is much to learn, that's what life is about.

Thanks.

Regards

Gerald
 

Paix

Senior Member
Little by little. Just like designing a radio, with an overall plan, then you get down to understanding how to make the component stages that you need. With Hippy and others to guide you, you will soon come to a rapid understanding and be the wiser for it.

You have already brought new ground to the forum for others to work on with you, increasing their|our understanding of the application side as you gain your Picaxe expertise.

Ian
 

vk3gjm

New Member
Hi All,

I have managed to sort through some of the great suggestions and ideas and have added some of my own ideas. I have added the lookup table with offset and it works, well up to a point.

The code is below. Sorry, don't know how to add the nice window others have added!

The code below is fairly self explained, however I have run into a snag and do not have the ability to show each port B bit status as coded by Hippy. For example, 30dB is equal to setting relays 00001100 and is made up of attenuators 1,2,4,4,10,20,40,40. As an output SERTXD I would still like to show 00001100 for now. I am struggling with finding the right syntax. Although Hippy had "SerTxd( #bit7,#bit6,#bit5,#bit4, " ", #bit3,#bit2,#bit1,#bit0, CR,LF )", I can not find a method to build this into the code.

I used the button command to assist with incrementing up/down in units and tenths, wait and then write relays. this saves the need to actuate each relay as you step a value, it waits and writes to port B. Please note the delays and button values are not all correct, I made them small to run them through the simulator.

Little by little, the bits and bytes are coming together, this is fun.

Regards

Gerald

code:

Code:
#Picaxe 20X2
#Terminal 9600

dirsB = $FF 
Symbol attenuation = b1
b2 = 0                ;used by button command
b3 = 0                ;used by button command
b6 = 0
b7 = 0
symbol WriteDelay = W2
   
Pause 500 ; To allow Terminal display to appear
  
attenuation = $1e
Gosub SetAttentuationRelays
  
Do
   
    ;Increase attenuation 1 dB increments
  button C.0,1,5,5,b2,1,IncreaseDB1
  goto DoNotIncrease
      
IncreaseDB1:
  attenuation = attenuation + 1 Max 121
  WriteDelay = 10         ;set timer to 1000 x 5ms = 5sec
  SerTxd( "Attenuation=", #attenuation, "dB", CR,LF )
DoNotIncrease:      
          
    ;Decrease attenuation 1 dB increments
  button C.1,1,5,5,b3,1,DecreaseDB1  
  goto DoNotDecrease:
  
DecreaseDB1:
  attenuation = attenuation Min 1 - 1
  WriteDelay = 10        ;set timer to 1000 x 5ms = 5sec
  SerTxd( "Attenuation=", #attenuation, "dB", CR,LF )
DoNotDecrease:
    
    ;Increase attenuation 10 dB increments
  button C.2,1,5,5,b6,1,IncreaseDB10
  goto DoNotIncrease10
      
IncreaseDB10:
  attenuation = attenuation + 10 Max 121
  WriteDelay = 10      ;set timer to 1000 x 5ms = 5sec
  SerTxd( "Attenuation=", #attenuation, "dB", CR,LF )
DoNotIncrease10:
    
    ;Decrease attenuation 10 dB increments
  button C.3,1,5,5,b7,1,DecreaseDB10  
  goto DoNotDecrease10:
  
DecreaseDB10:
  attenuation = attenuation Min 10 - 10
  WriteDelay = 10     ;set timer to 1000 x 5ms = 5sec
  SerTxd( "Attenuation=", #attenuation, "dB", CR,LF )
DoNotDecrease10:      
      
  pause 5   
  
  if WriteDelay = 1 then
    Gosub SetAttentuationRelays
  end if  
  
  if WriteDelay > 0 then 
      WriteDelay = WriteDelay -1                          
  end if
  
Loop
  
SetAttentuationRelays:
  LOOKUP attenuation,($00,$01,$02,$03,$04,$05,$06,$07,$0c,$0d,$0e,$0f,$12,$13,$14,$15,$16,$17,$1c,$1d,$1e,$1f,$22,$23,$24,$25,$26,$27,$2c,$2d,$2e,$2f,$32,$33,$34,$35,$36,$37,$3c,$3d,$3e,$3f,$42,$43,$44,$45,$46,$47,$4c,$4d,$4e,$4f,$52,$53,$54,$55,$56,$57,$5c,$5d,$5e,$5f,$62,$63,$64,$65,$66,$67,$6c,$6d,$6e,$6f,$72,$73,$74,$75,$76,$77,$7c,$7d,$7e,$7f,$c2,$c3,$c4,$c5,$c6,$c7,$cc,$cd,$ce,$cf,$d2,$d3,$d4,$d5,$d6,$d7,$dc,$dd,$de,$df,$e2,$e3,$e4,$e5,$e6,$e7,$ec,$ed,$ee,$ef,$f2,$f3,$f4,$f5,$f6,$f7,$fc,$fd,$fe,$ff),b8
  
  pinsB = b8
  SerTxd( "Attenuation=", #attenuation, "dB", 9, "Relays=",b8,CR,LF )
  ;SerTxd( "Attenuation=", #attenuation, "dB", 9, "Relays=" )
  ;SerTxd( #bit7,#bit6,#bit5,#bit4, " ", #bit3,#bit2,#bit1,#bit0, CR,LF )
  Return
 
Last edited by a moderator:

hippy

Ex-Staff (retired)
As an output SERTXD I would still like to show 00001100 for now. I am struggling with finding the right syntax. Although Hippy had "SerTxd( #bit7,#bit6,#bit5,#bit4, " ", #bit3,#bit2,#bit1,#bit0, CR,LF )", I can not find a method to build this into the code.
That trick works because 'bit0' through 'bit7' are the component bits of 'b0'. To to use it; move the binary value to show into 'b0' and then issue the SERTXD. Your relay bits output is in 'b8' so you can do ...

b0 = b8
SerTxd( #bit7,#bit6,#bit5,#bit4, " ", #bit3,#bit2,#bit1,#bit0, CR,LF )
 

vk3gjm

New Member
Hi Hippy,

That did it, thanks. Now it is all starting to Gell.

Regards

Gerald

b0 = b8
SerTxd( #bit7,#bit6,#bit5,#bit4, " ", #bit3,#bit2,#bit1,#bit0, CR,LF )[/QUOTE]
 
Top