Header code
In this project, we use 2 header:
#include <reg52.h>
#include "PWM_test.c"
For Reg52.h, we use it as a default without edit or customize any code. we also use Reg52.h because we choose AT89S52 as our microchip and we dont want any trouble when we create a code and make the robot working.
For PWM_test.c, we use to control the speed and setup for the operation for DC motor. the code will shown below.
------------------------------------------------------PWM_test.c-----------------------------------------------------
/* Global variables and definition */
/*
Generates Timer interrupt Timer 0 at Osc Freq of 11.0592 MHz
Note: PWM width 255~166
Max time delay is 65.536 ms*/
#include <reg52.h>
unsigned char pwm_width;
bit pwm_flag = 0;
sbit PWM = P1^0;
void pwm_setup(unsigned char Pulse)
{
TMOD = 0;
//pwm_width = 160;
pwm_width = Pulse;
EA = 1;
ET0 = 1;
TR0 = 1;
}
/* Timer 0 Interrupt service routine */
void timer0() interrupt 1
{
if (!pwm_flag)
{ /* Start of High level */
pwm_flag = 1; /* Set flag */
PWM = 1; /* PWM set high */
TH0 = pwm_width; /* Load timer */
TF0 = 0; /* Clear interrupt flag */
}
else
{ /* Start of Low level */
pwm_flag = 0; /* Clear flag */
PWM = 0; /* Clear PWM pin */
TH0 = 255 - pwm_width; /* Load timer */
TF0 = 0; /* Clear Interrupt flag */
}
}
void pwm_stop()
{
TR0 = 0; /* Disable timer to disable PWM */
}
--------------------------------------------------------------------------------------------------------------------------
Comments
Post a Comment