复习下之前学的
=============================================
LED+继电器+蜂鸣器
=============================================
-
#include <REG52.H>
-
#include <intrins.h>
-
#include <personal.h> //个人定义头文件
-
#define TIME 500
-
-
sbit relay = P3^7;
-
sbit bp = P3^6;
-
-
void beep(u16 t)
-
{
-
u16 i;
-
for(i=0; i<t; i++)
-
{
-
bp = 1;
-
delay500us();
-
bp = 0;
-
delay500us();
-
}
-
}
-
-
void main()
-
{
-
u8 i = 1, aa, bb, cnt;
-
while(1)
-
{
-
relay = 0;
-
aa = 0x7F;
-
cnt = 0;
-
while(cnt < 8)
-
{
-
P1 = aa;
-
delay(TIME);
-
aa = _cror_(aa,1);
-
cnt++;
-
}
-
relay = 1;
-
P1 = 0xff;
-
bb = 0xfe;
-
cnt = 0;
-
beep(100);
-
while(cnt < 8)
-
{
-
P0 = bb;
-
delay(TIME);
-
bb = _crol_(bb,1);
-
cnt++;
-
}
-
P0 = 0xff;
-
}
-
}
=============================================
矩阵键盘
=============================================
-
#include <reg52.h>
-
#include <personal.h>
-
#include <intrins.h>
-
-
u8 code a[4] = {0xe,0xd,0xb,0x7};
-
u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
-
0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
-
};
-
-
-
void main()
-
{
-
u8 f4, l4, i;
-
while(1)
-
{
-
P1 = 0xf0;
-
P2 = 0x7f;
-
P0 = 0xbf;
-
f4 = P1;
-
if((f4&0xf0)!=0xf0)
-
{
-
delay(30);
-
f4 = P1;
-
if((f4&0xf0)!=0xf0)
-
{
-
P1 = 0x0f;
-
l4 = P1;
-
f4 = _cror_(f4, 4);
-
for(i=0; i<4; i++)
-
{
-
if(f4 == a[i])
-
{
-
f4 = i;
-
}
-
if(l4 == a[i])
-
{
-
l4 = i;
-
}
-
}
-
P0 = CODE[l4*4+f4];
-
}
-
}
-
else
-
{
-
P1 = 0xff;
-
}
-
delay(200);
-
}
-
}
=============================================
数码管
=============================================
-
#include <reg52.h>
-
#include <personal.h>
-
#include <intrins.h>
-
-
u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
-
0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
-
};
-
-
void main()
-
{
-
u8 i = 0, minis_pins = 0xfe;
-
while(1)
-
{
-
P2 = minis_pins;
-
P0 = CODE[i];
-
delay(1000);
-
minis_pins = _crol_(minis_pins, 1);
-
i = (++i) % 16;
-
}
-
}
=============================================
外部中断+数码管计数
=============================================
-
#include <reg52.h>
-
#include <intrins.h>
-
#include <personal.h>
-
-
u8 code CODE[16] = {0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82,
-
0xF8, 0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E
-
};
-
-
u8 a[8]= {0};
-
u8 cntl = 0x7f, cnt=0;
-
-
sbit key = P3^2;
-
sbit test = P1^7;
-
-
void disp()
-
{
-
u8 i, j;
-
for(i=0; i<=cnt; i++)
-
{
-
j=cntl; //Attention
-
j = _cror_(j, i); //j changed
-
P0 = CODE[a[7-i]]; //Attention
-
P2 = j;
-
P2 = 0xff;
-
-
}
-
}
-
-
void add(u8 cur_index) reentrant
-
{
-
if(a[cur_index] > 8)
-
{
-
a[cur_index] = 0;
-
if(cur_index == 7-cnt)
-
{
-
//Attention
-
cnt++;
-
}
-
add(cur_index-1);
-
}
-
else
-
{
-
a[cur_index]++;
-
}
-
}
-
-
void main()
-
{
-
EA = 1;
-
EX0 = 1;
-
IT0 = 1;
-
P2 = cntl;
-
P0 = CODE[0];
-
while(1)
-
{
-
disp();
-
}
-
}
-
-
void intkey(void) interrupt 0
-
{
-
if(!key)
-
{
-
delay(300);
-
while(!key)
-
{
-
;
-
}
-
add(7);
-
}
-
}
=======================================================
串口通信
=======================================================
2011-09-29 14:28 发表于百度空间,今搬至CU。
阅读(2106) | 评论(0) | 转发(0) |