点亮LED灯(一)
来源:互联网 发布:校园网mac地址修改 编辑:程序博客网 时间:2024/05/02 01:52
想当初都是用汇编来编写单片机的程序,那滋味现在想来都是满满的回忆,还是用C语言写起来爽快啊!
用C写单片机程序,只需要引入头文件<reg51.h>即可,实际上该头文件只是定义了特殊功能寄存器地址。
为了今后使用方便,我们封装一个函数,用来开关LED灯,通过参数来控制操作的P端口,并且引入掩码来操作单个和多个bit位。
本例程能达到逐个打开LED然后逐个关闭的效果。
#include <reg51.h>typedef unsigned char uchar; typedef unsigned char uint8; typedef unsigned int uint16; void LedOn(uint8 Px, uint8 mask){uint8 tmp;switch (Px){case 0:tmp = P0 & 0xff;P0 = mask | tmp;break;case 1:tmp = P1 & 0xff;P1 = mask | tmp;break;case 2:tmp = P2 & 0xff;P2 = mask | tmp;break;case 3:tmp = P3 & 0xff;P3 = mask | tmp;break;}}void LedOff(uint8 Px, uint8 mask){uint8 tmp;switch (Px){case 0:tmp = P0;P0 = (~mask) & tmp;break;case 1:tmp = P1;P1 = (~mask) & tmp;break;case 2:tmp = P2;P2 = (~mask) & tmp;break;case 3:tmp = P3;P3 = (~mask) & tmp;break;}}void Delayms(uint16 ms){uint8 i;while(ms--){for(i = 0; i < 120; i++);}}void main(){int i;while (1){for (i = 0; i < 8; i++){LedOn(0, 0x01 << i);Delayms(100);}for (i = 0; i < 8; i++){LedOff(0, 0x01 << i);Delayms(100);}}}
0 0
- 点亮LED灯(一)
- TQ210的裸机编程(一)点亮一个LED灯
- TQ210_裸机编程(一)——点亮LED灯
- 8051单片机学习篇(一)-----点亮led灯
- (C51学习一)如何点亮led灯
- 如何点亮一盏LED灯
- s5pv210学习<一>点亮LED灯
- ARM11裸机开发(一)---点亮LED
- STC89C52单片机学习(一)----点亮LED
- stm32学习笔记(一)点亮LED
- 1.1 zigbee基础(点亮LED灯)
- Arduino学习(三)点亮LED灯
- STM32-点亮LED灯(GPIO配置)
- 树莓派学习二(点亮LED灯)
- ATMEGA8A 点亮LED灯
- OK6410 点亮LED灯
- 点亮LED灯
- 点亮一个LED灯
- POJ 3280 Cheapest Palindrome 【区间DP】
- jsp与servlet之间页面跳转及参数传递实例
- linux下串口的操作
- HDOJ-----4607
- Roads in the North
- 点亮LED灯(一)
- PHP运行模式
- 【HDU】3579 - Hello Kiki(CRT)
- 用bootstrap 制作一个简洁美观的页面
- Roads in the North(树的直径)
- 126.Identify the two situations in which you use the alert log file in your database to check the de
- ubuntu1604快速源
- 分享一个很牛逼的学习网站
- HelloSpringMVC