Byte Variable Reasssigning

LED Maestro

Senior Member
Hi all,
Quick query. I'm making an LED clock, the intricacies I won't be going into here. It uses a PIC28x2, MAX7221 and a DS3234. The 7221 and the 3234 use SPI for comms. My knowledge of this protocol is still in its infancy but I have working code for communicating with both the 3234 and the 7221 separately (The code for the DS3234 was the example by Simmicht on the forum.) The 7221 code was adapted from a source which escapes my mind but it works though its probably crude to everyone here. Am I right in thinking it's using bit-banging?
My question is, I'm obviously using b0,b1,b2 etc for defining the variables but I've noticed that when I try to include the two separate codes together and change the bx values to accommodate, it won't communicate with either one of the chips. is there a way to overcome this? Obviously there is but I need guidance. See codes below.
Any and all help is greatly appreciated. If you need anything clarified then please let me know.
Thanks

DS3234
http://www.picaxeforum.co.uk/showthread.php?18557-DS3234-RTC-Code&highlight=DS3234

MAX7221
Code:
#PICAXE28x2

'=======================================================
'                     MAX7221 PINS
'=======================================================
symbol MOSI = pinB.5  'Pic Pin 26
symbol CS   = B.6     'Pic Pin 27
symbol Clk  = C.3     'Pic Pin 28
'=======================================================
'                   MAX7221 Registers
'                     DO NOT ALTER
'=======================================================
symbol No_Op   = 0
symbol Digit0  = 1
symbol Digit1  = 2
symbol Digit2  = 3
symbol Digit3  = 4  'for more digits, dig4 thru dig7 = 5 to 8 respectively
symbol Decode  = 9
symbol Intens  = 10
symbol ScanLim = 11
symbol ShutDwn = 12
symbol DigTest = 15
'=======================================================
'               MAX7221 Register Constants
'=======================================================
symbol Set_Off   = 0
symbol Set_On    = 1
symbol ScanLimit = 2   'Scan limit for digits 0 to 1 = 2
symbol DecMode   = 0   
symbol Intensity = 8   
'=======================================================
'                       Variables
'=======================================================
symbol Data_7221  = b0
symbol Register   = b1
symbol Tens       = b2
symbol Blanking   = b3
'=======================================================
'                     Main Program
'=======================================================

Initialise:
DIRSB = %11100000
LOW CS
LOW clk
MOSI = 0


do

gosub Initialise7221
gosub DisplayTest

Loop

'=======================================================
'                    Main Subroutines
'=======================================================

Initialise7221:
Register = Decode  : data_7221 = DecMode   : gosub shiftto7221
Register = Intens  : data_7221 = Intensity : gosub shiftto7221
Register = ScanLim : data_7221 = ScanLimit : gosub shiftto7221
Register = ShutDwn : data_7221 = Set_On    : gosub shiftto7221
Register = DigTest : data_7221 = Set_Off   : gosub shiftto7221

SetDigitDisplay:
if Blanking = Set_On then
Tens = 0
endif

Register = Digit0 : data_7221 = tens : gosub shiftto7221
Register = Digit1 : data_7221 = tens : gosub shiftto7221
return

DisplayTest:
Register = DigTest : data_7221 = Set_On  : gosub shiftto7221
pause 2000
Register = DigTest : data_7221 = Set_Off : gosub shiftto7221
pause 1000
return


'=======================================================
'                   MAX7221 SPI I/O
'=======================================================

shiftto7221:
; pulsout clk, 1 ; bit 15 is don't care - enable if two or more MAX7219 are daisy chained
; pulsout clk, 1 ; bit 14 is don't care
; pulsout clk, 1 ; bit 13 is don't care
; pulsout clk, 1 ; bit 12 is don't care
MOSI = bit11 : pulsout clk, 1
MOSI = bit10 : pulsout clk, 1
MOSI = bit9  : pulsout clk, 1
MOSI = bit8  : pulsout clk, 1
MOSI = bit7  : pulsout clk, 1
MOSI = bit6  : pulsout clk, 1
MOSI = bit5  : pulsout clk, 1
MOSI = bit4  : pulsout clk, 1
MOSI = bit3  : pulsout clk, 1
MOSI = bit2  : pulsout clk, 1
MOSI = bit1  : pulsout clk, 1
MOSI = bit0  : pulsout clk, 1
pulsout CS, 1
return
 

