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

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号