⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c51常量.htm

📁 单片机教程 用c语言实现编程 可以大大缩短我们的开发周期
💻 HTM
📖 第 1 页 / 共 3 页
字号:
		clipboardData.setData('Text',url);
		showAlert('已经把该社团网址复制到系统剪贴板,您可以使用(Ctrl+V或鼠标右键)粘贴功能,通过其他软件记录或发送给您的朋友。')
	}
	catch(e){}
}
function add_favorite(uid, url, name)
{
	window.open("/personal/favorite/add.php?member_id="+uid+"&url="+url+"&name="+name,'_blank','width=400,height=220,toolbar=0,locationbar=0,menubar=0,statbar=0'); 
	return false;
}

// 提示登录
function alertlert_login()
{
	showAlert('对不起,您目前没有权限,请先登录!');
	document.login_form1.account.focus();
}


//申请加入社团
function con_join(con_gid)
{
	showConfirm("您确认加入本社团吗?","window.location.href='/league/apply.php?g_id="+con_gid+"';","");
}


//退出本社团
function con_quiet(con_gid,con_uid)
{
	showConfirm("您确认退出本社团吗?","window.location.href='/league/member/quiet_member.php?g_id="+con_gid+"&q_uid="+con_uid+"';","");
}

</SCRIPT>
<!-- 回复框 -->
<DIV id=background style="DISPLAY: none"></DIV>
<DIV id=postbox 
style="DISPLAY: none; Z-INDEX: 50; LEFT: 769px; WIDTH: 600px; POSITION: absolute; TOP: 271px">
<FORM name=reply_comment action=/league/comment/index.php?gid=514465 method=post 
target=hidden_frame><INPUT id=act type=hidden value=add name=act> <INPUT id=r_id 
type=hidden name=r_id> <INPUT id=t_id type=hidden name=t_id> <INPUT id=f_uid 
type=hidden name=f_uid> 
<TABLE cellSpacing=0 cellPadding=0 width="70%" border=0>
  <TBODY>
  <TR>
    <TD height=13>&nbsp;</TD></TR>
  <TR>
    <TD class=box_common height=26>&nbsp;<SPAN 
      class=font_common_title>回复评论</SPAN></TD></TR></TBODY></TABLE>
<TABLE class=box_common01 cellSpacing=0 cellPadding=0 width="70%">
  <TBODY>
  <TR>
    <TD>
      <TABLE cellSpacing=8 cellPadding=0 width="100%" bgColor=#ffffff 
        border=0><TBODY>
        <TR>
          <TD class=font_common vAlign=top align=right height=63>内容:</TD>
          <TD class=font_common vAlign=top><TEXTAREA class=input_box01 id=input_area name=input_area rows=6 cols=48></TEXTAREA></TD></TR>
        <TR>
          <TD class=font_common vAlign=top align=right width="15%">原文:</TD>
          <TD class=font_common style="LINE-HEIGHT: 18px" vAlign=top 
            width="85%"><SPAN class=gray id=past_type></SPAN></TD></TR>
        <TR>
          <TD class=font_common vAlign=top align=middle colSpan=2><INPUT class=btn01 onclick=javascript:hidden_reply();submit_post(document.reply_comment); type=button value=发表 name=queding> 
            &nbsp;&nbsp; <INPUT class=btn01 onclick=hidden_reply(); type=reset value=取消> 
        </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></FORM></DIV><!-- 回复框 -->
<DIV id=mainBox>
<DIV id=mainBoxBg>
<TABLE class=module_title cellSpacing=0 cellPadding=0 border=0>
  <TBODY>
  <TR>
    <TD class=corner_left></TD>
    <TD class=corner_center><A 
      href="http://www.5jia1.com/league/?gid=514465">首页</A>&nbsp;&gt;&nbsp;<A 
      href="http://www.5jia1.com/league/blog/blog_index.php?gid=514465">社团文集</A>&nbsp;&gt;&nbsp;<A 
      href="http://www.5jia1.com/league/blog/show_comm.php?bid=361241">第四课 
      C51常量</A></TD>
    <TD class=corner_right></TD></TR></TBODY></TABLE>
