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
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- The difference between the two half-bridge drive circuits
- Allwinner heterogeneous multi-core AI intelligent vision V853 development board evaluation - v853 YOLO v5 quantization -> installation of yoloV5
- [ST NUCLEO-U575ZI-Q Review] + Multi-channel ADC data acquisition
- Today's live broadcast: TI Matter solutions help smart home innovation
- Years of points exchanged for a watch
- Raspberry Pi Official Manual 2023
- [Pingtouge Boliu BL606P audio and video development board] Unboxing and setting up the development environment HelloWorld
- TI enters the M0 MCU camp. What is it up to?
- [Boliu BL606P audio and video development board] CDK simulation experience
- It is recommended that the moderator add an EMC column or special discussion.