51单片机入门教程(部分代码整合)

   大家好,这次我要分享的代码包括:

01 静态数码管显示

02 动态数码管显示

03 模块化编程

04 数码管

05 LCD1602调试工具

06 矩阵键盘

07 矩阵键盘电子密码锁

08 定时器时钟

09 串口向电脑发送数据

10 串口向电脑发送数据--串口模块化(电脑每隔1s发送递增数据)

11 电脑通过串口控制LED

12 LED点阵屏显示图形1

13 LED点阵屏显示图形2----绘制笑脸

01 静态数码管显示

1.让LED6显示数字6

#include<REGX52.H>

void main()

{

          P2_4=1;

          P2_3=0;

          P2_2=1;

          P0=0x7D;

While(1)

{

}

}

2.让LED5显示数字6

#include<REGX52.H>

void main()

{

          P2_4=1;

          P2_3=0;

          P2_2=0;

          P0=0x7D;

While(1)

{

}

}

3.让LED7显示数字6

#include<REGX52.H>

void Nixie(unsigned char Location,Number)

{

switch(Location)

{

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=0x7D;

}      

void main()

{

        Nixie(7,2);

While(1)

{

}

}

4.用数组让LED7(第7位)显示数字2

#include<REGX52.H>

unsigned char NixieTable[ ]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};

void Nixie(unsigned char Location,Number)

{

switch(Location)

{

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=NixieTable[Number];

}      

void main()

{

        Nixie(7,2); //可更换

While(1)

{

}

}

02 动态数码管显示

#include<REGX52.H>

unsigned char NixieTable[ ]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};

void Delay(unsigned int xms)       //延时清零

{

   unsigned char i,j;

   while(xms--)

{

   i=2;

   j=239;

   do

   {

While (--j);

}  while (--i);

}

}

 

void Nixie(unsigned char Location,Number)

{

switch(Location)

{

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=NixieTable[Number];

}      

void main()

{

       

While(1)

{

Nixie(1,1);

Delay(20);

Nixie(2,2);

Delay(20);

Nixie(3,3);

Delay(20);

}

}

03  模块化编程

main.c

#include<REGX52.H>

#include “Delay.h”

void main()

{

     while(1)

 {

    P2_0=1;

Delay(500);

P2_0=0;

Delay(500);

}

}

Delay.c

void Delay(unsigned int xms)      

{

   unsigned char i,j;

   while(xms--)

{

   i=2;

   j=239;

   do

   {

While (--j);

}  while (--i);

}

}

Delay.h

#ifndef  __DELAY_H__

#define  __DELAY_H__

void Delay (unsigned int xms);

#endif

                                                      04   数码管

main.c

#include<REGX52.H>

#include “Delay.h”

#include “Nixie.h”

void main()

{

     while(1)

 {

Nixie(1,1);

Nixie(2,2);

Nixie(3,3);

Nixie(4,4);

Nixie(5,5);

Nixie(6,6);

}

}

Nixie.h

#ifndef  __NIXIE_H__

#define  __NIXIE_H__

void Nixie(unsigned char Location,Number);

#endif

Nixie.c

#include<REGX52.H>

#include “Delay.h”

unsigned char NixieTable[ ]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,0x00};

void Nixie(unsigned char Location,Number)

{

switch(Location)

{

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=NixieTable[Number];

Delay(1);

P0=0x00;

}      

05   LCD1602调试工具

main.c

#include<REGX52.H>

#include “LCD1602.h”

void main( )

{

     LCD_Init( );

     LCD_ShowChar(1,1,’A’);

     LCD_ShowString(1,3,”Hello”);

     LCD_ShowNum(1,9,123,3);

     LCD_ShowSignedNum(1,13,-66,2);

     LCD_ShowHexNum(2,1,0xA8,2);

   LCD_ShowBinNum(2,4,0xAA,8);

     While(1)

{

}

}

main.c

#include<REGX52.H>

#include “LCD1602.h”

Int Result;

void main( )

{

     LCD_Init( );

     Result=1+1;

     LCD_ShowNum(1,1,Result,3);

     While(1)

{

}

}

main.c

#include<REGX52.H>

#include “LCD1602.h”

#include “Delay.h”

int Result=0;

void main( )

{

LCD_Init();

while(1)

{

Result++;

Delay(1000);

LCD_ShowNum(1,1,Result,3);

}

}

06 矩阵键盘

main.c

#include<REGX52.H>

#include “Delay.h”

#include “LCD1602.h”

void main( )

{

LCD_Init();

LCD_ShowString(1,1,“HelloWorld”);

while(1)

{

}

}

MatrixKey.h

#ifndef __MATRIXKEY_H__

#define __MATRIXKEY_H__

#endif

MatrixKey.c

#include <REGX52.H>

#include “Delay.h”

unsigned char MatrixKey()

{

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_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;}

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;}

return KeyNumber;

}

main.c

#include<REGX52.H>

#include “Delay.h”

#include “LCD1602.h”

#include “MatrixKey.h”

unsigned char KeyNum;

void main( )

