Task 4: Control the on and off of the specified LED

Publisher:来来去去来来Latest update time:2020-03-13 Source: eefocusKeywords:Control Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 BIT0 BIT(0)

#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)

Keywords:Control Reference address:Task 4: Control the on and off of the specified LED

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

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号