Skip to content

Instantly share code, notes, and snippets.

@markuskreitzer
Created October 1, 2012 04:55
Show Gist options
  • Save markuskreitzer/3809534 to your computer and use it in GitHub Desktop.
Save markuskreitzer/3809534 to your computer and use it in GitHub Desktop.
PWM Code Draft For Lab 4, Session 7
#include <hidef.h> /* common defines and macros */
#include <mc9s12c32.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12c32"
#pragma CODE_SEG DEFAULT
#define PWMDTY0 DUTY_CYCLE // These HCS12 defines are too cryptic!
// ################
// Global Variables
// ################
int duty_perc = 0;
// ################
// Functions
// ################
void pwm_setup( void ){
// This function sets up Clock 1 and PWM Channel 5.
// Desired PWM Frequency = Bus Clock (4 MHz) / PeriodValue (250) / PrescaleA (4) / ScaleA (1)
PWME = 0x01; // Enable PWM.
PWMCLK = 0x01; // (1 or 2) Which of the two PWM clocks to use.
PWMPRCLK = 0x04; // PWM Prescale Clock Select.
PWMSCLA = 0x01; // PWM scale register for Clock SA.
PWMPER5 = 0xFA; // PWM Channel 5 Period. Set to 250.
PWMDTY5 = 0xFA; // PWM Channel 5 Duty Cycle: ( Duty / Period) * 100 = %duty cycle
}
void set_duty_cycle( int duty_perc ){
// Update the Duty Cycle. Receive values betwen 0 and 100;
if( duty_perc > 0x00 && duty_perc <= 0xFA){
DUTY_CYCLE = ( (duty_perc * period )/ 100);
}else{
// If invalid input, stop the motor.
DUTY_CYCLE = 0;
}
}
void main(void)
{
//Enabling portT for PWM0
/* MODRR =0x01;
The line MODRR=0x01 may not be
necessary if your PWM
in not multiplexed with the Timer module.
Check the block diagram of your circuit.
*/
while(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment