石家庄论坛's Archiver

lyq_4652 发表于 2008-8-7 13:44

科威PLC芯片组开发实例(九)

[font=宋体]实战攻坚![/font]
[font=Times New Roman] [/font]
[font=宋体]上面一讲我们讲述了该如何对照原理图的引脚分配情况来配置你的[/font][font=Times New Roman]INIT_CONFIG[/font][font=宋体]子程序,这一讲笔者说说其他[/font][font=Times New Roman]3[/font][font=宋体]个初始化应该如何编写。[/font]
[font=Times New Roman] [/font]
[font=宋体]我们先在[/font][font=Times New Roman]USER_DATA[/font][font=宋体]中定义几个用户变量:[/font]
[font=Times New Roman]INPUT_PORTA

EQU
DRAM_USER[/font]
[font=Times New Roman]INPUT_CNT


EQU
DRAM_USER+1[/font]
[font=Times New Roman]XINPUT


EQU
XRAM_USER[/font]
[font=Times New Roman] [/font]
[font=宋体]在解释这些变量的含义之前,我们首先聊下[/font][font=Times New Roman]PLC[/font][font=宋体]的工作状态。[/font]
[font=Times New Roman]PLC[/font][font=宋体]的输入端口是和梯形图中的[/font][font=Times New Roman]X[/font][font=宋体]端口相对应的,也就是说,短接[/font][font=Times New Roman]I0[/font][font=宋体]和[/font][font=Times New Roman]COM0([/font][font=宋体]输入端口统一地端子[/font][font=Times New Roman])[/font][font=宋体],梯形图中的[/font][font=Times New Roman]X0[/font][font=宋体]将会吸合,反应在元件监控画面上[/font][font=Times New Roman]X0[/font][font=宋体]右边会出现绿色的实心小正方形,表示它的触点已被吸合。同理,[/font][font=Times New Roman]I0~I7[/font][font=宋体]对应的便是[/font][font=Times New Roman]X0~X7[/font][font=宋体]。[/font]
[font=宋体]而[/font][font=Times New Roman]PLC[/font][font=宋体]的输出端口是和梯形图中的[/font][font=Times New Roman]Y[/font][font=宋体]端口相对应的,当用户在梯形图中把[/font][font=Times New Roman]Y0[/font][font=宋体]置[/font][font=Times New Roman]1[/font][font=宋体]或是元件监控里把[/font][font=Times New Roman]Y0[/font][font=宋体]强制置[/font][font=Times New Roman]1[/font][font=宋体],那么[/font][font=Times New Roman]O0[/font][font=宋体]端子便和[/font][font=Times New Roman]COM1[/font][font=宋体]自动短接了。同理,[/font][font=Times New Roman]O0~O5[/font][font=宋体]对应的便是[/font][font=Times New Roman]Y0~Y5[/font][font=宋体]。在[/font][font=Times New Roman]PLC[/font][font=宋体]端子口上是看不到[/font][font=Times New Roman]I0[/font][font=宋体]和[/font][font=Times New Roman]O0[/font][font=宋体]的,但是有它们对应的[/font][font=Times New Roman]X0[/font][font=宋体]和[/font][font=Times New Roman]Y0[/font][font=宋体]。[/font]
[font=宋体]下面我们讲讲这几个用户变量:[/font]
[font=Times New Roman]INPUT_PORTA[/font][font=宋体]:用来保存当前[/font][font=Times New Roman]8[/font][font=宋体]个输入端口的输入状态[/font][font=Times New Roman](X0~X7)[/font][font=宋体]的。[/font]
[font=Times New Roman]INPUT_CNT[/font][font=宋体]:输入端子采样次数指针。[/font]
[font=Times New Roman]XINPUT[/font][font=宋体]:输入端子采样值缓冲区。[/font]
[font=Times New Roman] [/font]
[font=宋体]这些概念朋友们先大概了解一下便好,至于怎么应用到它们,后面还会具体讲到。[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]INIT_START[/font][font=宋体]接下来,我们开始编写[/font][font=Times New Roman]INIT_START[/font][font=宋体]用户驱动子程序了,添加的代码如下:[/font]
[font=Times New Roman]MOV
INPUT_CNT,#08H
;1[/font]
[font=Times New Roman]CLR
A