<DIV class=module_content>
<DIV class=module_content_line>
<DIV class=bg>
<TABLE cellSpacing=10 cellPadding=0 width="100%" border=0>
  <TBODY>
  <TR>
    <TD align=middle>
      <TABLE class=box_common cellSpacing=1 cellPadding=5 width="100%" 
        border=0><TBODY>
        <TR class=box_common_title>
          <TD colSpan=2>
            <TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
              <TBODY>
              <TR>
                <TD width="35%"><B>第四课 C51常量</B></TD>
                <TD align=right width="65%">作者: <A 
                  href="http://www.5jia1.com/personal/?member_id=505887" 
                  target=_blank>张文俊</A> &nbsp;&nbsp;2007-08-14 
                  15:28:32&nbsp;&nbsp; </TD></TR></TBODY></TABLE></TD></TR>
        <TR class=box_content>
          <TD vAlign=top align=middle width="10%"><A 
            href="http://www.5jia1.com/personal/?member_id=505887" 
            target=_blank><IMG class=box01 height=98 
            src="4.files/s_0_personal_photo_505887.jpg" width=98 vspace=5 
            border=0></A><BR><IMG height=14 src="4.files/icon_boy.gif" width=14 
            border=0>&nbsp; <A 
            href="http://www.5jia1.com/personal/?member_id=505887" 
            target=_blank>张文俊</A> </TD>
          <TD>
            <TABLE class="font_content  autowrap" cellSpacing=12 cellPadding=0 
            width="100%" border=0>
              <TBODY>
              <TR>
                <TD class=line_h20 style="WIDTH: 790px">
                  <DIV style="OVERFLOW-X: auto; WIDTH: 100%">
                  <TABLE style="TABLE-LAYOUT: fixed; WORD-BREAK: break-all" 
                  cellSpacing=0 cellPadding=0 width="100%" border=0>
                    <TBODY>
                    <TR>
                      <TH vAlign=center align=middle height=32>
                        <H1>第四课 C51常量</H1></TH></TR>
                    <TR>
                      <TD align=middle bgColor=#f7f7f7 
                        height=25>作者:佚名&nbsp;&nbsp;&nbsp;&nbsp;来源:本站原创&nbsp;&nbsp;&nbsp;&nbsp;点击数: 
                        149&nbsp;&nbsp;&nbsp;更新时间:2007年07月18日&nbsp;&nbsp;&nbsp;</TD></TR></TBODY></TABLE>
                  <P>上一篇学习了 KEIL c&nbsp;&nbsp;单片机c语言 
                  编译器所支持的数据类型。而这些c51数据类型又是怎么用在常量和变量的定义中的呢?又有什么要注意的吗?常量就是在程序运行过程中不能改变值的量,而变量是能在程序运行过程中不断变化的量。变量的定义能使用所有c51编译器支持的数据类型,而常量的数据类型只有整型、浮点型、字符型、字符串型和位标量。这一篇学习常量定义和使用方法,而下一篇则学习单片机c语言的变量。</P>
                  <P>常量的数据类型说明是这样的</P>
                  <P>1. 整型常量能表示为十进制如 123,0,-89 等。十六进制则以 0x 开头如 0x34,-0x3B 
                  等。长整型就在数字后面加字母 L,如 104L,034L,0xF340 等。</P>
                  <P>2. 浮点型常量可分为 十进 制和指数表示形式 。十 进制由数字和小数点组成,如0.888,3345.345,0.0 
                  等,整数或小数部分为 0,能省略但必须有小数点。指数表 
                  示形式为[±]数字[.数字]e[±]数字,[]中的内容为可选项,其中内容根据具体情 
                  况可有可无,但其余部分必须有,如125e3,7e9,-3.0e-3。</P>
                  <P>3. 字符型常量是单引号内的字符,如‘a’,‘d’等,不能显示的控制字符,能 
                  在该字符前面加一个反斜杠“\”组成专用转义字符。常用转义字符表请看表 4-1。</P>
                  <P>4. 字符串型常量由双引号内的字符组成,如“test”,“OK”等。当引号内的没有字 
                  符时,为空字符串。在使用特殊字符时同样要使用转义字符如双引号。在 C 中字符 
                  串常量是做为字符类型数组来处理的,在存储字符串时系统会在字符串尾部加上\o 
                  转义字符以作为该字符串的结束符。字符串常量“A”和字符常量‘A’是不一样的, 前者在存储时多占用一个字节的字间。</P>
                  <P>5. 位标量,它的值是一个二进制。</P>
                  <TABLE cellSpacing=0 cellPadding=0 border=1>
                    <TBODY>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>转义字符</P></TD>
                      <TD width=189 vborder="0">
                        <P>含义</P></TD>
                      <TD width=189 vborder="0">
                        <P>ASCII 码(16/10 进制)</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\o</P></TD>
                      <TD width=189 vborder="0">
                        <P>空字符(NULL)</P></TD>
                      <TD width=189 vborder="0">
                        <P>00H/0</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\n</P></TD>
                      <TD width=189 vborder="0">
                        <P>换行符(LF)</P></TD>
                      <TD width=189 vborder="0">
                        <P>0AH/10</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\r</P></TD>
                      <TD width=189 vborder="0">
                        <P>回车符(CR)</P></TD>
                      <TD width=189 vborder="0">
                        <P>0DH/13</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\t</P></TD>
                      <TD width=189 vborder="0">
                        <P>水平制表符(HT)</P></TD>
                      <TD width=189 vborder="0">
                        <P>09H/9</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\b</P></TD>
                      <TD width=189 vborder="0">
                        <P>退格符(BS)</P></TD>
                      <TD width=189 vborder="0">
                        <P>08H/8</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\f</P></TD>
                      <TD width=189 vborder="0">
                        <P>换页符(FF)</P></TD>
                      <TD width=189 vborder="0">
                        <P>0CH/12</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\'</P></TD>
                      <TD width=189 vborder="0">
                        <P>单引号</P></TD>
                      <TD width=189 vborder="0">
                        <P>27H/39</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\"</P></TD>
                      <TD width=189 vborder="0">
                        <P>双引号</P></TD>
                      <TD width=189 vborder="0">
                        <P>22H/34</P></TD></TR>
                    <TR>
                      <TD width=189 vborder="0">
                        <P>\\</P></TD>
                      <TD width=189 vborder="0">
                        <P>反斜杠</P></TD>
                      <TD width=189 vborder="0">
                        <P>5CH/92</P></TD></TR></TBODY></TABLE>
                  <P>表 4-1&nbsp;&nbsp; &nbsp;常用转义字符表</P>
                  <P>常量可用在不必改变值的场合,如固定的数据表,字库等。常量的定义方式有几种,下 面来加以说明。</P>
                  <P>#difine &nbsp;False &nbsp;0x0; &nbsp;//用预定义语句能定义常量</P>
                  <P>#difine &nbsp;True &nbsp;0x1; &nbsp;//这里定义 False 为 0,True 为 
                  1</P>
                  <P>//在程序中用到 False 编译时自动用 0 替换,同理 True 替换为 1</P>
                  <P>unsigned &nbsp;int &nbsp;code &nbsp;a=100; &nbsp;//这一句用 
                  code 把 a 定义在程序存储器中并赋值</P>
                  <P>const &nbsp;unsigned &nbsp;int &nbsp;c=100; &nbsp;//用 const 
                  定义 c 为无符号 int 常量并赋值 以上两句它们的值都保存在程序存储器中,而程序存储器在运行中是不允许被修改的,</P>
                  <P>所以如果在这两句后面用了类似 a=110,a++这样的赋值语句,编译时将会出错。</P>
                  <P>下面写个跑马灯程序来实验一下典型的常量使用方法。先来看看电路图吧。它是在上一篇的</P><BR 
                  style="PAGE-BREAK-BEFORE: auto" clear=all>
                  <DIV class=Section17>
                  <P>实验电路的基础上增加几个 LED 组成的,也就是用 P1 口的全部引脚分别驱动一个 LED,电 路如图 4-1 
                  所示。</P>
                  <P>新建一个 RunLED 的项目,主程序如下:</P>
                  <P>#include &nbsp;&lt;AT89X51.H&gt; 
                  &nbsp;//预处理文件里面定义了特殊寄存器的名称如 P1 口定义为 P1</P>
                  <P>void &nbsp;main(void)</P>
                  <P>{</P>
                  <P>//定义花样数据</P>
                  <P>const &nbsp;unsigned &nbsp;char 
                  &nbsp;design[32]={0xFF,0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F,</P>
                  <P>0x7F,0xBF,0xDF,0xEF,0xF7,0xFB,0xFD,0xFE,0xFF,</P>
                  <P>0xFF,0xFE,0xFC,0xF8,0xF0,0xE0,0xC0,0x80,0x0,</P>
                  <P>0xE7,0xDB,0xBD,0x7E,0xFF};</P>
                  <P>unsigned &nbsp;int &nbsp;a; &nbsp;//定义循环用的变量</P>
                  <P>unsigned &nbsp;char &nbsp;b; &nbsp;//在 c51 
                  编程中因内存有限尽可能注意变量类型的使用</P>
                  <P>//尽可能使用少字节的类型,在大型的程序中很受用</P></DIV>
                  <DIV class=Section18>
                  <P>do{</P>
                  <P>for &nbsp;(b=0; &nbsp;b&lt;32; &nbsp;b++)</P>
                  <P>{</P></DIV><BR style="PAGE-BREAK-BEFORE: auto" clear=all>
                  <DIV class=Section19>
                  <P>}</P>
                  <P>}while(1);</P>
                  <P>}</P>
                  <P>for(a=0; &nbsp;a&lt;30000; &nbsp;a++); &nbsp;//延时一段时间</P>
                  <P>P1 &nbsp;= &nbsp;design[b]; &nbsp;//读已定义的花样数据并写花样数据到 P1 
                  口</P>
                  <P>程序中的花样数据能自以去定义,因这里我们的 LED 要 AT89c51 的 P1 引脚为低电平才 会点亮,所以我们要向 
                  P1 口的各引脚写数据 O 对应连接的 LED 才会被点亮,P1 口的八个引 脚刚好对应&nbsp; P1&nbsp; 
                  口特殊寄存器的八个二进位,如向 &nbsp;P1&nbsp; 口定数据 &nbsp;0xFE,转成二进制就是</P>
                  <P>11111110,最低位 D0 为 0 这里 P1.0 引脚输出低电平,LED1 被点亮。如此类推,大家不难算 
                  出自己想要做的效果了。大家编译烧写看看,效果就出来,显示的速度您能根据需要调整 延时 a 
                  的值,不要超过变量类型的值域就很行了。哦,您还没有实验板?那如何能知道程 序运行的结果呢?呵,不用急,这就来说说用 KEIL 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -