Technical discussions about Freescale Microcontrollers: M68HC11. (Freescale Semiconductor is a Subsidiary of Motorola).
Need help - kama_333 - Aug 23 12:10:00 2005
Hi for All
My attached program is used to generated PWM, but I dont know how
can make it work as variable frequency ,if you know how can we make
this program work as controlable freequency, by please help me? .
kaled
attach program****
******************************************************
*TIMER REGISTER ADDRESSES
REGBAS EQU $1000 * Register base
RAM EQU $0000
OC1M EQU $0C * OC1 action mask register
OC1D EQU $0D * OC1 action data register
TOC1 EQU $16 * Output compare 1 register
TOC2 EQU $18 * Output compare 2 register
TOC3 EQU $1A * Output compare 3 register
TOC4 EQU $1C
TOC5 EQU $1E
TCTL1 EQU $20 * Timer control register 1
TCTL2 EQU $21
TMSK1 EQU $22 * Timer interrupt mask register 1
TFLG1 EQU $23 * Timer interrupt flag register 1
TMSK2 EQU $24
TFLG2 EQU $25
PACTL EQU $26
TCNT EQU $0E * Timer counter register
AO EQU $88 * port A7 and A3 direction value
DO EQU $FF
CO EQU $F0
BO EQU $03
PORTA EQU $00
PWMP1P EQU 100 * MINIMUM 2*100*.5.-6,2KHz
PVOC1 EQU $00DF ;EVB Pseudo Vector for OC1
*PROGRAM VARIABLES
ORG RAM
PWMDC1 RMB 1 ;PA6
PWMDC2 RMB 1 ;PA5
PWMDC3 RMB 1 ;PA4
PWMDC4 RMB 1 ;PA3
PWMPER RMB 2 * The time between Oc1 intr
PERCENT RMB 2 * Ascii bcd #'s for %dty cy
PWIDTH RMB 2 * Hex # of pwm duty cycle %
*START OF MAIN PROGRAM
ORG $0100
opt c
START
LDS #$0047 * User's stack area on EVB
LDAA #$7E
STAA PVOC1
LDX #SV60C1
STX PVOC1+1
LDX #REGBAS ;initialize base register
BSET PACTL,X AO ;set PA3 and PA7 as output
*SV60C1 This subroutine initializes the oc1 and oc2 and sets
* toc1 and toc2 registers.
LDAA #50
STAA PWMDC1
STAA PWMDC2
STAA PWMDC3
STAA PWMDC4
LDAA #%01010101 * Set OM2 & OML
STAA TCTL1,X * OC2 sets its pin hi
LDAA #%01111000 * OC1 affects OC2 pin
STAA OC1M,X * Sets OC1 action mask
CLRB * OC1 sets all OCs low
ARNZ60 LDAA PWMDC1
CMPA #50
BLS ARNZ61
ADDB #%01000000
ARNZ61 LDAA PWMDC2
CMPA #50
BLS ARNZ62
ADDB #%00100000
ARNZ62 LDAA PWMDC3
CMPA #50
BLS ARNZ63
ADDB #%00010000
ARNZ63 LDAA PWMDC4
CMPA #50
BLS ARNZ64
ADDB #%00001000
ARNZ64 STAB OC1D,X
LDAA #PWMP1P
LDAB #100
MUL
STD PWMPER
STD TOC1,X
LDAA PWMDC1
BSR CALOFF
STD TOC2,X
LDAA PWMDC2
BSR CALOFF
STD TOC3,X
LDAA PWMDC3
BSR CALOFF
STD TOC4,X
LDAA PWMDC4
BSR CALOFF
STD TOC5,X
LDAA #$80
STAA TFLG1,X
STAA TMSK1,X
CLI
BRA *
CALOFF CMPA #50
BHS ARN6A
TAB
LDAA #100
SBA
ARN6A CMPA #100
BLS ARN6B
LDAA #100
ARN6B LDAB #PWMP1P
MUL
RTS
* OC1 INTERRUPT SERVICE ROUTINE
SV60C1 LDX #REGBAS
LDD TOC2,X
ADDD PWMPER
STD TOC2,X
LDD TOC3,X
ADDD PWMPER
STD TOC3,X
LDD TOC4,X
ADDD PWMPER
STD TOC4,X
LDD TOC5,X
ADDD PWMPER
STD TOC5,X
LDD TOC1,X
ADDD PWMPER
STD TOC1,X
BCLR TFLG1,X $7F
RTI
***********************************8

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )
Re: Need help - BobG...@... - Aug 23 12:21:00 2005
In a message dated 8/23/05 12:19:21 P.M. Eastern Daylight Time,
kama_333@kama... writes:
My attached program is used to generated PWM, but I dont know how
can make it work as variable frequency ,if you know how can we make
this program work as controlable freequency, by please help me? .
=====================
Usually PWM is used for lamp dimming or voltage adjustment. The PWM
frequency just needs to be high enough so that 'ripple' in the average voltage isnt
significant. Which application do you have that looks for different
frequencies AND different pulse width?
[Non-text portions of this message have been removed]

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )
Re: Need help - kama_333 - Aug 24 1:19:00 2005
Hallo Dear Sir:
AS you said I need pwm to drive the induction motor,my code can
generate that pulse at fixed frequency and fixed pule width.
I want make my program worke for different frequencies AND different
pulse width.
My idea is control the output frequency by using variable voltage at
the A/D port((i.e I want applying variable voltages to the AD port,
and by change the applying voltage "at analoge to digital port", the
output FREQUENCY "at timer port"will change also)).
I want help to modify my code for this applecation.
Regards
kama

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )
Re: Re: Need help - Mike McCarty - Aug 24 2:20:00 2005
kama_333 wrote:
> Hallo Dear Sir:
>
> AS you said I need pwm to drive the induction motor,my code can
> generate that pulse at fixed frequency and fixed pule width.
>
> I want make my program worke for different frequencies AND different
> pulse width.
>
> My idea is control the output frequency by using variable voltage at
> the A/D port((i.e I want applying variable voltages to the AD port,
> and by change the applying voltage "at analoge to digital port", the
> output FREQUENCY "at timer port"will change also)).
>
> I want help to modify my code for this applecation.
>
> Regards
>
> kama
Is this for the 11 or the 12? I can help some with the 11.
You can use OC1 and OC2 (for example) to control a single
pin. Use OC1, say, to set the pin high. Use OC2 to set
the pin back low. The frequency you want determines the
total time for the OC1 and OC2 compare values. The duty
cycle determines how much of the total count goes into
the OC1 part and how much into the OC2 part.
So, based on the frequency, compute the total duration, in
E-clock cycles of one PWM cycle Ttot. Multiply this by the
proportion of the time you want the pulse high, and apportion
this to OC1. Apportion the rest to OC2.
For example, suppose you have an E-clock of 2MHz, and want
a frequency of 1KHz. Then 1/1KHz = 1ms, which is 2000 cycles.
So the total duration of one PWM cycle is 2000 E-clocks.
Suppose you want a duty cycle of 30%. Then 30% * 2000 = 600.
The pulse must be high for 600 E-clocks, and low for 1400
E-clocks. So set up OC1 to control the same pin as OC2.
Set up OC1 to set the pin high, and OC2 to set the pin low.
Force OC1 to compare NOW.
Set up OC1 to fire in 2000 clocks, and OC2 to fire in 600 clocks.
Start going.
Each time you fire either OC1 or OC2, set them to fire in
another 2000 E-clocks. In this way, every 2000 clocks, the pin
goes high. Then 600 clocks later, the pin goes low.
Mike
--
p="p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}
This message made from 100% recycled bits.
You have found the bank of Larn.
I can explain it for you, but I can't understand it for you.
I speak only for myself, and I am unanimous in that!

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )
Re: Need help - kama_333 - Sep 1 23:45:00 2005
Hi for All,
I did some modify for the last program to be variable frequency,
But I
face Small problem with the delay , the delay is still short and
this cause the distortion in the output waveform, I want make the
delay longer?????
I attached my new progam ( with the variable frequency) please check
it.
regards
Kama
*********************************************************************
*******
*TIMER REGISTER ADDRESSES
REGBAS EQU $1000 * Register base
RAM EQU $0000
OC1M EQU $0C * OC1 action mask register
OC1D EQU $0D * OC1 action data register
TOC1 EQU $16 * Output compare 1 register
TOC2 EQU $18 * Output compare 2 register
TOC3 EQU $1A * Output compare 3 register
TOC4 EQU $1C
TOC5 EQU $1E
TCTL1 EQU $20 * Timer control register 1
TCTL2 EQU $21
TMSK1 EQU $22 * Timer interrupt mask
register 1
TFLG1 EQU $23 * Timer interrupt flag
register 1
TMSK2 EQU $24
TFLG2 EQU $25
PACTL EQU $26
TCNT EQU $0E * Timer counter register
AO EQU $88 * port A7 and A3 direction
value
DO EQU $FF
CO EQU $F0
BO EQU $03
PORTA EQU $00
AD_CTL EQU $30 ;analog digital control status
register-set the single channel
AD_R1 EQU $31 ;analog digital data register, read
data heere
OUTRHLF EQU $FFB5
OUTA EQU $FFB8
BUFFALO EQU $E0B2
*********************************************************************
************
*PSEUDO VECTOR EQUATES FOR OUTPUT COMPARE INTERRUPTS
PVTOF EQU $00D0
PVOC1 EQU $00DF
PVOC2 EQU $00DC
PVOC3 EQU $00D9
ORG RAM
TEMP_REG RMB 1 ;temperory register-1byte
RESULT_BUF RMB 5 ;result buffer register -5bytes
PWMP1P RMB 1
PWMDC1 RMB 1
PWMDC2 RMB 1
PWMDC3 RMB 1
PWMDC4 RMB 1
IC1DUN RMB 1
IC1MOD RMB 1
OVCNT1 RMB 1
RES1 RMB 2
HTEMP RMB 3
FRSTE RMB 2
PERC RMB 2
TEMP1 RMB 2
FREQH RMB 2
HPW RMB 2
DBUFR RMB 8
PWMPER RMB 2
OFFHI RMB 2
OFFID RMB 2
ORG $0100
LDS #$0047 * User's stack area on EVB
LDAA #$7E
STAA PVOC1
LDX #SV60C1
STX PVOC1+1
LDX #REGBAS
BSET PACTL,X AO
FR LDX #REGBAS
JSR ADCONV
JSR CONVERT
JSR BIN_BCD
JSR DISPLAY
*********************************************************************
******
*HERE YOU CAN SET THE VARIABLE FREQUENCY, ITS DEPEND ON VARIABLE
VOLTAGE
LDAB TEMP_REG+1 ;EXAMPLE 5V = 512 = 25.6ms = 39Hz
STAB PWMP1P
JMP ADD
*;ADC repeatly convert value,0ne channel only, port E6
ADCONV LDAA #%00100110 * Load A accum. with bits to set up A/D
control
STAA AD_CTL,X * A/D - continuous conversions, channel
0 only
LDY #$26
DEL DEY
BNE DEL
RTS
*********************************************************************
********
*5V=$FF=#256, 0.5v=$80=#128 , this is just example to get 5V is
equal 512
*PWMP1P (#512*100*0.5-6=25.6ms---39Hz)
*YOU CAN SET THE FREQUENCY VALUE BY SET THE ALGORITHM HERE BY USING
MUL,
*ADD, IDIV INSTRUCTION
*********************************************************************
********
CONVERT LDX #REGBAS
LDAA AD_R1,X ;read digital result on ADR1 register
LDAB #2 ;multiply by 2
MUL
STD TEMP_REG ;store digital result at temperory
register
RTS
*********************************************************************
********
*THIS THE ROUTINE DISPLAY THE ANSWER
DISPLAY LDAA RESULT_BUF+1
JSR OUTRHLF
LDAA #$2E
JSR OUTA
LDAA RESULT_BUF+2
JSR OUTRHLF
LDAA RESULT_BUF+3
JSR OUTRHLF
LDAA #$56
JSR OUTA
RTS
*********************************************************************
*
BIN_BCD LDY #RESULT_BUF+3 ;Conversation starts with lowest
digit
LDD TEMP_REG ;Load limit speed to be
converted
CON_LP LDX #$A ;Divide hex digit by 10
IDIV ;Quotient in X, Remainder in D
STAB 0,Y ;Stores 8 LSB's of remainder as BCD
digit
DEY
CPX #$0 ;Determines if last digit is stored
XGDX ;Exchanges remainder & quotient
BNE CON_LP
LDX #REGBAS ;Reload BASE into main program
RTS
*********************************************************************
**********
*INIT_OC- This subroutine initializes the oc1 and oc2 and sets
* toc1 and toc2 registers.
ADD LDAA #20
STAA PWMDC1
STAA PWMDC2
STAA PWMDC3
STAA PWMDC4
LDAA #%01010101 * Set OM2 & OML
STAA TCTL1,X * OC2 sets its pin hi
LDAA #%01111000 * OC1 affects OC2 pin
STAA OC1M,X * Sets OC1 action mask
CLRB * OC1 sets all OCs low
ARNZ60 LDAA PWMDC1
CMPA #50
BLS ARNZ61
ADDB #%01000000
ARNZ61 LDAA PWMDC2
CMPA #50
BLS ARNZ62
ADDB #%00100000
ARNZ62 LDAA PWMDC3
CMPA #50
BLS ARNZ63
ADDB #%00010000
ARNZ63 LDAA PWMDC4
CMPA #50
BLS ARNZ64
ADDB #%00001000
ARNZ64 STAB OC1D,X
LDAA PWMP1P
LDAB #100
MUL
STD PWMPER
STD TOC1,X
LDAA PWMDC1
BSR CALOFF
STD TOC2,X
LDAA PWMDC2
BSR CALOFF
STD TOC3,X
LDAA PWMDC3
BSR CALOFF
STD TOC4,X
LDAA PWMDC4
BSR CALOFF
STD TOC5,X
LDAA #$80
STAA TFLG1,X
STAA TMSK1,X
CLI
JSR DELAY
JMP FR
*********************************************************************
*
CALOFF CMPA #50
BHS ARN6A
TAB
LDAA #100
SBA
ARN6A CMPA #100
BLS ARN6B
LDAA #100
ARN6B LDAB PWMP1P
MUL
RTS
* OC1 INTERRUPT SERVICE ROUTINE
SV60C1 LDX #REGBAS
LDD TOC2,X
ADDD PWMPER
STD TOC2,X
LDD TOC3,X
ADDD PWMPER
STD TOC3,X
LDD TOC4,X
ADDD PWMPER
STD TOC4,X
LDD TOC5,X
ADDD PWMPER
STD TOC5,X
LDD TOC1,X
ADDD PWMPER
STD TOC1,X
BCLR TFLG1,X $7F
RTI
*********************************************************************
***************
* delay loop
DELAY PSHB * save registers
PSHA
LDAB #$FF * set loop count 1)\how can increse
DEL1 LDAA #$FF * set loop count 2)/ the delay????
DEL2 DECA * decriment counter 2
BNE DEL2 * branch if not 0
DECB * decriment counter 1
BNE DEL1 * branch if not 0
PULA * restore registers
PULB
RTS
*********************************************************************
*******************

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )
Re: Need help - Ian Rumble - Sep 2 3:04:00 2005
Hi Kama,
It all depends on how much longer you want your delay to be. If you
want it to be
a little longer then look at your DEL2 loop. In that loop you execute 2
instructions,
DECB and the BNE which takes whatever number of cycles. What would
happen
if you placed a NOP in the loop after DECB, or before it? What would
happen with
2 NOPs?
For every NOP, the DEL2 increases by 256 cycles.
Regards
Ian Rumble
Omnitronics
Australia.
>>> kama_333@kama... 2/09/2005 11:45:37 am >>>
Hi for All,
I did some modify for the last program to be variable frequency,
But I
face Small problem with the delay , the delay is still short and
this cause the distortion in the output waveform, I want make the
delay longer?????
I attached my new progam ( with the variable frequency) please check
it.
regards
Kama
*********************************************************************
*******
*TIMER REGISTER ADDRESSES
REGBAS EQU $1000 * Register base
RAM EQU $0000
OC1M EQU $0C * OC1 action mask register
OC1D EQU $0D * OC1 action data register
TOC1 EQU $16 * Output compare 1 register
TOC2 EQU $18 * Output compare 2 register
TOC3 EQU $1A * Output compare 3 register
TOC4 EQU $1C
TOC5 EQU $1E
TCTL1 EQU $20 * Timer control register 1
TCTL2 EQU $21
TMSK1 EQU $22 * Timer interrupt mask
register 1
TFLG1 EQU $23 * Timer interrupt flag
register 1
TMSK2 EQU $24
TFLG2 EQU $25
PACTL EQU $26
TCNT EQU $0E * Timer counter register
AO EQU $88 * port A7 and A3 direction
value
DO EQU $FF
CO EQU $F0
BO EQU $03
PORTA EQU $00
AD_CTL EQU $30 ;analog digital control status
register-set the single channel
AD_R1 EQU $31 ;analog digital data register, read
data heere
OUTRHLF EQU $FFB5
OUTA EQU $FFB8
BUFFALO EQU $E0B2
*********************************************************************
************
*PSEUDO VECTOR EQUATES FOR OUTPUT COMPARE INTERRUPTS
PVTOF EQU $00D0
PVOC1 EQU $00DF
PVOC2 EQU $00DC
PVOC3 EQU $00D9
ORG RAM
TEMP_REG RMB 1 ;temperory register-1byte
RESULT_BUF RMB 5 ;result buffer register -5bytes
PWMP1P RMB 1
PWMDC1 RMB 1
PWMDC2 RMB 1
PWMDC3 RMB 1
PWMDC4 RMB 1
IC1DUN RMB 1
IC1MOD RMB 1
OVCNT1 RMB 1
RES1 RMB 2
HTEMP RMB 3
FRSTE RMB 2
PERC RMB 2
TEMP1 RMB 2
FREQH RMB 2
HPW RMB 2
DBUFR RMB 8
PWMPER RMB 2
OFFHI RMB 2
OFFID RMB 2
ORG $0100
LDS #$0047 * User's stack area on EVB
LDAA #$7E
STAA PVOC1
LDX #SV60C1
STX PVOC1+1
LDX #REGBAS
BSET PACTL,X AO
FR LDX #REGBAS
JSR ADCONV
JSR CONVERT
JSR BIN_BCD
JSR DISPLAY
*********************************************************************
******
*HERE YOU CAN SET THE VARIABLE FREQUENCY, ITS DEPEND ON VARIABLE
VOLTAGE
LDAB TEMP_REG+1 ;EXAMPLE 5V = 512 = 25.6ms = 39Hz
STAB PWMP1P
JMP ADD
*;ADC repeatly convert value,0ne channel only, port E6
ADCONV LDAA #%00100110 * Load A accum. with bits to set up A/D
control
STAA AD_CTL,X * A/D - continuous conversions, channel
0 only
LDY #$26
DEL DEY
BNE DEL
RTS
*********************************************************************
********
*5V=$FF=#256, 0.5v=$80=#128 , this is just example to get 5V is
equal 512
*PWMP1P (#512*100*0.5-6=25.6ms---39Hz)
*YOU CAN SET THE FREQUENCY VALUE BY SET THE ALGORITHM HERE BY USING
MUL,
*ADD, IDIV INSTRUCTION
*********************************************************************
********
CONVERT LDX #REGBAS
LDAA AD_R1,X ;read digital result on ADR1 register
LDAB #2 ;multiply by 2
MUL
STD TEMP_REG ;store digital result at temperory
register
RTS
*********************************************************************
********
*THIS THE ROUTINE DISPLAY THE ANSWER
DISPLAY LDAA RESULT_BUF+1
JSR OUTRHLF
LDAA #$2E
JSR OUTA
LDAA RESULT_BUF+2
JSR OUTRHLF
LDAA RESULT_BUF+3
JSR OUTRHLF
LDAA #$56
JSR OUTA
RTS
*********************************************************************
*
BIN_BCD LDY #RESULT_BUF+3 ;Conversation starts with lowest
digit
LDD TEMP_REG ;Load limit speed to be
converted
CON_LP LDX #$A ;Divide hex digit by 10
IDIV ;Quotient in X, Remainder in D
STAB 0,Y ;Stores 8 LSB's of remainder as BCD
digit
DEY
CPX #$0 ;Determines if last digit is stored
XGDX ;Exchanges remainder & quotient
BNE CON_LP
LDX #REGBAS ;Reload BASE into main program
RTS
*********************************************************************
**********
*INIT_OC- This subroutine initializes the oc1 and oc2 and sets
* toc1 and toc2 registers.
ADD LDAA #20
STAA PWMDC1
STAA PWMDC2
STAA PWMDC3
STAA PWMDC4
LDAA #%01010101 * Set OM2 & OML
STAA TCTL1,X * OC2 sets its pin hi
LDAA #%01111000 * OC1 affects OC2 pin
STAA OC1M,X * Sets OC1 action mask
CLRB * OC1 sets all OCs low
ARNZ60 LDAA PWMDC1
CMPA #50
BLS ARNZ61
ADDB #%01000000
ARNZ61 LDAA PWMDC2
CMPA #50
BLS ARNZ62
ADDB #%00100000
ARNZ62 LDAA PWMDC3
CMPA #50
BLS ARNZ63
ADDB #%00010000
ARNZ63 LDAA PWMDC4
CMPA #50
BLS ARNZ64
ADDB #%00001000
ARNZ64 STAB OC1D,X
LDAA PWMP1P
LDAB #100
MUL
STD PWMPER
STD TOC1,X
LDAA PWMDC1
BSR CALOFF
STD TOC2,X
LDAA PWMDC2
BSR CALOFF
STD TOC3,X
LDAA PWMDC3
BSR CALOFF
STD TOC4,X
LDAA PWMDC4
BSR CALOFF
STD TOC5,X
LDAA #$80
STAA TFLG1,X
STAA TMSK1,X
CLI
JSR DELAY
JMP FR
*********************************************************************
*
CALOFF CMPA #50
BHS ARN6A
TAB
LDAA #100
SBA
ARN6A CMPA #100
BLS ARN6B
LDAA #100
ARN6B LDAB PWMP1P
MUL
RTS
* OC1 INTERRUPT SERVICE ROUTINE
SV60C1 LDX #REGBAS
LDD TOC2,X
ADDD PWMPER
STD TOC2,X
LDD TOC3,X
ADDD PWMPER
STD TOC3,X
LDD TOC4,X
ADDD PWMPER
STD TOC4,X
LDD TOC5,X
ADDD PWMPER
STD TOC5,X
LDD TOC1,X
ADDD PWMPER
STD TOC1,X
BCLR TFLG1,X $7F
RTI
*********************************************************************
***************
* delay loop
DELAY PSHB * save registers
PSHA
LDAB #$FF * set loop count 1)\how can increse
DEL1 LDAA #$FF * set loop count 2)/ the delay????
DEL2 DECA * decriment counter 2
BNE DEL2 * branch if not 0
DECB * decriment counter 1
BNE DEL1 * branch if not 0
PULA * restore registers
PULB
RTS
*********************************************************************
*******************
Yahoo! Groups Links
=========================================================================
The information in this e-mail is intended for the addressee only.
Unauthorised use, copying, disclosure or distribution by anyone else
is prohibited. Please let us know immediately if you receive this
e-mail in error. Thank you.
=========================================================================
[Non-text portions of this message have been removed]

(You need to be a member of hc11 -- send a blank email to hc11-subscribe@yahoogroups.com )