基础:
1. 点亮二极管的压降为 1.6–1.7V,工作电流为3–10 mA;低电平点亮
2.线与指的是它们任意一开关只要对地导通,这根线就一定是低电平。
3.单片机I/O默认输入高电平;
LED 灯点亮
电路图:
# include
int main()
{
P1=0xb3;
return 0;
}
sbit led0=P1^0;
sbit led1=P1^2; //用来声明P1.1口为程序所要控制的端口,"sbit"是KEIL专门用来声明某位IO口
int main()
{
//P1=0xb3; //10110011
led0=0;
led1=0;
return 0;
}
独立按键扫描
电路图:
# include
sbit led0=P1^0;
sbit key3=P3^5;
int main()
{
P3=0xff;
while(1){
if(key3==0)
led0=0;
else
led0=1;
}
return 0;
}
数码管动态显示
电路图: P2口的低四位进行片选,P0口进行段选
#include
#define uchar unsigned char
#define uint unsigned int
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar DuanXuan[]={0xfe,0xfc,0xfb,0xf7};
uchar st;
void delay(uint x){
while(x--);
}
int main()
{
while(1){
for(st=0;st<4;st++){
P2=DuanXuan[st];
P0=table1[st];
delay(100);
}
}
return 0;
}
#include
#define uchar unsigned char
#define uint unsigned int
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar table1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
uchar We[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf};
uchar st;
sbit WE=P2^7;
sbit DU=P2^6;
void delay(uint xms){
uint i,j;
for(i=xms; i>0; i--)
for(j=110; j>0; j--);
}
void main()
{
while(1){
for(st=0;st<6;st++){
WE=1;
P0=We[st];
WE=0;
DU=1;
P0=table[st];
DU=0;
delay(500);
}
}
}