Buzzer code example

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

Buzzer code example

2,503 Views
EducaFree
Contributor I

Hi, 
I'm trying to configure the buzzer of my microcontroller 
(MC9S08QE128) in CodeWarrior using the assembly language, 
but I can't find any example or documentation about this topic. 
I hope you can give me an example in order to configure the 
buzzer. 
Thank you for your help.

Labels (1)
Tags (1)
0 Kudos
5 Replies

883 Views
J2MEJediMaster
Specialist I

If you are using CodeWarrior for MCUs V10.2, a pure assembly-language example program for the MC9S08QE128 can be found at:

 

{CodeWarrior installation directory}\MCU\CodeWarrior_Examples\HC_Examples\mc9s08qe128_demo\.

 

For documentation, look under

 

{CodeWarrior installation directory}\MCU\Help\PDF

 

for the HCS08-RS08_Assembler_MCU_Eclipse.pdf manual.

 

Hope this helps,

 

---Tom


0 Kudos

883 Views
EducaFree
Contributor I

I checked the example but it demonstrates interrupts from the timer and uses general purpose timeouts, but it doesn't show how to configure or use the buzzer of the microcontroller. Do you know where can I find another example?

0 Kudos

883 Views
bigmac
Specialist III

Hello,

 

I do not have any assembly code.  However, if I assume that you are using the DEMOQE board, and with a jumper installed at the J19 position, the buzzer device is effectively connected between PTB5 and ground.  This is also the pin associated with TPM1CH1.

 

The simplest way to operate the buzzer is to set the TPM channel for edge-aligned PWM operation, with a frequency of 4kHz, the resonant frequency of the buzzer, and 50 percent duty.  For PWM mode, channel interrupts are not necessary.

 

TPM1MOD register setting will determine the PWM frequency, taking into account the bus clock you are using and the TPM prescaler.  TPM1C1V register will determine the duty, and should be set to (TPM1MOD + 1) / 2 for 50 percent.

 

Regards,

Mac

 

0 Kudos

883 Views
EducaFree
Contributor I

Hello,

 

Thank you, I could initialize the buzzer with your recommendations. But now the problem is that the "bip" sound is very slow, I tried to change the duty cycle and the PWM frequency as bigmac suggested but I can´t solve that problem. Do you have any other idea?

 

This is my code:

 

