Categories
03/09/2011
C51 Revision
By dch1 in Just Notes No Comments
今天复习跑马灯程序,感觉熟练多了,呵呵,基本上没有按照题目编,因为其实细节就是那些延迟的参数,那个很容易修改的。
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned charvoid delay1s(void);
uchar status;
void main()
{
status=0;
while (1)
{
delay1s();
status=status+1;
P0=~status;
}
}void delay1s(void)
{
uchar a,b,c;
for(c=46;c>0;c–)
for(b=152;b>0;b–)
for(a=70;a>0;a–);
_nop_(); //if Keil,require use intrins.h
}
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned charvoid delay1s(void);
uchar status;
void main()
{
while (1)
{
P0=0xFE;
delay1s();
for(status=1; status<8; status++)
{
P0=_crol_(P0,1);
delay1s();
}
//P0=0x7F;
for(status=7; status>1; status–)
{
P0=_cror_(P0,1);
delay1s();
}
}
}void delay1s(void)
{
uchar a,b,c;
for(c=46;c>0;c–)
for(b=152;b>0;b–)
for(a=70;a>0;a–);
_nop_(); //if Keil,require use intrins.h
}
Example 3: 8个发光管间隔200ms由上至下,再由下至上,再重复一次,然后全部熄灭再以300ms间隔全部闪烁5次。重复此过程。
#include <reg52.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned charvoid delay1s(void);
uchar status,s1;void main()
{
while (1)
{
P0=0xFE;
delay1s();
for(status=1; status<8; status++)
{
P0=_crol_(P0,1);
delay1s();
}
//P0=0x7F;
for(status=7; status>0; status–)
{
P0=_cror_(P0,1);
delay1s();
}
for(status=0; status<5; status++)
{
P0=0x00;
delay1s();
P0=~P0;
delay1s();
}
}
}void delay1s(void)
{
uchar a,b,c;
for(c=46;c>0;c–)
for(b=152;b>0;b–)
for(a=70;a>0;a–);
_nop_(); //if Keil,require use intrins.h
}
04/09/2011
C51 Programming Cont.
By dch1 in Just Notes No Comments
今天终于学习了一直以来想学的中断功能,也是单片机中三大核心之一(定时器、中断、串口通信)
还是挺好玩的,下面还是一些课后习题:
1、利用定时/计数器T0从P1.0输出周期为1s的方波,让发光二极管以1HZ闪烁,设晶振频率为12MHz。
另外,如果需要定时器1来执行,则需要这么写:
2、利用定时/计数器T1产生定时时钟,由P1口控制8个发光二极管,使8个指示灯依次一个一个闪动,闪动频率为10次/秒(8个灯依次亮一遍为一个周期),循环。
3、同时用两个定时器控制蜂鸣器发声,定时器0控制频率,定时器1控制同个频率持续的时间,间隔2s依次输出1,10,50,100,200,400,800,1k(hz)的方波。
这道题费的时间比较长,可能是没有完全掌握双定时器优先级的缘故,搞了一晚上,除了这道题,其余三道题基本上都是十几分钟内就搞定的。
PS: 暂时先放弃这道题吧…………
4、用定时器以间隔500MS在6位数码管上依次显示0、1、2、3….C、D、E、F,重复。