流水燈和蜂鳴器

haoo123333發表於2020-12-09

#先放原理圖
在這裡插入圖片描述
在這裡插入圖片描述
在這裡插入圖片描述

  • 看原理圖它打了label,只是說明了哪些連在一起,關注點是哪些連在一起,哪個管腳該輸出0還是1,而不是它打的label名稱,你自己不必非得使用它提供的label名稱呀。
  • 微控制器無非就是控制埠輸出0或1。燈的亮滅就是P1^0引腳的高低。程式裡面可以直接就P1^0=1地輸出1,但是打字多麻煩,所以要位定義,即sbit P1_0=P1^0
  • 以上這兩點是整個微控制器的核心

#No1單個LED的亮滅

簡單的一個燈的亮滅,週期1秒

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit P1_0=P1^0;//P1_0可以是任意變數,P1^0是位
void delay(); //一定要在main前面提前宣告
void main()
{
	while(1)
	{
		P1_0=0;
		delay();
		P1_0=1;
		delay();
	}
}
void delay()//延時1秒,別動340引數了!
{
	uint x,y;//區域性變數
	for(x=340;x>0;x--)
	{
		for(y=340;y>0;y--)
		{}
	}
}

#No2流水燈和蜂鳴器使用

流水燈順次點亮,且每一個週期2秒,第1秒蜂鳴器響,第2秒蜂鳴器不響

#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay();
sbit P2_0=P2^0;	//對應蜂鳴器使用。除P2^0口外還可以使用其他任意埠
void main()
{
	P2_0=1;
	P1=0xfe;   //對匯流排,直接操作。已經在<reg52.h>定義了。
	while(1)
	{
	delay();
	P2_0=~P2_0;	//巧妙使用取反。位取反,但不僅僅限於一位的取反
	delay();
	P1=_crol_(P1,1);
	}
}
void delay()
{
	uint x,y;//區域性變數
	for(x=340;x>0;x--)
	{
		for(y=340;y>0;y--)
		{}
	}
}

--------------------------------------------------------------------------
REG52.H

Header file for generic 80C52 and 80C32 microcontroller.
Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/

#ifndef __REG52_H__
#define __REG52_H__

/*  BYTE Registers  */
sfr P0    = 0x80;
sfr P1    = 0x90;
sfr P2    = 0xA0;
sfr P3    = 0xB0;
sfr PSW   = 0xP1_0;
sfr ACC   = 0xE0;
sfr B     = 0xF0;
sfr SP    = 0x81;
sfr DPL   = 0x82;
sfr DPH   = 0x83;
sfr PCON  = 0x87;
sfr TCON  = 0x88;
sfr TMOD  = 0x89;
sfr TL0   = 0x8A;
sfr TL1   = 0x8B;
sfr TH0   = 0x8C;
sfr TH1   = 0x8D;
sfr IE    = 0xA8;
sfr IP    = 0xB8;
sfr SCON  = 0x98;
sfr SBUF  = 0x99;

/*  8052 Extensions  */
sfr T2CON  = 0xC8;
sfr RCAP2L = 0xCA;
sfr RCAP2H = 0xCB;
sfr TL2    = 0xCC;
sfr TH2    = 0xCD;


/*  BIT Registers  */
/*  PSW  */
sbit CY    = PSW^7;
sbit AC    = PSW^6;
sbit F0    = PSW^5;
sbit RS1   = PSW^4;
sbit RS0   = PSW^3;
sbit OV    = PSW^2;
sbit P     = PSW^0; //8052 only

/*  TCON  */
sbit TF1   = TCON^7;
sbit TR1   = TCON^6;
sbit TF0   = TCON^5;
sbit TR0   = TCON^4;
sbit IE1   = TCON^3;
sbit IT1   = TCON^2;
sbit IE0   = TCON^1;
sbit IT0   = TCON^0;

/*  IE  */
sbit EA    = IE^7;
sbit ET2   = IE^5; //8052 only
sbit ES    = IE^4;
sbit ET1   = IE^3;
sbit EX1   = IE^2;
sbit ET0   = IE^1;
sbit EX0   = IE^0;

/*  IP  */
sbit PT2   = IP^5;
sbit PS    = IP^4;
sbit PT1   = IP^3;
sbit PX1   = IP^2;
sbit PT0   = IP^1;
sbit PX0   = IP^0;

/*  P3  */
sbit RD    = P3^7;
sbit WR    = P3^6;
sbit T1    = P3^5;
sbit T0    = P3^4;
sbit INT1  = P3^3;
sbit INT0  = P3^2;
sbit TXD   = P3^1;
sbit RXD   = P3^0;

/*  SCON  */
sbit SM0   = SCON^7;
sbit SM1   = SCON^6;
sbit SM2   = SCON^5;
sbit REN   = SCON^4;
sbit TB8   = SCON^3;
sbit RB8   = SCON^2;
sbit TI    = SCON^1;
sbit RI    = SCON^0;

/*  P1  */
sbit T2EX  = P1^1; // 8052 only
sbit T2    = P1^0; // 8052 only
             
/*  T2CON  */
sbit TF2    = T2CON^7;
sbit EXF2   = T2CON^6;
sbit RCLK   = T2CON^5;
sbit TCLK   = T2CON^4;
sbit EXEN2  = T2CON^3;
sbit TR2    = T2CON^2;
sbit C_T2   = T2CON^1;
sbit CP_RL2 = T2CON^0;

#endif
/*--------------------------------------------------------------------------
INTRINS.H

Intrinsic functions for C51.
Copyright (c) 1988-2004 Keil Elektronik GmbH and Keil Software, Inc.
All rights reserved.
--------------------------------------------------------------------------*/

#ifndef __INTRINS_H__
#define __INTRINS_H__

extern void          _nop_     (void);
extern bit           _testbit_ (bit);
extern unsigned char _cror_    (unsigned char, unsigned char);
extern unsigned int  _iror_    (unsigned int,  unsigned char);
extern unsigned long _lror_    (unsigned long, unsigned char);
extern unsigned char _crol_    (unsigned char, unsigned char);
extern unsigned int  _irol_    (unsigned int,  unsigned char);
extern unsigned long _lrol_    (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
extern void          _push_    (unsigned char _sfr);
extern void          _pop_     (unsigned char _sfr);

#endif

這裡面就是函式的宣告
_crol_ 字元迴圈左移
_cror_ 字元迴圈右移
_irol_ 整數迴圈左移
_iror_ 整數迴圈右移
_lrol_ 長整數迴圈左移
_lror_ 長整數迴圈右移
_nop_ 空操作 (相當於8051 NOP 指令)

相關文章