{

LCD_Init();

LCD_ShowString(1,1,“HelloWorld”);

while(1)

{

   KeyNum=MatrixKey();

   if(KeyNum)

{

 LCD_ShowNum(2,1,KeyNum,2);

}

}

}

                                               07  矩阵键盘电子密码锁

main.c

#include<REGX52.H>

#include “Delay.h”

#include “LCD1602.h”

#include “MatrixKey.h”

unsigned char KeyNum;

unsigned int Password,Count;

void main( )

{

LCD_Init();

LCD_ShowString(1,1,“Password:”);

while(1)

{

 KeyNum=MatrixKey();

 if(KeyNum)

{

if(KeyNum<=10) \\如果s1~s10按键按下,输入密码

{

    if(Count<4)   \\如果输入次数小于4

{

Password*=10;        \\密码左移一位

Password+=KeyNum%10;   \\获取一位密码

        Count++;     \\计次加一

}

  

LCD_ShowNum(2,1,Password,4);  \\更新显示

}

if(KeyNum==11) \\如果s11按键按下,确认

{

if(Password==2345) \\如果密码等于正确密码

{

LCD_ShowString(1,14,“OK ”); \\显示OK

Password=0;   \\密码清零

Count=0;       \\计次清零

LCD_ShowNum(2,1,Password,4);  \\更新显示

}

else   \\否则

{

LCD_ShowString(1,14,“ERR”);  \\显示ERR

Password=0;   \\密码清零

Count=0;       \\计次清零

LCD_ShowNum(2,1,Password,4);  \\更新显示

}

}

if(KeyNum==12)  \\如果s12按键按下,取消

{

Password=0;   \\密码清零

Count=0;       \\计次清零

LCD_ShowNum(2,1,Password,4);  \\更新显示

}

}

}

}

                                                        08    定时器时钟

main.c

#include<REGX52.H>

void Timer0_Init( )

{

void Timer0Init(void)  \\1毫秒@12.000MHz

{

TM0D &=0xF0; \\设置定时器模式

TM0D |=0x01; \\设置定时器模式

TL0=0x18; \\设置定时初值

TH0=0xFC; \\设置定时初值

TF0=0; \\消除TF0标志

TR0=1; \\定时器0开始计时

}

  \\TMOD=0x01;   \\0000 0001  省略

TMOD=TMOD&0xF0;\\把TM0D的低四位清零,高四位保持不变  可写作TMOD&=0xF0;

TMOD=TMOD|0x01; \\把TM0D的最低位置1,高四位保持不变  可写作TMOD|=0x01;

  TF0=0;

  TR0=1;

  TH0=64535/256;

  TL0=64535%256;

  ET0=1;

  EA=1;

  PT0=0;

}

void main( )

{

    Timer0_Init( );

while(1)

{

}

}

unsigned int T0Count;

void Timer0_Routine( ) interrupt 1

{

    TL0=0x18;  \\设置定时初值

TH0=0xFC;  \\设置定时初值

T0Count++;

    if(T0Count>=1000)

{

T0Count=0;

P2_0=~P2_0;

}

  }

                                        09  串口向电脑发送数据

main.c  初始

#include<REGX52.H>

#include ”Delay.h”

Void UartInit(void)  \\4800bps.@12.000HMz

{

PCON |=0x80; \\使能波特率倍速位SMOD

SCON=0x50;  \\8位数据,可变波特率

AUXR &=0xBF; \\定时器1时钟为Fosc/12,即12T

AUXR &=0xFE; \\串口1选择定时器1为波特率发生器

TMOD &=0x0F; \\清除定时器1模式位

TMOD |=0x20;  \\设定定时器1为8位自动重装方式

TL1=0xF3;  \\设定定时初值

TH1=0xF3; \\设定定时器重装值

ET1=0; \\禁止定时器1中断

TR1=1; \\启动定时器1

}

void UART_Init()

{

SCON=0x40;

PCON |= 0x80;

TM0D &=0x0F; \\设置定时器模式

TM0D |=0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

TF0=0; \\消除TF0标志

TR0=1;  \\定时器0开始计时

ET0=1;

EA=1;

    PT0=0;

}

void main()

{

While(1)

{

}

}

main.c  优化

#include<REGX52.H>

#include ”Delay.h”

void UART_Init()\\4800bps.@12.000HMz

{

SCON= 0x40;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

TF1= 0; \\消除TF0标志

TR1= 1;  \\定时器0开始计时

}

void UART_SendByte (unsigned char Byte)

{

SBUF=Byte;

while(TI==0);

TI=0;

}

void main( )

{

      UART_Init( );

      While(1)

{

 UART_SendByte(0x66);

         Delay(1);

}

}

main.c  终版(串口)

#include<REGX52.H>

#include ”Delay.h”

unsigned char Sec;

void UART_Init()\\4800bps.@12.000HMz

{

SCON= 0x40;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

TF1= 0; \\消除TF0标志

TR1= 1;  \\定时器0开始计时

}

void UART_SendByte (unsigned char Byte)

{

SBUF=Byte;

while(TI==0);

TI=0;

}

void main( )

{

      UART_Init( );

      While(1)

{

 UART_SendByte(Sec);

Sec++;

         Delay(1000);

}

}

