当前位置:网站首页>51 single chip microcomputer learning notes (2)
51 single chip microcomputer learning notes (2)
2022-07-25 14:37:00 【The world is darker than pupils】
LED Light flashing
Premise : Learn how to create and complete projects LED Lighting experiment , If you haven't learned , Please click the following to jump :51 MCU learning notes (1)
Thinking before experiment : Last study note , We have learned how to light a light-emitting diode (LED), So how can we make it flash this time ? We know , Light emitting diodes have single conductivity , And only when a current circuit is formed can it glow , And in SCM , One end of the LED has been connected to the positive pole , We just need to be on the other side (IO mouth ) Connect it to low level (0) It can be lit , Then if we let the other end change high and low levels with a certain period of time , Then we can realize the flashing of light-emitting diodes ! Let's start the experiment !
- New project , Name it template, The new document is named main.c, Save in the project folder . take c Add files to the project . Start editing the code .
#include <reg51.h> // introduce 51 SCM library file
typedef unsigned int u16; // take unsigned int In the code, use u16 Instead of , namely u16 i; Is to define an unsigned integer variable i
sbit led = P2^0; // Define SCM pins (IO)P2^0 by led, This pin is connected with a light-emitting diode , And one end of it has been connected to the positive pole
void delay(u16 i) // Define the delay function
{
while(i--); // Use while Function to realize self subtraction , When i=0 when , Out of the loop , Return the main function
}
void main() // Define the main function , The program enters from here
{
while(1) // Definition while loop , Condition is 1( really ), namely while It's been circulating
{
led = 0; // Set up P2^0 The pin is low level , Forming loop ,LED Lighten up
delay(50000); // Call delay function , About time delay 450ms
led = 1; // Set up P2^0 The pin is high level , At this time, both ends of the LED are high ,LED Extinguish
delay(50000); // Call delay function , About time delay 450ms
}
}- After the code is written , Compile it , Open the burning software to compile the generated hex The file is written into the single chip microcomputer for testing and phenomenon observation .
LED Running water lamp
Thinking before practice : How to achieve LED Running water lamp ? First of all, let's understand from the concept of water lamp , Water light refers to the bulbs that are currently placed one after another in sequence , And then next , The bulbs light up in turn , That is, the special flashing mode of the former off and the latter on , In the last experiment, we learned about light-emitting diodes (LED) The twinkle of , Now we want to realize the flashing of a row of diodes . First we need to know , In the circuit of MCU , That row of light-emitting diodes is common , That is, one end of them is connected to high level , Then we just need to turn on each LED in turn (IO Low level ) After extinction (IO High level ), Water lamp can be realized ! Then let's start the experiment ! 
- New project , Name it template, The new document is named main.c, Save in the project folder . take c Add files to the project . Start editing the code .
#include <reg51.h> // introduce 51 SCM library file
#include <intrins.h> // Introduce the library file with left-right shift function
typedef unsigned int u16; // take unsigned int In the code, use u16 Instead of , namely u16 i; Is to define an unsigned integer variable i
typedef unsigned char u8; // take unsigned char In the code, use u8 Instead of , namely u8 i; Is to define an unsigned variable i
#define led P2 // Use a macro to define , Defining variables led For single chip microcomputer P2.0-2.7 Pin
void delay(u16 i) // Define the delay letter
{
while(i--); // Use while Function to realize self subtraction , When i=0 when , Out of the loop , Return the main function
}
void main() // Define the main function , The program enters from here
{
u8 i; // Define unsigned character variables i
led = 0xfe; // The variable led The magnitude is 0xfe, Convert to binary, i.e 1111 1110, That is, only P2.0 Low level
delay(50000); // Call delay function , About time delay 450ms
while(1) // Definition while loop , Condition is 1( really ), namely while It's been circulating
{
for(i=0;i<7;i++) // Definition for loop , loop 7 Time
{
led = _crol_(led,1); // Call the shift left function , One bit left at a time , namely 1111 1110 ——> 1111 1101,0 Move one bit to the left in turn
delay(50000); // Call delay function , About time delay 450ms
} // After the left shift ,led = 0111 1111 0x7f
for(i=0;i<7;i++) // Definition for loop , loop 7 Time
{
led = _cror_(led,1); // Call the shift right function , One bit left at a time , namely 0111 1111 ——> 1011 1111,0 Move one bit to the right
delay(50000); // Call delay function , About time delay 450ms
}
}
}- After the code is written , Compile it , Open the burning software to compile the generated hex The file is written into the single chip microcomputer for testing and phenomenon observation .
thus , You've learned to LED Flashing and LED Running water lamp , May you sing all the way !
边栏推荐
- Resource not found: rgbd_launch 解决方案
- 基于PaddleOCR开发uni-app离线身份证识别插件
- 河源市区推出消防安全主题奶茶 助推夏季火灾防控
- Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]
- C language and SQL Server database technology
- That day, I installed a database for my sister... Just help her sort out another shortcut
- 关于左值和右值的一些问题总结[通俗易懂]
- RuntimeError: CUDA out of memory(已解决)[通俗易懂]
- 冈萨雷斯 数字图像处理 第一章绪论
- The concept and operation rules of calculus of variations
猜你喜欢
![Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]](/img/b6/62a346174bfa63fe352f9ef7596bfc.png)
Application practice: Great integrator of the paddy classification model [paddlehub, finetune, prompt]

Alibaba cloud installs mysql5.7

Realize a family security and environmental monitoring system (I)

Melodic + Realsense D435i 配置及错误问题解决

阿里云安装MYSQL5.7

微信公众号正式环境上线部署,第三方公众平台接入

【MySQL系列】-索引知多少

Idea error failed to determine a suitable driver class

006操作符简介

Maya modeling exercise
随机推荐
Famous handwritten note taking software recruit CTO · coordinate Shenzhen
51单片机学习笔记(2)
Dpkg package download addresses of various platforms (including arm64)
GameFramework制作游戏(一)
D2. Chopping Carrots (Hard Version) (每日一题)
sqli-labs Basic Challenges Less1-10
gson与fastjson
Gameframework making games (II) making UI interface
应用实践:Paddle分类模型大集成者[PaddleHub、Finetune、prompt]
Pytorch training code writing skills, dataloader, Einstein logo
From fish eye to look around to multi task King bombing -- a review of Valeo's classic articles on visual depth estimation (from fisheyedistancenet to omnidet) (Part I)
OverTheWire-Bandit
The concept and operation rules of calculus of variations
How to design a high concurrency system?
Niuke multi school E G J L
Thymeleaf controls whether display is displayed through style
Typora cannot open the prompt to install a new version solution
安防市场进入万亿时代,安防B2B网上商城平台精准对接深化企业发展路径
~5 new solution of CCF 2021-12-2 sequence query
Realsense-Ros安装配置介绍与问题解决