单片机课程设计题目全彩LED花样流水灯学院名称电气工程学院指导教师朱卫华班级电子1001班学号20104470126学生姓名施国梁2013年12月30日前言随着人们生活环境的不断改善和美化,在许多场合可以看到彩色霓虹灯不断变化闪烁。LED灯由于其丰富的灯光色彩,低廉的造价以及控制简单等特点而得到了广泛的应用,用彩灯来装饰街道和城市建筑物已经成为一种时尚。但目前市场上各式样的LED灯控制器大多数用全硬件电路实现,电路结构复杂、功能单一,这样一旦制作成品只能按照固定的模式闪亮,不能根据不同场合、不同时间段的需要来调节亮灯时间、模式、闪烁频率等动态参数。这种彩灯控制器结构往往有芯片过多、电路复杂、功率损耗大等缺点。此外从功能效果上看,亮灯模式少而且样式单调,缺乏用户可操作性,影响亮灯效果。因此有必要对现有的彩灯控制器进行改进。流水灯是一串按一定的规律像流水一样连续闪亮。流水灯控制是可编程控制器的一个应用,其控制思想在工业控制技术领域也同样适用。流水灯控制可用多种方法实现,但对现代可编程控制器而言,利用移位寄存器实现最为便利。通常用左移寄存器实现灯的单方向移动;用双向移位寄存器实现灯的双向移动。本案例利用价格低廉的AT89C51系列单片机控制基色LED灯泡从而实现丰富的变化。1.课程设计的目的和要求1.1设计目的近年来随着科技的发展,单片机的应用正在不断走向深入,同时带动传统控制检测日新月异更新,在实时检测和自动控制的单片机应用系统中,单片机往往是作为一个核心部件来使用,单片机方面知识是不够的,还应根据具体硬件结构,以及针对具体应用对象点的软件结合,加以完善。流水灯,可以更简单、方便的使用。通过本课程设计使学生进一步巩固单片机原理及应用的基本概念、基本理论,分析问题的基本方法,增强系统地运用已学的理论知识解决实际问题的能力和查阅资料的能力。培养一定的自学能力和独立分析问题、解决问题的能力,能通过独立思考、查阅工具书、参考文献,寻找解决方案。1.2设计要求设计流水灯的基本要求:设计一个流水灯系统,应用AT89C51试验系统,电路开启后红、绿和蓝三种颜色的灯在时钟信号作用下按一定规律转换且循环状态。2.硬件总体设计2.1.设计思想整个系统工作由软件程序控制运行,根据需要,可以上电后系统经过初始化,进入用户设定模式状态。基于AT89C51单片机的彩灯控制方案,实现对LED彩灯的控制。本方案以AT89C51单片机作为主控核心,与驱动等模块组成核心主控制模块。在主控模块上设有晶振电路和16个LED显示二极管,根据需要编写若干种亮灯模式,根据各种亮灯时间的不同需要,在不同时刻输出灯亮或灯灭的控制信号。2.2模块分析(1)核心部件89C51单片机是整个彩灯循环系统的核心是控制彩灯循环闪烁等等一切功能的部件;其中内部有ROM、有RAM、有并行I/O口等,在51单片机内部有一个CPU用来运算、控制,有四个并行I/O口,分别是P0、P1、P2、P3,有ROM,用来存放程序,有RAM,用来存放中间结果,此外还有定时/计数器,串行I/O口,中断系统,以及一个内部的时钟电路。(2)复位系统电路中C1、R2……组成复位电路,它的作用是将单片机内部特殊功能寄存器和端口寄存器恢复到初始状态,从内部FLASH存储器的初始状态开始执行(3)震荡系统单片机本身如同一个复杂的同步时序电路,为了保证同步工作,电路应在唯一的时钟信号控制下,严格地按规定时序工作。而时钟电路就用于产生单片机工作所需要的时钟信号。(4)闪烁系统利用单片机的P口控制:LED的发光闪烁,再利用编程实现流水灯的电路,用软件来实现对LED的控制,每个LED接一个限流电阻,来控制流入LED发光管的电流。图3-1-1硬件系统方框图复位电路AT89C51核心部件闪烁系统震荡系统2.2软件总体设计用查表的方法控制点亮流水灯,即移位的思想。以下是部分程序(C):0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,//一个灯亮(从左到右)扫描到P0口1行0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x00,//P0--》P3从两边到中间逐个点亮部分的P0逐个点亮0x83,0x83,0x83,0xC1,0xC1,0xC1,0xE0,0xE0,0xE0,0x70,0x70,0x70,0x38,0x38,0x38,0x1C,0x1C,0x1C,0x0E,0x0E,0x0E,0x07,0x07,0x07,0x83,0x83,0x83,0xC1,0xC1,0xC1,0xE0,0xE0,0xE0,0x70,0x70,0x70,0x38,0x38,0x38,0x1C,0x1C,0x1C,0x0E,0x0E,0x0E,0x07,0x07,0x07,0x83,0x83,0x83,0xC1,0xC1,0xC1,0xE0,0xE0,0xE0,0x70,0x70,0x70,0x38,0x38,0x38,0x1C,0x1C,0x1C,0x0E,0x0E,0x0E,0x07,0x07,0x07,0x83,0x83,0x83,0xC1,0xC1,0xC1,0xE0,0xE0,0xE0,0x70,0x70,0x70,0x38,0x38,0x38,0x1C,0x1C,0x1C,0x0E,0x0E,0x0E,0x07,0x07,0x07,//5个灯一起滚动亮从右到左105行0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xff,0xff,0x00,0x00,0xff,0xff,0x00,0x00,//全亮灭几次110行0x7e,0x7e,0x7e,0xfc,0xfc,0xfc,0xf9,0xf9,0xf9,0xf3,0xf3,0xf3,0xe7,0xe7,0xe7,0xcf,0xcf,0xcf,0x9f,0x9f,0x9f,0x3f,0x3f,0x3f,0x7e,0x7e,0x7e,0xfc,0xfc,0xfc,0xf9,0xf9,0xf9,0xf3,0xf3,0xf3,0xe7,0xe7,0xe7,0xcf,0xcf,0xcf,0x9f,0x9f,0x9f,0x3f,0x3f,0x3f,0x7e,0x7e,0x7e,0xfc,0xfc,0xfc,0xf9,0xf9,0xf9,0xf3,0xf3,0xf3,0xe7,0xe7,0xe7,0xcf,0xcf,0xcf,0x9f,0x9f,0x9f,0x3f,0x3f,0x3f,//两个灯一起滚动亮从左到右119行0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,0x7e,0x7d,0x7b,0x77,0x6f,0x5f,0x3f,0x3f,0x3f,0x3f,0x3e,0x3d,0x3b,0x37,0x2f,0x1f,0x1f,0x1f,0x1f,0x1e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FD,0xFD,0xFB,0xFB,0xF7,0xF7,0xEF,0xEF,0xDF,0xDF,0xBF,0xBF,0x7F,0x7F,0x7E,0x7E,0x7D,0x7D,0x7B,0x7B,0x77,0x77,0x6F,0x6F,0x5F,0x5F,0x3F,0x3F,0xBE,0xBE,0xBE,0xBD,0xBD,0xBB,0xBB,0xB7,0xB7,0xAF,0xAF,0x9F,0x9F,0x5F,0x5F,0xDF,0xDF,0xDE,0xDE,0xDD,0xDD,0xDB,0xDB,0xD7,0xD7,0xCF,0xCF,0xAF,0xAF,0xEF,0xEF,0xEE,0xEE,0xED,0xED,0xEB,0xEB,0xE7,0xE7,0xD7,0xD7,0xB7,0xB7,0x77,0x77,0xF7,0xF7,0xF6,0xF6,0xF5,0xF5,0xF3,0xF3,0xEB,0xEB,0xDB,0xDB,0xBB,0xBB,0x7B,0x7B,0xFB,0xFB,0xFA,0xFA,0xF9,0xF9,0xF5,0xF5,0xED,0xED,0xDD,0xDD,0xBD,0xBD,0x7D,0x7D,0xFD,0xFD,0xFD,0xFD,0xFB,0xFA,0xF6,0xF6,0xEE,0xEE,0xDE,0xDE,0xBE,0xBE,0x7E,0x7E,0xFE,0xFE,0xFF,0xFF,0xFF,//推动159行由于程序繁杂后面程序内容省略······3、硬件设计3.1AT89C51单片机的硬件结构由图2.1可以看出,单片机内部主要包含下列几个部件:一个8位CPU;一个时钟电路;4Kbyte程序存储器;128byte数据存储器;两个16位定时/计数器;64Kbyte扩展总线控制电路;四个8-bit并行I/O端口;一个可编程串行接口;五个中断源,其中包括两个优先级嵌套中断。AT89C51硬件结构框图3.2主要特性AT89C51