Method for outputting PWM wave by using 51 single chip microcomputer in robot design

This function of PWM has special modules in high-end single-chip microcomputers such as Freescale and STM32. When using this type of chip to implement PWM function, it only needs to set the corresponding register to realize cycle and duty cycle control. But if you want to use 51 single-chip, it is ok, but it is more troublesome. In this case, an internal timer is needed to implement it. It can be implemented by two timers or by a timer.

The method of using two timers is to use timer T0 to control the frequency, and timer T1 to control the duty ratio. The general programming idea is this: the T0 timer interrupt causes an I0 port to output a high level, and the timer T1 is started during the interrupt of this timer T0, and this T1 is to let the IO port output a low level, thus changing the timing. The initial value of T0 can change the frequency. Changing the initial value of timer T1 can change the duty cycle.

The following focuses on the method of implementing PWM with a timer. Because most of the motors used in smart cars on the market are TT geared motors, the best working frequency of this motor is 1000HZ through repeated experiments (too high to easily whistle, too low motor is easy to shake), so below For example, the period is 1ms (1000HZ). To generate PWM waves of other frequencies, you only need to make simple modifications in the program.

When using a timer (such as timer T0), you first need to determine the period T and duty cycle D of the PWM. After determining these, you can use the timer to generate a time base t, such as the timer overflows n times. It is the time of the high level of PWM, then D*T=n*t, similarly, how many time reference n is needed to find the PWM low level time.

Because here we generate a PWM with a period of 1ms (1000HZ), we can set the interrupt interval to 0.01ms, and then interrupt 100 times to be 1ms. In the interrupt subroutine, you can set a variable such as TIme. In the interrupt subroutine, there are three important statements: 1. When TIme>=100, TIme is cleared (this statement guarantees the frequency is 1000HZ), 2. When TIme >n (n should change between 0-100), let the corresponding I/O port of the single chip output high level, when time

2. Program 1, so that the I/O port of the MCU outputs a fixed frequency PWM wave.

The following is a specific procedure according to the above ideas:

/************************************************* ******************/

/ * Program name: The microcontroller outputs a fixed frequency PWM wave * /

/ * Crystal: 11.00592 MHz CPU model: STC89C52 * /

/* Function: P2^0 port output period is 1ms (1000HZ), PWM wave with duty ratio is %80*/

/************************************************* ****************/

#include

#define uint unsigned int

#define uchar unsigned char

Sbit PWM1=P2^0;//Connect IN1 control forward

Sbit PWM2=P2^1;//Connect IN2 control inversion

Uchar time;

Void main()

{

TMOD=0x01; / / timer 0 working mode 1

TH0=0xff;//(65536-10)/256;// initial value timing

TL0=0xf7;//(65536-10)%256;//0.01ms

EA=1; / / open total interruption

ET0=1; / / open timer 0 interrupt

TR0=1; / / start timer 0

While(1)

{

}

}

Void delay(uint z)

{

Uint x,y;

For(x=z;x"0;x--)

For(y=500;y"0;y--);

}

Void tim0() interrupt 1

{

TR0=0; / / when the initial value is given, the timer is turned off

TH0=0xff;//(65536-10)/256;// initial value timing

TL0=0xf7;//(65536-10)%256;//0.01ms

TR0=1; / / open the timer

Time++;

If(time)=100) time=0;//1khz

If(time<=20) PWM1=0;//point-to-space ratio %80

Else PWM1=1;

PWM2=0;

}

Program description:

1. Determination of frequency: For the 11.0592M crystal oscillator, the PWM output frequency is 1KHZ. At this time, the timer is interrupted once for 0.01ms. When the number of interruptions is 100 times, it is 1KHZ (0.01ms*100=1ms, which is 1000HZ) When the timer counter is initialized to TH0=FF, TL0=F7.

2, on the determination of the duty cycle: At this time we change the value of time from 0-100, you can change the duty cycle from %0-%100, the above program time "= 20 hours PWM1=0; else PWM1=1; means that the time output of %20 is low level, and the time of %80 is high level, that is, the duty ratio is %80. To get other duty cycles, such as %60, just change the value of time to 40. (The program is if(time"=40) PWM1=0; else PWM1=1;)

