51单片机教程(一)

LED灯控制

  1. 点亮LED灯光
#include <REGX51.H>
​
void main(){
    P2 = 0xfe // 1111 1110
}
  1. 让LED灯进行闪烁
#include <REGX52.H>
​
typedef unsigned int u16;
​
//延时函数
void delay(u16 i)       
{
    while(i--);
}
​
 void main(){
     while(1){
        P2 = 0xfe;
        delay(50000);   //大概延时450ms
        P2 = 0xff;
        delay(50000);
     }
 }

独立按键模块

图片[1]-51单片机教程(一)-四曲博客
  1. 独立按键控制LED灯亮灭
#include <REGX52.H>
​
void main(){
    while(1){
        if (P3_0 == 0){ //读取p3.0管脚状态,按下为0
            P2_0 = 0;  //打开P2.0管脚灯光
        }else{
            P2_0 = 1;   //关闭p2.0管脚灯光
        }
    }
}

2.按键的抖动

图片[2]-51单片机教程(一)-四曲博客
#include <REGX52.H>
​
void Delay1ms()     //@11.0592MHz
{
    unsigned char i, j;
​
    i = 2;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}
​
//延时函数,s为毫秒数
void delay(int s){
    int i = 0;
    for(i = 0;i < s ;i++){
        Delay1ms();
    }
}
​
void main(){
    while(1){
        //按下开关后执行
        if (P3_0 == 0){
            delay(20);  //按键消抖
            while(P3_0 == 0);
            delay(20);  //按键消抖
            P2_0 = ~P2_0;  //P2_0寄存器设置为取反
        }
    }
}
  1. 控制LED显示二进制
#include <REGX52.H>
​
void Delay1ms()     //@11.0592MHz
{
    unsigned char i, j;
​
    i = 2;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}
​
void delay(int s){
    int i = 0;
    for(i = 0;i < s ;i++){
        Delay1ms();
    }
}
​
void main(){
    unsigned char LEDNum = 0;
    
    while(1){
        if (P3_0 == 0){
            delay(20);
            while(P3_0 == 0);
            delay(20);
            
            LEDNum ++;
            P2 = ~LEDNum;
            
        }
    }
}
  1. 用按键控制LED移位
#include <REGX52.H>
​
void Delay1ms()     //@11.0592MHz
{
    unsigned char i, j;
​
    i = 2;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}
​
void delay(int s){
    int i = 0;
    for(i = 0;i < s ;i++){
        Delay1ms();
    }
}
​
void main(){
    unsigned char LEDNum = 1;
    int i = 0;
    P2 = ~LEDNum;
    
    while(1){
        if (P3_0 == 0){
            delay(20);
            while(P3_0 == 0);
            delay(20);
            
            //如果数字大于128,将数字设置为1,也就是0000 0001
            if (LEDNum >= 128){
                LEDNum = 1;
            }else{
                LEDNum = LEDNum << 1 ;
            }
            P2 = ~LEDNum;
            
        }
        if (P3_1 == 0){
            delay(20);
            while(P3_1 == 0);
            delay(20);
            
            //如果数字小于1之后,将数字置为128,也就是1000 0000
            if (LEDNum <= 1){
                LEDNum = 128;
            }else{
                LEDNum = LEDNum >> 1 ;
            }
            P2 = ~LEDNum;
            
        }
    }
}

数码管控制

一位数码管显示

由于8个数码管的引脚都是共用的,因此每个数码管显示的数字都是相同的,如果想要不同,则需要使用动态数码管显示

#include <REGX52.H>
​
//数字列表,采用小端存储
unsigned char number[] = {0x3F,0x6,0x5b,0x4f,0x66,0x6d,0x7c,0x7,0x7f,0x6f};
​
void Digitaltube(unsigned char position,num){
    //利用74JC138译码器进行选择位置
    switch (position){
        case 1:P2_4 = 1;P2_3 = 1;P2_2 = 1;break;
        case 2:P2_4 = 1;P2_3 = 1;P2_2 = 0;break;
        case 3:P2_4 = 1;P2_3 = 0;P2_2 = 1;break;
        case 4:P2_4 = 1;P2_3 = 0;P2_2 = 0;break;
        case 5:P2_4 = 0;P2_3 = 1;P2_2 = 1;break;
        case 6:P2_4 = 0;P2_3 = 1;P2_2 = 0;break;
        case 7:P2_4 = 0;P2_3 = 0;P2_2 = 1;break;
        case 8:P2_4 = 0;P2_3 = 0;P2_2 = 0;break;
    }
    
    //选择相应的数码管进行显示
    P0 = number[num];
}
​
void main(){
    while(1){
        Digitaltube(3,9);
    }
}

动态数码管显示

通过快速显示多个数字,利用视觉暂留,表现出显示多位的效果

