51单片机-PASCAL语言实例9:串口编程之多字节收发

// Test9.pas 
Program NewProgram;
// 本程序使用 Trubo51( https://turbo51.com/ ) 单片机 PASCAL 编译器
// 
// 
// 实例 9 演示了单片机串口 按自定协议收发串口数据
// 假设有PC可以发送如下多种命令给单片机        
//     .打开或关闭指定的LED灯 
//     .打开或关闭蜂鸣器
//     .查询LED灯的状态或蜂鸣器状态
//  为此,设计PC发送的命令帧为 4 个字节:
//     起始|命令|设备号|检验
//     起始:固定为 $01
//     命令
//          $4F(字符O) : 开启
//          $43(字符C) : 关闭
//          $51(字符Q ): 查询 
//        检验为前面三个字符值按位与之值
//    设备号:$31(字符 0)--$38(字符 7) LED1 -- LED8 
//              $39 (字符 9)  蜂鸣器
              
              
//      单片机发回PC的应答帧为 4 个字节:起始|设备号|状态|检验 
//      起始:固定为 $01
//        设备:
//      命令
//          $4F(O ): 已开
//          $43(C ): 已关
//   
//        检验为前面三个字符值按位与之值  
    
//  CPU 电路图请看 实例 1 
//  
//  
uses
    Delay,MyDefine;
 
Type
    
    TCmdFrame=Array[1..4] of Byte ;
var
    RxFrame,TxFrame:TCmdFrame;
    RxCnt,TxCnt:byte;
    
const
    // 命令
    CMD_BEGIN=$01  ;
    CMD_OPEN= $4F  ;        // 开启
    CMD_CLOSE=$43  ;        // 关闭
    CMD_QUERY=$51  ;        // 查询
    
    // 设备状态
    DEV_ON=CMD_OPEN ;
    DEV_OFF=CMD_CLOSE;
    
    //设备编号
    DEV_LED1=$30 ; // LED1 
    DEV_LED2=$31 ;
    DEV_LED3=$32 ; 
    DEV_LED4=$33 ;
    DEV_LED5=$34 ;
    DEV_LED6=$35 ;
    DEV_LED7=$36 ;
    DEV_LED8=$37 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
