当前位置:网站首页>Introduction to using 51 single chip microcomputer to control steering gear
Introduction to using 51 single chip microcomputer to control steering gear
2022-06-30 15:07:00 【A programmer who likes noodles】
The steering gear (sg90, The board is common 51)
My little white , Yesterday I used 51 Several small functions of the steering gear are realized , Help a friend who is just learning like me ~(。・`ω´・)
If there is any mistake, please give us some advice
connection
Red power supply
Brown ground
Orange data cable
Code
I hope to realize two functions
- The foundation rotates at a certain angle
- Realization PWM Controls the corner gradient
- Achieve key control angle and
speed( There is a problem with the delay function , So the control effect is not good ) - A little skill learned today , Every function realized , All should be tested in time
Program 1
// _nop_() Delay one machine cycle
#include <reg52.h>
#include <intrins.h>
sbit PWM = P3^7; // Set up PWM Output I/O port
unsigned char count = 0;
unsigned char timer1 ;
/* about 180° The steering gear t = 0.5ms——————- The rudder turns 0 ° t = 1.0ms——————- The rudder turns 45° t = 1.5ms——————- The rudder turns 90° t = 2.0ms——————- The rudder turns 135° t = 2.5ms——————- The rudder turns 180 */
/* Delay program */
void delay1s(void) // error 0us
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
/* Timer T0 initialization */
void Timer0_Init()
{
TMOD &= 0x00;
TMOD |= 0x01; // Timer T0 Set to mode 1
TH0 = 0xff; // Timing constant 0.1ms Crystal vibration is 11.0592MHz
TL0 = 0xa4;
ET0 = 1;
TR0 = 1;
EA=1;
}
/*T0 Interrupt initialization */
void Time0_Init() interrupt 1
{
TR0 = 0;
TH0 = 0xff; // 0.1ms
TL0 = 0xa4;
if(count <= timer1) //5==0° 15==90°
{
PWM = 1;
}
else
{
PWM = 0;
}
count++;
if (count >= 200) //T = 20ms Zero clearing
{
count = 0;
}
TR0 = 1; // Turn on T0
}
void main()
{
Timer0_Init();
while(1)
{
timer1 =5;// The steering gear is restored to 0° The location of
count=0;// Let the timer count again
delay1s();
timer1 =15;// The steering gear rotates 90°
count=0;
delay1s();
}
}
Program 2
Program 2 And procedure 1 Inside count and timer1 The functions of the two values are exactly the opposite , This needs attention ...
#include <reg52.h>
#include <intrins.h>
sbit PWM = P3^7; // Set up PWM Output I/O port
typedef unsigned int u16;
typedef unsigned char u8;
u8 count = 0;
u8 value,timer1;// Used to adjust the duty cycle
bit DIR;
/* about 180° The steering gear t = 0.5ms——————- The rudder turns 0 ° t = 1.0ms——————- The rudder turns 45° t = 1.5ms——————- The rudder turns 90° t = 2.0ms——————- The rudder turns 135° t = 2.5ms——————- The rudder turns 180 */
/* Delay program */
void delay1s(void) // error 0us
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
void delay100ms(void) // error 0us
{
unsigned char a,b,c;
for(c=19;c>0;c--)
for(b=20;b>0;b--)
for(a=130;a>0;a--);
}
/* Timer T0 initialization */
void Timer0_Init()
{
TMOD &= 0x00;
TMOD |= 0x01; // Timer T0 Set to mode 1
TH0 = 0xff; // Timing constant 0.1ms Crystal vibration is 11.0592MHz
TL0 = 0xa4;
ET0 = 1;
TR0 = 1;
EA=1;
}
/*T0 Interrupt initialization */
void Time0_Init() interrupt 1
{
TR0=0;
TH0 = 0xff; // 0.1ms
TL0 = 0xa4;
count++;
if(count>100)
{
count=0;
if(DIR==1)
{
value++;
delay100ms();
}
if(DIR==0)
{
value--;
delay100ms();
}
}
if(value==25)
{
DIR=0;
}
if(value==5)
{
DIR=1;
}
if(timer1>200)
{
timer1=0;
}
if(timer1<value)
{
PWM=1;
}
else
{
PWM=0;
}
timer1++;
TR0=1;
}
void main()
{
Timer0_Init();
while(1)
{
delay1s();
}
}
Program 3
Use the key to control the angle , This is easy to implement , To control the angle, you only need to modify the program 1 in timer1 Value
// _nop_() Delay one machine cycle
#include <reg52.h>
#include <intrins.h>
sbit PWM = P3^7; // Set up PWM Output I/O port
typedef unsigned int u16;
typedef unsigned char u8;
u8 count = 0;
u8 timer1;
u8 timer;
u16 n;
sbit k1=P3^1;// The angle increases
sbit k2=P3^0;// The angle decreases
/* about 180° The steering gear t = 0.5ms——————- The rudder turns 0 ° t = 1.0ms——————- The rudder turns 45° t = 1.5ms——————- The rudder turns 90° t = 2.0ms——————- The rudder turns 135° t = 2.5ms——————- The rudder turns 180 */
/* Delay program */
void delay(u16 n)// One n Approximate delay 1ms
{
u16 x,y;
for(x=0;x<114;x++)
for(y=0;y<n;y++);
}
void delay1s(void) // error 0us
{
unsigned char a,b,c;
for(c=167;c>0;c--)
for(b=171;b>0;b--)
for(a=16;a>0;a--);
_nop_(); //if Keil,require use intrins.h
}
/* Key scanner */
void keyscan()
{
if(k1==0)
{
if(k1==0)
delay(100);
{
timer+=5;
}
while(!k1);
}
if(k2==0)
{
if(k2==0)
delay(100);
{
timer-=5;
}
while(!k2);
}
}
/* Timer T0 initialization */
void Timer0_Init()
{
TMOD &= 0x00;
TMOD |= 0x01; // Timer T0 Set to mode 1
TH0 = 0xff; // Timing constant 0.1ms Crystal vibration is 11.0592MHz
TL0 = 0xa4;
ET0 = 1;
TR0 = 1;
EA=1;
}
/*T0 Interrupt initialization */
void Time0_Init() interrupt 1
{
TR0 = 0;
TH0 = 0xff; // 0.1ms
TL0 = 0xa4;
if(count <= timer1) //5==0° 15==90°
{
PWM = 1;
}
else
{
PWM = 0;
}
count++;
if (count >= 200) //T = 20ms Zero clearing
{
count = 0;
}
TR0 = 1; // Turn on T0
}
void main()
{
Timer0_Init();
timer1=5;
n=500;
while(1)
{
keyscan();
if(timer>25)
{
timer=5;
}
timer1=timer;
count=0;
delay1s();
timer1=5;
count=0;
delay1s();
}
}
边栏推荐
- Add attributes to multimode
- Sum of CCF digits (full mark code + problem solving idea) 201512-1
- [extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
- Color classification of sorting
- Win10 backup backup shows that creating a shared protection point on the shadow failed
- 1150 traveling salesman problem (25 points)
- Binary rotation array (1)
- 1130: find the first character that appears only once
- 1151 LCA in a binary tree (30 points)
- Matlab two-dimensional array example (extract data)
猜你喜欢

Matlab construction operation example

FoxPro and I

Repair of incorrect deletion of win10 boot entry

CCF numerical sorting (Full Score code + problem solving ideas + skill summary) 201503-2

How to get palindrome number in MATLAB (using fliplr function)
![[matlab] 2D drawing summary](/img/de/6bb5385f440a2997dbf9cbb9a756eb.jpg)
[matlab] 2D drawing summary

val_ Loss decreases first and then increases or does not decrease but only increases

J - Borg maze (minimum spanning tree +bfs)

Scattered knowledge of C language (unfinished)

Shangpinhui knowledge points of large e-commerce projects
随机推荐
Industry analysis | the future of real-time audio and video
Basic learning notes of C language
1132: stone scissors cloth
Sorting by character frequency
Bucket sorting (C language)
1019 general palindromic number (20 points)
[extensive reading of papers] analyzing connections between user attributes, images, and text
Matlab function for limit, definite integral, first-order derivative, second-order derivative (classic examples)
Shangpinhui knowledge points of large e-commerce projects
Knowledge learned from the water resources institute project
这种零件该怎么编程加工?
[extensive reading of papers] multi modal sarcasm detection and human classification in code mixed conversations
Matlab finds prime numbers within 100
1151 LCA in a binary tree (30 points)
Minimum covering substring of two pointers
CCF adjacent number pairs (Full Score code + problem solving ideas + skill summary) 201409-1
Basic requirements for tool use in NC machining of vertical machining center
Binary rotation array (2)
C. Registration system(map)
001 basic knowledge (unfinished)