Microcontroller Remote Control stepper motor, LED lights and buzzer

Source: Internet
Author: User
Tags 0xc0

Microcontroller Remote Control stepper motor, LED lights and buzzer

The stepping motor module, LED lamp and buzzer module of the single-chip microcomputer are controlled by the C # language to enable the stepping motor to perform forward, reverse, and stop and control the speed; the LED lamp module performs selective breathing expression; the start and end of the buzzer module.

 

The host computer controls the stepper motor, LED lamp, and buzzer modules of the single chip microcomputer through the serial port and custom communication protocol (8 bytes. In the process of controlling the stepping motor, INT0 interrupt is used for timely speed response in order to make the operation respond in a timely manner. The LED lamp uses bit operation to control the flashing position of the lamp, the single-chip microcomputer modules and operations are reasonably utilized.

Note: Due to the limitation of the number of timers, more modules cannot be controlled.

#include

sbit WEI = P2 ^ 7;
sbit DUAN = P2 ^ 6;
#define DataPort P0

unsigned char code dofly_DuanMa [] = {0x3f, 0x06,0x5b, 0x4f, 0x66,0x6d, 0x7d, 0x07,0x7f, 0x6f, 0x77,0x7c, 0x39,0x5e, 0x79,0x71,0x40}; // Show segment value ~ 9
unsigned char code dofly_WeiMa [] = {0xfe, 0xfd, 0xfb, 0xf7,0xef, 0xdf, 0xbf, 0x7f}; // corresponding to the corresponding digital tube light, that is, the bit code
unsigned char TempData [8]; // Global variable that stores the displayed value
sbit SPK1 = P1 ^ 4;

void delay (int t)
{
   while (t--);
}


sbit A1 = P1 ^ 0; // Define the stepper motor connection port
sbit B1 = P1 ^ 1;
sbit C1 = P1 ^ 2;
sbit D1 = P1 ^ 3;

#define Coil_AB1 {A1 = 1; B1 = 1; C1 = 0; D1 = 0;} // AB phase is powered on, other phases are powered off
#define Coil_BC1 {A1 = 0; B1 = 1; C1 = 1; D1 = 0;} // The BC phase is powered on and the other phases are powered off
#define Coil_CD1 {A1 = 0; B1 = 0; C1 = 1; D1 = 1;} // CD phase is powered on, other phases are powered off
#define Coil_DA1 {A1 = 1; B1 = 0; C1 = 0; D1 = 1;} // D phase is powered on, other phases are powered off
#define Coil_A1 {A1 = 1; B1 = 0; C1 = 0; D1 = 0;} // A phase is powered on, other phases are powered off
#define Coil_B1 {A1 = 0; B1 = 1; C1 = 0; D1 = 0;} // B phase is powered on, other phases are powered off
#define Coil_C1 {A1 = 0; B1 = 0; C1 = 1; D1 = 0;} // C phase is powered on, other phases are powered off
#define Coil_D1 {A1 = 0; B1 = 0; C1 = 0; D1 = 1;} // D phase is powered on, other phases are powered off
#define Coil_OFF {A1 = 0; B1 = 0; C1 = 0; D1 = 0;} // all power off

unsigned char Speed;
unsigned char dir = 0;
unsigned char code rst [] = {0xe4,0xc0,0xe0,0xc0,0xe0,0x32}; // reset code
void Init_Timer0 (void);
void DelayUs2x (unsigned char t)
{
 while (-t);
}

void DelayMs (unsigned char t)
{
     
 while (t--)
 {
     // Approximate delay 1mS
     DelayUs2x (245);
DelayUs2x (245);
 }
}

void Display (unsigned char FirstBit, unsigned char Num)
{
      static unsigned char i = 0;
Ranch

DataPort = 0; // Empty the data to prevent alternate ghosting
       DUAN = 1; // Segment latch
       DUAN = 0;

       DataPort = dofly_WeiMa [i + FirstBit]; // Get bitcode
       WEI = 1; // bit latch
       WEI = 0;

       DataPort = TempData [i]; // Get display data, segment code
       DUAN = 1; // Segment latch
       DUAN = 0;
       
i ++;
       if (i == Num)
i = 0;


}

void Rorate ()
{
   unsigned int i = 512; // one rotation time
   Init_Timer0 ();

   EA = 1; // disconnect in the global
   EX0 = 1; // external interrupt 0 on
   IT0 = 1; // 1 means edge trigger

    // Speed = speed;
    TempData [0] = dofly_DuanMa [Speed / 10]; // Decomposed display information, if you want to display 68
    TempData [1] = dofly_DuanMa [Speed% 10]; // 68/10 = 6 68% 10 = 8
    Coil_OFF
    while (i-&& dir == 0) // forward
    {
Coil_A1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_AB1 // Coil_AB1 encountered {A1 = 1; B1 = 1; C1 = 0; D1 = 0;} instead
      DelayMs (Speed); // Change this parameter to adjust the motor speed,
       P3 = 0xeb;
P3 = 0xff; // The smaller the number, the larger the speed, and the smaller the torque
Coil_B1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_BC1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
Coil_C1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_CD1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
Coil_D1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_DA1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
}
   Coil_OFF
     i = 512;
     
while ((i-) && dir) // Reverse
{
       Coil_A1
       DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_DA1 // Coil_AB1 encountered {A1 = 1; B1 = 1; C1 = 0; D1 = 0;} instead
      DelayMs (Speed); // Change this parameter to adjust the motor speed,
       P3 = 0xeb;
P3 = 0xff; // The smaller the number, the larger the speed, and the smaller the torque
Coil_D1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
       Coil_CD1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
Coil_C1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_BC1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
Coil_B1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
      Coil_AB1
      DelayMs (Speed);
P3 = 0xeb;
P3 = 0xff;
}
Ranch
}

/ * ------------------------------------------------
                    Serial port initialization
------------------------------------------------ * /
void InitUART (void)
{
    SCON = 0x50; // SCON: Mode 1, 8-bit UART, enable reception
    TMOD | = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
    TH1 = 0xFD; // TH1: reload value 9600 baud crystal oscillator 11.0592MHz
    TR1 = 1; // TR1: timer 1 is on
    EA = 1; // Open total interrupt
    ES = 1; // Enable serial port interrupt
}


                         
/ * ------------------------------------------------
                    Main function
------------------------------------------------ * /

unsigned int judge [8];
int cnt = 0;

void main (void)
{
 
   InitUART ();

   ES = 1; // Open serial port interrupt
  while (1)
    {
Ranch
       if (judge [0] == 0xFF && judge [1] == 0xFE && judge [6] == 0xFD && judge [7] == 0xFC)
{
// (* ((void (*) ()) (rst))) (); // Use the rst array as a function call to reset
if (judge [2] == 0x00) // specified stepper
{
P3 = 0xeb;
P3 = 0xff;
if (judge [5]! = 0)
               Rorate ();
}
else if (judge [2] == 0x01) // Specify LED
{
Ranch
unsigned int CYCLE = 600, PWM_LOW = 0; // Define the period and assign a value
Ranch
Ranch

               while (1) // main loop
               {

                 WEI = 0; // Bit latch is set to 0 level to prevent the digital tube from turning on when the LED is on
                 DUAN = 0; // Set the segment latch to 0 level to prevent the digital tube from turning on when the LED is on
                    P0 = 0xFF;
                    delay (60000); // Specially add delay, you can see the process of extinction
                    for (PWM_LOW = 1; PWM_LOW0; PWM_LOW-) {// the opposite process from gradually brightening

   P0 = judge [3];
   delay (PWM_LOW);
   P0 = 0xFF;
  delay (CYCLE-PWM_LOW);
     
                }
}
Ranch
}
else if (judge [2] == 0x02)
{
while (1)
{
Ranch
                   DelayMs (1); // Send a square wave of about 500Hz. The higher the frequency, the sharper the sound.
SPK1 =! SPK1;

}
}
       }
    }
}




/ * ------------------------------------------------
                     Serial interrupt program
------------------------------------------------ * /
void UART_SER (void) interrupt 4 // serial interrupt service routine
{
    unsigned char Temp; // Define a temporary variable
   
   if (RI) // Judgment is received interrupt
     {
RI = 0; // The flag is cleared
Temp = SBUF; // The value read into the buffer
Ranch
judge [cnt ++] = Temp;
      if (cnt == 8 || judge [0]! = 0xFF)
      cnt = 0;

      SBUF = Temp; // Send the received value back to the computer
}
   if (TI) // If it is a transmission flag, clear it
     TI = 0;
}

/ * ------------------------------------------------
                    Timer initialization routine
------------------------------------------------ * /
void Init_Timer0 (void)
{
 TMOD | = 0x01; // Using mode 1, 16-bit timer, using "|" symbol can not be affected when using multiple timer
 // TH0 = 0x00; // given initial value
 // TL0 = 0x00;
 EA = 1; // Total interrupt open
 ET0 = 1; // Timer interrupt is on
 TR0 = 1; // Timer switch is on
 PT0 = 1; // Priority is on
}
/ * ------------------------------------------------
                 Timer interrupt subroutine
------------------------------------------------ * /
void Timer0_isr (void) interrupt 1
{
 TH0 = (65536-2000) / 256; // Reassign 2ms
 TL0 = (65536-2000)% 256;
 
  Display (0,8);
 
}

// External interrupt routine
void ISR_INT0 (void) interrupt 0
{
    
         Speed = judge [4];
dir = judge [3];
   TempData [0] = dofly_DuanMa [Speed / 10]; // Decomposed display information, if you want to display 68
TempData [1] = dofly_DuanMa [Speed% 10]; // 68/10 = 6 68% 10 = 8
Ranch
}

C # side:

 

namespace microcontroller
{
    public partial class frm: Form
    {
        public frm ()
        {
            InitializeComponent ();

        }

        private SerialPort com;

        //letter of agreement
       ///

       /// FF FE 00 (motor) 00 (direction) 00 (speed) 00 (stop) FD FC
       ///
       ///
       ///

        private void btn_LED_Click (object sender, EventArgs e)
        {
            com = new SerialPort ();
            com.BaudRate = 9600;
            com.PortName = "COM4";
            com.DataBits = 8;
            com.Open ();
            Byte [] data = new Byte [8];
            data [0] = 0xFF;
            data [1] = 0xFE;
            if (rb_R.Checked == true)
            {
                data [2] = 0x00;
                if (rb_clock.Checked == true)
                {
                    data [3] = 0x00;
                }
                else if (rb_anclock.Checked == true)
                {
                    data [3] = 0x01;
                }
                if (cmb_speed.Text.ToString () == "Stop")
                {
                    data [4] = 0x00;
                    data [5] = 0x00;
                }
                else
                {
                    data [4] = Byte.Parse (cmb_speed.Text);
                    data [5] = 0x01;
                }
            }
            else if (rb_LED.Checked == true)
            {
                data [2] = 0x01;
                uint num = 255;
                if (checkBox1.Checked == true)
                {
                    num = num & 254;
                }
                if (checkBox2.Checked == true)
                {
                    num = num & 253;
                }
                if (checkBox3.Checked == true)
                {
                    num = num & 251;
                }
                if (checkBox4.Checked == true)
                {
                    num = num & 247;
                }
                if (checkBox5.Checked == true)
                {
                    num = num & 239;

                }
                if (checkBox6.Checked == true)
                {
                    num = num & 223;
                }
                if (checkBox7.Checked == true)
                {
                    num = num & 191;
                }
                if (checkBox8.Checked == true)
                {
                    num = num & 127;
                }

                byte [] c = System.BitConverter.GetBytes (num);
                data [3] = c [0];

                if (led_s.Text.ToString () == "enabled")
                    data [2] = 0x02;
                data [4] = 0x00;
                data [5] = 0x00;
            }
         
            data [6] = 0xFD;
            data [7] = 0xFC;
            com.Write (data, 0, 8);
            com.Close ();
        }
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.