PWM using Demo9S08LL16

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

PWM using Demo9S08LL16

Jump to solution
1,722 Views
JAllen
Contributor I

Hello,

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. 

 

Thanks,

 

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
main:
_Startup:
            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.
           
           
mainLoop:
           
            NOP
            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.
           
           
               ;Subroutines
              
DELAY:   LDA   DELAY1       ; Load A w/ delay count.
            STA   TEMP1           ; Store delay count in TEMP1.
            feed_watchdog
DLY1:     LDX   #255           ; Load X w/255.
            feed_watchdog
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.
             feed_watchdog
             RTS                     ;Return from subroutine.
           
             END

Labels (1)
0 Kudos
1 Solution
569 Views
peg
Senior Contributor IV

Hello and welcome to the fora, John.

 

Firstly, checkout the MOV instruction, if nothing else it makes for a tidier listing.

Try moving the write to TPM1C1SC to above the modulo set.

I have only looked at your TPM setup here as you say the rest works.

 

View solution in original post

0 Kudos
3 Replies
570 Views
peg
Senior Contributor IV

Hello and welcome to the fora, John.

 

Firstly, checkout the MOV instruction, if nothing else it makes for a tidier listing.

Try moving the write to TPM1C1SC to above the modulo set.

I have only looked at your TPM setup here as you say the rest works.

 

0 Kudos
569 Views
JAllen
Contributor I

Thanks Peg,

 

Your suggestion worked.  I moved TPM1C1SC and changed the LDA/STA's to MOV's.  The code is at the bottom of this post.

 

As I'm sure is obvious, I'm a novice programmer.  May I ask another question?  

 

I attempted to set the busclk frequency to 8MHz, but from the period of the LED blink, it appears to be closer to 6MHz.  Also, when I run the program from Codewarrior certain info displays in the  "Command" window.    The last few lines that appear are listed below.  Is this the busclk frequency and why does it change?

 

Postload command file correctly executed.
Frequency change to ~6004612hz.
STARTED
Frequency change to ~4175752hz.
RUNNING
Frequency change to ~5079600hz.
Frequency change to ~6428550hz.
Frequency change to ~4235805hz.
Frequency change to ~6698722hz.
Frequency change to ~5884507hz.
Frequency change to ~4235805hz.
Frequency change to ~5995177hz.
Potential Reset occurred

in>

 

Also, I'd like to ask about the forum.  I'm determined to learn assembly language and build a weather station using the Demo9S08LL16.  I will have lots of questions.  Is it proper to use the forum in this way?  Also, I'm anxious to learn the best pogramming practices, so everyone is welcome to critique my code.

 

Thanks again,

 

John Allen

 

The code that works:

 


; variable/data section
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
main:
_Startup:
            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.
            MOV   #$04,ICSC1       ;FLL selected,Ref Clock/1, IRC selected.
            MOV   #$40,ICSC2       ; BDIV = 2, Range = Low
            MOV   #$0B,ICSTRM      ; Use maximum period -> 31.25 KHz.
            MOV   #$10,ICSSC       ; Default, DMX32=0,busclk=8MHz, FLL factor=512.
            MOV   #$0F,TPM1SC      ; Clock = busclk, PRESCALE = 128.
            MOV   #$38,TPM1C1SC    ; Edge aligned, high true pulse.
            MOV   #$FE,TPM1MODL    ;
            MOV   #$FF,TPM1MODH    ; Divide TPM clock by 65535->Period~=1.04 sec.
            MOV   #$FF,TPM1C1VL    ; Low byte for duty cycle (TPM1C1H).
            MOV   #$AF,TPM1C1VH     ; High byte for duty cycle (7FFF).
            MOV   #$FF,PTCDD       ; Set PTC as an output.
            MOV   #$FF,PTCD        ; Store $FF in PTCD.
 
            MOV   #16,DELAY1      ; STore blink rate in DELAY1.
                                  ; (Lower # -> higher blink rate) 16=65.3ms blink rate.
mainLoop:
           
            NOP
            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.
           
           
               ;Subroutines
              
DELAY:      MOV   DELAY1,TEMP1        ; Store delay count in TEMP1.
            feed_watchdog
DLY1:       LDX   #255         ; Load X w/255.
            feed_watchdog
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.
            feed_watchdog
            RTS               ;Return from subroutine.
           
            END

          
 

0 Kudos
569 Views
peg
Senior Contributor IV

Hello again,

 

If you have indeed set the ICS to 31.25, with the settings you have this would yeild a BUSCLK of 4MHz.

31250 x 512 / 2 / 2 = 4000000. Don't for get the fixed divide by two between ICSOUT and BUSCLK!

 

I am not sure why CW is reporting the frequency changing.

 

Also, ask all the questions you like! This is what these fora are for.

 

0 Kudos