51 MCU - potentiometer control servo

Publisher:WhisperingRainLatest update time:2021-08-30 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Question requirements


In order to make the ADC knowledge cooperate with other modules to realize different functions, here we plan to control the position of the servo propeller by twisting the potentiometer (sliding resistor). If we twist the potentiometer clockwise, the servo propeller will also rotate from one end to the other.


2.main.c test code


#include

#include //See Chapter 6, Lecture 8 for details

#include //See Chapter 8, Lecture 11 for details

#include //See Chapter 11, Lecture 3 for details

#include //See Chapter 14 for details

  

sbit PWMOUT=P1^7; //Servo signal line interface

u8 flag20ms = 0;

u8 PWM_VAL = 0;

 

void main()

{

    u8 val;

    u8 str[10];

    EA = 1;     

    InitLcd1602(); //Initialize LCD screen

    TIM1_Init(20000,6); //Timing 20ms, 6 is fine-tuning to make the timing more accurate

    TIM0_Init(100,9); //Timing 0.1ms, dedicated to servos, 9 is fine-tuning to make timing more accurate

   

    LcdShowStr(6, 0, "AIN0");

    while (1)

    {

        if (flag20ms) //Get the conversion value of ADC channel 0 every 20ms

        {

            flag20ms = 0;

            val = GetADCValue(0); //Get the conversion value of ADC channel 0   

            ValueToString(str, val); //Convert voltage value to string format

            LcdShowStr(6, 1, str); //Display the voltage of channel 0    

            PWM_VAL=val/14+5; //The value range of PWM_VAL is 5~23

        }

    }

}

   

void TIM0_IRQHandler() interrupt 1

    static u8 pwm=0;

    TH0 = T0RH; //Reload the reload value

    TL0 = T0RL;

   

    pwm++;

    if(pwm>=200)pwm=0; //pwm changes between 0~199 with an interval of 0.1ms and a period of 20ms

   

    if(pwm    else PWMOUT=0;

}

  

void TIM1_IRQHandler() interrupt 3

{  

    TH1 = T1RH; //Reload the reload value

    TL1 = T1RL;

   

    flag20ms = 1;

}


Let's plug back the jumper caps that we unplugged in the previous chapter to test AD and DA. There is a potentiometer in the middle of the bottom of the development board. You can use a flat-blade screwdriver to rotate the potentiometer and observe that the servo propeller rotates. In addition, it is best to use an external power supply for the servo. Using the development board to power the servo will cause the LCD screen to lose voltage and flicker, and the stability of the servo is not very good. When I tested it, I used another 5V power supply voltage to power the servo. The connection diagram is as follows

15.10.png

Reference address:51 MCU - potentiometer control servo

Previous article:51 MCU-AD and DA synthesis file
Next article:51 single chip microcomputer - remote control servo

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号