当前位置:网站首页>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 !
边栏推荐
- Why do China Construction and China Railway need this certificate? What is the reason?
- Two Sum
- 冈萨雷斯 数字图像处理 第一章绪论
- Doris learning notes integration with other systems
- 用GaussDB(for Redis)存画像,推荐业务轻松降本60%
- Idea error failed to determine a suitable driver class
- 各种平台dpkg包下载地址(包括arm64)
- Introduction to PHP
- H5页面input输入框弹起数字键盘,需要支持小数点
- [Nuxt 3] (十一) 传送 & 模块
猜你喜欢

PHP website design ideas

元器件采购系统的主要功能,数字化采购助力元器件企业飞速发展

The security market has entered a trillion era, and the security B2B online mall platform has been accurately connected to deepen the enterprise development path

006操作符简介

Filters get the data in data; Filters use data in data

Vs2017 large factory ERP management system source code factory general ERP source code

GameFramework制作游戏(二)制作UI界面

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

Can the variable name be in Chinese? Directly fooled people

D2. Chopping Carrots (Hard Version) (每日一题)
随机推荐
Summary of some problems about left value and right value [easy to understand]
Dpkg package download addresses of various platforms (including arm64)
[MySQL series] - how much do you know about the index
Awk from getting started to digging in (23) awk built-in variables argc, argc -- command line parameter transfer
Niuke multi school E G J L
kibana操作es
C language and SQL Server database technology
Throwing OutOfMemoryError “Could not allocate JNI Env“
软件测试 -- 1 软件测试知识大纲梳理
bond0脚本
网络安全应急响应技术实战指南(奇安信)
Examples of bio, NiO, AIO
006操作符简介
MySQL 45 talks about | 06 global locks and table locks: Why are there so many obstacles to adding a field to a table?
The main function of component procurement system, digital procurement helps component enterprises develop rapidly
Thymeleaf setting disabled
C language and SQL Server database technology
MySQL table operation
IP地址分类,判断一个网段是子网超网
gson与fastjson