1.位移法控制程序:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
void delayn50ms(uint n)
{
uint i,j;
for(j=0;j<n;j++)
for(i=0;i<=10000;i++);
}
main()
{
uchar k,d;
while(1)
{
d=0xfe;
for(k=0;k<8;k++)
{
P2=d;
d=d<<1|0x01;
delayn50ms(5);
}
}
}
2.数组法:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar code tab[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
- void delayn50ms(uint n)
- {
- uint i,j;
- for(j=0;j<n;j++)
- for(i=0;i<=10000;i++);
- }
- main()
- {
- uchar k;
- while(1)
- {
- for(k=0;k<8;k++)
- {
- P2=tab[k];
- delayn50ms(5);
- }
- }
- }
复制代码
|