一天的单片机电子实习挺有趣的,特别是老师拿出的机器狗,俄罗斯方块单片机,太强了。上午的电子实习主要是写流水灯,有七个样例只需要写出来五个就能拿到四分,下午是做电子表,实现基础功能拿到四分,在增加一些功能如闹钟,日历等可以加分。此外在写交通表也可以加分。
#1.流水灯
#include<stc8a.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1 = P3 ^ 2;
sbit k2 = P3 ^ 3;
sbit k3 = P3 ^ 4;
sbit k4 = P3 ^ 5;
sbit led = P4 ^ 1;
void delay_t(uint t)
{
uint a; uchar b;
for(b=0;b<4;b++)
for(a=0;a<t;a++);
}
void start_1()
{
uchar a, b;
b = 0xfe;
for(a=0;a<7;a++)
{
P0 = b;
delay_t(50000);
b = _crol_(b , 1);
}
for(a=0;a<7;a++)
{
P0 = b;
delay_t(50000);
b = _cror_(b , 1);
}
}
void start_2()
{
uchar a, b;
b = 0xfe;
for(a=0;a<7;a++)
{
P0 = 0xe7 & b;
delay_t(500000);
b = _crol_(b, 1);
}
for(a=0;a<7;a++)
{
P0 = 0xe7 & b;
delay_t(500000);
b = _cror_(b, 1);
}
}
void start_3()
{
uchar a, b, c;
b = 0xfe;
c = 0x7f;
for(a=0;a<4;a++)
{
P0 = b & c;
delay_t(500000);
b = _crol_(b, 1);
c = _cror_(c, 1);
}
for(a=0;a<3;a++)
{
P0 = b & c;
delay_t(500000);
b = _crol_(b, 1);
c = _cror_(c, 1);
}
}
void start_4()
{
uchar a, b, c;
b = 0x7f;
c = 0x7f;
for(a=0;a<8;a++)
{
P0 = b;
delay_t(500000);
b = _cror_(b, 1);
c = b & c;
b = c;
}
b = 0x01;
c = 0x01;
for(a=0;a<8;a++)
{
P0 = b;
delay_t(500000);
b = _crol_(b, 1);
c = b | c;
b = c;
}
}
void start_5()
{
uchar a, b, c, d, e, t;
b = 0xfe;
c = b;
d = 0xff;
for(e=8;e>0;e--)
{
for(t=0;t<e;t++)
{
P0 = b & d;
delay_t(500000);
b = _crol_(b, 1);
}
d = P0;
b = c;
}
}
void main()
{
led =0;
while(1)
{
start_1();
start_2();
start_3();
start_4();
start_5();
}
}
#2.电子表 想着增加一个闹钟的功能但做不出来,我增加了一个时间内置好的闹钟,按f5的时候可以关闭闹钟。
#include<STC8A.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf};
uchar select[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};
sbit k1 = P3 ^ 2;
sbit k2 = P3 ^ 3;
sbit k3 = P3 ^ 4;
sbit k4 = P3 ^ 5;
sbit k5 = P4 ^ 0;
sbit beep = P4 ^ 2;
bit flag1, flag2, flag3, flag4, flag5;
char hour, minute, second;
void delay()
{
uint i = 100;
while(i--);
}
void init()
{
P0M1 = 0x00;
P0M0 = 0xff;
P1M1 = 0x00;
P1M0 = 0xff;
P2M1 = 0x00;
P2M0 = 0xff;
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0xC0;
TH0 = 0x63;
TR0 = 1;
ET0 = 1;
EA = 1;
}
void display()
{
P1 = select[0];
P0 = table[second%10];
delay();
P1 = select[1];
P0 = table[second/10];
delay();
P1 = select[2];
P0 = table[10];
delay();
P1 = select[3];
P0 = table[minute%10];
delay();
P1 = select[4];
P0 = table[minute/10];
delay();
P1 = select[5];
P0 = table[10];
delay();
P1 = select[6];
P0 = table[hour%10];
delay();
P1 = select[7];
P0 = table[hour/10];
delay();
}
void Beep()
{
if(k5 == 1)
{
if(flag5 == 0)
{
flag5 = 1;
beep = ~beep;
}
}
if(k5 == 0)
{
flag5 = 0;
}
}
void set_time()
{
if(k1 == 0)
{
if(flag1 == 0)
{
flag1 = 1;
hour++;
}
}
if(k1 == 1)
{
flag1 = 0;
}
if(k2 == 0)
{
if(flag2 == 0)
{
flag2 = 1;
hour --;
}
}
if(k2 == 1)
{
flag2 = 0;
}
if(k3 == 0)
{
if(flag3 == 0)
{
flag3 = 1;
minute++;
}
}
if(k3 == 1)
{
flag3 = 0;
}
if(k4 == 0)
{
if(flag4 == 0)
{
flag4 = 1;
minute--;
}
}
if(k4 == 1)
{
flag4 = 0;
}
}
void main()
{
init();
P2 = 0x00;
while(1)
{
display();
set_time();
}
}
void timer0()interrupt 1
{
uchar t0;
t0++;
if(t0 == 60)
{
t0 = 0;
second++;
}
if(second == 60)
{
second = 0;
minute++;
}
if(minute == 60)
{
hour++;
minute=0;
}
if(hour == 24)
{
hour=0;
}
if(minute == 12 && hour == 12)
{
Beep();
}
}
#3.交通表 一个朋友搞出来了。
#include<STC8A.h>
#define uchar unsigned char
#define uint unsigned int
uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar select[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar redt,greent,time,sign;
sbit k1=P3^2;
sbit k2=P3^3;
sbit k3=P3^4;
sbit k4=P3^5;
sbit dxg=P2^0;
sbit dxy=P2^1;
sbit dxr=P2^2;
sbit nbg=P2^3;
sbit nby=P2^4;
sbit nbr=P2^5;
bit flag1,flag2,flag3,flag4;
void delay()
{
uint i=1000;
while(i--);
}
void init()
{
P0M1=0x00;
P0M0=0xff;
P1M1=0x00;
P1M0=0xff;
P2M1=0x00;
P2M0=0xff;
AUXR&=0x7F;
TMOD&=0xF0;
TL0=0xC0;
TH0=0x63;
TR0=1;
ET0=1;
EA=1;
}
void light(char a)
{
if(a=='r')
{
if((redt-time)>4)
{
nbr=1;
nby=0;
nbg=0;
dxr=0;
dxy=0;
dxg=1;
}
if((redt-time)<=4)
{
nbr=1;
nby=0;
nbg=0;
dxr=0;
dxy=1;
dxg=0;
}
}
if(a=='g')
{
if((greent-time)>4)
{
nbr=0;
nby=0;
nbg=1;
dxr=1;
dxy=0;
dxg=0;
}
if((greent-time)<=4)
{
nbr=0;
nby=1;
nbg=0;
dxr=1;
dxy=0;
dxg=0;
}
}
}
void display_red()
{
light('r');
P1=select[0];
P0=table[(redt-time)%10];
delay();
P1=select[1];
P0=table[(redt-time)/10];
delay();
P1=select[6];
P0=table[(redt-time)%10];
delay();
P1=select[7];
P0=table[(redt-time)/10];
delay();
}
void display_green()
{
light('g');
P1=select[0];
P0=table[(greent-time)%10];
delay();
P1=select[1];
P0=table[(greent-time)/10];
delay();
P1=select[6];
P0=table[(greent-time)%10];
delay();
P1=select[7];
P0=table[(greent-time)/10];
delay();
}
void set_time()
{
if(k1==0)
{
if(flag1==0)
{
flag1=1;
redt++;
}
}
if(k1==1)
{
flag1=0;
}
if(k2==0)
{
if(flag2==0)
{
flag2=1;
redt--;
}
}
if(k2==1)
{
flag2=0;
}
if(k3==0)
{
if(flag3==0)
{
flag3=1;
greent++;
}
}
if(k3==1)
{
flag3=0;
}
if(k4==0)
{
if(flag4==0)
{
flag4=1;
greent--;
}
}
if(k4==1)
{
flag4=0;
}
}
void main()
{
init();
P1=0x00;
redt=30;
greent=30;
while(1)
{
if(sign==0)
{
display_red();
}
if(sign==1)
{
display_green();
}
set_time();
}
}
void timer0()interrupt 1
{
uchar t0;
t0++;
if(t0==50)
{
t0=0;
time++;
}
if(sign==1)
{
if(greent==time)
{
time=0;
sign=0;
}
}
if(sign==0)
{
if(redt==time)
{
time=0;
sign=1;
}
}
}
单片机是真的很有意思,可惜这样的电子实习只有八个课时。
|