用C语言编写单片机由16个LED灯组成的爱心心跳

时间: 2024-02-19 07:58:47 浏览: 22
以下是用C语言编写单片机由16个LED灯组成的爱心心跳的示例: ```c #include <reg51.h> sbit LED1 = P1^0; sbit LED2 = P1^1; sbit LED3 = P1^2; sbit LED4 = P1^3; sbit LED5 = P1^4; sbit LED6 = P1^5; sbit LED7 = P1^6; sbit LED8 = P1^7; sbit LED9 = P2^0; sbit LED10 = P2^1; sbit LED11 = P2^2; sbit LED12 = P2^3; sbit LED13 = P2^4; sbit LED14 = P2^5; sbit LED15 = P2^6; sbit LED16 = P2^7; // 定义8个爱心的图案 unsigned char code Heart[8][8] = { {0x00, 0x00, 0x1C, 0x3E, 0x7C, 0xF8, 0xF0, 0xE0}, {0x00, 0x00, 0x1C, 0x3E, 0x7C, 0xF8, 0xF0, 0xE0}, {0x00, 0x00, 0x38, 0x7C, 0xFE, 0xFE, 0xFC, 0xF8}, {0x00, 0x00, 0x70, 0xF8, 0xFE, 0xFE, 0xFC, 0xF8}, {0x00, 0x01, 0xE0, 0xF0, 0xFC, 0xFE, 0xFE, 0xFC}, {0x00, 0x03, 0xC0, 0xE0, 0xF8, 0xFC, 0xFE, 0xFC}, {0x00, 0x07, 0x80, 0xC0, 0x7C, 0xFE, 0xFE, 0x7C}, {0x00, 0x0F, 0x00, 0x80, 0x3E, 0xFE, 0xFE, 0x38} }; void delay(unsigned int t) { unsigned int i, j; for(i = 0; i < t; i++) { for(j = 0; j < 100; j++); } } void main() { unsigned int i, j, k; unsigned char row = 0xFE; // 初始显示第一列 while(1) { for(i = 0; i < 8; i++) { for(k = 0; k < 8; k++) { if(Heart[i][k] & 0x80) LED1 = 1; else LED1 = 0; if(Heart[i][k] & 0x40) LED2 = 1; else LED2 = 0; if(Heart[i][k] & 0x20) LED3 = 1; else LED3 = 0; if(Heart[i][k] & 0x10) LED4 = 1; else LED4 = 0; if(Heart[i][k] & 0x08) LED5 = 1; else LED5 = 0; if(Heart[i][k] & 0x04) LED6 = 1; else LED6 = 0; if(Heart[i][k] & 0x02) LED7 = 1; else LED7 = 0; if(Heart[i][k] & 0x01) LED8 = 1; else LED8 = 0; if(Heart[i][k] & 0x80) LED9 = 1; else LED9 = 0; if(Heart[i][k] & 0x40) LED10 = 1; else LED10 = 0; if(Heart[i][k] & 0x20) LED11 = 1; else LED11 = 0; if(Heart[i][k] & 0x10) LED12 = 1; else LED12 = 0; if(Heart[i][k] & 0x08) LED13 = 1; else LED13 = 0; if(Heart[i][k] & 0x04) LED14 = 1; else LED14 = 0; if(Heart[i][k] & 0x02) LED15 = 1; else LED15 = 0; if(Heart[i][k] & 0x01) LED16 = 1; else LED16 = 0; delay(1); // 延时1ms } } delay(10); // 延时10ms } } ``` 这段代码使用了16个LED灯来显示16个爱心,每个爱心由8行8列的点阵组成。每隔10ms,LED灯会显示下一列的点阵,直到显示完16列后重新开始。

相关推荐

最新推荐

recommend-type

单片机C语言程序设计:8X8LED 点阵显示数字

名称:按键控制 8X8LED 点阵屏显示图形 说明:每次按下 K1 时,会使 8X8LED点阵屏循环显示不同图形。本例同时使用外部中断和定时中断。
recommend-type

单片机C语言程序设计:T0控制LED实现二进制计数

说明:本例对按键的计数没有使用查询法,没有使用外部中断函数,没有使用定时或计数中断函数。而是启用了计数器,连接在 T0 引脚的按键每次按下时,会使计数寄存器的值递增,其值通过 LED以二进制形式显示
recommend-type

使用C语言编写圣诞表白程序

圣诞节快到了,让我们用C语言制作一个圣诞表白程序吧,下面通过本文学习下实现代码
recommend-type

红绿灯原理C语言源代码 单片机编写

用AT89C52芯片编写红绿灯原理,其中没有数字显示,只有灯的变化情况。
recommend-type

51单片机DIY制作实例:旋转LED数字显示电子钟(含C语言源程序)

该文是一篇基于51单片机的DIY制作详述,作者此时是51单片机初学者,这个制作也可以作为不少单片机学习者的练手实验,如进行多样衍生会得到不错的设计。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。