* ======= PWM Speed Control Program for DC Motors ======== */
/* The crystal oscillator uses 11.0592MHz, and the generated PWM frequency is approximately 91Hz */
#include <reg52.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit en1 = P2^0; /* L298 Enable A */
sbit en2 = P2^1; /* L298 Enable B */
sbit s1 = P2^2; /* L298 Input 1 */
sbit s2 = P2^3; /* L298 Input 2 */
sbit s3 = P2^4; /* L298 Input 3 */
sbit s4 = P2^5; /* L298 Input 4 */
uchar t = 0; /* interrupt counter */
uchar m1 = 0; /* Motor 1 speed value */
uchar m2 = 0; /* Motor 2 speed value */
uchar tmp1, tmp2; /* current motor speed values */
/* Motor control function: index - motor number (1 or 2); speed - motor speed (-100 to 100) */
void motor(uchar index, char speed)
{
if (speed >= -100 && speed <= 100)
{
if (index == 1) /* Processing for Motor 1 */
{
m1 = abs(speed); /* Take absolute value of speed */
if (speed < 0) /* If speed is negative, reverse direction */
{
s1 = 0;
s2 = 1;
}
else /* If not negative, move forward */
{
s1 = 1;
s2 = 0;
}
}
if (index == 2) /* Processing for Motor 2 */
{
m2 = abs(speed); /* Set speed for Motor 2 */
if (speed < 0) /* Reverse direction for Motor 2 */
{
s3 = 0;
s4 = 1;
}
else /* Move forward for Motor 2 */
{
s3 = 1;
s4 = 0;
}
}
}
}
void delay(uint j) /* Simple delay function */
{
for (; j > 0; j--);
}
void main()
{
char i;
TMOD = 0x02; /* Set Timer 0 to mode 2 */
TH0 = 0x9B; /* Initial value for timer */
TL0 = 0x9B;
EA = 1; /* Enable global interrupts */
ET0 = 1; /* Enable Timer 0 interrupt */
TR0 = 1; /* Start Timer 0 */
while (1) /* Demo of motor control */
{
for (i = 0; i <= 100; i++) /* Forward acceleration */
{
motor(1, i);
motor(2, i);
delay(5000);
}
for (i = 100; i > 0; i--) /* Forward deceleration */
{
motor(1, i);
motor(2, i);
delay(5000);
}
for (i = 0; i <= 100; i++) /* Reverse acceleration */
{
motor(1, -i);
motor(2, -i);
delay(5000);
}
for (i = 100; i > 0; i--) /* Reverse deceleration */
{
motor(1, -i);
motor(2, -i);
delay(5000);
}
}
}
void timer0() interrupt 1 /* Timer 0 interrupt service routine */
{
if (t == 0) /* Accept new value after one PWM cycle */
{
tmp1 = m1;
tmp2 = m2;
}
t++;
if (t == 100) t = 0; /* One PWM signal is generated by 100 interrupts */
}
The JUK universal Screw Terminal Block series has the typical features which are decisive for practical applications:
l The universal foot allows the terminal blocks to be easily snapped onto the NS35 or NS32 DIN Rail with G shape.
l Closed screw guide holes ensure screwdriver operation perfect.
l For terminal block with different wire cross-sectional areas, complete accessories are available, such as end plates, partition plates, etc.
l Potential distribution achieved by fixed bridges in the terminal center or insertion bridges in the clamping space.
l Same shape and pitch Grounding Terminal Blocks as the JUK universal series.
l Adopt ZB marker strip system,achieve unified identification.
Cable Terminal Connectors,90 Degree Spade Terminal Connector,Insulated Spade Terminal,2 Pin Terminal Connector
Wonke Electric CO.,Ltd. , https://www.wkdq-electric.com