MENU

3 LED闪烁

July 3, 2020 • 51单片机

--- 当前时间: ---

3 LED闪烁

3.1 C语言常用的预处理命令

typedef的使用:

typedef unsigned char u8;    // 1 byte: 0 ~ 255
typedef unsigned int u16;    // 2 byte: 0 ~ 65535

定义关键词以增强程序的可移植性,因为不同的编译软件上C语言的数据类型的关键词的位宽是不一样的

3.2 while循环函数

while(i < 10)
{
    i = i + 1;    // The things in parentheses are Boolean values
}

表达式为真执行循环体

3.3 延时函数

void delay(u16 i)    // The delay is about 10 us when i = 1
{
    while(i--);
}

但是上述函数的延时不准确,通常准确些的延时要用到定时器

3.4 实例代码

main.c

#include "reg52.h"

typedef unsigned char u8;    // 1 byte: 0 ~ 255
typedef unsigned int u16;    // 2 byte: 0 ~ 65535

sbit led = P0^0;            // Assign the port address to the variable led

void delay(u16 i)            // The delay is about 10 us when i = 1
{
    while(i--);
}

void main()
{
    while(1)
    {
        led = 1;            // Set to high level, cheers!
        delay(50000);        // Delay 450 ms
        led = 0;            // Set to low level, cheers!
        delay(50000);        // Delay 450 ms
    }
}
Last Modified: August 21, 2020
Archives QR Code
QR Code for this page
Tipping QR Code