Hey, I decided to complete this TM1637 sketch in AtmelStudio, so here it is:
#define DIO_PIN 0 ; PB0
#define CLK_PIN 1 ; PB1
#define SERVO_PIN 2 ; PB2
.dseg
;/*0*/ /*1*/ /*2*/ /*3*/ /*4*/ /*5*/ /*6*/ /*7*/ /*8*/ /*9*/
digits: .db 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
.cseg
; setup()
in r24, 0x17
ori r24, 0x03
out 0x17, r24
; initialise display and set brightness
; 0x88 is dim and increasing value to 0x8C increases brightness
rcall TM1637_start
ldi r24, 0x8c
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
rcall TM1637_stop
ldi r16, 0xff
ldi r17, 0xff
ldi r18, 0xff
ldi r19, 0xff
rcall TM1637_write_segments_arguments_r16_r17_r18_r19_destroys_r24
; this is the loop():
theloop:
ldi r16, 0x07
ldi r17, 0x07
ldi r18, 0x07
ldi r19, 0x07
rcall TM1637_write_segments_arguments_r16_r17_r18_r19_destroys_r24
rjmp theloop
TM1637_DELAY_US_uses_r20: ; 50 us at 1.2 mhz
; rcall takes 3 cycles, ret takes 4 cycles, we need another 53 cycles
ldi r20, 25 ; 1 cycle x 25
L1: dec r20
breq L1 ; 1 cycle for true and 2 for false
ret
TM1637_start:
sbi PORTB, DIO_PIN
sbi PORTB, CLK_PIN
rcall TM1637_DELAY_US_uses_r20
cbi PORTB, DIO_PIN
ret
TM1637_stop:
cbi PORTB, CLK_PIN
rcall TM1637_DELAY_US_uses_r20
cbi PORTB, DIO_PIN
rcall TM1637_DELAY_US_uses_r20
sbi PORTB, CLK_PIN
rcall TM1637_DELAY_US_uses_r20
sbi PORTB, DIO_PIN
ret
; This function does the same thing as the routine in THE LOOP
; but this one doesn't work well:
TM1637_write_segments_arguments_r16_r17_r18_r19_destroys_r24:
rcall TM1637_start
ldi r24, 0x40
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
rcall TM1637_stop
rcall TM1637_start
ldi r24, 0xc0
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
mov r24, r16
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
mov r24, r17
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
mov r24, r18
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
mov r24, r19
rcall TM1637_write_byte_parameter_r24_destroys_r25_returns_r24
rcall TM1637_stop
ret
TM1637_write_byte_parameter_r24_destroys_r25_returns_r24:
ldi r25, 0x08 ; r25 will serve as counter in for loop (i)
forloop:
cbi 0x18, 1 ; CLK_PIN - LOW
rcall TM1637_DELAY_US_uses_r20
sbrs r24, 0 ; skip if first bit is 1
rjmp r24_first_bit_aint_1
sbi 0x18, 0 ; DIO - HIGH
rjmp r24_first_bit_is_1
r24_first_bit_aint_1:
cbi 0x18, 0 ; DIO_PIN - LOW
r24_first_bit_is_1:
sbi 0x18, 1 ; CLK_PIN - HIGH
rcall TM1637_DELAY_US_uses_r20
lsr r24 ; r24 shift right by one place
subi r25, 0x01 ; i--
brne forloop ; jump if i isn't null
cbi 0x18, 1 ; CLK - LOW
rcall TM1637_DELAY_US_uses_r20 ; mozda je suvisno, al za ta 2 bajta nema veze
sbi 0x18, 1 ; CLK_PIN - HIGH
ret
And so far it does what it should - displays numbers on the 7-seg display.
BUT, if I replace the Loop with this:
theloop:
; working with byte array from data segment:
clr r27
ldi r26, digits
ld r16, x+
ld r17, x+
ld r18, x+
ld r19, x+
rcall TM1637_write_segments_arguments_r16_r17_r18_r19_destroys_r24
rjmp theloop
...does not work.
Debugger helped but I still don't know why LD instruction does not work as I expected.
Debugger facts:
r26 contains value 0x60 but all LD instructions put ZERO in destination registers instead of reading my array.
I am watching the label "digits" and it's address is 0x60 indeed.
I cannot seem to utilize LD rd, x as in memory address 0x60 there are only ??'s, I don't know where my array is.
Help please?