' ' A small test program to exercise the PWM functions on ' a 68HC912B32 ' ' This file is written for the 68HC12 by Kevin Ross (kevinro@nwlink.com) ' It is written in SBASIC. ' To compile this file, you need to invoke the following commands: ' ' sbasic adtest.bas /c8000 /v0000 /m6812 > adtest.asm ' as12 adtest.asm ' declare i include "regs12.lib" main: ' Turn off the COP pokeb copctl,0 ' Enable serial port poke sc0bdh, 26 ' Set for 19200 baud pokeb sc0cr2,$0c ' Enable transceiver print print "****####****" print "pwmtest running" ' Set the concatenate PWM channel bits so we have channels 0 and 1 acting ' as 8 bit counters, and channels 2/3 acting as a single 16-bit counter ' ' There are two independent clocks on the PWM. Clock A and Clock B. ' The low 6 bits of PWCLK set the prescalar values (3 bits each). ' ClockA is going to run at full speed. ClockB is to be divided by 4 ' pokeb PWCLK, $82 ' ' PWPOL is the Clock Select and Polarity control register. Clock A ' runs channels 0 and 1, and CLOCK B is going to run channels 2 and 3 ' Polarities are set so that the signal is low at the start of the ' cycle, and flips high when the duty count is reached on channel 0. ' ' Channel 2/3 is set to be high at the start, and go low at the end ' of the duty period. ' pokeb PWPOL, $0C ' For now, we will assume all of the prescalars should be set to zero pokeb pwscal0, $0 pokeb pwscal1, $0 ' Lets setup a 40khz signal on the 8-bit PWM channel 0 with a 50% duty cycle. ' If you do the math, 8mhz/40k turns out to be 200, ' which means that our period is 200 clocks. If you check the documentation, ' it turns out that the correct value is 200 - 1 because of the way that ' PWPER is compared in left aligned mode. ' pokeb pwper0, 200 - 1 pokeb pwdty0, 100 - 1 ' Now setup channel 2/3 with a 20 millisecond period, and a 1.52 millisecond duty ' period. This should center a servo motor. 1.52ms / 500ns = 3040 ' poke pwper2,40000 - 1 poke pwdty2,3040 - 1 ' Now enable channel 0 and channel 2/3 in the PWEN register. Due to a bug in the ' XC and previous versions of the B32, you need to turn on both channel 2 and 3 ' to get this to work. pokeb pwen, $0D do i = inkey() i = i AND $ff loop