DevKara
DevKara

Reputation: 3

Having problems with timer timings in 8051 assembly

I'm currently working on a school task that requires me to utilize timers to create 50% and 25% duty cycle waves from 8051s p2 ports. The 8051 is connected to a switch which is connected to P2.0. When switch=0, we have a 230Hz wave on p2.6 and a 460Hz wave on p2.7, both 50%. When switch=1, ports interchange frequencies, and waves reduce to 25%. I calculated the on and off times for both 50% and 25% and initialized reload values of TH/TL for each step (that I found suitable/necessary) but this did not result in the intended waveforms. Switch=0 case works flawlessly with an error less than %0.3 but the other case results in the waveforms below. The first image and second image come one after the other. I post them like this to increase understandability: here

here

I thought it had to do with either my code flow or my timer initializations but I couldn't find the source of error. I will provide the code below:

ORG 0

    SETUP:
        ;both timers set as mode 1 (16-bit counter)
        MOV TMOD, #11H

    MAIN:
        MOV C, P2.0
        JC MODE1

    MODE0:
        ; This part corresponds to switch = 0. F2 gets timer 1, F1 gets timer 0

        ;initialize timer 0
        MOV TL0, #0FAH
        MOV TH0, #0F7H
        SETB TR0

        ;initialize timer 1
        MOV TL1, #17H
        MOV TH1, #0FCH
        SETB TR1

    LOOPM0:
        MOV C, P2.0
        JC MODE1
        JB TF0, TIMER0
        JB TF1, TIMER1
        SJMP LOOPM0

    TIMER0:
        CPL P2.6
        ;Reset timer 0
        CLR TF0
        MOV TL0, #0FAH
        MOV TH0, #0F7H

        SJMP LOOPM0

    TIMER1:
        CPL P2.7
        ;Reset timer 1
        CLR TF1
        MOV TL1, #17H
        MOV TH1, #0FCH

        SJMP LOOPM0

    MODE1:
        CLR TF1
        CLR TF0
        ; This part corresponds to switch =1, F1 gets timer 1, F2 gets timer 0
        ;Setup for OFF1, F2

        ;TH0/TL0 = FA21
        MOV TL0, #21H
        MOV TH0, #0FAH
        SETB TR0

        ;Setup for OFF1, F1
        ;TH1/TL1 = FD10
        MOV TL1, #11H
        MOV TH1, #0FDH
        SETB TR1

    LOOPM1:
        MOV C, P2.0
        JNC MODE0
        JB TF0, STIMER0
        JB TF1, STIMER1
        SJMP LOOPM1

    STIMER0:
        CPL P2.7
        CLR TF0

        ;TH0/TL0 = FC17
        MOV TL0, #17H
        MOV TH0, #0FCH

        SJMP LOOPM2
    STIMER1:
        CPL P2.6
        CLR TF1

        ;TH1/TL1 = FE0B
        MOV TL1, #0BH
        MOV TH1, #0FEH

        SJMP LOOPM2


    LOOPM2:
        MOV C, P2.0
        JNC MODE0
        JB TF0, STIMER00
        JB TF1, STIMER11
        SJMP LOOPM2

    STIMER00:
        CPL P2.7
        CLR TF0
        ;TH0/TL0 = FA21
        MOV TL0, #21H
        MOV TH0, #0FAH
        SJMP LOOPM1

    STIMER11:
        CPL P2.6
        CLR TF1
        ;TH1/TL1 = FD12
        MOV TL1, #11H
        MOV TH1, #0FDH
        SJMP LOOPM1



END

P.S. I would be very happy if you could just state my error and give small suggestions, rather than correcting the whole code since I want to try and learn from my mistakes.

Upvotes: 0

Views: 107

Answers (0)

Related Questions