Of course, when writing a program, you can also define a flag such as flag, which determines whether the output is high or low according to the state of the flag. If you define flag=1, the output is high. Use a variable to record the number of timer interrupts. The interrupt will make the variable of the number of interrupts +1. In the interrupt program, it is judged whether the value of this variable has reached n. If the time to indicate the high level is enough, then the flag is changed to 0, the output is low, and the interrupt is recorded. The value of the variable is cleared to zero. Every time it is interrupted, it is still +1. According to the flag=0, it is judged whether the value of the record variable has reached n. If it is reached, it means that the low time of PWM is enough, then change flag=1. The output changes to a high level, and the number of recording variables is cleared, restarted, and you can get the PWM waveform you want in this loop. This method is not an example here. Please try to write it yourself.

3, program 2, use the microcontroller I / O port to output PWM wave, and can control the positive and negative through the button

In the program we usually need to control the forward and reverse of the motor, such as a positive and negative control through a button, at this time we can also set a flag such as flag. In the main program, when the button is pressed each time, the flag is inverted. Then, when the flag is 1 in the subroutine, the forward rotation routine is performed, and when the flag is 0, the reverse rotation routine is executed. The following program functions are 1000HZ output from the I/O port P2^0 and P2^1 of the MCU, the duty ratio is %50, and the P3^7 button can be used to control the positive and negative rotation of the positive motor.

/************************************************* ******************/

/ * Program name: PWM DC motor speed control * /

/ * Crystal: 11.00592 MHz CPU model: STC89C52 * /

/* function: PWM wave control of DC motor, can be controlled by button to reverse */

/************************************************* ****************/

#include

#define uint unsigned int

#define uchar unsigned char

Uchar time, count=50, flag=1; / / low-level duty cycle

Sbit PWM1=P2^0;//PWM channel 1, reverse pulse

Sbit PWM2=P2^1; // PWM channel 2, forward pulse

Sbit key_turn=P3^7; //motor commutation

/************Function declaration **************/

Void delayxms(uint z);

Void Motor_turn(void);

Void timer0_init(void);

/********* main function **********************/

Void main(void)

{

Timer0_init();

While(1)

{

Motor_turn();

}

}

/****************Delay processing**********************/

Void delayxms(uint z)//delay xms program

