ATMEGA16 written by AVR stdio controls the forward and reverse rotation and speed of the stepper motor

Publisher:innovator8Latest update time:2019-11-09 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

ATMEGA16 written by AVR stdio controls the forward and reverse rotation and speed of the stepper motor. It is free for everyone to refer to and criticize.

It is suitable for beginners. There are seven buttons in total. Three buttons control three speeds of forward rotation, three buttons control three speeds of reverse rotation, and one button controls stop.

This program controls the stepper motor and is connected to the stepper motor driver, so there is only one pulse. If you want to connect the stepper motor directly, you only need to make a slight change.

 

Main program preview:

#define F_CPU 800000UL

#include

#include

#define INT8U unsigned int

#define INT16U unsigned int


const INT8U FFW[]={0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00}; // output 8 pulses from port p0 in one beat


//Button definition

#define K1_DOWN()((PIND &_BV(PD0)) == 0x00) //K1: forward speed 1

#define K2_DOWN()((PIND &_BV(PD1)) == 0x00) //K2: forward speed 2

#define K3_DOWN()((PIND &_BV(PD2)) == 0x00) //K3: forward speed 3

#define K4_DOWN()((PIND &_BV(PD3)) == 0x00) //K4: stop

#define K5_DOWN()((PIND &_BV(PD4)) == 0x00) //K5: reverse speed 1

#define K6_DOWN()((PIND &_BV(PD5)) == 0x00) //K6: reverse speed 2

#define K7_DOWN()((PIND &_BV(PD6)) == 0x00) //K7: reverse speed 3


void STEP_MOTOR_FWRUN_1(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)//loop output 16*8 beats

     { 

        //if (K1_DOWN())break;

                 if (K2_DOWN())break;

                 if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating

          

             if (K5_DOWN())break;

                 if (K6_DOWN())break;

                 if (K7_DOWN())break;

          PORTB=FFW[j];

          _delay_us(50);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}


void STEP_MOTOR_FWRUN_2(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)

     { 

       if (K1_DOWN())break;

                // if (K2_DOWN())break;

                 if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating


             if (K5_DOWN())break;

                 if (K6_DOWN())break;

                 if (K7_DOWN())break;

          PORTB=FFW[j];

          _delay_us(150);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}


void STEP_MOTOR_FWRUN_3(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)

     { 

       if (K1_DOWN())break;

                 if (K2_DOWN())break;

                // if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating

           

             if (K5_DOWN())break;

                 if (K6_DOWN())break;

                 if (K7_DOWN())break;

          PORTB=FFW[j];

          _delay_us(300);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}



void STEP_MOTOR_CWRUN_5(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)

     { 

      if (K1_DOWN())break;

                 if (K2_DOWN())break;

                 if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating

           

            // if (K5_DOWN())break;

                 if (K6_DOWN())break;

                 if (K7_DOWN())break;

           

           PORTB=FFW[j];

          _delay_us(300);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}


void STEP_MOTOR_CWRUN_6(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)

     { 

        if (K1_DOWN())break;

                 if (K2_DOWN())break;

                 if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating

         

             if (K5_DOWN())break;

                 //if (K6_DOWN())break;

                 if (K7_DOWN())break;

           

           PORTB=FFW[j]; //Forward when direction is 0

          _delay_us(150);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}


void STEP_MOTOR_CWRUN_7(INT8U n)