; Include derivative-specific definitions            INCLUDE 'derivative.inc'            ; export symbols            XDEF _Startup, main            ; we export both '_Startup' and 'main' as symbols. Either can            ; be referenced in the linker .prm file or from C/C++ later on                                                XREF __SEG_END_SSTACK   ; symbol defined by the linker for the end of the stack; variable/data sectionMY_ZEROPAGE: SECTION  SHORT         ; Insert here your data definition  org $1000ActualRatio DC.B 0MyCode:     SECTIONmain:_Startup:            LDHX   #__SEG_END_SSTACK ; initialize the stack pointer            TXS   CLI   ; enable interruptsmainLoop:        ; SCGC1: TPM3=1,TPM2=1,TPM1=1,ADC=1,IIC=1,SCI2=1,SCI1=1         LDA     #$FF        STA     SCGC1                                                       ; SCGC2: DBG=1,FLS=1,IRQ=1,KBI=1,ACMP=1,RTC=1,SPI=1         LDA     #$FF        STA     SCGC2        ; Common initialization of CPU registers       ; PTBDD: PTBDD5=1 */       LDA     #$20        STA     PTBDD           ; PTBD: PTBD5=0 */ CLRA        STA     PTBDD        ; PTAPE: PTAPE3=1,PTAPE2=1         LDA     PTAPE        ORA     #$0C        STA     PTAPE        ; PTDPE: PTDPE3=1,PTDPE2=1         LDA     PTDPE        ORA     #$0C        STA     PTDPE; PTASE: PTASE7=0,PTASE6=0,PTASE4=0,PTASE3=0,PTASE2=0,PTASE1=0,PTASE0=0         LDA     PTASE        AND     #$20        STA     PTASE; PTBSE: PTBSE7=0,PTBSE6=0,PTBSE5=0,PTBSE4=0,PTBSE3=0,PTBSE2=0,PTBSE1=0,PTBSE0=0         CLRA        STA     PTBSE                                               ; PTCSE: PTCSE7=0,PTCSE6=0,PTCSE5=0,PTCSE4=0,PTCSE3=0,PTCSE2=0,PTCSE1=0,PTCSE0=0         CLRA        STA     PTCSE                                               ; PTDSE: PTDSE7=0,PTDSE6=0,PTDSE5=0,PTDSE4=0,PTDSE3=0,PTDSE2=0,PTDSE1=0,PTDSE0=0         CLRA        STA     PTDSE                                               ; PTESE: PTESE7=0,PTESE6=0,PTESE5=0,PTESE4=0,PTESE3=0,PTESE2=0,PTESE1=0,PTESE0=0         CLRA        STA     PTESE                                               ; PTADS: PTADS7=1,PTADS6=1,PTADS5=0,PTADS4=1,PTADS3=1,PTADS2=1,PTADS1=1,PTADS0=1         LDA     #$DF        STA     PTADS                                               ; PTBDS: PTBDS7=1,PTBDS6=1,PTBDS5=1,PTBDS4=1,PTBDS3=1,PTBDS2=1,PTBDS1=1,PTBDS0=1         LDA     #$FF        STA     PTBDS                                                       ; PTCDS: PTCDS7=1,PTCDS6=1,PTCDS5=1,PTCDS4=1,PTCDS3=1,PTCDS2=1,PTCDS1=1,PTCDS0=1         LDA     #$FF        STA     PTCDS                                               ; PTDDS: PTDDS7=1,PTDDS6=1,PTDDS5=1,PTDDS4=1,PTDDS3=1,PTDDS2=1,PTDDS1=1,PTDDS0=1         LDA     #$FF        STA     PTDDS                                               ; PTEDS: PTEDS7=1,PTEDS6=1,PTEDS5=1,PTEDS4=1,PTEDS3=1,PTEDS2=1,PTEDS1=1,PTEDS0=1         LDA     #$FF        STA     PTEDS     PWM1_Init:; TPM1SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=0,PS2=0,PS1=0,PS0=0 */        CLRA        STA     TPM1SC             ;  Disable device; TPM1C1SC: CH1F=0,CH1IE=0,MS1B=1,MS1A=1,ELS1B=1,ELS1A=1,??=0,??=0 */        LDA     #$3C         ;  HERE I change the level but anything happens!              STA     TPM1C1SC             ;  Set up PWM mode  ; TPM1MOD: BIT15=0,BIT14=0,BIT13=0,BIT12=1,BIT11=0,BIT10=0,BIT9=0,BIT8=0,BIT7=0,BIT6=1,BIT5=1,BIT4=0,BIT3=0,BIT2=0,BIT1=0,BIT0=1 */        LDA     #$80         STA     TPM1MOD         ; PWM frequency;  TPM1SC: TOF=0,TOIE=0,CPWMS=0,CLKSB=0,CLKSA=1,PS2=0,PS1=0,PS0=0 */        LDA     #$08        STA     TPM1SC     ;  Run the counter (set CLKSB:CLKSA)

 

0 Kudos

883 Views
bigmac
Specialist III

Hello,

 

Since you have not initialised the ICS module (nor trimmed the internal reference), the power up default bus frequency will lie within the range 4.4 MHz +/-25 percent.  This will dictate the frequency accuracy of the generated tone.

 

I can see some problems with your code -

  1. The TPM1C1SC value of $33 seems inappropriate.  I might have expected a value of $38 or $3C.
  2. You have not taken into account that the TPM1MOD register is 16-bit.  You are setting the high byte only.  For a nominal 4.4 MHz bus with TPM prescale of 1, I would expect a 16-bit value of 1100 decimal.
  3. You have not initialised TPM1C1V 16-bit register - the duty will be uncontrolled (and could even be 100 percent, i.e no output).
TPMMODVAL  EQU 1099      ; Period of 1100 TPM clock periodsPWMDUTY    EQU (TPMMODVAL+1)/2...     CLR   TPM1SC        ; Disable TPM1 module     MOV   #$38,TPM1C1SC ; Edge aligned PWM mode     LDHX  #TPMMODVAL    ; Nominal 4kHz TPM overflow rate     STHX  TPM1MOD     LDHX  #PWMDUTY      ; 50 percent duty     STHX  TPM1C1SC     MOV   #$08,TPM1SC   ; Enable TPM1, prescale 1

 

 

Note that the value of TPMDUTY automatically changes should TPMMODVAL be altered, perhaps to adjust the for a different bus frequency.

 

Once initialised, you might also use some macros to control the buzzer.  In this case, you would probably not enable the TPM1 module within the initialisation code above.

 

BUZZ_ON: MACRO         MOV  #$08,TPM1SC  ; Enable TPM1 module         ENDMBUZZ_OFF:MACRO         CLR  TPM1SC       ; Disable TPM1 module         ENDM

 

Regards,

Mac

 

0 Kudos