AVR microcontroller PWM speed regulation program and schematic diagram

Publisher:stampieLatest update time:2019-10-19 Source: eefocusKeywords:avr Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Source program:

#include

#include


#define uint unsigned int

#define uchar unsigned char

#define Left OCR1A

#define Right OCR1B

#define ADD 1.15; //macro definition, acceleration coefficient


flying i;

uchar flag; //Whether to decelerate flag

float TCRT5000_IN;


uint Max_Speed;

uchar Min_Speed;


/*************************************************

Function name: Init_TCRT5000

Function: Initialize infrared sensor TCRT5000

Function description: Set the PA port as input and monitor the sensor signal **********/

void Init_TCRT5000()

{

DDRA = 0X00;

PORT = 0XFF;

}


/*************************************************

Function name: Max()

Function: Determine the maximum speed

Function description: Determine the maximum speed and put its value into Max_Speed*****/

void Max()

{

if(Left >= Right)

{

  Max_Speed = Left * ADD;

}

else

{

  Max_Speed = Right * ADD;

}

if(Max_Speed >= 0xf7)

{

  Max_Speed = 0xf7;

}

Min_Speed = 0x20 ;

}


/*************************************************

Function name: Scan_TCRT5000

Function: TCRT5000 scans and returns information

Function description: **********/

void Scan_TCRT5000()

{

TCRT5000_IN = PINA&0x1F;

switch(TCRT5000_IN)

{

   /* OOOOO *///Total destruction

   case 0x00:

       break;

   /* ***** *///Zenryo

   case 0x1f:

       OCR1A=0x00;

       OCR1B=0x00;

           break; 

   /*  O*OOO  */

   case 0x08:

       OCR1A=Min_Speed;

           OCR1B=Max_Speed;

           break;

   /*  *OOOO  */

   case 0x10:

       OCR1A=Min_Speed;

           OCR1B=Max_Speed;

              break;

   /* OR**YES */

   case 0x0c:

       OCR1A=Min_Speed;

           OCR1B=Max_Speed;

           break;

   /*  **OOO  */

   case 0x18:

       OCR1A=Min_Speed;

           OCR1B=Max_Speed;

           break;

    /*  ***00*/

    case 0x1c:

       OCR1A=Min_Speed;

           OCR1B=Max_Speed;

           break;

   /*  OOO*O  */

   case 0x02:

           OCR1A=Max_Speed;

           OCR1B=Min_Speed;

           break;

   /*  OOOO*  */

   case 0x01:

       OCR1A=Max_Speed;

           OCR1B=Min_Speed;

           break;

   /* YES**OR */

   case 0x06:

       OCR1A=Max_Speed;

           OCR1B=Min_Speed;

           break;

   /*  OOO**  */

   case 0x03:

       OCR1A=Max_Speed;

           OCR1B=Min_Speed;

           break;

   /* AND*** */

   case 0x07:

       OCR1A=Max_Speed;

           OCR1B=Min_Speed;

           break;  

   default:

       break;

  }

}


/*************************************************

Function name: Init_Speed

Function: Timer 1 is initialized to generate two PWM signals

Function description: Control the car speed by changing the global variables Left and Right**/

void Init_Speed()

{

DDRD |= 0X30;

TCCR1A=0xA1;

TCCR1B=0x0A;

OCR1A=0xa0;

OCR1B=0xa0;

}


/**********************************************

Function name: Stop

Function: Stop the car

Function description: Right=0, Left=0************************/

void Stop()

{

OCR1A=0x00;

OCR1B=0x00;

}


/**********************************************

Function name: Init_Timer0

Function: Initialize timer 0

Function Description: Initialize timer 0

          Set frequency division to 1024

                  Trigger an interrupt once every 100ms, speed increases by 1**********/

void Init_Timer0()

{

TCCR0 = 0x05;

TCNT0 = 0x00;

TIMSK |= BIT(2);

SREG |= BIT(7);

}


void main()