{

   INT8U i,j;

   

   for(i=0;i   {

     for(j=0;j<16;j++)

     { if (K1_DOWN())break;

                 if (K2_DOWN())break;

                 if (K3_DOWN())break;

       if (K4_DOWN())return; //When K4 is pressed midway, the motor stops rotating

        

             if (K5_DOWN())break;

                 if (K6_DOWN())break;

                // if (K7_DOWN())break;

           PORTB=FFW[j];

          _delay_us(50);


}

PORTB=0x01; //After the last circle, the output is 0x01 and the motor returns to the starting point

}

}

void beep()

{

        PORTC=0x40;

        _delay_ms(120);

        PORTC=0X00;

}


void initonbeep()

{

PORTC=0x40;

_delay_ms(120);

PORTC=0X00;

_delay_ms(120);

PORTC=0x40;

_delay_ms(120);

PORTC=0X00;

_delay_ms(120);

PORTC=0x40;

_delay_ms(120);

PORTC=0X00;

_delay_ms(120);

PORTC=0x40;

_delay_ms(120);

PORTC=0X00;

}



int main()

{

   INT8U r=65535;

   DDRB=0xFF;PORTB=FFW[0]; //control output

   DDRD=0X00;PORTD=0xFF; //Key input

   DDRA=0X00;PORTA=0xFF; //Direction signal

   DDRC=0XFF;PORTC=0x00; //Buzzer

   initonbeep();

  

   while(1)

   {

    


     if(K1_DOWN())

     {

                  _delay_us(200);

                  if(K1_DOWN())

     {

                 beep();

         //while(K1_DOWN()); //Wait for K1 to be released

                 PORTA=0x00; //direction signal is 0

         STEP_MOTOR_FWRUN_1(r); //Click to rotate forward r circles

                 

      }

         }          

          if(K2_DOWN())

     {

                   _delay_us(200);

                  if(K2_DOWN())

     {

                 beep();

         //while(K1_DOWN()); //Wait for K1 to be released

                 PORTA=0x00; //direction signal is 0

         STEP_MOTOR_FWRUN_2(r); //Click to rotate forward r circles

      }

         }          

          if(K3_DOWN())

     {

                   _delay_us(200);

                  if(K3_DOWN())

     {

                 beep();

         //while(K1_DOWN()); //Wait for K1 to be released

                 PORTA=0x00; //direction signal is 0

         STEP_MOTOR_FWRUN_3(r); //Click to rotate forward r circles

      }

         }          

      if(K5_DOWN())

      {

                    _delay_us(200);

                  if(K5_DOWN())

     {

                 beep();

         //while(K2_DOWN()); //Wait for K2 to be released

         PORTA=0x01; //direction signal is 1

         STEP_MOTOR_CWRUN_5(r); //Motor reverses r turns

       }

          }           

         if(K6_DOWN())

      {

                    _delay_us(200);

                  if(K6_DOWN())

     {

                 beep();

         //while(K2_DOWN()); //Wait for K2 to be released

         PORTA=0x01; //direction signal is 1

         STEP_MOTOR_CWRUN_6(r); //Motor reverses r turns

       }

          }           

         if(K7_DOWN())

      {

                    _delay_us(200);

                  if(K7_DOWN())

     {

                 beep();

         //while(K2_DOWN()); //Wait for K2 to be released

         PORTA=0x01; //direction signal is 1

         STEP_MOTOR_CWRUN_7(r); //Motor reverses r turns

       }

          }        

                

     }

}



Keywords:AVR Reference address:ATMEGA16 written by AVR stdio controls the forward and reverse rotation and speed of the stepper motor

Previous article:Mega128 waveform generator simulation and source program LCD display
Next article:Infrared control motor system source code based on ATmega128 microcontroller

Recommended ReadingLatest update time:2024-11-16 08:59

Design of Intelligent Temperature Controller for Dry-Type Transformer Based on ATmega16 Single Chip Microcomputer
Introduction: This paper discusses the design of an intelligent temperature controller for dry-type transformers based on the ATmega16 microcontroller. The temperature controller has low power consumption, advanced technology, complete functions, simple operation, reliable performance, and can work stably for a long t
[Microcontroller]
Design of Intelligent Temperature Controller for Dry-Type Transformer Based on ATmega16 Single Chip Microcomputer
Homemade AVR high voltage programmer, unlocking avr microcontroller with wrong fuse bit
I'm using Atmel's AT90USB128x/64x and ATmega32U4 to design a keyboard. One of the AT90 microcontrollers was locked due to a wrong fuse setting, so the programmer couldn't flash the program to it. I found several ways to unlock it online: Use the output pin of the active crystal oscillator to the crystal oscillator i
[Microcontroller]
Homemade AVR high voltage programmer, unlocking avr microcontroller with wrong fuse bit
AVR MCU Tutorial - Toggle Switch
There are 4 toggle switches above the buttons. Switches and buttons are very similar in principle and usage, but they have different uses - buttons will pop up when they are pressed and released, while switches can save their state. switch.h defines switch-related functions. switch_status corresponds to button_down,
[Microcontroller]
AVR--IO setting programming
AVR's C language is based on ANSI C. It does not extend bit operations (Boolean operations) like 51, although there are SBI/CBI/SBIC/SBIS instructions in the assembly instructions. Therefore, bit logic operations are required to implement them, which must be mastered. The operation methods of IO ports and function reg
[Microcontroller]
AVR--IO setting programming
AVR128 MCU minimum system serial port, external interrupt, marquee, uCOS-II transplantation
Experiment name: Serial communication experiment Function description: The computer sends data to the development board through the serial port, and the development board sends the data back as is. Experiment purpose: Learn serial communication Experiment description: MCU--M128           internal 8M crystal oscillator
[Microcontroller]
Microchip Releases Secure Application Designs for dsPIC®, PIC18® and AVR® MCUs
Microchip Releases New ISO 26262 Functional Safety Package for dsPIC®, PIC18® and AVR® MCUs to Simplify Design of ASIL B and ASIL C Safety Applications Microchip’s certified functional safety solutions accelerate development and certification of automotive safety applications To
[Automotive Electronics]
Microchip Releases Secure Application Designs for dsPIC®, PIC18® and AVR® MCUs
Design of pure hardware circuit of intelligent car control system based on ATmega16
Introduction: The article uses ATmega16 microcontroller as the control core, RF2401 wireless transceiver module for remote control communication, DS18B20 for temperature detection, and ultrasonic and other sensor materials to detect signals and obstacles. Finally, it makes full use of the serial port resources and com
[Microcontroller]
Design of pure hardware circuit of intelligent car control system based on ATmega16
AVR Watchdog Detailed Explanation
Introduction: AVR watchdog is a hardware unit. When the program runs away for some reason, it resets the program, just like a puppy guarding the door. The AVR watchdog is a hardware unit that resets the program when it runs away for some reason, just like a puppy guarding the door. //Watchdog initialize // presc
[Microcontroller]
AVR Watchdog Detailed Explanation
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号