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(); } }
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
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- DSP2812 software phase lock problem
- Wireless charging
- Apple allows employees to discuss salaries publicly, sparking heated debate. Do you know how much your colleagues earn?
- Current control circuit
- Review summary: TI LAUNCHXL-CC1352P-4
- DE1-SOC development board usage learning
- The use of stack in DSP
- 3D printed python pen holder
- TI Smart POS System Block Diagram Analysis and Its Seven Key Functional Module Solutions
- Cubietruck dual-core A20 development board