{

flying speed;

TCRT5000_IN = PINA&0x1F;

Init_Speed();

Init_TCRT5000();

Init_Timer0();

while(1)

{

  TCRT5000_IN = PINA&0x1F;

  while(TCRT5000_IN != 0x04)

  {

   SREG &= ~BIT(7); //Disable interrupt

   Scan_TCRT5000();

   TCRT5000_IN = PINA&0x1F;

   flag = 1;

  }


  while(TCRT5000_IN == 0x04)

  {

   if(flag == 1)

   {

           Left = 0xb0;

    Right = 0xb0;

        flag =0;

        SREG |= BIT(7); //Enable interrupt

   } 

   TCRT5000_IN = PINA&0x1F;

  }

}

}


/*Timer 0 interrupt function*/

#pragma interrupt_handler Timer0:10

void Timer0()

{

TCNT0 = 0x00;

i++;

if(i == 100)

{

  i=0; 

  Left++;

  if(Left >= 0xfe)

  {

   Left = 0xfe;

  }

  OCR1B++;

  if(OCR1B >= 0xfe)

  {

   OCR1B = 0xfe;

  }

  Max();

}

}



Keywords:avr Reference address:AVR microcontroller PWM speed regulation program and schematic diagram

Previous article:A high performance avr microcontroller frequency meter program 10khz
Next article:AVR register configuration assistant made with excel --- easily handle AVR

Recommended ReadingLatest update time:2024-11-23 08:07

51 MCU PWM control LED on and off
Design idea: Use timer interrupt to generate the minimum time unit T0, every m T0 forms a sub- cycle T10, of which the first n T0 is the LED conduction time, that is, Ton=n*T0, then Toff=(mn)*T0 ; every X T10 forms a mother cycle T20, of which the first y T10 is the effective control time, and the subsequent (xy) T10
[Microcontroller]
AVR EEPROM simple experiment
#include #include intrinsics.h #define uchar unsigned char #define uint unsigned int __flash volatile seg ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; __flash flying tab ={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; #define EEWE 1 #define EEWE 0 #define EEWE 2 #define k1 PIND_Bit4 //Address change
[Microcontroller]
Two methods teach you how to effectively use the 51 microcontroller to generate PWM waves
The 89C51 chip does not have its own PWM generator. If you want to use 51 to generate PWM waves, you must use software programming to simulate it. Methods can be roughly divided into two methods: software delay and timer generation. They will be introduced one by one below. 1 Software delay method Use the software d
[Microcontroller]
Two methods teach you how to effectively use the 51 microcontroller to generate PWM waves
N76E003 PWM polarity control
Some of you may not understand PWM polarity control (yes, I am talking about myself...), so let's explore the principle and procedure of polarity control in this blog post. If there are any errors in the description, I hope you can point them out. First, let's take a look at the description of polarity control in th
[Microcontroller]
N76E003 PWM polarity control
AVR MCU driver CH451 example program_C language
1) Introduction to CH451 CH451 can be used in the fields of LED digital tube display drive, keyboard scan control, μP monitoring circuit, etc. The chip package includes dual in-line DIP28, SOP28 and DIP24S package. It has a built-in RC oscillator circuit, which can directly and dynamically drive 8-bit digital tubes
[Microcontroller]
AVR MCU driver CH451 example program_C language
Machine cycle and clock cycle of avr microcontroller
Machine cycle: the time to read an instruction word from the memory. Simply put, it is the time taken to execute a single-cycle instruction. The AVR microcontroller uses the Harvard structure (data and program are stored separately) and a reduced instruction set. One machine cycle is equal to one clock cycle. That is,
[Microcontroller]
AVR MCU timer, PWM, ICP examples
/*********************************************** **** AVR Timer Usage Examples *** ****                                              *** **** Compiler: WINAVR20050214 *** ***********************************************/ /* This program simply demonstrates how to use the ATMEGA16 timer AVR timer key point
[Microcontroller]
AVR chip introduction
AVR microcontroller series are complete and can be applied to various occasions. AVR microcontrollers have 3 grades: Low-end Tiny series AVR MCU: mainly Tiny11/12/13/15/26/28, etc.; Mid-range AT90S series AVR MCU: mainly AT90S1200/2313/8515/8535, etc. (are being phased out or transitioned to Mega, f
[Microcontroller]
AVR chip introduction
Latest Microcontroller Articles
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号