【51单片机】实用代码 第一周,点亮LED
相信大家也是刚刚购买51单片机,我这边是最便宜的AT89C52RC。不过现在单片机也是十分便宜,这边就不多叙述了。
基本上一周学习一个模块,会持续更新。
1.点亮LED(没什么好叙述的,简单的课题)
#include //AT89C52RS基本都是低电平触发void main()
{while(1){P2=0xFE; //1111 1110}
}
2.单LED闪烁
#include
#include //为了使_nop_();正常使用void Delay500ms() //大家可以在小程序直接转换
{unsigned char i, j, k;_nop_();i = 4;j = 205;k = 187;do{do{while (--k);} while (--j);} while (--i);
}void main()
{while(1){P2=0xFE; //1111 1110Delay500ms();P2=0xFF; //1111 1111Delay500ms();}
}
3.流水灯
- 简易
#include #define uint unsigned int //用uint代表unsigned int
#define uchar unsigned charvoid delay( ) //数值可以更改,简单的循环语句
{uchar i,j;for(i=0;i<255;i++)for(j=0;j<255;j++);
}void main()
{while(1) //和前面单的一样无脑放就完事{P2=0xFE;//1111 1110Delay();P2=0xFD;//1111 1101Delay();P2=0xFB;//1111 1011Delay();P2=0xF7;//1111 0111Delay();P2=0xEF;//1110 1111Delay();P2=0xDF;//1101 1111Delay();P2=0xBF;//1011 1111Delay();P2=0x7F;//0111 1111Delay();}
}
2.intrins.h 运用函数
#include
#include //_crol_和_crol_的头文件#define uint unsigned int //用uint代表unsigned int
#define uchar unsigned char // //用uchar代表unsigned charuchar temp;void delay( )
{uint x,y;for(x = z;x > 0;x--)for(y = 114;y > 0;y--);
}void main()
{temp = 0xfe; //1111 1110P2 = temp;delay(100);//循环往左移while(1){temp = _crol_(temp, 1);//循环往左移一位,变为1111 1101P2 = temp; delay();}//循环往右移while(1){temp = _cror_(temp, 1);//循环往右移一位,变为0111 1111P2 = temp; delay();}
}
3.数组(和前面简易的差不多,都是套用)
#include #define uchar unsigned char
#define uint unsigned intuchar code shuzu[8] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
//这个是数组,shuzu可以改变名字,共阴极触发,大家可以去了解一下电路图void delay( ) //数值可以更改,简单的循环语句
{uchar i,j;for(i=0;i<255;i++)for(j=0;j<255;j++);
}void main()
{uchar i;while(1){for(i=0;i<8;i++) //流水灯只有八个口{P2 = shuzu[i];delay( );}}
}
4.定时器流水灯(女朋友叫我睡觉了,有空再补)
最后,基本就是这些了,对于流水灯就两点
1.低电平激活 2.好的延时函数
对于我目前的学习而言:
1中就数组,单独列,函数(勉强算是)。
2中简单的delay,定时器等。
这篇文章纯属借鉴参考,如有雷同,纯属巧合
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