{

Uint x,y;

For(y=z;x"0;x--)

For(y=110;y"0;y--);

}

/************ Motor forward and reverse control **************/

Void Motor_turn(void)

{

If(key_turn==0)

{

Delayxms(2);//The time here cannot be too long, otherwise the interrupt will be conflicted.

If(key_turn==0)

{

Flag=~flag;

}

While(!key_turn);

}

}

/***********Timer 0 Initialization***********/

Void timer0_init(void)

{

TMOD=0x01; //Timer 0 works in mode 1

TH0=(65536-10)/256;

TL0=(65536-10)%256;

TR0=1;

ET0=1;

EA=1;

}

/**************Timed 0 interrupt handling ******************/

Void timer0_int(void) interrupt 1

{

TR0=0; / / set the timer initial value period, turn off the timer

TH0=(65536-10)/256;

TL0=(65536-10)%256;

TR0=1;

If(flag==1)//The motor is turning forward

{

PWM1=0;

Time++;

If(time{

PWM2=1;

}

Else

PWM2=0;

If(time)=100)

{

Time=0;

}

}

Else //motor reversal

{

PWM2=0;

Time++;

If(time{

PWM1=1;

}

Else

PWM1=0;

If(time)=100)

{

Time=0;

}

}

}

4, the program 4, the single-chip output PWM, and can control the positive and negative and achieve speed

In order to make everyone thoroughly master this aspect, a complicated program is given below, which realizes the function of controlling the forward and reverse through one button and adjusting the speed between 0 and 20 by the other two buttons.

/************************************************* ******************/

/ * Program name: PWM DC motor speed control * /

/ * Crystal: 11.00592 MHz CPU model: STC89C52 * /

/ * DC motor PWM wave control, can be controlled by the button to reverse the rotation and adjust the speed between 0 and 20 * /

/************************************************* ****************/

#include

#define uint unsigned int

#define uchar unsigned char

Uchar time, count=50, flag=1; / / low-level duty cycle

Sbit PWM1=P2^0;//PWM channel 1, reverse pulse

Sbit PWM2=P2^1; // PWM channel 2, forward pulse

Sbit key_add=P3^5;//Motor acceleration

Sbit key_dec=P3^6;//motor deceleration

Sbit key_turn=P3^7;//motor commutation

/************Function declaration **************/

Void delayxms(uint z);

Void Motor_turn();

Void Motor_add();

Void Motor_dec();

Void timer0_init();

/********* main function **********************/

Void main()

{

Timer0_init();

While(1)

{

Motor_turn();

Motor_add();

Motor_dec();

}

}

/****************Delay processing**********************/

Void delayxms(uint z)//delay xms program

{

Uint x,y;

For(y=z;x"0;x--)

For(y=110;y"0;y--);

}

/************ Motor forward and reverse control **************/

Void Motor_turn()

{

If(key_turn==0)

{

Delayxms(2);//The time here cannot be too long, otherwise the interrupt will be conflicted.

If(key_turn==0)

{

Flag=~flag;

}

While(!key_turn);

}

}

Void Motor_add()//Motor acceleration

{

If(key_add==0)

{

Delayxms(2);//The time here cannot be too long, otherwise the interrupt will be conflicted.

If(key_add==0)

{

Count+=5;

If(count)=100)

{

Count=0;

}

}

While(!key_add);

}

}

Void Motor_dec()//Motor acceleration and deceleration

{

If(key_dec==0)

{

Delayxms(2);//The time here cannot be too long, otherwise the interrupt will be conflicted.

If(key_dec==0)

{

Count-=5;

If(count)=100)

{

Count=0;

}

}

While(!key_dec);

}

}

/***********Timer 0 Initialization***********/

Void timer0_init()

{

TMOD=0x01; //Timer 0 works in mode 1

TH0=(65536-10)/256;

TL0=(65536-10)%256;

TR0=1;

ET0=1;

EA=1;

}

/**************Timed 0 interrupt handling ******************/

Void timer0_int() interrupt 1

{

TR0=0; / / set the timer initial value period, turn off the timer

TH0=(65536-10)/256;

TL0=(65536-10)%256;

TR0=1;

If(flag==1)//The motor is turning forward

{

PWM1=0;

Time++;

If(time{

PWM2=1;

}

Else

PWM2=0;

If(time)=100)

{

Time=0;

}

}

Else //motor reversal

{

PWM2=0;

Time++;

If(time{

PWM1=1;

}

Else

PWM1=0;

If(time)=100)

{

Time=0;

}

}

}

5, using the single-chip output PWM to simply control the car straight

I believe that through the above explanation, we have been able to support how to use the 51 single-chip microcomputer to generate PWM waves. A program is given below, and the PWM wave is output through two I/O ports of the single-chip microcomputer, so that the car can go straight.

#include

#define uint unsigned int

#define uchar unsigned char

Sbit PWM1=P2^0;//Connect IN1 control forward

Sbit PWM2=P2^1;//Connect IN2 control inversion

Sbit PWM3=P2^2;//Connect IN3 to control forward

Sbit PWM4=P2^3;//Connect IN4 control inversion

Sbit PWM5=P2^4;//Connect IN3 to control forward

Sbit PWM6=P2^5;//Connect IN4 control inversion

Sbit PWM7=P2^6;//Connect IN3 to control forward

Sbit PWM8=P2^7;//Connect IN4 control inversion

Uchar time;

Void main()

{

TMOD=0x01; / / timer 0 working mode 1

TH0=0xff;//(65536-10)/256;// initial value timing

TL0=0xf7;//(65536-10)%256;//0.01ms

EA=1; / / open total interruption

ET0=1; / / open timer 0 interrupt

TR0=1; / / start timer 0

While(1)

{

}

}

Void delay(uint z)

{

Uint x,y;

For(x=z;x"0;x--)

For(y=500;y"0;y--);

}

Void tim0() interrupt 1

{

TR0=0; / / when the initial value is given, the timer is turned off

TH0=0xff;//(65536-10)/256;// initial value timing

TL0=0xf7;//(65536-10)%256;//0.01ms

TR0=1; / / open the timer

Time++;

If(time)=100) time=0;//1khz

PWM2=0;

PWM4=0;

If(time<=75) PWM1=1;

Else PWM1=0;

If(time"=80) PWM3=1;

Else PWM3=0;

PWM6=0;

PWM8=0;

If(time<=50) PWM5=1;

Else PWM5=0;

If(time"=50) PWM7=1;

Else PWM7=0;

}

Galvanized Aircraft Cable

Galvanized aircraft cable

Main construction: 7x7 1/16" to 1/4"

7x19 3/32" to 3/8"

Surface: galvanized, PVC-coated, stainless, balck treated

Packing: 10000ft,5000ft,2500ft per reel or as per customer's request

galv. wire rope, GAC, MIL-83420,Steel Wire Rope,Galvanized Wire Rope,Galvanized Steel Wire Rope,Galvanized Aircraft Cable

ROYAL RANGE INTERNATIONAL TRADING CO., LTD , https://www.royalrangelgs.com