10   串口向电脑发送数据

              ---串口模块化(电脑每隔1s发送递增数据)

main.c

#include<REGX52.H>

#include “Delay.h”

#include “UART.h”

unsigned char Sec;

void main( )

{

      UART_Init( );

      While(1)

{

 UART_SendByte(Sec);

Sec++;

         Delay(1000);

}

}

UART.h

#ifndef __UART_H__

#define

void UART_Init( ) \\4800bps.@12.000HMz

{

SCON= 0x40;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

ET1= 0; \\禁止定时器1中断

TR1= 1; \\启动定时器1

}

void UART_SendByte (unsigned char Byte)

#endif

UART.c

#include<REGX52.H>

/**

  * @brief 串口初始化,4800bps.@12.000HMz

  * @param 无

  * @retval 无

  */

void UART_Init( )

{

SCON= 0x40;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

TF1= 0; \\消除TF0标志

TR1= 1;  \\定时器0开始计时

}

/**

  * @brief 串口发送一个字节数据

  * @param  Byte 要发送的一个字节数据

  * @retval 无

  */

void UART_SendByte (unsigned char Byte)

{

SBUF=Byte;

while(TI==0);

TI=0;

}

                                                    11  电脑通过串口控制LED

main.c

#include<REGX52.H>

#include “Delay.h”

#include “UART.h”

void main( )

{

      UART_Init( );

      While(1)

{

}

}

void UART_Routine( )  interrupt 4

{

  P2=0x00;   \\全亮

}

main.c

#include<REGX52.H>

#include “Delay.h”

#include “UART.h”

void main( )

{

      UART_Init( );

      While(1)

{

}

}

void UART_Routine( )  interrupt 4

{

   if(R1==1)

 {

P2=SBUF; \\右亮或P2=~SBUF;\\左亮

UART_SendByte(SBUF);

RI=0;

}

}

UART.h

#ifndef __UART_H__

#define

void UART_Init( ) \\4800bps.@12.000HMz

{

SCON= 0x40;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

ET1= 0; \\禁止定时器1中断

TR1= 1; \\启动定时器1

}

void UART_SendByte (unsigned char Byte)

#endif

UART.c

#include<REGX52.H>

/**

  * @brief 串口初始化,4800bps.@12.000HMz

  * @param 无

  * @retval 无

  */

void UART_Init( )

{

SCON= 0x50;

PCON |= 0x80;

TM0D &= 0x0F; \\设置定时器模式

TM0D |= 0x20; \\设置定时器模式

TL1= 0xF3;  \\设定定时初值

TH1= 0xF3; \\设定定时器重装值

ET1= 0; \\禁止定时器1中断

TR1= 1;  \\启动定时器1

EA=1;

ES=1;

}

/**

  * @brief 串口发送一个字节数据

  * @param  Byte 要发送的一个字节数据

  * @retval 无

  */

void UART_SendByte (unsigned char Byte)

{

SBUF=Byte;

while(TI==0);

TI=0;

}

/*串口中断函数模板

void UART_Routine( )  interrupt 4

{

   if(R1==1)

 {

RI=0;

}

}

*/

                                                          12   LED点阵屏显示图形1

main.c

#include<REGX52.H>

sbit RCK=P3^5;       //RCLK

sbit SCK=P3^6;       //SRCLK

sbit SER=P3^4;      //SER

Void  _74HC595_WriteByte(unsignd char Byte)

{

   unsignd char i;

   for(i=0;i<8;i++)

  {

 SER=Byte & (0x80>>i);

     SCK=1;

     SCK=0;

}

  RCK=0;

  RCK=0;

}

void main( )

{

     SCK=0;

     RCK=0;

     _74HC595_WriteByte(0xF0);

 While(1)

{

}

}

                                         13   LED点阵屏显示图形2(绘制笑脸)

main.c

#include<REGX52.H>

sbit RCK=P3^5;       //RCLK

sbit SCK=P3^6;       //SRCLK

sbit SER=P3^4;      //SER

Void  _74HC595_WriteByte(unsignd char Byte)

{

   unsignd char i;

   for(i=0;i<8;i++)

  {

 SER=Byte & (0x80>>i);

     SCK=1;

     SCK=0;

}

  RCK=1;

  RCK=0;

}

void MatrixLED_ShowColumn( unsigned char Column,Data)

{

  _74HC595_WriteByte(Data);

  P0=~(0x80>>Cloumn );

  Delay(1);

  P0=0xFF;

}

void main( )

{

     SCK=0;

     RCK=0;

 While(1)

{

      MatrixLED_ShowColumn(0,0x3C);

MatrixLED_ShowColumn(1,0x42);

MatrixLED_ShowColumn(2,0xA9);

MatrixLED_ShowColumn(3,0x85);

MatrixLED_ShowColumn(4,0x85);

MatrixLED_ShowColumn(5,0xA9);

MatrixLED_ShowColumn(6,0x42);

MatrixLED_ShowColumn(7,0x3C);

}

}

总结

     综上,是我这次分享的全部内容,是我学习后纯手打的,难免有错误疏漏,欢迎大家批评指正!感谢你看到这里~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值