AnsweredAssumed Answered

PWM using Demo9S08LL16

Question asked by John Allen on Apr 15, 2009
Latest reply on Apr 17, 2009 by David Payne


I am trying to blink LED2 (PTC3/TPM1CH1) using PWM.  The code below blinks LED3 (PTC4) usng a timing loop, but LED2 (TPM1CH1) stays low.  Can anyone tell me what the problem is?  I plan to eventually use TPM1CH1 in the PWM mode as a 32768Hz clock for a IIC module. 




John Allen


MY_ZEROPAGE: SECTION  SHORT         ; Insert here your data definition

              DELAY1:  DS.B 1       ; LED flash rate, DELAY1=16 -> 65.3 ms w/10 MHz busclk.

              TEMP1:   DS.B 1       ; Count buffer.
; code section
MyCode:     SECTION
            LDHX   #__SEG_END_SSTACK ; initialize the stack pointer
            TXS                    ; Transfer the index register to the stack pointer.
            CLI                     ; enable interrupts by clearing the interupt mask bit.
            LDA   #$04           ;Load A w/$04.
            STA   ICSC1         ;FLL selected,Ref Clock/1, IRC selected.
            LDA   #$40          ; A -> $40.
            STA   ICSC2        ; BDIV = 2, Range = Low
            LDA   #$FF          ; Load A w/$FF.
            STA   ICSTRM      ; Use maximum period -> 31.25 KHz.
            LDA   #$10          ; Load A w/$10->busclk=8MHz, FLL factor = 512.
            STA   ICSSC        ; Default, DMX32 = 0.
            LDA   #$AE
            STA   SCGC1
            LDA   #$0F            ; LOAD A w/$0F.
            STA   TPM1SC        ; Clock = busclk, PRESCALE = 128.
            LDA   #$FE            ; LOAD A W/$FF.
            STA   TPM1MODL    ;
            LDA   #$FE            ;
            STA   TPM1MODH    ; Divide TPM clock by 65535->Period~=1.04 sec.
            LDA   #$7F            ; $7FFF For 50% duty cycle.
            STA   TPM1C1VH     ; High byte for duty cycle (TPM1C1H).
            LDA   #$FF             ; $7FFF For 50% duty cycle.
            STA   TPM1C1VL     ; Low byte for duty cycle.
            LDA   #$38             ;
            STA   TPM1C1SC     ; Disable interrupt, Edge aligned, high true pulse.
            LDA   #$FF             ; Config all utputs on port C as outputs.
            STA   PTCDD          ; Set PTC as an output.
            LDA   #$FF             ; Set A to $FF.
            STA   PTCD            ; Store $FF in PTCD.
            LDA   #16              ; Load w/blink rate (Lower # -> higher blink rate) 16=65.3ms blink rate.
            STA   DELAY1         ; STore blink rate in DELAY1.
            LDA   #$10        ; Bit 4 in the accumulator is set to 1.
            EOR   PTCD        ; Invert the PTC4 state.
            STA   PTCD        ; Put result on PTC.
            JSR   DELAY       ; DELAY.
            JMP   mainLoop    ; Start over again.
DELAY:   LDA   DELAY1       ; Load A w/ delay count.
            STA   TEMP1           ; Store delay count in TEMP1.
DLY1:     LDX   #255           ; Load X w/255.
DLY2:     LDA   #255           ; Load A w/255.
DLY3:     DBNZA DLY3         ; Decrement A until zero then continue.
             DBNZX DLY2         ; Decrement X & branch if not zero to DLY2.
             DEC   TEMP1         ; Decrement delay count.
             BNE   DLY1           ; continue until delay count is zero.
             RTS                     ;Return from subroutine.