//DC motor control using the L298N H-Bridge with HCS12 Tower with Channel P2 of PWM //HCS12 Tower does not come with H-bridge driver //We use L298N H-bridge chip (or module) for this experiment. You can buy the H-Bridge module from the following site: //https://www.bananarobotics.com/shop/L298N-Dual-H-Bridge-Motor-Driver?gclid=CM28h-OQib0CFTBgMgod0CMAow //On HCS12 Tower, we connect the PP2 (PWM chan 2) to EN1 pin of L298N. And //PT0 is connected to In1 and PT1 to In2 input pins of the L298N. //The L298N chip allows to have an external power source of up to 46V (4.5V-46V) to drive DC motor. DO NOT USE MORE THAN 9 V //Steps to connect and run this program to control DC motor with L298N H-Bridge //1)Connect an external 5V DC power to the Vss //2)Connect the + lead of your DC motor to MOTORA1 pin //3)Connect the - lead of your DC motor to MOTORA2 pin //4)Connect Ena to PP2 The PP2 is accessible via the Axiom Peripgheral board //5)Connect In1=PT0 Notice the PT0 is accessible via the Axiom Peripgheral board //6)Connect In2=PT1 Notice the PT1 is accessible via the Axiom Peripgheral board //Compile (F7), Download (F5) and run(F5), this program //This program will turn the DC motor clockwise(CW) for 3 seconds, //then it stops and turns CCW for 3 seconds. It always stops and rests for 1 second before changing direction //It does that continuosly. // Written and tested by M. Mazidi. // Tested by Brandon Collison //for PWM and DC motor control see chapter 17 of HCS12 book by Mazidi & Causey. #include /* common defines and macros */ #include "derivative.h" /* derivative-specific definitions */ void MSDelay(unsigned int); void main(void) { /* put your own code here */ DDRT = 0x03; //PORTT as output is controlled by PT0 and PT1 pins PTT=0b00000000; //Stop DC motor(PT0=0 and PT1=0). //NOTICE ONLY ONE OF THEM CAN BE ON FOR TURNING CLOCKWISE (CW PT0=1) OR COUNTER CLOCKWISE (CCW PT1=1) //DO NOT NOT USE PT0=1 AND PT1=1 AT THE SAME TIME. PWMPRCLK=0x30; //Clock B, Divide by 8, 6.25MHz/2^3 = 781.25KHz PWMSCLB=50; //Clock SB scale, 2*50 = 100, 781.25KHz/100 = 7.8125KHz PWMCLK=0x04; //ClockB for Chan 2 PWMPOL=0x04; //high and low for polarity for Chan 2 PWMCAE=0; //Left aligned PWMCTL=0; //8-bit chan and PWM freeze during wait PWMCNT2=0; //start the PWMCount2 with 0 value PWME=0x04; //Enable PWM chan 2 for(;;) { PWMPER2=100; //PWM_Period Freq. = ClockSB/100= 7.8125KHz Hz/100=78.125 Hz PWMDTY2=60; //Duty Cycle=60% (60% of 100). Change this value and experiment with duty cycle. //As another example, If you have a PWMPER2 = 50 and you want 60% Duty Cycle //50 * 60% = 30, PWMDTY2 = 30 PTT=0b00000001; //turn on for clockwise direction (DO NOT USE 0b00000011 FOR BOTH ON) MSDelay(3000); //run for 3 sec PTT=0x00000000; //stop the motor MSDelay(1000); //wait 1 sec for motor to stop completely before changing direction PTT=0b00000010; //change direction to CCW (DO NOT USE 0b00000011 FOR BOTH ON) MSDelay(3000); //run for 3 sec PTT=0x00000000; //stop the motor. MSDelay(1000); //wait 1 sec for motor to stop completely before changing direction. } } //j is for 1 msec delay (approax). Not Measured by Scope yet // void MSDelay(unsigned int itime) { unsigned int i; unsigned int j; for(i=0;i