﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-紫-文章分类-51/ARM</title><link>http://www.cppblog.com/xtong27/category/17248.html</link><description>成功在于有计划好习惯并且能坚持不懈</description><language>zh-cn</language><lastBuildDate>Tue, 18 Oct 2011 08:22:15 GMT</lastBuildDate><pubDate>Tue, 18 Oct 2011 08:22:15 GMT</pubDate><ttl>60</ttl><item><title>51万能历</title><link>http://www.cppblog.com/xtong27/articles/150602.html</link><dc:creator>红云</dc:creator><author>红云</author><pubDate>Sun, 10 Jul 2011 10:43:00 GMT</pubDate><guid>http://www.cppblog.com/xtong27/articles/150602.html</guid><wfw:comment>http://www.cppblog.com/xtong27/comments/150602.html</wfw:comment><comments>http://www.cppblog.com/xtong27/articles/150602.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xtong27/comments/commentRss/150602.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xtong27/services/trackbacks/150602.html</trackback:ping><description><![CDATA[/*<br />project:万能历<br />作者:红云<br />时间:2011.7.10<br />功能:万能历,显示年月日时分秒,四个键盘调节月日时分<br />资源:STC89C52+Nokia5510LCD<br />*/<br />
<p>#include &lt;reg52.h&gt;<br />#include &lt;intrins.h&gt;</p>
<p>#define uchar unsigned char<br />#define uint unsigned int</p>
<p>sbit&nbsp;&nbsp;&nbsp; SCE&nbsp; = P0^0;&nbsp; //片选<br />sbit&nbsp;&nbsp;&nbsp; RST&nbsp; = P0^1;&nbsp; //复位,0复位<br />sbit&nbsp;&nbsp;&nbsp; DC&nbsp;&nbsp; = P0^2;&nbsp; //1写数据，0写指令<br />sbit&nbsp;&nbsp;&nbsp; SDIN = P1^5;&nbsp; //数据<br />sbit&nbsp;&nbsp;&nbsp; SCLK = P1^7;&nbsp; //时钟</p>
<p>sbit key1=P3^2;//控制月;<br />sbit key2=P3^3;//控制日;<br />sbit key3=P3^4;//控制时;<br />;l<br />sbit key4=P3^5;//控制分;</p>
<p>static int year=2011,month=7,day=10,hour=18,minute=20,second=30;<br />static uint num=0;<br />uchar code Array1024[]={<br />/*--&nbsp; 文字:&nbsp; 0&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0xC0,0x30,0x08,0x08,0x08,0x08,0x30,0xC0,0x00,0x00,0x1F,0x60,0x80,0x80,0x80,<br />0x80,0x60,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 1&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xFF,0x80,<br />0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 2&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x70,0x08,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0xC0,0xA0,0x90,0x88,0x84,<br />0x83,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 3&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x70,0x80,0x81,0x81,0x81,<br />0x42,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 4&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x00,0x0C,0x0B,0x08,0x88,0x88,<br />0xFF,0x88,0x88,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 5&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0xF8,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x73,0x82,0x81,0x81,0x81,<br />0x42,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 6&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x80,0x60,0x10,0x08,0x08,0x08,0x08,0x30,0x00,0x00,0x3F,0x42,0x81,0x81,0x81,<br />0x81,0x42,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 7&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x38,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x00,0xFC,0x03,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 8&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0xE0,0x10,0x08,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x38,0x45,0x82,0x82,0x82,<br />0x82,0x45,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 9&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0xE0,0x10,0x08,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x61,0x82,0x84,0x84,0x84,<br />0x44,0x32,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; :&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=10x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=10x24&nbsp; --*/<br />0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0xC1,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00<br />};<br />uchar code Array1924[]={<br />/*--&nbsp; 文字:&nbsp; 年&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x80,0x40,0x30,0x88,0x17,0x12,0x10,0x10,0x10,0xF0,0x10,0x10,0x10,0x90,0x88,<br />0x08,0x00,0x00,0x00,0x10,0x10,0x10,0x0F,0x11,0x11,0x11,0x11,0x11,0xFF,0x11,0x11,<br />0x11,0x10,0x10,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 月&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFE,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x38,0x07,0x04,0x04,0x04,0x04,0x04,0x04,<br />0x04,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x01,0x01,0x03,0x01,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 日&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x00,0x00,0xFC,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0xFE,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x81,0x81,0x81,0x81,0x81,0x81,0x81,0x81,<br />0x81,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 时&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0xF8,0x08,0x08,0x08,0xF8,0x28,0x20,0x20,0x20,0x20,0x20,0x20,0xFE,0x20,<br />0x20,0x20,0x00,0x00,0x00,0xFF,0x21,0x21,0x21,0x7F,0x00,0x00,0x01,0x06,0x04,0x00,<br />0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x01,0x03,0x01,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 分&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x00,0x80,0x40,0x30,0x1C,0x06,0x00,0x00,0x00,0x0E,0x30,0x60,0xC0,0x80,<br />0x00,0x00,0x00,0x00,0x02,0x01,0x00,0x01,0x81,0x61,0x1D,0x03,0x01,0x01,0x01,0xF1,<br />0x1F,0x00,0x01,0x01,0x01,0x00,0x00,0x00,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,<br />0x01,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 秒&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x40,0x48,0x44,0xFC,0x44,0x42,0x60,0x40,0xC0,0x60,0x00,0xFE,0x02,0x10,0x20,<br />0x60,0xC0,0x00,0x00,0x10,0x08,0x06,0xFF,0x01,0x02,0x02,0x06,0x01,0x80,0x80,0x4F,<br />0x20,0x10,0x0C,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x02,0x02,0x02,0x01,0x01,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; :&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC1,0xC1,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />&nbsp;<br />/*--&nbsp; 文字:&nbsp; 0&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0xC0,0x30,0x08,0x08,0x08,0x08,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x1F,0x60,0x80,0x80,0x80,0x80,0x60,0x1F,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 1&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x80,0x80,0xFF,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 2&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x70,0x08,0x08,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0xC0,0xA0,0x90,0x88,0x84,0x83,0xE0,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 3&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x70,0x80,0x81,0x81,0x81,0x42,0x3C,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 4&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x0C,0x0B,0x08,0x88,0x88,0xFF,0x88,0x88,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 5&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0xF8,0x08,0x08,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x73,0x82,0x81,0x81,0x81,0x42,0x3C,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 6&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x80,0x60,0x10,0x08,0x08,0x08,0x08,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x3F,0x42,0x81,0x81,0x81,0x81,0x42,0x3C,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 7&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0x38,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 8&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0xE0,0x10,0x08,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x38,0x45,0x82,0x82,0x82,0x82,0x45,0x38,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,</p>
<p>/*--&nbsp; 文字:&nbsp; 9&nbsp; --*/<br />/*--&nbsp; Trebuchet MS14;&nbsp; 此字体下对应的点阵为：宽x高=19x19&nbsp;&nbsp; --*/<br />/*--&nbsp; 高度不是8的倍数，现调整为：宽度x高度=19x24&nbsp; --*/<br />0x00,0xE0,0x10,0x08,0x08,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x61,0x82,0x84,0x84,0x84,0x44,0x32,0x0F,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,<br />0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00<br />};</p>
<p>void delay(uint ii)<br />{<br />&nbsp;uint i,x;<br />&nbsp;for (x=0;x&lt;ii;x++)<br />&nbsp;{<br />&nbsp;&nbsp;for (i=0;i&lt;100;i++);<br />&nbsp;}<br />}<br />&nbsp;<br />void Lcd_Write_Byte(uchar dt,uchar comm)<br />{<br />&nbsp;uint i;<br />&nbsp;SCE=0;&nbsp;//片选<br />&nbsp;DC=comm;<br />&nbsp;for(i=0;i&lt;8;i++)<br />&nbsp;{<br />&nbsp;&nbsp;if(dt&amp;0x80)<br />&nbsp;&nbsp;&nbsp;SDIN=1;<br />&nbsp;&nbsp;else<br />&nbsp;&nbsp;&nbsp;SDIN=0;<br />&nbsp;&nbsp;dt&lt;&lt;=1;<br />&nbsp;&nbsp;SCLK=0;<br />&nbsp;&nbsp;SCLK=1;<br />&nbsp;}<br />&nbsp;DC=1;<br />&nbsp;SCE=1;<br />&nbsp;SDIN=1;<br />}<br />/*---------------------------------------<br />LCD_init: 3310LCD初始化<br />编写日期：20080918 <br />-----------------------------------------&nbsp; */<br />void Lcd_Init(void)<br />{<br />&nbsp;RST=0;<br />&nbsp;delay(10);<br />&nbsp;RST=1;<br />&nbsp;Lcd_Write_Byte(0x21,0);//初始化Lcd,功能设定使用扩充指令100001<br />&nbsp;Lcd_Write_Byte(0xC6,0);//设定液晶偏置电压11000110&nbsp;&nbsp;&nbsp;&nbsp; <br />&nbsp;Lcd_Write_Byte(0x06,0);//&nbsp; 110<br />&nbsp; &nbsp;Lcd_Write_Byte(0x13,0);//(0x10,0);;//1:48&nbsp; 10011<br />&nbsp;Lcd_Write_Byte(0x20,0);//使用基本指令&nbsp; 100000<br />&nbsp;Lcd_Write_Byte(0x0C,0);//设定显示模式，正常显示&nbsp; 1100<br />&nbsp;&nbsp;<br />}<br />/*-------------------------------------------<br />LCD_set_XY: 设置LCD坐标函数<br />输入参数：X：0－83&nbsp; Y：0－5<br />编写日期：20080918 <br />---------------------------------------------*/<br />void LCD_set_XY(uchar X, uchar Y)<br />{<br />&nbsp;Lcd_Write_Byte(0x40 | Y, 0);// column&nbsp;&nbsp; 01000000&nbsp;64&nbsp;设置RAM的Y地址<br />&nbsp;Lcd_Write_Byte(0x80 | X, 0);// row&nbsp;&nbsp;&nbsp; 10000000&nbsp; 128&nbsp;设置RAM的X地址<br />} <br />/*------------------------------------------<br />LCD_clear: LCD清屏函数<br />编写日期：20080918 <br />--------------------------------------------*/<br />void LCD_clear(void)<br />{<br />&nbsp;uchar k,t;<br />&nbsp;LCD_set_XY(0,0);<br />&nbsp;for(t=0;t&lt;6;t++)<br />&nbsp;{ <br />&nbsp;&nbsp;for(k=0;k&lt;84;k++)<br />&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;Lcd_Write_Byte(0x00,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;} <br />&nbsp;}<br />}</p>
<p>/*---------------------------------------------<br />LCD_Fill: 填充LCD中所有点<br />编写日期：20110710 <br />-----------------------------------------------*/<br />void LCD_Fill(void)<br />{<br />&nbsp;uchar k,t;<br />&nbsp;LCD_set_XY(0,0);<br />&nbsp;for(t=0;t&lt;6;t++)<br />&nbsp;{ <br />&nbsp;&nbsp;for(k=0;k&lt;84;k++)<br />&nbsp;&nbsp;{ <br />&nbsp;&nbsp;&nbsp;Lcd_Write_Byte(0xff,1);<br />&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;} <br />&nbsp;}<br />}</p>
<p>/*---------------------------------------------<br />LCD_Write_Word: 在屏中写汉字.<br />在每page页开始的地方写w*h大的字符 (屏大小84*48 6页,每页84列)<br />编写日期：20110710 <br />-----------------------------------------------*/<br />LCD_Write_Word(uchar dat,uchar x,uchar page,uchar w,uchar h)<br />{<br />&nbsp;uchar j,k,i=0;</p>
<p>&nbsp;for(k=0;k&lt;h/8;k++)<br />&nbsp;{<br />&nbsp;&nbsp;LCD_set_XY(x,page++);<br />&nbsp;&nbsp;for(j=0;j&lt;w;j++)<br />&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;Lcd_Write_Byte(Array1924[dat*57+i++],1);&nbsp;&nbsp;<br />&nbsp;&nbsp;}<br />&nbsp;}<br />&nbsp; <br />}</p>
<p>/*---------------------------------------------<br />LCD_Write_Digit: 在屏中写数字.<br />在每page页开始的地方写w*h大的字符 (屏大小84*48 6页,每页84列)<br />编写日期：20110710 <br />-----------------------------------------------*/<br />LCD_Write_Digit(uchar dat,uchar x,uchar page,uchar w,uchar h)<br />{<br />&nbsp;uchar j,k,i=0;</p>
<p>&nbsp;for(k=0;k&lt;h/8;k++)<br />&nbsp;{<br />&nbsp;&nbsp;LCD_set_XY(x,page++);<br />&nbsp;&nbsp;for(j=0;j&lt;w;j++)<br />&nbsp;&nbsp;{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />&nbsp;&nbsp;&nbsp;Lcd_Write_Byte(Array1024[dat*30+i++],1);&nbsp;&nbsp;<br />&nbsp;&nbsp;}<br />&nbsp;}<br />&nbsp; <br />}<br />/*---------------------------------------------<br />LCD_DisplayYear(int year):显示年<br />编写日期：20110710 <br />-----------------------------------------------*/<br />LCD_DisplayYear(int year)<br />{<br />&nbsp;LCD_Write_Digit(year/1000,&nbsp; 0, 0,10,24);//2<br />&nbsp;LCD_Write_Digit(year/100%10,10,0,10,24);//0<br />&nbsp;LCD_Write_Digit(year/10%10, 20,0,10,24);//1<br />&nbsp;LCD_Write_Digit(year%10,&nbsp;&nbsp;&nbsp; 30,0,10,24);//1<br />&nbsp;LCD_Write_Word(0,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 40,0,19,24);//年<br />}</p>
<p>/*---------------------------------------------<br />LCD_DisplayMonDay(int month,int day):显示月日<br />编写日期：20110710 <br />-----------------------------------------------*/<br />LCD_DisplayMonDay(int month,int day)<br />{<br />&nbsp;LCD_Write_Digit(month/10,&nbsp; 0, 2,10,24);//0<br />&nbsp;LCD_Write_Digit(month%10,&nbsp; 10,2,10,24);//7<br />&nbsp;LCD_Write_Word(1,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 20,2,19,24);//月<br />&nbsp;LCD_Write_Digit(day/10,&nbsp;&nbsp;&nbsp; 39,2,10,24);//1<br />&nbsp;LCD_Write_Digit(day%10,&nbsp;&nbsp;&nbsp; 49,2,10,24);//0<br />&nbsp;LCD_Write_Word(2,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 59,2,19,24);//月<br />}</p>
<p>/*---------------------------------------------<br />LCD_DisplayTime(int hour,int minute):显示时分<br />编写日期：20110710 <br />-----------------------------------------------*/<br />LCD_DisplayTime(int hour,int minute,int second)<br />{<br />&nbsp;LCD_Write_Digit(hour/10,&nbsp; 0, 4,10,24);//0<br />&nbsp;LCD_Write_Digit(hour%10,&nbsp; 10,4,10,24);//7<br />&nbsp;LCD_Write_Digit(10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 20,4,10,24);//:?<br />&nbsp;LCD_Write_Digit(minute/10,30,4,10,24);//1<br />&nbsp;LCD_Write_Digit(minute%10,40,4,10,24);//0<br />&nbsp;LCD_Write_Digit(10,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 50,4,10,24);//:<br />&nbsp;LCD_Write_Digit(second/10,60,4,10,24);//1<br />&nbsp;LCD_Write_Digit(second%10,70,4,10,24);//0<br />}</p>
<p>/*---------------------------------------------<br />TimeInit(void)定时器初始化(选哪个定时器好?)<br />编写日期：20110710 <br />-----------------------------------------------*/<br />void TimeInit(void)<br />{<br />&nbsp;TMOD=0x01;<br />&nbsp;TH0=(65536-45872)/256;<br />&nbsp;TL0=(65536-45872)%256;<br />&nbsp;EA=1;<br />&nbsp;ET0=1;//开定时中断;<br />&nbsp;TR0=1;&nbsp;<br />}</p>
<p>/*---------------------------------------------<br />Int0_time(void)定时器中断函数(具体时间如何?)<br />编写日期：20110710 <br />-----------------------------------------------*/<br />void Int0_time() interrupt 1&nbsp;&nbsp; //定时中断;<br />{<br />&nbsp;TH0=(65536-45872)/256;&nbsp;//50MS<br />&nbsp;TL0=(65536-45872)%256;<br />&nbsp;num++;<br />&nbsp;if(num&gt;=20)//每秒<br />&nbsp;{<br />&nbsp;&nbsp;second++;<br />&nbsp;&nbsp;if(second&gt;=60)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;minute++;<br />&nbsp;&nbsp;&nbsp;if(minute==60)<br />&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;hour++;<br />&nbsp;&nbsp;&nbsp;&nbsp;if(hour==24)<br />&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(day==30)//约定日期为30天一个月<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;month++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(month==12)<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;year++;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;day=1;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour=1;<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;minute=1;<br />&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;second=1;<br />&nbsp;&nbsp;}<br />&nbsp;&nbsp;num=0;<br />&nbsp;}</p>
<p>}</p>
<p>void KeyScan(void)<br />{<br />&nbsp;if(key1==0)//重启;<br />&nbsp;{<br />&nbsp;&nbsp;delay(10); //用多少时间合适;为什么? 抖动10ms<br />&nbsp;&nbsp;if(key1==0)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;month++;<br />&nbsp;&nbsp;&nbsp;if(month&gt;=24)month=1;<br />&nbsp;&nbsp;&nbsp;while(!key1);<br />&nbsp;&nbsp;}<br />&nbsp;}&nbsp;<br />&nbsp;if(key2==0)//暂停;<br />&nbsp;{<br />&nbsp;&nbsp;delay(10);<br />&nbsp;&nbsp;if(key2==0)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;day++;<br />&nbsp;&nbsp;&nbsp;if(day&gt;=30)day=1;<br />&nbsp;&nbsp;&nbsp;while(!key2);<br />&nbsp;&nbsp;}<br />&nbsp;}<br />&nbsp;if(key3==0)//停止;<br />&nbsp;{<br />&nbsp;&nbsp;delay(10);<br />&nbsp;&nbsp;if(key3==0)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;hour++;<br />&nbsp;&nbsp;&nbsp;if(hour&gt;=24)hour=1;<br />&nbsp;&nbsp;&nbsp;while(!key3);<br />&nbsp;&nbsp;}<br />&nbsp;}<br />&nbsp;if(key4==0)//停止;<br />&nbsp;{<br />&nbsp;&nbsp;delay(10);<br />&nbsp;&nbsp;if(key4==0)<br />&nbsp;&nbsp;{<br />&nbsp;&nbsp;&nbsp;minute++;<br />&nbsp;&nbsp;&nbsp;if(minute&gt;=60)minute=1;<br />&nbsp;&nbsp;&nbsp;while(!key4);<br />&nbsp;&nbsp;}<br />&nbsp;}<br />}</p>
<p><br />/*---------------------------------------------<br />main()主函数<br />编写日期：20110710 <br />-----------------------------------------------*/<br />void main()<br />{<br />&nbsp;Lcd_Init();<br />&nbsp;LCD_clear();<br />&nbsp;TimeInit();<br />&nbsp;while(1)<br />&nbsp;{<br />&nbsp;&nbsp;LCD_DisplayYear(year);//要消耗时间<br />&nbsp;&nbsp;LCD_DisplayMonDay(month,day);&nbsp; //要消耗时间<br />&nbsp;&nbsp;LCD_DisplayTime(hour,minute,second);&nbsp; //要消耗时间<br />&nbsp;&nbsp;KeyScan();//要消耗时间<br />&nbsp;}</p>
<p>}</p><img src ="http://www.cppblog.com/xtong27/aggbug/150602.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xtong27/" target="_blank">红云</a> 2011-07-10 18:43 <a href="http://www.cppblog.com/xtong27/articles/150602.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>程序编译中的字节对齐（转）</title><link>http://www.cppblog.com/xtong27/articles/150160.html</link><dc:creator>红云</dc:creator><author>红云</author><pubDate>Mon, 04 Jul 2011 15:29:00 GMT</pubDate><guid>http://www.cppblog.com/xtong27/articles/150160.html</guid><wfw:comment>http://www.cppblog.com/xtong27/comments/150160.html</wfw:comment><comments>http://www.cppblog.com/xtong27/articles/150160.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/xtong27/comments/commentRss/150160.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/xtong27/services/trackbacks/150160.html</trackback:ping><description><![CDATA[<h3>一.什么是字节对齐,为什么要对齐?</h3>
<p>&nbsp;&nbsp;&nbsp; 现代计算机中内存空间都是按照byte划分的，从理论上讲似乎对任何类型的变量的访问可以从任何地址开始，但实际情况是在访问特定类型变量的时候经常在特定的内存地址访问，这就需要各种类型数据按照一定的规则在空间上排列，而不是顺序的一个接一个的排放，这就是对齐。<br />&nbsp;&nbsp;&nbsp; 对齐的作用和原因：各个硬件平台对存储空间的处理上有很大的不同。一些平台对某些特定类型的数据只能从某些特定地址开始存取。比如有些架构的CPU在访问一个没有进行对齐的变量的时候会发生错误,那么在这种架构下编程必须保证字节对齐.其他平台可能没有这种情况，但是最常见的是如果不按照适合其平台要求对数据存放进行对齐，会在存取效率上带来损失。比如有些平台每次读都是从偶地址开始，如果一个int型（假设为32位系统）如果存放在偶地址开始的地方，那么一个读周期就可以读出这32bit，而如果存放在奇地址开始的地方，就需要2个读周期，并对两次读出的结果的高低字节进行拼凑才能得到该32bit数据。显然在读取效率上下降很多。</p>
<h3>二.字节对齐对程序的影响:</h3>
<p>&nbsp;&nbsp;&nbsp; 先让我们看几个例子吧(32bit,x86环境,gcc编译器):<br />设结构体如下定义：<br />struct A<br />{<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />struct B<br />{<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />现在已知32位机器上各种数据类型的长度如下:<br />char:1(有符号无符号同)&nbsp;&nbsp;&nbsp;&nbsp;<br />short:2(有符号无符号同)&nbsp;&nbsp;&nbsp;&nbsp;<br />int:4(有符号无符号同)&nbsp;&nbsp;&nbsp;&nbsp;<br />long:4(有符号无符号同)&nbsp;&nbsp;&nbsp;&nbsp;<br />float:4&nbsp;&nbsp;&nbsp;&nbsp; double:8<br />那么上面两个结构大小如何呢?<br />结果是:<br />sizeof(strcut A)值为8<br />sizeof(struct B)的值却是12</p>
<p>结构体A中包含了4字节长度的int一个，1字节长度的char一个和2字节长度的short型数据一个,B也一样;按理说A,B大小应该都是7字节。<br />之所以出现上面的结果是因为编译器要对数据成员在空间上进行对齐。上面是按照编译器的默认设置进行对齐的结果,那么我们是不是可以改变编译器的这种默认对齐设置呢,当然可以.例如:<br />#pragma pack (2) /*指定按2字节对齐*/<br />struct C<br />{<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />#pragma pack () /*取消指定对齐，恢复缺省对齐*/<br />sizeof(struct C)值是8。<br />修改对齐值为1：<br />#pragma pack (1) /*指定按1字节对齐*/<br />struct D<br />{<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />#pragma pack () /*取消指定对齐，恢复缺省对齐*/<br />sizeof(struct D)值为7。<br />后面我们再讲解#pragma pack()的作用.</p>
<h3>三.编译器是按照什么样的原则进行对齐的?</h3>
<p>&nbsp;&nbsp;&nbsp; 先让我们看四个重要的基本概念：<br /><font color="#0000ff">1.数据类型自身的对齐值：<br /></font>对于char型数据，其自身对齐值为1，对于short型为2，对于int,float,double类型，其自身对齐值为4，单位字节。<br /><font color="#3300ff">2.结构体或者类的自身对齐值：</font>其成员中自身对齐值最大的那个值。<br /><font color="#0000ff">3.指定对齐值</font>：#pragma pack (value)时的指定对齐值value。<br /><font color="#0000ff">4.数据成员、结构体和类的有效对齐值：</font>自身对齐值和指定对齐值中小的那个值。<br />有了这些值，我们就可以很方便的来讨论具体数据结构的成员和其自身的对齐方式。有效对齐值N是最终用来决定数据存放地址方式的值，最重要。有效对齐N，就是表示&#8220;对齐在N上&#8221;，也就是说该数据的"存放起始地址%N=0".而数据结构中的数据变量都是按定义的先后顺序来排放的。第一个数据变量的起始地址就是数据结构的起始地址。结构体的成员变量要对齐排放，结构体本身也要根据自身的有效对齐值圆整(就是结构体成员变量占用总长度需要是对结构体有效对齐值的整数倍，结合下面例子理解)。这样就不能理解上面的几个例子的值了。<br />例子分析：<br />分析例子B；<br />struct B<br />{<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />假设B从地址空间0x0000开始排放。该例子中没有定义指定对齐值，在笔者环境下，该值默认为4。第一个成员变量b的自身对齐值是1，比指定或者默认指定对齐值4小，所以其有效对齐值为1，所以其存放地址0x0000符合0x0000%1=0.第二个成员变量a，其自身对齐值为4，所以有效对齐值也为4，所以只能存放在起始地址为0x0004到0x0007这四个连续的字节空间中，复核0x0004%4=0,且紧靠第一个变量。第三个变量c,自身对齐值为 2，所以有效对齐值也是2，可以存放在0x0008到0x0009这两个字节空间中，符合0x0008%2=0。所以从0x0000到0x0009存放的都是B内容。再看数据结构B的自身对齐值为其变量中最大对齐值(这里是b）所以就是4，所以结构体的有效对齐值也是4。根据结构体圆整的要求， 0x0009到0x0000=10字节，（10＋2）％4＝0。所以0x0000A到0x000B也为结构体B所占用。故B从0x0000到0x000B 共有12个字节,sizeof(struct B)=12;<font color="#3300ff">其实如果就这一个就来说它已将满足字节对齐了, 因为它的起始地址是0,因此肯定是对齐的,之所以在后面补充2个字节,是因为编译器为了实现结构数组的存取效率,试想如果我们定义了一个结构B的数组,那么第一个结构起始地址是0没有问题,但是第二个结构呢?按照数组的定义,数组中所有元素都是紧挨着的,如果我们不把结构的大小补充为4的整数倍,那么下一个结构的起始地址将是0x0000A,这显然不能满足结构的地址对齐了,因此我们要把结构补充成有效对齐大小的整数倍.其实诸如:对于char型数据，其自身对齐值为1，对于short型为2，对于int,float,double类型，其自身对齐值为4，这些已有类型的自身对齐值也是基于数组考虑的,只是因为这些类型的长度已知了,所以他们的自身对齐值也就已知了.<br /></font>同理,分析上面例子C：<br />#pragma pack (2) /*指定按2字节对齐*/<br />struct C<br />{<br />&nbsp;&nbsp;&nbsp; char b;<br />&nbsp;&nbsp;&nbsp; int a;<br />&nbsp;&nbsp;&nbsp; short c;<br />};<br />#pragma pack () /*取消指定对齐，恢复缺省对齐*/<br />第一个变量b的自身对齐值为1，指定对齐值为2，所以，其有效对齐值为1，假设C从0x0000开始，那么b存放在0x0000，符合0x0000%1= 0;第二个变量，自身对齐值为4，指定对齐值为2，所以有效对齐值为2，所以顺序存放在0x0002、0x0003、0x0004、0x0005四个连续字节中，符合0x0002%2=0。第三个变量c的自身对齐值为2，所以有效对齐值为2，顺序存放<br />在0x0006、0x0007中，符合 0x0006%2=0。所以从0x0000到0x00007共八字节存放的是C的变量。又C的自身对齐值为4，所以C的有效对齐值为2。又8%2=0,C 只占用0x0000到0x0007的八个字节。所以sizeof(struct C)=8.</p>
<h3>四.如何修改编译器的默认对齐值?</h3>
<p>1.在VC IDE中，可以这样修改：[Project]|[Settings],c/c++选项卡Category的Code Generation选项的Struct Member Alignment中修改，默认是8字节。<br />2.在编码时，可以这样动态修改：#pragma pack .<font color="#ff0000">注意:是pragma而不是progma.</font></p>
<h3>五.针对字节对齐,我们在编程中如何考虑?</h3>
<p><br />&nbsp;&nbsp;&nbsp; 如果在编程的时候要考虑节约空间的话,那么我们只需要假定结构的首地址是0,然后各个变量按照上面的原则进行排列即可,基本的原则就是把结构中的变量按照类型大小从小到大声明,尽量减少中间的填补空间.还有一种就是为了以空间换取时间的效率,我们显示的进行填补空间进行对齐,比如:有一种使用空间换时间做法是显式的插入reserved成员：<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct A{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char a;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char reserved[3];//使用空间换时间<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int b;<br />}<br /><br />reserved成员对我们的程序没有什么意义,它只是起到填补空间以达到字节对齐的目的,当然即使不加这个成员通常编译器也会给我们自动填补对齐,我们自己加上它只是起到显式的提醒作用.</p>
<h3>六.字节对齐可能带来的隐患:</h3>
<p>&nbsp;&nbsp;&nbsp; 代码中关于对齐的隐患，很多是隐式的。比如在强制类型转换的时候。例如：<br />unsigned int i = 0x12345678;<br />unsigned char *p=NULL;<br />unsigned short *p1=NULL;</p>
<p>p=&amp;i;<br />*p=0x00;<br />p1=(unsigned short *)(p+1);<br />*p1=0x0000;<br />最后两句代码，从奇数边界去访问unsignedshort型变量，显然不符合对齐的规定。<br />在x86上，类似的操作只会影响效率，但是在MIPS或者sparc上，可能就是一个error,因为它们要求必须字节对齐.</p>
<h3>七.如何查找与字节对齐方面的问题:</h3>
<p>如果出现对齐或者赋值问题首先查看<br />1. 编译器的big little端设置<br />2. 看这种体系本身是否支持非对齐访问<br />3. 如果支持看设置了对齐与否,如果没有则看访问时需要加某些特殊的修饰来标志其特殊访问操作。</p>
<p>八.相关文章:转自<a href="http://blog.csdn.net/goodluckyxl/archive/2005/10/17/506827.aspx"><u>http://blog.csdn.net/goodluckyxl/archive/2005/10/17/506827.aspx</u></a></p><quote></quote>
<p>ARM下的对齐处理 <br />from DUI0067D_ADS1_2_CompLib</p>
<p>3.13 type qulifiers</p>
<p>有部分摘自ARM编译器文档对齐部分</p>
<p>对齐的使用:<br />1.aligned(number) [number为最小对齐的字节数］</p>
<p>对结构体的对齐的边界进行定义，注意与 pragma 使用的不同。<br />2.__packed <br />__packed是进行一字节对齐<br />1.不能对packed的对象进行对齐<br />2.所有对象的读写访问都进行非对齐访问<br />3.float及包含float的结构联合及未用__packed的对象将不能字节对齐<br />4.__packed对局部整形变量无影响<br />5.强制由unpacked对象向packed对象转化是未定义,整形指针可以合法定<br />义为packed。<br />&nbsp;&nbsp;&nbsp;&nbsp; __packed int* p; //__packed int 则没有意义<br />6.对齐或非对齐读写访问带来问题<br />__packed struct STRUCT_TEST<br />{<br />char a;<br />int b;<br />char c;<br />} ;&nbsp;&nbsp;&nbsp; //定义如下结构此时b的起始地址一定是不对齐的<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //在栈中访问b可能有问题,因为栈上数据肯定是对齐访问[from CL]<br />//将下面变量定义成全局静态不在栈上 <br />static char* p;<br />static struct STRUCT_TEST a;<br />void Main()<br />{<br />__packed int* q; //此时定义成__packed来修饰当前q指向为非对齐的数据地址下面的访问则可以</p>
<p>p = (char*)&amp;a;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />q = (int*)(p+1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br /><br />*q = 0x87654321; <br />/*&nbsp;&nbsp; <br />得到赋值的汇编指令很清楚<br />ldr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r5,0x20001590 ; = #0x12345678<br />[0xe1a00005]&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r0,r5<br />[0xeb0000b0]&nbsp;&nbsp; bl&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; __rt_uwrite4 //在此处调用一个写4byte的操作函数 <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />[0xe5c10000]&nbsp;&nbsp; strb&nbsp;&nbsp;&nbsp;&nbsp; r0,[r1,#0]&nbsp;&nbsp; //函数进行4次strb操作然后返回保证了数据正确的访问<br />[0xe1a02420]&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2,r0,lsr #8<br />[0xe5c12001]&nbsp;&nbsp; strb&nbsp;&nbsp;&nbsp;&nbsp; r2,[r1,#1]<br />[0xe1a02820]&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2,r0,lsr #16<br />[0xe5c12002]&nbsp;&nbsp; strb&nbsp;&nbsp;&nbsp;&nbsp; r2,[r1,#2]<br />[0xe1a02c20]&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2,r0,lsr #24<br />[0xe5c12003]&nbsp;&nbsp; strb&nbsp;&nbsp;&nbsp;&nbsp; r2,[r1,#3]<br />[0xe1a0f00e]&nbsp;&nbsp; mov&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc,r14<br />*/</p>
<p>/*<br />如果q没有加__packed修饰则汇编出来指令是这样直接会导致奇地址处访问失败<br />[0xe59f2018]&nbsp;&nbsp; ldr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2,0x20001594 ; = #0x87654321<br />[0xe5812000]&nbsp;&nbsp; str&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; r2,[r1,#0]<br />*/</p>//这样可以很清楚的看到非对齐访问是如何产生错误的<br />//以及如何消除非对齐访问带来问题<br />//也可以看到非对齐访问和对齐访问的指令差异导致效率问题<br />} <img src ="http://www.cppblog.com/xtong27/aggbug/150160.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/xtong27/" target="_blank">红云</a> 2011-07-04 23:29 <a href="http://www.cppblog.com/xtong27/articles/150160.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>