单片机 【三极管和蜂鸣器】
三极管结构图
========================================================蜂鸣器
#include <reg52.h>
sbit led=P1^0;
sbit key=P3^0;
sbit key2=P3^1;
sbit key3=P3^2;
sbit buzzer=P2^3;
void delay(unsigned int x)
{
unsigned char i;
while(x--)
{
for(i=0;i<120;i++);
}
}
void main(void)
{
while(1)
{
if(key == 0)
{
delay(10);
if(key == 0)
{
led=0;
}
}
if(key2 == 0)
{
delay(10);
led=1;
}
if(key3 == 0)
{
buzzer=0;
delay(500);
buzzer=1;
delay(500);
}
}
}
===============================================蜂鸣器
#include<reg52.h>
#define ON 0
#define OFF 1
sbit FM=P2^3;
void delay(unsigned int x)
{
while(x--);
}
void main()
{
while(1)
{
FM=ON;
delay(50000);
FM=OFF;
delay(50000);
}
}
========================================点亮一个灯,同时响铃一次
#include<reg52.h>
#define ON 0;
#define OFF 1;
sbit FM=P2^3;
void delay(unsigned int x)
{
while(x--)
{
unsigned char i;
for(i=0;i<120;i++);
}
}
void main()
{
unsigned char i;
P1=0xfe;
for(i=0;i<8;i++)
{
delay(600);
P1<<=1;
P1=P1|0x01;
FM=ON;
delay(250);
FM=OFF;
}
}