串口是计算机上一种非常通用设备通信的协议(不要与通用串行总线Universal Serial Bus或者USB混淆)。大多数计算机包含两个基于RS232的串口串口同时也是仪器仪表设备通用的通信协议;很多GPIB兼容的设备也带有RS- 232口。同时,串口通信协议也可以用于获取远程采集设备的数据。 串口通信的概念非常简单,串口按位(bit)发送和接收字节。尽管比按字节(byte)的并行通信慢,但是串口可以在使用一根线发送数据的同时用另一根线接收数据。它很简单并且能够实现远距离通信。比如IEEE488定义并行通行状态时,规定设备线总常不得超过20米,并且任意两个设备间的长度不得超过2米;而对于串口而言,长度可达1200米。 典型地,串口用于ASCII码字符的传输。通信使用3根线完成:(1)地线,(2)发送,(3)接收。由于串口通信是异步的,端口能够在一根线上发送数据同时在另一根线上接收数据。其他线用于握手,但是不是必须的。串口通信最重要的参数是波特率、数据位、停止位和奇偶校验。对于两个进行通行的端口,这些参数必须匹配: a,波特率:这是一个衡量通信速度的参数。它表示每秒钟传送的bit的个数。例如300波特表示每秒钟发送300个bit。当我们提到时钟周期时,我们就是指波特率例如如果协议需要4800波特率,那么时钟是4800Hz。这意味着串口通信在数据线上的采样率为4800Hz。通常电话线的波特率为 14400,28800和36600。波特率可以远远大于这些值,但是波特率和距离成反比。高波特率常常用于放置的很近的仪器间的通信,典型的例子就是 GPIB设备的通信。 b,数据位:这是衡量通信中实际数据位的参数。当计算机发送一个信息包,实际的数据不会是8位的,标准的值是5、7和8位。如何设置取决于你想传送的信息。比如,标准的ASCII码是0~127(7位)。扩展的ASCII码是0~255(8位)。如果数据使用简单的文本(标准 ASCII码),那么每个数据包使用7位数据。每个包是指一个字节,包括开始/停止位,数据位和奇偶校验位。由于实际数据位取决于通信协议的选取,术语 “包”指任何通信的情况。 c,停止位:用于表示单个包的最后一位。典型的值为1,1.5和2位。由于数据是在传输线上定时的,并且每一个设备有其自己的时钟,很可能在通信中两台设备间出现了小小的不同步。因此停止位不仅仅是表示传输的结束,并且提供计算机校正时钟同步的机会。适用于停止位的位数越多,不同时钟同步的容忍程度越大,但是数据传输率同时也越慢。 d,奇偶校验位:在串口通信中一种简单的检错方式。有四种检错方式:偶、奇、高和低。当然没有校验位也是可以的。对于偶和奇校验的情况,串口会设置校验位(数据位后面的一位),用一个值确保传输的数据有偶个或者奇个逻辑高位。例如,如果数据是011,那么对于偶校验,校验位为0,保证逻辑高的位数是偶数个。如果是奇校验,校验位位1,这样就有3个逻辑高位。高位和低位不真正的检查数据,简单置位逻辑高或者逻辑低校验。这样使得接收设备能够知道一个位的状态,有机会判断是否有噪声干扰了通信或者是否传输和接收数据是否不同步。
以下是 Pascal 程序设计语言的文法实例: ``` <program> ::= program <identifier> ; <block> . <block> ::= <declaration-part> <statement-part> <declaration-part> ::= <label-declaration-part> <constant-definition-part> <type-definition-part> <variable-declaration-part> <procedure-and-function-declaration-part> <label-declaration-part> ::= label <label> { , <label> } ; <label> ::= <unsigned-integer> <constant-definition-part> ::= const <constant-definition> { ; <constant-definition> } ; <constant-definition> ::= <identifier> = <constant> <constant> ::= <unsigned-number> | <sign> <unsigned-number> | <string> | <identifier> <type-definition-part> ::= type <type-definition> { ; <type-definition> } ; <type-definition> ::= <identifier> = <type> <type> ::= <simple-type> | <structured-type> | <pointer-type> <simple-type> ::= <scalar-type> | <subrange-type> | <enumerated-type> | <set-type> <scalar-type> ::= <identifier> <subrange-type> ::= <constant> .. <constant> <enumerated-type> ::= ( <identifier> { , <identifier> } ) <set-type> ::= set of <base-type> <base-type> ::= <scalar-type> <structured-type> ::= <array-type> | <record-type> <array-type> ::= array [ <index-range> { , <index-range> } ] of <component-type> <index-range> ::= <simple-type> <component-type> ::= <type> <record-type> ::= record <field-list> end <field-list> ::= <fixed-part> | <fixed-part> ; <variant-part> | <variant-part> <fixed-part> ::= <record-section> { ; <record-section> } ; <variant-part> ::= case <tag-field> of <variant> { ; <variant> } ; [ else <field-list> ] end ; <tag-field> ::= <identifier> : <type> <variant> ::= <variant-selector> : ( <field-list> ) <variant-selector> ::= <constant> .. <constant> | <identifier> <variable-declaration-part> ::= var <variable-declaration> { ; <variable-declaration> } ; <variable-declaration> ::= <identifier> { , <identifier> } : <type> <procedure-and-function-declaration-part> ::= <procedure-declaration> | <function-declaration> | <procedure-declaration> ; <function-declaration> <procedure-declaration> ::= procedure <identifier> ; [ <formal-parameter-list> ] ; <block> ; <function-declaration> ::= function <identifier> ; [ <formal-parameter-list> ] : <type> ; <block> ; <formal-parameter-list> ::= ( [ <parameter-group> { ; <parameter-group> } ] ) <parameter-group> ::= <identifier-list> : <parameter-type> <identifier-list> ::= <identifier> { , <identifier> } <parameter-type> ::= <simple-type> | <structured-type> | <identifier> <statement-part> ::= <compound-statement> <compound-statement> ::= begin <statement> { ; <statement> } end <statement> ::= <simple-statement> | <structured-statement> <simple-statement> ::= <assignment-statement> | <procedure-call-statement> | <goto-statement> | <empty-statement> <assignment-statement> ::= <variable-access> := <expression> <procedure-call-statement> ::= <identifier> [ <actual-parameter-list> ] <actual-parameter-list> ::= <expression> { , <expression> } <goto-statement> ::= goto <label> <empty-statement> ::= <structured-statement> ::= <compound-statement> | <conditional-statement> | <repetitive-statement> | <with-statement> <conditional-statement> ::= if <expression> then <statement> [ else <statement> ] <repetitive-statement> ::= <while-statement> | <repeat-statement> | <for-statement> <while-statement> ::= while <expression> do <statement> <repeat-statement> ::= repeat <statement> { ; <statement> } until <expression> <for-statement> ::= for <control-variable> := <for-list> do <statement> <control-variable> ::= <identifier> <for-list> ::= <initial-value> [ to | downto ] <final-value> <initial-value> ::= <expression> <final-value> ::= <expression> <with-statement> ::= with <record-variable-list> do <statement> <record-variable-list> ::= <variable-access> { , <variable-access> } <expression> ::= <simple-expression> | <simple-expression> <relational-operator> <simple-expression> <relational-operator> ::= = | <> | < | <= | >= | > <simple-expression> ::= <term> | <sign> <term> | <simple-expression> <adding-operator> <term> <sign> ::= + | - <adding-operator> ::= + | - | or <term> ::= <factor> | <term> <multiplying-operator> <factor> <multiplying-operator> ::= * | / | div | mod | and <factor> ::= <variable-access> | <unsigned-constant> | ( <expression> ) | <function-designator> <variable-access> ::= <variable> { <indexed-selector> | <field-selector> } <variable> ::= <identifier> <indexed-selector> ::= [ <expression> { , <expression> } ] <field-selector> ::= . <identifier> <unsigned-constant> ::= <unsigned-number> | <string> | <constant-identifier> <function-designator> ::= <identifier> [ <actual-parameter-list> ] <string> ::= '<character> { <character> }' <constant-identifier> ::= <identifier> ``` 注意:这只是 Pascal 的一个简单实现,实际上 Pascal 的语法非常复杂,因此这里只提供了一部分重要的规则。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值