This series of tutorials takes AVR microcontroller as the object and introduces the rapid development method of microcontroller.
Reference textbook: "Microcontroller Technology and Application Project Tutorial" Luan Qiuping Electronic Industry Press 2019.6 1st edition
This article introduces the method of controlling the on and off of a specified LED.
1. Understanding bit operations in C language
//Add include files
#include "../include.h"
//Define system constants
//Define global variables
//Main program
int main(void)
{
//Define local variables
// Target board initialization, this function will automatically initialize the corresponding peripheral files
TARGET_Init();
// Initialize global variables
//When power is turned on, the corresponding operations are performed
DDRC = 0xFF;
//Background main loop
while(1)
{
//Complete your own project logic here
PORTC = 0xFB;
TARGET_Delayms(1000, 1);
PORTC = 0xFF;
TARGET_Delayms(1000, 1);
// Feed the dog statement, most projects should not remove it
#if INTERNAL_PERIPHERAL_WDT_MODE != 0
TARGET_WatchDogReset();
#endif
}
return 0; //never execute
}
2. Manipulate the specified bit in the register
//Add include files
#include "../include.h"
//Define system constants
//Define global variables
//Main program
int main(void)
{
//Define local variables
// Target board initialization, this function will automatically initialize the corresponding peripheral files
TARGET_Init();
// Initialize global variables
//When power is turned on, the corresponding operations are performed
DDRC |= (1 << 2);
//Background main loop
while(1)
{
//Complete your own project logic here
PORTC &= ~(1 << 2);
TARGET_Delayms(1000, 1);
PORTC |= (1 << 2);
TARGET_Delayms(1000, 1);
// Feed the dog statement, most projects should not remove it
#if INTERNAL_PERIPHERAL_WDT_MODE != 0
TARGET_WatchDogReset();
#endif
}
return 0; //never execute
}
3. Set high, set low and invert function macros
//Add include files
#include "../include.h"
//Define system constants
//Define global variables
//Main program
int main(void)
{
//Define local variables
// Target board initialization, this function will automatically initialize the corresponding peripheral files
TARGET_Init();
// Initialize global variables
//When power is turned on, the corresponding operations are performed
SETBIT(DDRC, BIT2);
//Background main loop
while(1)
{
//Complete your own project logic here
CLRBIT(PORTC, BIT2);
TARGET_Delayms(1000, 1);
SETBIT(PORTC, BIT2);
TARGET_Delayms(1000, 1);
// Feed the dog statement, most projects should not remove it
#if INTERNAL_PERIPHERAL_WDT_MODE != 0
TARGET_WatchDogReset();
#endif
}
return 0; //never execute
}
//Function macro, the following content is not related to transplantation
#define SETBIT(x, y) (x |= y)
#define CLRBIT(x, y) (x &= ~y)
#define CPLBIT(x, y) (x ^= y)
#define CHKBIT(x, y) (x & y)
#define BIT(x) (1< #define BIT1 BIT(1) #define BIT2 BIT(2) #define BIT3 BIT(3) #define BIT4 BIT(4) #define BIT5 BIT(5) #define BIT6 BIT(6) #define BIT7 BIT(7) #define BIT8 BIT(8) #define BIT9 BIT(9) #define BITA BIT(10) #define BITB BIT(11) #define BITC BIT(12) #define BITD BIT(13) #define BITE BIT(14) #define BITF BIT(15)
Previous article:Task 5: Apply hierarchical software design ideas to achieve multi-channel LED flashing
Next article:Task 3: Directly apply registers to light up the LED
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Today's live: TI uses DLP pico projection technology to design augmented reality smart glasses
- Laser driver circuit
- Looking for Windows XP SP3 ghost system!
- After the epidemic, will companies continue to work overtime like crazy?
- [RVB2601 Creative Application Development] CDK Development Environment Simple Application Experience Sharing
- What are radio waves?
- 【CH579M-R1】+Drive LSM303DLH Magnetic Field Sensor
- BKP backup register
- [A-Current Signal Detection Device] Shaanxi Province First Prize_Topic A
- There is a bug in the 28335IO port. Has anyone encountered the following situation?