“TI”杯电子设计竞赛D题声音定位系统
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
2012年“TI”杯辽宁省电子设计竞赛声音定位系统(D题)
学校:
参赛队员:
目录
1.1主要任务 .....................................................................................................................
1.2设计要求 .....................................................................................................................
1.2.1基本要求...............................................................................................................
1.2.2发挥部分............................................................................................................... 2方案设计与论证...............................................................................................................
2.1方案对比 .....................................................................................................................
2.1.1声源模块...............................................................................................................
2.1.2接收模块放大芯片...............................................................................................
2.1.3单片机...................................................................................................................
2.1.4显示模块...............................................................................................................
2.1.5定位算法选择....................................................................................................... 3硬件部分设计...................................................................................................................
3.1系统框图 .....................................................................................................................
3.2声响模块 .....................................................................................................................
3.3从单片机最小系统 .....................................................................................................
3.4声音接收模块 .............................................................................................................
3.5信号处理模块 .............................................................................................................
4软件部分设计...................................................................................................................
4.1软件流程图 .................................................................................................................
4.2中断流程图 .................................................................................................................
5测试结果与分析...............................................................................................................
5.1测试方法与仪器 .........................................................................................................
5.2测试数据 .....................................................................................................................
5.3测试结果分析 .............................................................................................................
5.4测试结果 .....................................................................................................................
参考文献..............................................................................................................................
附录A ..................................................................................................................................
附录B ..................................................................................................................................
附录C...................................................................................................................................
摘要
本设计采用TI公司生产的超低功耗单片机MSP430G2452和G2553分别作为定位系统的声源产生模块和信息处理模块,实现了声音信号的发生、信号收集处理和屏幕显示以及语音提示等功能。
该设计采用2节家用1.5V电池供电,实现了低功耗状况下的声音定位系。
声源接收单元信号反应灵敏,能较准确的得到信号并及时传送到信号处理单元。
以MSP430为主的Launchpad也符合了低功耗的标准,并且可以实现信号的运算处理以及控制LCD液晶屏幕显示测试数据。
在定位算法上采用的是归一正方形算法。
通过调试修正以后,能实现基本的声源定位并显示。
关键词:声音定位,低功耗,MSP430
1概述
1.1主要任务
设计一套声音定位系统。
在一块不大于1m2的平板上贴一张500mm×350mm的坐标纸,在其四角外侧分别固定安装一个声音接收模块,声音接收模块通过导线将声音信号传输到信息处理模块,声音定位系统根据声响模块通过空气传播到各声音接收模块的声音信号,判定声响模块所在的位置坐标。
系统结构示意图如图1.1所示。
图1.1声音定位系统结构示意图
1.2设计要求
1.2.1基本要求
(1)设计制作一个声响模块,含信号产生电路、放大电路和微型扬声器等,每按键一次发声一次,声音信号的基波频率为500Hz左右,声音持续时间约为1s。
要求声响模块采用3V以下电池供电,功耗不大于200mW。
(2)设计制作四路声音接收模块,由麦克风、放大电路等组成,并分别与信息处理模块相连接,以便将频率为500Hz左右的信号传送至信息处理模块。
(3)设计制作一个信息处理模块,要求该模块能根据从声音接收模块传来的信号判断声响模块所在位置的x、y坐标,并以数字形式显示x、y坐标值,位置坐标值误差的绝对值不大于30mm。
1.2.2发挥部分
(1)改善接收信号的放大电路性能,改进算法,进一步提高定位精度。
(2)控制声响模块以不间断的连续周期波的形式发出声音信号,其基波频率为500Hz 左右。
当声响模块在坐标纸上移动时,声音定位系统能连续跟踪显示声响模块的x、y
坐标值,随机停止声响模块的移动,能立即稳定地显示声响模块的x、y坐标值,误差的绝对值不大于10mm。
(3)具有显示声响模块移动轨迹的功能。
当声响模块在坐标纸上按指定路径移动时,液晶显示屏能动态显示声响模块移动的轨迹,显示的轨迹与声响模块移动的路径一致。
(4)其它。
2方案设计与论证
2.1方案对比
本设计分以下几个模块组成:声响模块、声音接收模块、信号处理模块。
现对模块的选用方案进行论证。
2.1.1声源模块
设计要求声响模块采用3V以下电池供电,功耗不大于200mW。
方案一:蜂鸣器
蜂鸣器有源和无源的区别:这里的“源”不是指电源。
而是指震荡源。
也就是说,有源蜂鸣器内部带震荡源,所以只要一通电就会叫。
而无源内部不带震荡源,所以如果用
直流信号无法令其鸣叫。
必须用2K~5K的方波去驱动它。
有源蜂鸣器往往比无源的贵,就是因为里面多个震荡电路。
方案三:喇叭
8欧0.5瓦喇叭价格实惠,声音响亮,声音纯度较高,并且完全满足本次设计的需求,故本次采用的是喇叭作为发声源。
综上所述,选取便宜可控的喇叭更符合设计需要。
2.1.2接收模块放大芯片
方案一:LM358
LM358内部包括有两个独立的、高增益、内部频率补偿的双运算放大器,适合于电源电压范围很宽的单电源使用,也适用于双电源工作模式,在推荐的工作条件下,电源电流与电源电压无关。
它的使用范围包括传感放大器、直流增益模块和其他所有可用单电源供电的使用运算放大器的场合。
LM358引脚如图2.1所示。
图2.1LM358结构图
方案二:LM386
LM386是美国国家半导体公司生产的音频功率放大器,主要应用于低电压消费类产品。
为使外围元件最少,电压增益内置为20。
但在1脚和8脚之间增加一只外接电阻和电容,便可将电压增益调为任意值,直至200。
输入端以地为参考,同时输出端被自动偏置到电源电压的一半,在6V电源电压下,它的静态功耗仅为24mW,使得LM386特别适用于电池供电的场合。
LM286引脚如图2.2所示。
图2.2LM386管脚图
方案三:LM393
LM393是高增益,宽频带器件,象大多数比较器一样,如果输出端到输入端有寄生电容而产生耦合,则很容易产生振荡。
这种现象仅仅出现在当比较器改变状态时,输出电压过渡的间隙,电源加旁路滤波并不能解决这个问题,标准PC板的设计对减小输入—输出寄生电容耦合是有助的。
减小输入电阻至小于10K将减小反馈信号,而且增加甚至很小的正反馈量(滞回1.0~10mV)能导致快速转换,使得不可能产生由于寄生电容引起的振荡,除非利用滞后,否则直接插入IC并在引脚上加上电阻将引起输入—输出在很短的转换周期内振荡,如果输入信号时脉冲波形,并且上升和下降时间相当快,则滞回将不需要。
LM293引脚如图2.3所示。
图2.3LM393管脚图
由于接收信号微弱,要得到更精准的声源信号,选取了工作电源范围更宽、输入失调电压更小的LM393。
2.1.3单片机
方案一:MSP430
MSP430系列单片机是美国德州仪器公司(TI)1996年开始推向市场的一种16位超低功耗、具有精简指令集的混合信号处理器。
称之为混合信号处理器,是由于其针对实际应用需求,将多个不同功能的模拟电路、数字电路模块和微处理器集成在一个芯片上,以提供“单片机”解决方案。
该系列单片机多应用于需要电池供电的便携式仪器仪表中。
方案二:AT89C51
AT89C51是一种带4K字节FLASH存储器的低电压、高性能CMOS8位微处理器,俗称单片机。
AT89C2051是一种带2K字节闪存可编程可擦除只读存储器的单片机。
单片
机的可擦除只读存储器可以反复擦除1000次。
该器件采用ATMEL高密度非易失存储器制造技术制造,与工业标准的MCS-51指令集和输出管脚相兼容。
由于将多功能8位CPU 和闪烁存储器组合在单个芯片中,ATMEL的AT89C51是一种高效微控制器,AT89C2051
是它的一种精简版本。
AT89C51单片机为很多嵌入式控制系统提供了一种灵活性高且价廉的方案。
设计需要2片MCU,都采用TI公司提供的MSP430G2xxx系列芯片。
其中声源产生模块选用以MSP430G2452为核心的单片机最小系统;信息处理系统采用MSP430G2553为主的Launchpad作为数据处理和显示模块控制。
2.1.4显示模块
LCD12864为带中文字库的128X64是一种具有4位/8位并行、2线或3线串行多种接口方式,内部含有国标一级、二级简体中文字库的点阵图形液晶显示模块。
其显示分辨率为128×64,内置8192个16*16点汉字,和128个16*8点ASCII字符集.利用该模块灵活的接口方式和简单方便的操作指令,可构成全中文人机交互图形界面。
可以显示8×4行16×16点阵的汉字.也可完成图形显示.低电压低功耗是其又一显着特点。
由该模块构成的液晶显示方案与同类型的图形点阵液晶显示模块相比,不论硬件电路结构或显示程序都要简洁得多,且该模块的价格也略低于相同点阵的图形液晶模块。
2.1.5定位算法选择
方案一:直角坐标系法。
直接采用三个点定位的方法,利用函数关系式算出坐标。
如图2.4所示。
图2.4直角坐标系法测距
根据距离不同,接收声源就会产生时间差,两个时间差,两个变量,故可以求出(x,y)的坐标值。
-
其中c=340m/s。
这种方法虽然思路简单,但是运算量大,将会影响单片机运算速度,增加我们的运算难度,故不采用。
方案二:归一化正方形阵。
如图2.5所示。
图2.5归一化正方形
传感器阵列采用正方形,传感器的位置坐标为(L,L),(-L,L),(-L,-L),(L,-L)(L,-L)。
则声源位置(x,y)可由下式算出:
(2.2)
式中2L—正方阵的边长,Δt1,Δt2,Δt3分别为传感器2,3,4相对于1的时差,c 是声音传播速度。
3硬件电路设计
3.1系统框图
系统主要由三大模块组成:声响模块、声音接收模块、信号处理模块。
声响模块产生声音信号,由声音接收模块接收信号并产生电流脉冲,经总线系统传送到信号处理模块进行运算处理,将测试数据显示在LCD显示屏上。
系统框图如图3.1所示。
图3.1系统总框图
系统总电路图见附录二。
3.2声响模块
通过单片机产生500Hz的方波信号,利用单管放大后给喇叭发声。
其中电位器可对音量进行调节。
声响电路如图3.2所示。
图3.2声响模块电路图
3.3从单片机最小系统
单片机最小系统采用的是TI公司生产的超低功耗的MSP430G2452单片机为核心,其中9号端口为P2.1,实际电路中需和声源模块相连接。
从单片机最小系统如图3.3所示。
图3.3从单片机最小系统
3.4声音接收模块
声音就收模块采用LM393电位比较器设计,即接收到信号就会输出高电平,否则输出低电平。
LM393电路如图3.4所示。
图3.4接收系统模块电路
3.5信号处理模块
信号处理模块由总线控制单元,MSP430G2553为核的Launchpad以及LCD显示屏共同组成。
单片机内部运算处理后将结果送入LCD显示。
信号处理电路如图3.5所示。
图3.5主单片机及显示
4软件部分设计
4.1软件流程图
设计采用MSP430G2553为核心的Launchpad负责信号的处理并控制LCD显示屏显示相应数据。
主程序流程图如图4.1所示。
图4.1主程序流程
4.2中断流程图
中断程序开始后,系统首先对声音接收模块采集到的信息进行判断,找到距离声源最近的接收器,再按时间差分别得出其它3个接收器的距离差,再通过单片机就可以计算出声源所在位置,发出屏幕显示数据后退出中断。
中断流程如图4.2所示。
图4.2中断程序流程
5测试结果与分析
5.1测试方法与仪器
先测试4个定点(分别为(10,10)(45,10)(10,25)(45,25))来标定4个声音接收模块的精度等级,然后采取随机取点方式得到测试数据。
测试中主要用到的仪器有直流稳压电源,示波器等。
5.2测试数据
测试结果见表5.1.
测试平均误差在5cm以内,部分坐标点能达到3cm标准,符合设计要求。
5.4测试结果
本设计制作完成了题目要求的基本部分的全部要求和发挥部分的大部分要求,达到设计要求。
个别指标由于时间有限只完成相应的软件和硬件设计,整体调试还未能全部完成。
参考文献
[1]范红刚,魏学海,陈和.51单片机自学笔记;北京,北京航空航天大学出版社2010年1月第1版
[2]王宜怀,曹金华.嵌入式系统设计实战;北京,北京航空航天大学出版社2011年5月第1版
[3]闻怡洋.无线设备编程指南;北京,北京大学出版社2004年7月第1版
附录A
附录B
系统总电路图
附录C
程序源代码:
#include"msp430g2553.h"
#include"math.h"
#defineCS12864_SETP2OUT|=BIT0
#defineCS12864_CLRP2OUT&=~BIT0
#defineSID12864_SETP2OUT|=BIT1
#defineSID12864_CLRP2OUT&=~BIT1
#defineSCLK12864_SETP2OUT|=BIT2
#defineSCLK12864_CLRP2OUT&=~BIT2
signedints_1=0;
signedints_2=0;
unsignedcharss;
unsignedchartank=0;
unsignedcharstate=0;
//4004
unsignedinttime=0;
unsignedchari;
unsignedinttemp;
#defineISD_SS_SET(P2OUT|=BIT4)
#defineISD_SS_CLR(P2OUT&=~BIT4)
#defineISD_SCK_SET(P2OUT|=BIT3)
#defineISD_SCK_CLR(P2OUT&=~BIT3)
#defineISD_MOSI_SET(P2OUT|=BIT5)
#defineISD_MOSI_CLR(P2OUT&=~BIT5)
//************************************
//ISD4002spi串行发送子程序,8位数据
//************************************
voidspi_send(unsignedcharisdx)
{ unsignedcharisx_counter;
ISD_SS_CLR;//ss=0,打开spi通信端
ISD_SCK_CLR;
for(isx_counter=0;isx_counter<8;isx_counter++)//先发低位再发高位,依次发送。
{ if((isdx&0x01)==1)
ISD_MOSI_SET;
else
ISD_MOSI_CLR;
isdx=isdx>>1;
ISD_SCK_SET;
__delay_cycles(6);
ISD_SCK_CLR;
__delay_cycles(6);
}
}
//********发送掉电指令,并延迟50ms
voidisd_pd(void)//4004
{
__delay_cycles(10000);
spi_send(0x20);
ISD_SS_SET;
__delay_cycles(60000);
spi_send(0x20);
ISD_SS_SET;
__delay_cycles(120000);
}
voidisd_stop()//4004
{
__delay_cycles(1000);
/*spi_send(0);//发送放音起始地址低位
__delay_cycles(3);
spi_send(0);//发送放音起始地址高位
__delay_cycles(3);*/
spi_send(0x10);//发送setplay指令字节
ISD_SS_SET;
}
voidisd_setplay(unsignedcharadl,unsignedcharadh)//4004 {
__delay_cycles(10000);
spi_send(0x20);
ISD_SS_SET;
__delay_cycles(25000);
__delay_cycles(1000);
spi_send(adl);//发送放音起始地址低位
//__delay_cycles(3);
spi_send(adh);//发送放音起始地址高位
//__delay_cycles(3);
spi_send(0xE0);//发送setplay指令字节
ISD_SS_SET;
__delay_cycles(20);
spi_send(0xF0);//发送setplay指令字节
ISD_SS_SET;
}
voidisd_rec(unsignedcharadl,unsignedcharadh)//4004//4004 {
__delay_cycles(10000);
spi_send(0x20);
ISD_SS_SET;
__delay_cycles(25000);
isd_stop();
spi_send(0x20);
ISD_SS_SET;
__delay_cycles(50000);
__delay_cycles(1000);
spi_send(adl);//发送放音起始地址低位
spi_send(adh);//发送放音起始地址高位
spi_send(0xa0);//发送setplay指令字节
ISD_SS_SET;
__delay_cycles(20);
spi_send(0xb0);//发送setplay指令字节
ISD_SS_SET;
}
constunsignedchars1[51][36]={
108,108,107,106,105,105,104,103,103,102,101,101,100,99,99,98,97,97,96,95,95,94,94, 93,93,92,92,91,91,90,90,89,89,89,88,88,106,106,105,105,104,103,103,102,101,101,100,99,9 9,98,97,97,96,96,95,94,94,93,93,92,92,91,91,90,90,89,89,88,88,88,87,87,105,104,104,103,1 02,102,101,100,100,99,99,98,97,97,96,95,95,94,94,93,93,92,92,91,91,90,90,89,89,88,88,87, 87,87,86,86,103,102,102,101,101,100,100,99,98,98,97,97,96,95,95,94,94,93,93,92,91,91,90, 90,89,89,89,88,88,87,87,86,86,86,85,85,101,101,100,100,99,99,98,97,97,96,96,95,95,94,93, 93,92,92,91,91,90,90,89,89,88,88,87,87,87,86,86,85,85,85,84,84,99,99,98,98,97,97,96,96,95 ,95,94,94,93,93,92,91,91,90,90,89,89,88,88,88,87,87,86,86,85,85,85,84,84,84,83,83,97,97,9 6,96,96,95,95,94,94,93,93,92,92,91,91,90,90,89,89,88,88,87,87,86,86,86,85,85,84,84,84,83, 83,82,82,82,95,95,95,94,94,93,93,93,92,92,91,91,90,90,89,89,88,88,87,87,86,86,86,85,85,84 ,84,84,83,83,82,82,82,81,81,81,93,93,93,92,92,92,91,91,90,90,89,89,89,88,88,87,87,86,86,8 5,85,85,84,84,83,83,83,82,82,82,81,81,81,80,80,80,91,91,91,91,90,90,89,89,89,88,88,87,87, 87,86,86,85,85,85,84,84,83,83,83,82,82,81,81,81,80,80,80,79,79,79,79,89,89,89,89,88,88,88 ,87,87,87,86,86,85,85,85,84,84,83,83,83,82,82,82,81,81,81,80,80,80,79,79,79,78,78,78,77,8 8,87,87,87,87,86,86,86,85,85,85,84,84,83,83,83,82,82,82,81,81,81,80,80,80,79,79,79,78,78, 78,77,77,77,77,76,86,85,85,85,85,84,84,84,84,83,83,83,82,82,82,81,81,80,80,80,80,79,79,79 ,78,78,78,77,77,77,77,76,76,76,76,75,84,83,83,83,83,83,82,82,82,81,81,81,81,80,80,80,79,7 9,79,78,78,78,77,77,77,77,76,76,76,76,75,75,75,75,74,74,82,82,81,81,81,81,80,80,80,80,79, 79,79,79,78,78,78,77,77,77,77,76,76,76,76,75,75,75,75,74,74,74,74,73,73,73,80,80,79,79,79 ,79,79,78,78,78,78,77,77,77,77,76,76,76,76,75,75,75,75,74,74,74,74,73,73,73,73,73,72,72,7 2,72,78,78,77,77,77,77,77,77,76,76,76,76,76,75,75,75,75,74,74,74,74,73,73,73,73,73,72,72, 72,72,72,71,71,71,71,71,76,76,76,75,75,75,75,75,75,74,74,74,74,74,73,73,73,73,73,72,72,72 ,72,72,71,71,71,71,71,70,70,70,70,70,70,70,74,74,74,74,73,73,73,73,73,73,72,72,72,72,72,7 2,71,71,71,71,71,71,70,70,70,70,70,70,69,69,69,69,69,69,68,68,72,72,72,72,71,71,71,71,71, 71,71,71,70,70,70,70,70,70,69,69,69,69,69,69,69,68,68,68,68,68,68,68,68,67,67,67,70,70,70 ,70,70,69,69,69,69,69,69,69,69,69,68,68,68,68,68,68,68,68,67,67,67,67,67,67,67,67,66,66,6 6,66,66,66,68,68,68,68,68,68,68,67,67,67,67,67,67,67,67,67,67,66,66,66,66,66,66,66,66,66, 66,65,65,65,65,65,65,65,65,65,66,66,66,66,66,66,66,66,66,65,65,65,65,65,65,65,65,65,65,65 ,65,65,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,63,6 3,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,63,62,62,62,62,62,62,62,62,62,62, 62,62,62,62,62,62,62,62,62,62,62,62,62,62,62,61,61,61,61,61,61,61,61,61,61,61,61,61,61,60 ,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,60,6 0,60,60,60,60,60,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,58,59,59, 59,59,59,59,59,59,59,59,59,59,59,59,56,56,56,56,56,56,56,56,56,56,56,56,57,57,57,57,57,57 ,57,57,57,57,57,57,57,57,57,57,57,57,57,57,57,58,58,58,54,54,54,54,54,54,54,54,54,55,55,5 5,55,55,55,55,55,55,55,55,55,55,56,56,56,56,56,56,56,56,56,56,56,56,56,56,52,52,52,52,52, 52,52,53,53,53,53,53,53,53,53,53,53,54,54,54,54,54,54,54,54,54,54,55,55,55,55,55,55,55,55 ,55,50,50,50,50,50,51,51,51,51,51,51,51,51,51,52,52,52,52,52,52,52,52,53,53,53,53,53,53,5 3,53,54,54,54,54,54,54,48,48,48,48,49,49,49,49,49,49,49,49,50,50,50,50,50,50,51,51,51,51, 51,51,51,52,52,52,52,52,52,52,52,53,53,53,46,46,46,46,47,47,47,47,47,47,48,48,48,48,48,48 ,49,49,49,49,49,49,50,50,50,50,50,50,51,51,51,51,51,51,52,52,44,44,44,45,45,45,45,45,45,4
6,46,46,46,46,47,47,47,47,47,48,48,48,48,48,49,49,49,49,49,50,50,50,50,50,50,50,42,42,43, 43,43,43,43,43,44,44,44,44,44,45,45,45,45,46,46,46,46,47,47,47,47,47,48,48,48,48,48,49,49 ,49,49,49,40,40,41,41,41,41,41,42,42,42,42,43,43,43,43,44,44,44,44,45,45,45,45,46,46,46,4 6,47,47,47,47,47,48,48,48,48,38,38,39,39,39,39,40,40,40,40,41,41,41,41,42,42,42,43,43,43, 43,44,44,44,44,45,45,45,45,46,46,46,46,47,47,47,36,37,37,37,37,37,38,38,38,39,39,39,39,40 ,40,40,41,41,41,42,42,42,43,43,43,43,44,44,44,44,45,45,45,45,46,46,34,35,35,35,35,36,36,3 6,36,37,37,37,38,38,38,39,39,40,40,40,40,41,41,41,42,42,42,43,43,43,43,44,44,44,44,45,32, 33,33,33,33,34,34,34,35,35,35,36,36,37,37,37,38,38,38,39,39,39,40,40,40,41,41,41,42,42,42 ,43,43,43,43,44,31,31,31,31,32,32,32,33,33,33,34,34,35,35,35,36,36,37,37,37,38,38,38,39,3 9,39,40,40,40,41,41,41,42,42,42,43,29,29,29,29,30,30,31,31,31,32,32,33,33,33,34,34,35,35, 35,36,36,37,37,37,38,38,39,39,39,40,40,40,41,41,41,41,27,27,27,28,28,28,29,29,30,30,31,31 ,31,32,32,33,33,34,34,35,35,35,36,36,37,37,37,38,38,38,39,39,39,40,40,40,25,25,25,26,26,2 7,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,34,35,35,36,36,36,37,37,38,38,38,39,39, 39,23,23,24,24,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33,34,34,34,35,35,36 ,36,36,37,37,38,38,38,21,21,22,22,23,23,24,24,25,25,26,26,27,27,28,29,29,30,30,31,31,32,3 2,32,33,33,34,34,35,35,35,36,36,36,37,37,19,19,20,20,21,21,22,23,23,24,24,25,25,26,27,27, 28,28,29,29,30,30,31,31,32,32,33,33,33,34,34,35,35,35,36,36,17,18,18,19,19,20,20,21,22,22 ,23,23,24,25,25,26,26,27,27,28,29,29,30,30,31,31,31,32,32,33,33,34,34,34,35,35,15,16,16,1 7,18,18,19,20,20,21,21,22,23,23,24,25,25,26,26,27,27,28,28,29,29,30,30,31,31,32,32,33,33, 33,34,34,14,14,15,15,16,17,17,18,19,19,20,21,21,22,23,23,24,24,25,26,26,27,27,28,28,29,29 ,30,30,31,31,32,32,32,33,33,12,12,13,14,15,15,16,17,17,18,19,19,20,21,21,22,23,23,24,25,2 5,26,26,27,27,28,28,29,29,30,30,31,31,31,32,32,
};
constunsignedchars2[51][36]={
93,92,90,88,86,84,82,80,78,77,75,73,71,69,67,65,63,61,59,57,55,53,51,49,47,45,43,42, 40,38,36,34,32,30,28,27,93,91,89,87,86,84,82,80,78,76,74,73,71,69,67,65,63,61,59,57,55,53 ,51,49,47,46,44,42,40,38,36,34,33,31,29,27,92,90,89,87,85,83,82,80,78,76,74,72,70,69,67,6 5,63,61,59,57,55,53,51,50,48,46,44,42,40,38,37,35,33,31,30,28,91,90,88,87,85,83,81,80,78, 76,74,72,70,68,67,65,63,61,59,57,55,53,52,50,48,46,44,42,40,39,37,35,33,32,30,29,91,89,88 ,86,84,83,81,79,77,76,74,72,70,68,66,65,63,61,59,57,55,54,52,50,48,46,44,43,41,39,37,36,3 4,32,31,29,90,89,87,86,84,82,81,79,77,75,74,72,70,68,66,65,63,61,59,57,55,54,52,50,48,46, 45,43,41,39,38,36,34,33,31,30,89,88,87,85,83,82,80,79,77,75,73,72,70,68,66,64,63,61,59,57 ,56,54,52,50,48,47,45,43,41,40,38,37,35,33,32,31,89,87,86,84,83,81,80,78,76,75,73,71,70,6 8,66,64,63,61,59,57,56,54,52,50,49,47,45,44,42,40,39,37,36,34,33,31,88,87,85,84,82,81,79, 78,76,75,73,71,69,68,66,64,63,61,59,57,56,54,52,51,49,47,45,44,42,41,39,38,36,35,33,32,88 ,86,85,83,82,80,79,77,76,74,73,71,69,68,66,64,63,61,59,57,56,54,52,51,49,47,46,44,43,41,4 0,38,37,35,34,32,87,86,84,83,82,80,79,77,75,74,72,71,69,67,66,64,62,61,59,58,56,54,53,51, 49,48,46,45,43,41,40,38,37,36,34,33,86,85,84,82,81,80,78,77,75,74,72,70,69,67,66,64,62,61 ,59,58,56,54,53,51,50,48,46,45,43,42,40,39,38,36,35,34,86,85,83,82,81,79,78,76,75,73,72,7 0,69,67,66,64,62,61,59,58,56,54,53,51,50,48,47,45,44,42,41,39,38,37,35,34,85,84,83,81,80, 79,77,76,75,73,72,70,69,67,65,64,62,61,59,58,56,55,53,51,50,48,47,45,44,43,41,40,39,37,36
,35,85,83,82,81,80,78,77,76,74,73,71,70,68,67,65,64,62,61,59,58,56,55,53,52,50,49,47,46,4 4,43,42,40,39,38,37,35,84,83,82,81,79,78,77,75,74,72,71,70,68,67,65,64,62,61,59,58,56,55, 53,52,50,49,48,46,45,43,42,41,39,38,37,36,84,82,81,80,79,78,76,75,74,72,71,69,68,67,65,64 ,62,61,59,58,56,55,53,52,51,49,48,46,45,44,42,41,40,39,38,36,83,82,81,80,78,77,76,75,73,7 2,71,69,68,66,65,64,62,61,59,58,56,55,54,52,51,49,48,47,45,44,43,42,40,39,38,37,83,82,80, 79,78,77,76,74,73,72,70,69,68,66,65,63,62,61,59,58,57,55,54,52,51,50,48,47,46,44,43,42,41 ,40,38,37,82,81,80,79,78,76,75,74,73,71,70,69,67,66,65,63,62,61,59,58,57,55,54,53,51,50,4 9,47,46,45,44,42,41,40,39,38,82,81,80,78,77,76,75,74,72,71,70,69,67,66,65,63,62,61,59,58, 57,55,54,53,51,50,49,48,46,45,44,43,42,40,39,38,81,80,79,78,77,76,75,73,72,71,70,68,67,66 ,65,63,62,61,59,58,57,55,54,53,52,50,49,48,47,45,44,43,42,41,40,39,81,80,79,78,77,75,74,7 3,72,71,69,68,67,66,64,63,62,61,59,58,57,56,54,53,52,51,49,48,47,46,45,43,42,41,40,39, 80,79,78,77,76,75,74,73,72,70,69,68,67,66,64,63,62,61,59,58,57,56,54,53,52,51,50,48, 47,46,45,44,43,42,41,40,80,79,78,77,76,75,74,73,71,70,69,68,67,65,64,63,62,61,59,58,57,56 ,55,53,52,51,50,49,47,46,45,44,43,42,41,40,80,79,78,77,76,74,73,72,71,70,69,68,67,65,64,6 3,62,61,59,58,57,56,55,53,52,51,50,49,48,47,46,44,43,42,41,40,79,78,77,76,75,74,73,72,71, 70,69,68,66,65,64,63,62,61,59,58,57,56,55,54,52,51,50,49,48,47,46,45,44,43,42,41,79,78,77 ,76,75,74,73,72,71,70,69,67,66,65,64,63,62,61,59,58,57,56,55,54,53,51,50,49,48,47,46,45,4 4,43,42,41,78,78,77,76,75,74,73,72,70,69,68,67,66,65,64,63,62,61,59,58,57,56,55,54,53,52, 51,50,48,47,46,45,44,43,42,42,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,59,58 ,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,78,77,76,75,74,73,72,71,70,69,68,67,66,6 5,64,63,62,61,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,77,77,76,75,74,73,72, 71,70,69,68,67,66,65,64,63,62,61,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,43,77 ,76,75,74,74,73,72,71,70,69,68,67,66,65,64,63,62,61,59,58,57,56,55,54,53,52,51,50,49,48,4 7,46,46,45,44,43,77,76,75,74,73,72,71,70,70,69,68,67,66,65,64,63,62,61,59,58,57,56,55,54, 53,52,51,50,50,49,48,47,46,45,44,43,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60 ,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,76,75,75,74,73,72,71,70,69,68,67,6 6,65,64,63,62,61,60,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,45,44,76,75,74,73,73, 72,71,70,69,68,67,66,65,64,63,62,61,60,60,59,58,57,56,55,54,53,52,51,50,49,48,47,47,46,45 ,44,76,75,74,73,72,71,71,70,69,68,67,66,65,64,63,62,61,60,60,59,58,57,56,55,54,53,52,51,5 0,49,49,48,47,46,45,44,75,75,74,73,72,71,70,70,69,68,67,66,65,64,63,62,61,60,60,59,58,57, 56,55,54,53,52,51,50,50,49,48,47,46,45,45,75,74,74,73,72,71,70,69,69,68,67,66,65,64,63,62 ,61,60,60,59,58,57,56,55,54,53,52,51,51,50,49,48,47,46,46,45,75,74,73,73,72,71,70,69,68,6 8,67,66,65,64,63,62,61,60,60,59,58,57,56,55,54,53,52,52,51,50,49,48,47,47,46,45,75,74,73, 72,72,71,70,69,68,67,67,66,65,64,63,62,61,60,60,59,58,57,56,55,54,53,53,52,51,50,49,48,48 ,47,46,45,74,74,73,72,71,71,70,69,68,67,66,66,65,64,63,62,61,60,60,59,58,57,56,55,54,54,5 3,52,51,50,49,49,48,47,46,46,74,73,73,72,71,70,70,69,68,67,66,65,65,64,63,62,61,60,60,59, 58,57,56,55,55,54,53,52,51,50,50,49,48,47,47,46,74,73,73,72,71,70,69,69,68,67,66,65,65,64 ,63,62,61,60,60,59,58,57,56,55,55,54,53,52,51,51,50,49,48,47,47,46,74,73,72,72,71,70,69,6 8,68,67,66,65,64,64,63,62,61,60,60,59,58,57,56,56,55,54,53,52,52,51,50,49,48,48,47,46,74, 73,72,71,71,70,69,68,68,67,66,65,64,64,63,62,61,60,60,59,58,57,56,56,55,54,53,52,52,51,50 ,49,49,48,47,46,73,73,72,71,70,70,69,68,67,67,66,65,64,64,63,62,61,60,60,59,58,57,56,56,5
5,54,53,53,52,51,50,50,49,48,47,47,73,72,72,71,70,70,69,68,67,67,66,65,64,64,63,62,61,60, 60,59,58,57,56,56,55,54,53,53,52,51,50,50,49,48,48,47,73,72,72,71,70,69,69,68,67,66,66,65 ,64,63,63,62,61,60,60,59,58,57,57,56,55,54,54,53,52,51,51,50,49,48,48,47,73,72,71,71,70,6 9,69,68,67,66,66,65,64,63,63,62,61,60,60,59,58,57,57,56,55,54,54,53,52,51,51,50,49,49,48, 47,
};
unsignedcharx_char[8],data[5][20];
unsignedcharlcd_x,lcd_y;
unsignedinty=0,x=0,y1,y2,z=0,j=0;
unsignedchars_x,s_y;
unsignedchartime0,v0=0,v1=0,v2=0,v3=0,v4=0,s;
unsignedinta,b,c;
signedintT[4][4];
voidserial_write_byte(unsignedchardat)
{
unsignedcharss;
for(ss=0;ss<8;ss++)
{
if(dat&0x80)
SID12864_SET;
else
SID12864_CLR;
SCLK12864_CLR;//一个时钟周期
__delay_cycles(40);
SCLK12864_SET;
__delay_cycles(40);
SCLK12864_CLR;
dat=dat<<1;
__delay_cycles(30);
SID12864_CLR;//
}
}
/***********************************************************
函数名称:LCD_write_byte
函数功能:写一个字节的数据到12864液晶,包括指令或数据
入口参数:RW、RS、W_data
出口参数:无
备注:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
(一般RW都设为0,即只向液晶写数据,不读数据)
RS=1,写入的是数据;RS=0,写入的是指令;
一般模式:RW=0,RS=1;写数据
RW="0",RS=0;写指令
***********************************************************/ voidLCD_write_byte(unsignedcharRW,unsignedcharRS,unsignedcharW_data) {
unsignedintH_data,L_data,S_ID=0xf8;//11111RWRS0
if(RW==0)//RW=0,MCU写一个数据到液晶;
{
S_ID&=~0x04;
}
else//RW=1,从液晶读数据到MCU
{
S_ID|=0X04;
}
if(RS==0)//RS=1,写入的是数据
{
S_ID&=~0x02;
}
else//RS=0,写入的是指令;
{
S_ID|=0X02;
}
H_data=W_data;
H_data&=0xf0;//屏蔽低4位的数据
L_data=W_data;//xxxx0000格式
L_data&=0x0f;//屏蔽高4位的数据
L_data<<=4;//xxxx0000格式
CS12864_SET;
serial_write_byte(S_ID);//发送S_ID
serial_write_byte(H_data);//发送H_data
serial_write_byte(L_data);//发送L_data
CS12864_CLR;
__delay_cycles(12);
}
/***********************************************************
函数名称:LCD_init
函数功能:12864液晶初始化
入口参数:dat8bit
出口参数:无
备注:
***********************************************************/ voidLCD_Init(void)
{
__delay_cycles(12);
LCD_write_byte(0,0,0x30);//功能设置8位数据,基本指令
__delay_cycles(12);
LCD_write_byte(0,0,0x04);//点设定:显示字符/光标从左到右移位,DDRAM地址加1
__delay_cycles(12);
LCD_write_byte(0,0,0x0C);//显示设定:开显示,显示光标,当前显示位反白闪动
__delay_cycles(12);
LCD_write_byte(0,0,0x01);//写指令:清除显示,清DDRAM
__delay_cycles(12);
LCD_write_byte(0,0,0x02);//写指令:DDRAM地址归位
__delay_cycles(12);
LCD_write_byte(0,0,0x80);//写指令:设置DDRAM地址,把显示地址设为0X80,即为第一行的首位
__delay_cycles(12);
}
/***********************************************************
函数名称:LCD_setxy
函数功能:设置显示汉字位置X(1~16),Y(1~4)
入口参数:X(1~16),Y(1~4)
出口参数:无
备注:x坐标
Line1|80H81H82H83H84H85H86H87H
Line2|90H91H92H93H94H95H96H97H
Line3|88H89H8AH8BH8CH8DH8EH8FH
Line4|98H99H9AH9BH9CH9DH9EH9FH
***********************************************************/
voidLCD_setxy(unsignedcharx,unsignedchary)
{
switch(y)
{
case1:
LCD_write_byte(0,0,0X7F+x);break;//Line1
case2:
LCD_write_byte(0,0,0X8F+x);break;//Line2
case3:
LCD_write_byte(0,0,0X87+x);break;//Line3
case4:
LCD_write_byte(0,0,0X97+x);break;//Line4
default:break;
}
}
/***********************************************************
函数名称:LCD_WriteStr
函数功能:在指定位置显示字符串
入口参数:dis_addr_x————汉字显示X坐标(1~16)
dis_addr_y————汉字显示Y坐标(1~4)
str———————————显示字符串中文占两个标准字符,英文和标点占一个字符
出口参数:无
备注:
***********************************************************/
voidLCD_WriteStr(unsignedchardis_addr_x,unsignedchardis_addr_y,unsignedchar*str) {
unsignedcharLCD_temp;
LCD_setxy(dis_addr_x,dis_addr_y);
LCD_temp=*str;
__delay_cycles(500);
while(LCD_temp!=0x00)//显示字符串子程序,字符码为0时退出
{
LCD_write_byte(0,1,LCD_temp);
LCD_temp=*(++str);
}
}
voidLCD_Graph_Mode_Set(unsignedcharbSelect)
{
LCD_write_byte(0,0,0x34);//扩展指令集
if(bSelect)
{
LCD_write_byte(0,0,0x36);//打开绘图模式
}
else
{
LCD_write_byte(0,0,0x34);//关闭绘图模式
}
}
/***************************清屏********************************/
voidlcd_clr()
{
unsignedcharss,j;
//unsignedintt;
LCD_Graph_Mode_Set(0);//先关闭图形显示功能
for(j=0;j<32;j++)//垂直坐标Y
{
for(ss=0;ss<8;ss++)//水平坐标X
{
LCD_write_byte(0,0,0x80+j);//(扩展扩展指令集)设置GDRAM垂直Y位置
LCD_write_byte(0,0,0x80+ss);//(扩展扩展指令集)设置GDRAM水平X位置
LCD_write_byte(0,1,0x00);//放入数据高字节img[j*16+ss*2]
LCD_write_byte(0,1,0x00);//放入数据低字节img[j*16+ss*2+1] }
}
for(j=32;j<64;j++)//垂直坐标Y
{
for(ss=0;ss<8;ss++)//水平坐标X
{
LCD_write_byte(0,0,0x80+j-32);
LCD_write_byte(0,0,0x88+ss);
LCD_write_byte(0,1,0x00);//img[j*16+ss*2]
LCD_write_byte(0,1,0x00);//img[j*16+ss*2+1]
}
}
LCD_Graph_Mode_Set(1);//最后打开图形显示功能
}
/***********************************************************
函数名称:LCD_ImgDisplay
函数功能:全屏显示128*64个象素的图形
入口参数:unsignedcharcode*img要显示的图形
出口参数:无
备注:
液晶屏坐标说明:
******************************************************************/ voidLCD_ImgDisplay(unsignedcharx,unsignedchary)
{
unsignedintt;
LCD_Graph_Mode_Set(0);//先关闭图形显示功能
LCD_write_byte(0,0,0x80+(y-1)%32);
LCD_write_byte(0,0,0x80+((y-1)/32)*8+(x-1)/16);
t=0x8000>>((x-1)%16);
LCD_write_byte(1,0,0x80+(y-1)%32);
data[((y-1)/32)*8+(x-1)/16][(y-1)%32]|=t;
LCD_write_byte(0,1,data[((y-1)/32)*8+(x-1)/16][(y-1)%32]/256);
LCD_write_byte(0,1,data[((y-1)/32)*8+(x-1)/16][(y-1)%32]%256); LCD_write_byte(0,1,t/256);
LCD_write_byte(0,1,t%256);
LCD_Graph_Mode_Set(1);//最后打开图形显示功能
LCD_write_byte(0,0,0x30);
}
/**************初始化DCO*************/ voidInitializeClocks(void)
{
BCSCTL1=CALBC1_1MHZ;
DCOCTL=CALDCO_1MHZ;
BCSCTL2&=~(DIVS_0);//SMCLK为0分频DCOSMCLK=DCO=16MHz }
/*IO口初始化,选择IO口方向,上拉,特殊功能*/
voidini_io(void)
{
/*****************P1口*****************/
P1DIR|=BIT0;//P1.0设置为输出,初始化输出高电平
P1OUT&=~BIT0;
P1DIR|=BIT6;//P1.6设置为输出,初始化输出高电平
P1OUT&=~BIT6;
P1DIR&=~BIT4;//将P1.4口设置为输入,外部中断
P1OUT|=BIT4;
P1REN|=BIT4;
P1DIR&=~BIT3;//将P1.3口设置为输入,外部中断
P1OUT|=BIT3;
P1REN|=BIT3;
P1DIR&=~BIT2;//将P1.2口设置为输入,外部中断
P1OUT|=BIT2;
P1REN|=BIT2;
P1DIR&=~BIT1;//将P1.1口设置为输入,外部中断
P1OUT|=BIT1;
P1REN|=BIT1;
P2DIR|=BIT0+BIT1+BIT2;
P2DIR|=BIT3+BIT4+BIT5;
P2OUT|=BIT4+BIT5;
ISD_SCK_CLR;
}
/***********TIMEA初始化********************/
voidini_timea(void)
{。