#include <REGX52.H>
​
​
unsigned char number[] = {0x3F,0x6,0x5b,0x4f,0x66,0x6d,0x7c,0x7,0x7f,0x6f};
​
void Delay1ms()     //@11.0592MHz
{
    unsigned char i, j;
​
    i = 2;
    j = 199;
    do
    {
        while (--j);
    } while (--i);
}
​
void delay(int s){
    int i = 0;
    for(i = 0;i < s ;i++){
        Delay1ms();
    }
}
​
​
void Digitaltube(unsigned char position,num){
    switch (position){
        case 1:P2_4 = 1;P2_3 = 1;P2_2 = 1;break;
        case 2:P2_4 = 1;P2_3 = 1;P2_2 = 0;break;
        case 3:P2_4 = 1;P2_3 = 0;P2_2 = 1;break;
        case 4:P2_4 = 1;P2_3 = 0;P2_2 = 0;break;
        case 5:P2_4 = 0;P2_3 = 1;P2_2 = 1;break;
        case 6:P2_4 = 0;P2_3 = 1;P2_2 = 0;break;
        case 7:P2_4 = 0;P2_3 = 0;P2_2 = 1;break;
        case 8:P2_4 = 0;P2_3 = 0;P2_2 = 0;break;
    }
    P0 = number[num];
    
    //延迟一秒后将数码管关闭,消除重影
    delay(1);
    P0 = 0;
    
}
​
​
void main(){
    //每一轮循环快速显示多个数字,既可实现显示多位数据
    while(1){
        Digitaltube(1,1);
        Digitaltube(2,2);
        Digitaltube(3,8);
    }
}

LCD1602调试工具

图片[3]-51单片机教程(一)-四曲博客

代码文件连接:LCD1602调试工具 .h文件 .c文件

矩阵键盘

矩阵键盘扫描

矩阵键盘是将多个独立按键进行连接,并使用8个IO口来进行控制,在使用时,需要对整个矩阵键盘进行扫描,判断按下的是哪个按键,再进行相应的判断设计

函数中所使用的delay()函数由上面函数来进行定义

/**
   * @brief 矩阵键盘扫描,返回矩阵按键键值
   * @param 无
   * @retval 返回矩阵按键键值
*/
unsigned char KeyNumber(){
    unsigned char keynumber = 0;
    
    P1 = 0xff;  //关闭所有线路通道
    P1_3 = 0;   //打开第一列
    
    //扫描第一列中的每一项
    if (P1_7 == 0) {delay(20);while(P1_7 == 0);delay(20);keynumber = 1;}
    if (P1_6 == 0) {delay(20);while(P1_6 == 0);delay(20);keynumber = 5;}
    if (P1_5 == 0) {delay(20);while(P1_5 == 0);delay(20);keynumber = 9;}
    if (P1_4 == 0) {delay(20);while(P1_4 == 0);delay(20);keynumber = 13;}
    
    P1 = 0xff;
    P1_2 = 0; //打开第二列
    if (P1_7 == 0) {delay(20);while(P1_7 == 0);delay(20);keynumber = 2;}
    if (P1_6 == 0) {delay(20);while(P1_6 == 0);delay(20);keynumber = 6;}
    if (P1_5 == 0) {delay(20);while(P1_5 == 0);delay(20);keynumber = 10;}
    if (P1_4 == 0) {delay(20);while(P1_4 == 0);delay(20);keynumber = 14;}
    
    P1 = 0xff;
    P1_1 = 0;
    if (P1_7 == 0) {delay(20);while(P1_7 == 0);delay(20);keynumber = 3;}
    if (P1_6 == 0) {delay(20);while(P1_6 == 0);delay(20);keynumber = 7;}
    if (P1_5 == 0) {delay(20);while(P1_5 == 0);delay(20);keynumber = 11;}
    if (P1_4 == 0) {delay(20);while(P1_4 == 0);delay(20);keynumber = 15;}
    
    P1 = 0xff;
    P1_0 = 0;
    if (P1_7 == 0) {delay(20);while(P1_7 == 0);delay(20);keynumber = 4;}
    if (P1_6 == 0) {delay(20);while(P1_6 == 0);delay(20);keynumber = 8;}
    if (P1_5 == 0) {delay(20);while(P1_5 == 0);delay(20);keynumber = 12;}
    if (P1_4 == 0) {delay(20);while(P1_4 == 0);delay(20);keynumber = 16;}
    
    return keynumber;
    
}

利用数码管显示矩阵按键

暂时只能在数码管中显示一位数字

void main(){
    unsigned char key = 0;
    
    while(1){
        key = KeyNumber();
        
        if (key < 10 && key != 0) Digitaltube_static(1,key);
    }
}

Digitaltube_static()函数定义如下:

unsigned char number[] = {0x3F,0x6,0x5b,0x4f,0x66,0x6d,0x7c,0x7,0x7f,0x6f};
​
​
/**
   * @brief 在指定位置的数码管上显示数字
   * @param position 位置  范围1 - 8
   * @param num 数值  范围0 - 9
   * @retval 无
*/
void Digitaltube_static(unsigned char position,num){
    P0 = 0xff;
    
    switch (position){
        case 1:P2_4 = 1;P2_3 = 1;P2_2 = 1;break;
        case 2:P2_4 = 1;P2_3 = 1;P2_2 = 0;break;
        case 3:P2_4 = 1;P2_3 = 0;P2_2 = 1;break;
        case 4:P2_4 = 1;P2_3 = 0;P2_2 = 0;break;
        case 5:P2_4 = 0;P2_3 = 1;P2_2 = 1;break;
        case 6:P2_4 = 0;P2_3 = 1;P2_2 = 0;break;
        case 7:P2_4 = 0;P2_3 = 0;P2_2 = 1;break;
        case 8:P2_4 = 0;P2_3 = 0;P2_2 = 0;break;
    }
    P0 = number[num];
    
}
© 版权声明
THE END
喜欢就支持以下吧
点赞13 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片