9S08QG8 PWM in assembly

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

9S08QG8 PWM in assembly

Jump to solution
801 Views
wolfgangs
Contributor I

Hello everyone, I am trying to program a continuous 100Hz 50% duty cycle signal on pin 16 (TPMCH0) using the 9s08qg8 in assembly.  I am using the internal oscillator (FBI mode) f=16kHz.  I observe some strange behavior.  As soon as power is supplied to the MCU, pin 16 illuminates for about 3.5 seconds at what appears to be full duty.  Even after changing various variables, this behavior continues.  Can anyone help me please?  Thanks.

 

; Include derivative-specific definitions

        INCLUDE 'derivative.inc'

 

; export symbols to be refer. in the linker .prm file

        XDEF _Startup, main

 

        XREF __SEG_END_SSTACK

; symbol defined by the linker for the end of the stack

 

 

; variable/data section

MY_ZEROPAGE: SECTION  SHORT

 

; no variables defined

 

 

; Code section

MyCode: SECTION

 

_Startup:

main:   LDHX  #__SEG_END_SSTACK ; init stack pointer

        TXS

        CLI                     ; enable interrupts

        LDA   #%01110011

        STA   SOPT1

        MOV   #%01000100,ICSC1

        MOV   #%00000000,ICSC2

        MOV   #%00001000,MTIMCLK     ;timer not necessarily used.

        MOV   #62,MTIMMOD     

        MOV   #%00000000,MTIMSC 

           

      MOV   #156,TPMMODL                ;Bus clock / TPMMODL = 15625/156=100Hz

      MOV   #156,TPMC0VL                 ;100% duty, but tried 0-100%

      MOV   #$28,TPMC0SC                ;no interupt, edge aligned, high-true pulse

      MOV   #$08,TPMSC                      ;Edge aligned, no interupt, using bus clock, prescale=1  

LOOP  BRA   LOOP                              ;run forever

      SWI

 

;*****************************************************

;      ROUTINES

;*****************************************************

;timer not necessarily used.

TIMER   BRCLR 7,MTIMSC,TIMER      ;WAIT FOR TOF

        BCLR  7,MTIMSC            ;CLEAR TOF

        RTS


Labels (1)
0 Kudos
Reply
1 Solution
579 Views
bigmac
Specialist III

Hello,

There would be a problem with your code because you are writing to the low byte only of the 16-bit registers TPMMOD and TPMC0V.  Both bytes require to be written for the new values to take effect.  The following code snippet should achieve 50 percent PWM duty -


   LDHX  #155                ; Bus clock/(TPMMOD+1) = 15625 / 156 = 100Hz

   STHX  TPMMOD

   LDHX  #78                 ; 50% duty

   STHX  TPMC0V

However, I might also expect that your bus frequency will actually be considerably higher than 15.625 kHz.  I suggest that you check your ISC settings.

Regards,

Mac

View solution in original post

0 Kudos
Reply
2 Replies
580 Views
bigmac
Specialist III

Hello,

There would be a problem with your code because you are writing to the low byte only of the 16-bit registers TPMMOD and TPMC0V.  Both bytes require to be written for the new values to take effect.  The following code snippet should achieve 50 percent PWM duty -


   LDHX  #155                ; Bus clock/(TPMMOD+1) = 15625 / 156 = 100Hz

   STHX  TPMMOD

   LDHX  #78                 ; 50% duty

   STHX  TPMC0V

However, I might also expect that your bus frequency will actually be considerably higher than 15.625 kHz.  I suggest that you check your ISC settings.

Regards,

Mac

0 Kudos
Reply
579 Views
wolfgangs
Contributor I

Thanks a lot bigmac!  That is exactly what I needed!

0 Kudos
Reply