LED Maestro

Senior Member
Below is my combined code for the DS3234 and MAX7221. The terminal displays unusual readbacks and I think this is caused by the Byte assigning of the DS3234 Variables.
Any advice?
Thanks

Code:
#picaxe28X2
#terminal 19200
#no_data
#no_table

setfreq M16
'======================================================='
'                  Time/Date Constants		        
'======================================================='
SYMBOL abyte = B55    
SYMBOL aWord = W27
SYMBOL SECS  = b12
SYMBOL MINS  = b13
SYMBOL HOUR  = b14
SYMBOL DAY   = b15
SYMBOL DATE  = b16
SYMBOL MONTH = b17
SYMBOL YEAR  = b18
'======================================================='
'                     DS3234 PINS				  
'======================================================='   
SYMBOL MISO   = pinC.4 ' Pic Pin 15 
SYMBOL CLK    = C.3    ' Pic Pin 14 
SYMBOL CS_RTC = C.2    ' Pic Pin 13 
'======================================================='
'                   DS3234 VARIABLES			  
'======================================================='
Symbol _Tmp_1            = b4
Symbol _Tmp_2            = b5
Symbol _Tmp_Data_Snd     = b6
Symbol _Tmp_Data_Rcv     = b7
Symbol _Tmp_bit          = b8 ' MISO Bit Value
Symbol _Register_Name    = b9 ' Register Name
Symbol _Register_Value_L = b10 ' Register Value LSB
Symbol _Register_Value_M = b11 ' Register Value MSB
Symbol _Register_Value_W = w3 ' Register Value as WORD
'======================================================='
'                    MAX7221 PINS				  
'======================================================='
'SYMBOL CLK = B.7     'Pic Pin 28
SYMBOL CS_7221  = B.6     'Pic Pin 27
SYMBOL MOSI = pinB.5  'Pic Pin 26
'======================================================='
'                   MAX7221 Registers			  
'                     DO NOT ALTER				  
'======================================================='
symbol No_Op   = 0
symbol Digit0  = 1
symbol Digit1  = 2
'symbol Digit2  = 3
'symbol Digit3  = 4  
'symbol Digit4  = 5
'symbol Digit5  = 6
'symbol Digit6  = 7
'symbol Digit7  = 8
symbol Decode  = 9
symbol Intens  = 10
symbol ScanLim = 11
symbol ShutDwn = 12
symbol DigTest = 15
'======================================================='
'               MAX7221 Register Constants		  
'======================================================='
symbol Set_Off   = 0
symbol Set_On    = 1
symbol ScanLimit = 2 
symbol DecMode   = 0   
symbol Intensity = 10   
'======================================================='
'                   MAX7221 Variables			  
'======================================================='
symbol Data_7221  = b0
symbol Register   = b1
symbol Tens       = b2
symbol Blanking   = b3
'======================================================='
'                  MAX7221 Initilisation		  	  
'======================================================='
Initialise:
HIGH CS_RTC
DIRSB = %11100000
LOW CS_7221
LOW CLK
MOSI = 0

gosub Initialise7221
goto DisplayTest
'======================================================='
'             MAX7221 Initilisation Subroutines		  
'======================================================='
Initialise7221:
Register = Decode  : data_7221 = DecMode   : gosub Snd_7221
Register = Intens  : data_7221 = Intensity : gosub Snd_7221
Register = ScanLim : data_7221 = ScanLimit : gosub Snd_7221
Register = ShutDwn : data_7221 = Set_On    : gosub Snd_7221
Register = DigTest : data_7221 = Set_Off   : gosub Snd_7221

SetDigitDisplay:
if Blanking = Set_On then
Tens = 0
endif

Register = Digit0 : data_7221 = tens : gosub Snd_7221
Register = Digit1 : data_7221 = tens : gosub Snd_7221
'Register = Digit2 : data_7221 = tens : gosub Snd_7221
'Register = Digit3 : data_7221 = tens : gosub Snd_7221
'Register = Digit4 : data_7221 = tens : gosub Snd_7221
'Register = Digit5 : data_7221 = tens : gosub Snd_7221
'Register = Digit6 : data_7221 = tens : gosub Snd_7221
'Register = Digit7 : data_7221 = tens : gosub Snd_7221
return

