华罗聪 发表于 2023-1-4 23:00

蓝桥杯单片机学习感悟【02-蜂鸣器与继电器的基本应用】

本帖最后由 华罗聪 于 2023-1-5 11:20 编辑

那&符号是表示与的意思, &只有1&1的时候才是1,其他都是0,而 | 则相反,只有0 | 0的时候才是0

电路图
ULN2003达林顿管——输入和输出是相反的

代码呈现:
#include "reg52.h"

sbit HC138_A=P2^5;
sbit HC138_B=P2^6;
sbit HC138_C=P2^7;

void Delay(unsigned int t)
{
      while(t--);
      while(t--);
}

void HC138(unsigned char n)
{
      switch(n)
      {
                case 4:            //P2=(P2&0xlf)|0x80;
                        HC138_C=1;
                        HC138_B=0;
                        HC138_A=0;
                break;
                case 5:            //P2=(P2&0xlf)|0xa0;
                        HC138_C=1;
                        HC138_B=0;
                        HC138_A=1;
                break;
                case 6:            //P2=(P2&0xlf)|0xc0;
                        HC138_C=1;
                        HC138_B=1;
                        HC138_A=0;
                break;
                case 7:            //P2=(P2&0xlf)|0xe0;
                        HC138_C=1;
                        HC138_B=1;
                        HC138_A=1;
                break;
      }
}


void abcdef(unsigned char channel,unsigned char dat)
{
      HC138(channel);
      P0=dat;
}

void LEDRunning()
{
      unsigned char i;
      
      for(i=0;i<3;i++)
      {
                abcdef(4,0x00);
                Delay(60000);
                Delay(60000);
                abcdef(4,0xff);
                Delay(60000);
                Delay(60000);
      }
      
      for(i=1;i<=8;i++)
      {
                P0=0xff<<i;
                Delay(60000);
                Delay(60000);
      }
      
      abcdef(5,0x10);
      Delay(60000);
      Delay(60000);
      abcdef(5,0x00);
      
      
      
      for(i=1;i<=8;i++)
      {
                abcdef(4,~(0xff<<i));
                Delay(60000);
                Delay(60000);
      }
      
      abcdef(5,0x40);
      Delay(60000);
      Delay(60000);
abcdef(5,0x00);
      Delay(60000);
      Delay(60000);
      
}      

void InitSystem()
{
      abcdef(5,0x00);
}

void main()
{
      InitSystem();
      while(1)
      {
                LEDRunning();
      }
}



页: [1]
查看完整版本: 蓝桥杯单片机学习感悟【02-蜂鸣器与继电器的基本应用】