Key Control Motor display speed, key motor speed
Program flowchart
Code
# Include <reg52.h>
# Define uchar unsigned char
Sbit dula = P2 ^ 6;
Sbit wela = P2 ^ 7;
Sbit jia_key = P3 ^ 6;
Sbit jian_key = P3 ^ 7;
Sbit zf_key = P3 ^ 5;
Bit flag = 0;
Uchar num = 0, show_num = 2, maichong = 4, table_begin = 0;
Uchar code table1 [] = {0x01,0x02,0x04,0x08,0x08,0x04,0x02,0x01 };
Uchar code table [] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79,0x71 };
Void delay (uchar I)
{
Uchar j, k;
For (j = I; j> 0; j --)
For (k = 125; k> 0; k --);
}
Void display ()
{
Dula = 0;
P0 = table [show_num];
Dula = 1;
Dula = 0;
Wela = 0;
P0 = 0xfe;
Wela = 1;
Wela = 0;
Delay (5 );
P0 = table [0];
Dula = 1;
Dula = 0;
P0 = 0xfd;
Wela = 1;
Wela = 0;
Delay (5 );
}
Void key ()
{
If (jia_key = 0)
{
Delay (5 );
If (jia_key = 0)
{
Num ++;
If (num = 4)
Num = 3;
While (jia_key = 0 );
}
}
If (jian_key = 0)
{
Delay (5 );
If (jian_key = 0)
{
If (num! = 0)
Num --;
Else
Num = 0;
While (jian_key = 0 );
}
}
If (zf_key = 0)
{
Delay (5 );
If (zf_key = 0)
{
Flag = ~ Flag;
While (zf_key = 0 );
}
}
}
Void dispose ()
{
Switch (num)
{
Case 0:
Show_num = 2;
Maichong = 5;
Break;
Case 1:
Show_num = 4;
Maichong = 4;
Break;
Case 2:
Show_num = 6;
Maichong = 6;
Break;
Case 3:
Show_num = 8;
Maichong = 2;
Break;
}
If (flag = 0)
{
Table_begin = 0;
}
Else
Table_begin = 4;
}
Void qudong ()
{
Uchar I, j;
For (j = 0 + table_begin; j <4 + table_begin; j ++)
{
P1 = table1 [j];
For (I = 0; I <maichong; I ++)
{
Display ();
}
}
}
Void main ()
{
While (1)
{
Key ();
Dispose ();
Qudong ();
}
}
How can I control the speed of a motor with one key?
Example of stepper motor driver
Requirements: As shown in control circuit 3, after the motor is started, the motor does not turn, press the START key, the motor rotates, the speed is 25 rpm, press the plus 1 key, the speed increases, press the minus 1 key, the speed is reduced. The maximum speed is 100 rpm, and the lowest speed is 25 rpm. Press the stop key to stop the motor. The speed value must be displayed on the digital tube.
1. Requirement Analysis
According to the above analysis, to change the speed, as long as you change P1.0 ~ P1.3 the time needed to take turns to lower the level can meet the requirements, this time should not be implemented by delay, because it will affect the implementation of other functions. This is implemented on a regular basis. Next, calculate the scheduled time.
According to the requirements, the minimum speed is 25 rpm, and the step angle of the above stepping motor is 7.5, that is, every 48 pulses is 1 week, that is, at the lowest speed, it must be 1200 pulses/minute, equivalent to 50 ms/pulse. The maximum speed is 100 rpm, that is, 48000 pulses/min, equivalent to 12.5 ms/pulse. The following table can be listed.
Table 1 Relationship between stepper motor speed and timer constant
Speed single step time (us) TH1 TL1 actual timing (us)
25 50000 76 0 49996.8
26 48077 82 236 48074.18
27 46296 89 86 46292.61
28 44643 95 73 44640.155
?? ... ... ... ...
100 12500 211 0 12499.2
The table not only calculates the TH1 and TL1, but also calculates the real timing time under this timing constant, we can estimate the error between the real speed and the theoretical speed based on the calculated value.
In the table, TH1 and TL1 are the initial values calculated based on the scheduled time. The crystal oscillator used here is 11.0592 M. With the above table, it is not difficult to implement the program. The timer/Counter T1 is used as the timer, and the timer time is reached and then the output foot can be switched.
2. Program Implementation
Define S1 as the boot key for the DSB-1A lab board, S2 as the stop key, S3 as the plus 1 key, S4 as the minus 1 key, the procedure is as follows:
StartEnd bit 01 H; start and stop sign
MinSpd EQU 25; starting rotation speed
MaxSpd EQU 100; Maximum Rotation Speed
Speed DATA 23 H; flow Speed count
DjCount DATA 24 H; controls a value output by the motor, initially 11110 111
Hidden EQU 10 H; blanking code
Counter DATA 57 H; display Counter
Dispbuf data 58 H; Display Buffer
ORG 0000 H
AJMP MAIN
ORG 000BH
JMP DISP
ORG 001BH
JMP DJZD
ORG 30 H
MAIN:
Mov sp, # 5FH
MOV P1, # 0FFH
Mov a, # Hidden
MOV DispBuf,
MOV DispBuf + 1,
MOV DispBuf + 2,
MOV DjCount, # 11110111B
Mov speed, # MinSpd; the starting rotation SPEED is sent to the counter
CLR StartEnd; stopped
Mov tmod, # 00010001B;
MOV TH0, # HIGH (65536-3000)
MOV TL0, # LOW (65536-3000)
MOV TH1, # 0FFH;
MOV TL1, # 0FFH
SETB TR0
SETB EA
SETB ET0
SETB ET1
LOOP: acall key; keyboard Program
JNB F0, m_NEXT1; no key to continue
Acall keyproc; otherwise, the keyboard handler is called.
M_NEXT1:
Mov a, Speed
Mov B ...... remaining full text>
Input the stepper motor speed value from the keyboard and display it to the digital tube assembly language
Course Design?
Compilation is not so learned. I tried to write it for five minutes. I 'd better rely on myself. Otherwise I didn't learn anything.