0

I was trying to have some fun with avr-assembly on my arduino and I tried the following codes to blink my LED

first I tried the ISR approach and when It didn't work I tried the CTC mode one, both codes are listed respectively. on the first one the LED lightsup and stay that way (no blinking) on the second one it just stays off

one more thing to note is on trying to load OCR1AL first The led would blink nicely if my hand got near PORTD pins for some reason,

I guessed it's some grounding problem and It's fixed when I pulled high all PORTD pins and it disappeared completely when I figured out that I need to load (the high byte) OCR1AH first

.nolist
.include "m328Pdef.inc" 
.list

.org    0x0000
jmp     Init


.org    0x0016
jmp     TIMER1_COMPA

.EQU    LED_PIN = PB1

Init:
    ;ldi    r16, 255
    ;out        DDRD, r16
    ;out    PORTD, r16

    ;First configure The led pin as output and initilize it to high
    sbi     DDRB, LED_PIN
    sbi     PORTB, LED_PIN

    ;Second Enable Global Interrupts
    sei

    ;Enable the Output compare Interrupt in the TIMSK1 Register
    ldi     r16, 0b00000010 
    sts     TIMSK1, r16

    ; Reset the TCCR1A 
    ldi     r16, 0b00000000
    sts     TCCR1A, r16

    ; Set the prescalar to 256 in the Timer Control 1B register
    ldi     r16, 0b00000111 
    sts     TCCR1B, r16

    ;Now load the value for Output compare register 31250 or   01111011 00100000  or 15625 =  00111101 00001001
    ldi     r16, 0b00111101
    ldi     r17, 0b00001001 
    sts     OCR1AH, r16
    sts     OCR1AL, r17


LOOP:

    rjmp    LOOP

TIMER1_COMPA:
    ;First Reset the coutner 
    ldi     r16, 0
    sts     TCNT1H, r16
    sts     TCNT1L, r16

    ;Second Toggle the Pin (LED) state
    sbi     PINB, LED_PIN

    reti



CTC:

.nolist
.include "m328Pdef.inc" 
.list

.org    0x0000
jmp     Init


.org    0x0016
jmp     TIMER1_COMPA

.EQU    LED_PIN = PB1

Init:
    ;ldi    r16, 255
    ;out        DDRD, r16
    ;out    PORTD, r16

    ;First configure The led pin as output and initilize it to high
    sbi     DDRB, LED_PIN
    sbi     PORTB, LED_PIN

    ;Second Enable Global Interrupts
    sei

    ;Enable the Output compare Interrupt in the TIMSK1 Register
    ldi     r16, 0b00000010 
    sts     TIMSK1, r16

    ; Reset the TCCR1A 
    ldi     r16, 0b01000000
    sts     TCCR1A, r16

    ; Set the prescalar to 256 in the Timer Control 1 registers
    ldi     r16, 0b00001111 
    sts     TCCR1B, r16

    ;Now load the value for Output compare register 31250 or   01111011 00100000  or 15625 =  00111101 00001001
    ldi     r16, 0b00111101
    ldi     r17, 0b00001001 
    sts     OCR1AH, r16
    sts     OCR1AL, r17


LOOP:
    rjmp    LOOP

TIMER1_COMPA:
    ;First Reset the coutner 
    ;ldi    r16, 0
    ;sts    TCNT1H, r16
    ;sts    TCNT1L, r16

    ;;Second Toggle the Pin (LED) state
    ;sbi        PINB, LED_PIN

    reti

1 Answer 1

3

I am not set up to test your code right now, perhaps later. But:

    ; Set the prescalar to 256 in the Timer Control 1B register
    ldi     r16, 0b00000111 
    sts     TCCR1B, r16

When set to ones, the low order three bits of TCCR1B, select an external clock source at the T1 pin, for clocking on a rising edge. Probably what's happening is that when you finger is near enough the T1 pin you are capacitively coupling mains frequency to it causing it to tick.

/256 prescaler would have 100 in the lower three bits.

If I manage to get a proper environment up to test your code, I'll look into it a bit more thoroughly.

3
  • OPS, Yup that Was it although I read the datasheet and saw that it was 100 don't know how it ended up as 111. anyway as a suggestion for an environment I use avrdude and avra assembler on linux mint terminal with the arduino uno Commented May 22, 2021 at 5:07
  • and the main frequency in my country is around 60 HZ and the LED clearly didn't blink on that frequency it was fast but not that fast at all. How is that? is there an explanation for the altered frequency? Commented May 22, 2021 at 5:25
  • Well, the frequency of what appears on T1 is being effectively divided by what you're doing with your ISR/TCNT1/OCRA1A; one cycle on T1 is not one execution of sbi PINB, LED_PIN, and each toggling corresponds to just the high or just the low period of the LED cycle. Then, you might also have high frequency components being seen at T1. If you wanted to experiment with that, the thing to do would be to feed a solid 60Hz into T1 to eliminate the unknown components there. By the way, your ISR should probably be preserving r16.
    – timemage
    Commented May 22, 2021 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.