DisplayTest:
Register = DigTest : data_7221 = Set_On  : gosub Snd_7221
LOW CS_7221
pause 2000
Register = DigTest : data_7221 = Set_Off : gosub Snd_7221
LOW CS_7221
pause 1000
Goto MainRead
'======================================================='
'                      DS3234 Read				  
'======================================================='
MainRead:

    _Register_Name    = 0x00
    _Register_Value_L = 0

    LOW CS_RTC
    _Tmp_Data_Snd = _Register_Name
    gosub Snd_Rcv
    _Tmp_Data_Snd = 0

    gosub Snd_Rcv:SECS  = _TMP_Data_Rcv  
    gosub Snd_Rcv:MINS  = _TMP_Data_Rcv
    gosub Snd_Rcv:HOUR  = _TMP_Data_Rcv
    gosub Snd_Rcv:DAY   = _TMP_Data_Rcv
    gosub Snd_Rcv:DATE  = _TMP_Data_Rcv
    gosub Snd_Rcv:MONTH = _TMP_Data_Rcv
    gosub Snd_Rcv:YEAR  = _TMP_Data_Rcv

    HIGH CS_RTC
    
    SECS  = BCDTOBIN SECS               
    MINS  = BCDTOBIN MINS
    HOUR  = BCDTOBIN HOUR
    DAY   = BCDTOBIN DAY
    DATE  = BCDTOBIN DATE              
    MONTH = BCDTOBIN MONTH
    YEAR  = BCDTOBIN YEAR
    
      aWord=HOUR
    GoSub SerTxdWriteWord00
    sertxd(":")
    Aword= MINS
    GoSub    SerTxdWriteWord00
    sertxd(":")
    aWord=SECS
    GoSub SerTxdWriteWord00
    sertxd(" ",#DAY," ")

    aWord=DATE
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=MONTH 
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=YEAR
    GoSub SerTxdWriteWord00
    Sertxd(cr,lf)
    
    Pause 2000
    
goto MainRead
    

'======================================================='
'                   MAX7221 Display				  
'======================================================='

'register = digit0 : data_7221 = SECS : gosub Snd_7221

'Goto MainRead

'======================================================='
'                   DS3234 SPI I/O				  
'======================================================='

Snd_Rcv:


For _Tmp_2 = 1 to 8 ' 8 bIt 
    LOW CLK
     
'Send Data MOSI 
    '_Tmp_1 = 128
    '_Tmp_bit = _Tmp_Data_Snd & _Tmp_1
    '_Tmp_Data_Snd = _Tmp_Data_Snd<<1

    'IF _TMP_bit = 128 then
    '    HIGH MOSI
    'Else
    '    LOW MOSI
    'Endif
    
'Receve Data MISO
    High CLK
    _Tmp_Data_Rcv = _Tmp_Data_Rcv<<1

    IF MISO = 1 then
        _Tmp_Data_Rcv = _Tmp_Data_Rcv+1
    Endif
Next _Tmp_2 ' Next Bit


RETURN

'======================================================='
'                   MAX7221 SPI I/O				  
'======================================================='

Snd_7221:
; pulsout clk, 1 ; bit 15 is don't care - enable if two or more MAX7219 are daisy chained
; pulsout clk, 1 ; bit 14 is don't care
; pulsout clk, 1 ; bit 13 is don't care
; pulsout clk, 1 ; bit 12 is don't care
MOSI = bit11 : pulsout CLK, 1
MOSI = bit10 : pulsout CLK, 1
MOSI = bit9  : pulsout CLK, 1
MOSI = bit8  : pulsout CLK, 1
MOSI = bit7  : pulsout CLK, 1
MOSI = bit6  : pulsout CLK, 1
MOSI = bit5  : pulsout CLK, 1
MOSI = bit4  : pulsout CLK, 1
MOSI = bit3  : pulsout CLK, 1
MOSI = bit2  : pulsout CLK, 1
MOSI = bit1  : pulsout CLK, 1
MOSI = bit0  : pulsout CLK, 1
pulsout CS_7221, 1
RETURN

'=======================================================
'                   Terminal Display
'=======================================================

SerTxdWriteWord00000:
B0= aWord / 10000
B0 = B0 + 48
SerTxd (B0)                 ; output 10 thousands value data

SerTxdWriteWord0000:
aWord = aWord //10000
B0= aWord / 1000
B0 = B0 + 48
SerTxd (B0)                 ; output Thousands value data

SerTxdWriteWord000:
aWord = aWord //1000
B0= aWord / 100
B0 = B0 + 48
SerTxd (B0)                 ; output Hundreds value data

SerTxdWriteWord00:
aWord = aWord //100
B0= aWord / 10
B0 = B0 + 48
SerTxd (B0)                 ; output Tens value data

SerTxdWriteWord0:
aWord = aWord //10
B0= aWord
B0 = B0 + 48
SerTxd (B0)                 ; output units data
RETURN
 

eggdweather

Senior Member
Symbol _Tmp_1 = b4
Symbol _Tmp_2 = b5
Symbol _Tmp_Data_Snd = b6
Symbol _Tmp_Data_Rcv = b7
Symbol _Tmp_bit = b8 ' MISO Bit Value
Symbol _Register_Name = b9 ' Register Name
Symbol _Register_Value_L = b10 ' Register Value LSB
Symbol _Register_Value_M = b11 ' Register Value MSB
Symbol _Register_Value_W = w3 ' Register Value as WORD
'======================================================='
_Register_Value_W = w3 is the same as _Tmp_Data_Snd = b6 and _Tmp_Data_Rcv = b7

Try changing w3 to w26 or somewhere higher.
w0 = b0,b1
w1 = b2,b3
w2 = b4,b5
w3 = b6,b7* is the same as w3
 

LED Maestro

Senior Member
Hi, Thanks for your reply.
I changed the Bytes to Words. It didn't work but was surprising is that the time displayed as 00:00:01 and started counting the seconds. the date was set at 01/01/00. Do I need to change the format of the data I'm sending, its currently in binary as opposed to hex (easier for me to comprehend!)
 

eggdweather

Senior Member
Has the clock been set yet?
All of the data from the clock is in BCD, but it is being converted to binary. e.g. SECS = BCDTOBIN SECS
Example SECs = 59 in BCD becomes 0x3B in HEX or 0x00111011 in binary (0011 1011)

Can you print ABCD to the display? Can you print 0123 to the display?

The reason I ask if you convert 59 BCD to binary and then print 0x3B it will most likely print ASCII Character for 0x3B or ';' a semicolon, do you get weird characters
 

AllyCat

Senior Member
Hi,

The MAX7221 SPI I/O routine appears to use bit0 to bit15, which overlay bytes b0 - b3 (and w0 - w1). Therefore those bit/byte names must NOT be changed.

Any other code using the above variables will need to be changed (again avoiding words which overlay any used byte variables). If you "run out" of variables, they will either have to be shared (e.g. used only "locally" in subroutines), or stored "indirectly" using POKE/PEEK or @bptr.

Cheers, Alan.
 

LED Maestro

Senior Member
Hi Guys, The clock had been set and read back correctly from the terminal when using the code found in the link to the other post, the problem arises when I add it to my existing 7221 code. I don't get weird characters within the terminal, just irregular numbers for the seconds, minutes, hours etc. My apologies though, I realised there was a BCD to Binary conversion in there after I replied.
Ally, thank you for that insight, I may have to use them locally then. In terms of the two SPI subroutines for the 7221 and the 3234, would there be a better way to do/code this? It's just that, to me, there are two remarkably different layouts of code for the same type of information transfer. Obviously the 7221 and the 3234 are 16 and 8 bit devices respectively, but is there a way to make it more uniform or simpler?
Thanks
 

LED Maestro

Senior Member
Here is a copy of the input buffer of the terminal

80:65:01 165 40/45/58
20:65:00 45 50/15/62
50:45:20 15 62/03/65
62:15:50 3 65/00/65
00:00:00 1 00/00/00
14:34:59 6 18/04/15***
20:65:00 45 58/15/62
50:45:20 15 62/03/65
62:15:50 3 65/00/65
00:00:00 1 00/00/00
14:35:05 6 18/04/15***
20:65:00 45 50/15/62
50:45:20 15 62/03/65
62:25:50 7 64/01/65
14:00:28 80 00/09/35
14:35:10 6 18/04/15***

The read backs with the astix are correct at the time I was reading them. Below is the code I was using. What could be causing this? The DS3234 was set prior to programming the code below but as Far as I am aware, nothing should have been changed.

Code:
#PICAXE28x2
#terminal 19200
#no_data
#no_table

setfreq M16
'=======================================================
'                     Main Symbols
'=======================================================
SYMBOL abyte = B55    
SYMBOL aWord = W27
SYMBOL SECS  = B10
SYMBOL MINS  = B11
SYMBOL HOUR  = B12
SYMBOL DAY   = B13
SYMBOL DATE  = B14
SYMBOL MONTH = B15
SYMBOL YEAR  = B16
'=======================================================
'                     DS3234 PINS
'=======================================================
SYMBOL MISO   = pinC.4 ' Pin 15 
SYMBOL CS_RTC = C.2    ' Pin 13 
'=======================================================
'                   DS3234 VARIABLES
'=======================================================
Symbol _Tmp_1            = w0
Symbol _Tmp_2            = w1
Symbol _Tmp_Data_Snd     = w2
Symbol _Tmp_Data_Rcv     = w3
Symbol _TMP_bit          = w4 ' MISO Bit Value
Symbol _Register_Name    = w5 ' Register Name
Symbol _Register_Value_W = w8 ' Register Value as WORD
Symbol _Register_Value_L = w6 ' Register Value LSB
Symbol _Register_Value_M = w7 ' Register Value MSB
'=======================================================
'                     MAX7221 PINS
'=======================================================
symbol MOSI = pinB.5  'Pin 26
symbol CS   = B.6     'Pin 27
symbol Clk  = C.3    'Pin 28
'=======================================================
'                   MAX7221 Registers
'                     DO NOT ALTER
'=======================================================
symbol No_Op   = 0
symbol Digit0  = 1
symbol Digit1  = 2
symbol Digit2  = 3
symbol Digit3  = 4  'for more digits, dig4 thru dig7 = 5 to 8 respectively
symbol Decode  = 9
symbol Intens  = 10
symbol ScanLim = 11
symbol ShutDwn = 12
symbol DigTest = 15
'=======================================================
'               MAX7221 Register Constants
'=======================================================
symbol Set_Off   = 0
symbol Set_On    = 1
symbol ScanLimit = 2   'Scan limit for digits 0 to 1 = 2
symbol DecMode   = 0   
symbol Intensity = 8   
'=======================================================
'                       Variables
'=======================================================
symbol Data_7221  = b0
symbol Register   = b1
symbol Tens       = b2
symbol Blanking   = b3
'=======================================================
'                     Main Program
'=======================================================

Initialise:
HIGH CS_RTC
DIRSB = %11100000
LOW CS
LOW clk
MOSI = 0

gosub Initialise7221
gosub DisplayTest

'=======================================================
'                 DS3234 Read And DEBUG
'=======================================================

do

    _Register_Name    = 0x00
    _Register_Value_L = 0

    LOW CS_RTC
    _Tmp_Data_Snd = _Register_Name
    gosub Snd_Rcv
    _Tmp_Data_Snd = 0

    gosub Snd_Rcv:SECS  = _TMP_Data_Rcv  
    gosub Snd_Rcv:MINS  = _TMP_Data_Rcv
    gosub Snd_Rcv:HOUR  = _TMP_Data_Rcv
    gosub Snd_Rcv:DAY   = _TMP_Data_Rcv
    gosub Snd_Rcv:DATE  = _TMP_Data_Rcv
    gosub Snd_Rcv:MONTH = _TMP_Data_Rcv
    gosub Snd_Rcv:YEAR  = _TMP_Data_Rcv

    HIGH CS_RTC

    SECS  = BCDTOBIN SECS               
    MINS  = BCDTOBIN MINS
    HOUR  = BCDTOBIN HOUR
    DAY   = BCDTOBIN DAY
    DATE  = BCDTOBIN DATE              
    MONTH = BCDTOBIN MONTH
    YEAR  = BCDTOBIN YEAR

    'Output values to terminal 
    
    aWord=HOUR
    GoSub SerTxdWriteWord00
    sertxd(":")
    Aword= MINS
    GoSub    SerTxdWriteWord00
    sertxd(":")
    aWord=SECS
    GoSub SerTxdWriteWord00
    sertxd(" ",#DAY," ")

    aWord=DATE
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=MONTH 
    GoSub SerTxdWriteWord00
    sertxd("/")
    aWord=YEAR
    GoSub SerTxdWriteWord00
        Sertxd(cr,lf)

    pause 2000
loop
'=======================================================
'                    Main Subroutines
'=======================================================

Initialise7221:
Register = Decode  : data_7221 = DecMode   : gosub shiftto7221
Register = Intens  : data_7221 = Intensity : gosub shiftto7221
Register = ScanLim : data_7221 = ScanLimit : gosub shiftto7221
Register = ShutDwn : data_7221 = Set_On    : gosub shiftto7221
Register = DigTest : data_7221 = Set_Off   : gosub shiftto7221

SetDigitDisplay:
if Blanking = Set_On then
Tens = 0
endif

Register = Digit0 : data_7221 = tens : gosub shiftto7221
Register = Digit1 : data_7221 = tens : gosub shiftto7221
Register = Digit2 : data_7221 = tens : gosub shiftto7221
register = Digit3 : data_7221 = tens : gosub shiftto7221
return

DisplayTest:
Register = DigTest : data_7221 = Set_On  : gosub shiftto7221
pause 2000
Register = DigTest : data_7221 = Set_Off : gosub shiftto7221
pause 1000
return

'=======================================================
'                   MAX7221 SPI I/O
'=======================================================

shiftto7221:
; pulsout clk, 1 ; bit 15 is don't care - enable if two or more MAX7219 are daisy chained
; pulsout clk, 1 ; bit 14 is don't care
; pulsout clk, 1 ; bit 13 is don't care
; pulsout clk, 1 ; bit 12 is don't care
MOSI = bit11 : pulsout clk, 1
MOSI = bit10 : pulsout clk, 1
MOSI = bit9  : pulsout clk, 1
MOSI = bit8  : pulsout clk, 1
MOSI = bit7  : pulsout clk, 1
MOSI = bit6  : pulsout clk, 1
MOSI = bit5  : pulsout clk, 1
MOSI = bit4  : pulsout clk, 1
MOSI = bit3  : pulsout clk, 1
MOSI = bit2  : pulsout clk, 1
MOSI = bit1  : pulsout clk, 1
MOSI = bit0  : pulsout clk, 1
pulsout CS, 1
return

'=======================================================
'                   DS3234 SPI I/O
'=======================================================

Snd_Rcv:

For _Tmp_2 = 1 to 8 ' 8 bit 
    LOW CLK
    
'Send Data MOSI 
   ' _Tmp_1 = 128
   ' _TMP_bit = _Tmp_Data_Snd & _Tmp_1
   ' _Tmp_Data_Snd = _Tmp_Data_Snd<<1

    'IF _TMP_bit = 128 then
    '    HIGH MOSI
    'Else
    '    LOW MOSI
    'Endif
    
'Receve Data MISO
    High CLK
    _Tmp_Data_Rcv = _Tmp_Data_Rcv<<1

    IF MISO = 1 then
        _Tmp_Data_Rcv = _Tmp_Data_Rcv+1
    Endif
Next _Tmp_2 ' Next Bit

RETURN

'=======================================================
'                   Terminal Display
'=======================================================

SerTxdWriteWord00000:
B0= aWord / 10000
B0 = B0 + 48
SerTxd (B0)                 ; output 10 thousands value data

SerTxdWriteWord0000:
aWord = aWord //10000
B0= aWord / 1000
B0 = B0 + 48
SerTxd (B0)                 ; output Thousands value data

SerTxdWriteWord000:
aWord = aWord //1000
B0= aWord / 100
B0 = B0 + 48
SerTxd (B0)                 ; output Hundreds value data

SerTxdWriteWord00:
aWord = aWord //100
B0= aWord / 10
B0 = B0 + 48
SerTxd (B0)                 ; output Tens value data

SerTxdWriteWord0:
aWord = aWord //10
B0= aWord
B0 = B0 + 48
SerTxd (B0)                 ; output units data
RETURN
 

eggdweather

Senior Member
Seems almost certain it is a read error, perhaps associated with timing. Try slowing the read rate down to begin with, a reading every 5-secs or so, a delay loop delay(5000) to do that.
 

LED Maestro

Senior Member
Hi, Thanks for the suggestion. I added the delay but its still giving incorrect values this time with only 1 correct readback, see below. It shouldn't be erroring on the read though as I just copied the code from the 'set time' code from the forum. I will continue and have a play about with it.

01:65:07 165 80/85/40
20:65:00 45 58/15/62
62:15:50 3 65/00/65
15:21:04 6 18/04/80***
00:65:01 165 40/45/50
50:45:20 15 62/03/65
00:00:00 1 00/00/00
01:64:07 165 80/85/40
40:85:80 25 58/07/64
14:00:28 80 00/09/82
00:82:12 7 00/25/80
58:25:20 7 64/01/65
 

LED Maestro

Senior Member
Right, I decided to separate the SPI pins for the 3234 and the 7221 (They shared the same clock pin) and this seems to have cured the problem. The 7221 initialises and tests the display and then continues on to read the 3234 with no problem. This isn't an issue as such, just slightly confusing as to why it wouldn't work. Anyway, Thanks to all for your help.
 

AllyCat

Senior Member
Hi,

Glad that you've got it working, I couldn't see any "obvious" fault in the code.

As for the different subroutine structures, that's because there's usually more than one (satisfactory) way to program a function and all authors have their own preferred methods:

The "bitn" method is generally faster and easier to understand, once the "overlaying" of the bits onto bytes is understood. It's also easy to read-out the bits from either end (or even in any arbitrary sequence should you wish to), but the program size tends to be large. Also, if the bit-addressable bytes/words are dedicated to a specific function, then they can't be used for anything else!

The other method uses a more "traditional" microcontroller method, particularly when a "Carry" flag is available (it isn't with PICaxe). One of the "end" bits (of a byte or word) is tested, then transmitted, and the whole byte/word shifted by one bit (left or right) to reveal a new bit to test (generally shifting left is easier). That's repeated until all the data has been sent, which tends to take longer because of all the jumps backwards and loop counting, etc., but usually produces more compact code.

Typically, the (end) bit is tested by ANDing the byte/word with a "mask" value, e.g. IF B4 AND 128 = 128 THEN... (or W4 AND $8000 = 0 THEN..., etc.). However, PICaxe syntax requires these instructions to be split into two elements, so personally I prefer: IF B4 > 127 THEN.... (or IF W4 < $8000 THEN... etc.) which tests the top bit in a single line of code.

Cheers, Alan.
 

LED Maestro

Senior Member
Hi Alan,
Ah right that makes sense. Thank you for your explanation, it's much appreciated as I'm always looking to learn more.
One final question if I may. I'm testing this on a single CC 7 seg display, I have more on the way. I am curious, it's currently set to display the seconds on dig0 of the 7221 using Code B decode within the 7221 which its doing very happily, display 0-9. I also have it displaying the seconds on 8 LED's without decode so it's display 0-9 on the first 4 LED's and the tens on the next 4 LED's as per BCD. Question, how would I display the units and tens on a the 7 seg display? Obviously still new to this and I can't figure out how to read the two separate bits from the 3234 (Bit 4-6 for the tens and bit 0-3 for the seconds according to the data sheet)
Thanks
 

AllyCat

Senior Member
Hi,

If you want to split "packed BCD" into separate digit bytes, then the simplest method is probably:

tens = seconds / 16
units = seconds // 16 (// gives the remainder) or units = seconds AND 15 (or seconds & 15 , etc.)

Values > 9 may produce a "useful" display, or might need to be handled as an error, depnding on the display device.

I don't know much specifically about the 7221, but generally, multiple digits can be displayed in many different ways (depending on the application). Conceptually, "fully parallel" (i.e. one wire/pin for every segment) is the easiest, "multiplexing" the most common (i.e. separate segment and digit control lines dispaying one digit at a time, but switching so fast that the eye sees them all illuminated), or "time division" is the simplest (i.e. displaying digits, or digit fields, in sequence, for example as somethimes seen in alternating Time / Temperature public displays).

Cheers, Alan.
 

LED Maestro

Senior Member
Hi Alan,

That's brilliant, I was wondering if a /16 etc was going to be the case. I'll write up some code and see how it goes.

The 7221 has multiplexing built into it so that's one thing I'm thankful for, though I have other applications that uses 8x8x8 LED cubes I made up. POV is an amazing thing haha.
Anyway, thank you again for all your help with this.

Best wishes
Jay
 
Top