;2[/font]
[font=Times New Roman]MOV
DPTR,#RAM_PX+1
;3[/font]
[font=Times New Roman]MOVX
@DPTR,A
;4[/font]
[font=Times New Roman]MOV
DPTR,#RAM_PY+1
;5[/font]
[font=Times New Roman]MOVX
@DPTR,A
;6[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]1.
[/font][font=宋体]我们将[/font][font=Times New Roman]INPUT_CNT[/font][font=宋体]这个变量初始化为[/font][font=Times New Roman]8[/font][font=宋体],这也就意味着,采样的次数我们限定它为[/font][font=Times New Roman]8[/font][font=宋体]次,如果你设置成[/font][font=Times New Roman]4[/font][font=宋体],那么输入端口的采样次数就为[/font][font=Times New Roman]4[/font][font=宋体],现在只要知道这些就好,我们以后在讲到[/font][font=Times New Roman]TMS[/font][font=宋体]这个用户驱动子程序时,会详细给大家分析为什么采样输入端口以及如何采样它。[/font]
[font=Times New Roman]2.
[/font][font=宋体]不用讲了吧[/font][font=Times New Roman]…[/font]
[font=Times New Roman]3.
[/font][font=宋体]这个是将间接寻址指针指向[/font][font=Times New Roman]X0~X7[/font][font=宋体]所在的地址单元,间接寻址指针的表达方式有[/font][font=Times New Roman]2[/font][font=宋体]种,笔者惯用的是以[/font][font=Times New Roman]DPTR[/font][font=宋体]形式来进行寻址。至于另外一种,呵呵,自己查说明书吧。[/font]
[font=Times New Roman]4.
[/font][font=宋体]这句话的意思是把寄存器[/font][font=Times New Roman]A[/font][font=宋体]的值赋给[/font][font=Times New Roman]X0~X7[/font][font=宋体],因为[/font][font=Times New Roman]A[/font][font=宋体]的值为[/font][font=Times New Roman]0[/font][font=宋体],实际上这句话的作用就是把[/font][font=Times New Roman]X0~X7[/font][font=宋体]所代表的寄存器的值清零。看到这里,可能有的朋友就又有问题了,为什么[/font][font=Times New Roman]X0~X7[/font][font=宋体]所在的地址不是[/font][font=Times New Roman]#RAM_PX[/font][font=宋体]而是[/font][font=Times New Roman]#RAM_PX+1[/font][font=宋体]呢,其实这个是和内核定义梯形图单元的寻址地址有关。内核规定,梯形图的单元寄存器都是低位在后,高位在前,也就是说,[/font][font=Times New Roman]#RAM_PX[/font][font=宋体]并不是对应的[/font][font=Times New Roman]X0~X7[/font][font=宋体],而是[/font][font=Times New Roman]X10~X17[/font][font=宋体]。[/font]
[font=Times New Roman]5.
[/font][font=宋体]这个是将间接寻址指针指向[/font][font=Times New Roman]Y0~Y7[/font][font=宋体]所在的地址单元。[/font]
[font=Times New Roman]6.
[/font][font=宋体]同[/font][font=Times New Roman]4[/font][font=宋体]。[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]INIT_RUN[/font][font=宋体]直接让它返回吧,在这个系统里面,没必要配置这个。[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]INIT_SET[/font][font=宋体]然后紧接着配置[/font][font=Times New Roman]INIT_SET[/font][font=宋体]:[/font]
[font=Times New Roman]CLR
A


[/font]
[font=Times New Roman]MOV
DPTR,#RAM_PX+1
[/font]
[font=Times New Roman]MOVX
@DPTR,A
[/font]
[font=Times New Roman]MOV
DPTR,#RAM_PY+1
[/font]
[font=Times New Roman]MOVX
@DPTR,A
[/font]
[font=宋体]这里也是清除[/font][font=Times New Roman]X0~X7[/font][font=宋体]和[/font][font=Times New Roman]Y0~Y7[/font][font=宋体]的寄存器值,那为什么在这里又要清零呢?会不会跟之前的重复了呢?[/font]
[font=宋体]当然不会,注意到系统会在什么时候调用[/font][font=Times New Roman]INIT_SET[/font][font=宋体]呢?是在内核重新上电后进入梯型图下载状态或[b]内核从梯型图运型状态进入梯型图下载状态时调用此程序一次。[/b][/font][b][/b]
[font=宋体]看到了吧,[/font][font=Times New Roman]PLC[/font][font=宋体]在不断电的情况下,也会在上面黑体的部分调用到它,举个例子,[/font][font=Times New Roman]PLC[/font][font=宋体]在运行过程当中,[/font][font=Times New Roman]Y3[/font][font=宋体]处于闭合状态,现在你要重新灌梯形图了,这个时候你当然希望[/font][font=Times New Roman]Y3[/font][font=宋体]不再闭合了,而是等你重新下载梯形图完毕以后,系统重新判断[/font][font=Times New Roman]Y3[/font][font=宋体]到底该不该为[/font][font=Times New Roman]1[/font][font=宋体]。由于这个时候[/font][font=Times New Roman]PLC[/font][font=宋体]并不断电,那么调用[/font][font=Times New Roman]INIT_START[/font][font=宋体]也就无从谈起,也就无法调用那里面的清零语句。所以你必须要在[/font][font=Times New Roman]INIT_SET[/font][font=宋体]把它两给的值清除了,才能保证[/font][font=Times New Roman]PLC[/font][font=宋体]的正常工作。[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]STEP[/font][font=宋体]也是空的,没必要配置[/font][font=Times New Roman]~[/font]
[font=Times New Roman] [/font]
[font=宋体]下一讲里,我们讲[/font][font=Times New Roman]TMS[/font][font=宋体]用户驱动子程序,也就是用户[/font][font=Times New Roman]2.5ms[/font][font=宋体]中断了,这个可是个重点哦[/font][font=Times New Roman]~^-^~![/font]
[font=Times New Roman] [/font]
[font=宋体]本帖首发中国工控网科威[/font][font=Times New Roman]PLC[/font][font=宋体]论坛,转载请声明![/font]

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.