科威PLC芯片组开发实例(十)
[font=宋体]实战攻坚![/font][font=Times New Roman] [/font]
[font=宋体]下面我们讲讲该在[/font][font=Times New Roman]TMS[/font][font=宋体]用户驱动子程序里面写些什么代码。[/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]PLC[/font][font=宋体]对[/font][font=Times New Roman]X[/font][font=宋体]端口的状态的判断,那么我们必须得剔除掉这些毛刺,使系统工作在一个稳定的环境。[/font]
[font=宋体]我们规定,如果连续采样[/font][font=Times New Roman]8[/font][font=宋体]次[/font][font=Times New Roman]X[/font][font=宋体]端口的值都是一致的,就判断此刻[/font][font=Times New Roman]X[/font][font=宋体]端口的状态是实际的输入状态,这个也就是为什么我们把[/font][font=Times New Roman]INPUT_CNT[/font][font=宋体]这个变量设置为[/font][font=Times New Roman]8[/font][font=宋体]的原因。[/font]
[font=Times New Roman] [/font]
[font=宋体]还是老规矩,笔者贴出[/font][font=Times New Roman]TMS[/font][font=宋体]的代码并为大家分析:[/font]
[font=Times New Roman]TMS[/font][font=Times New Roman]TMS:
MOV
C,KEY_SET
;1[/font]
[font=Times New Roman]
MOV
P4.3,C
;2[/font]
[font=Times New Roman]
MOV
C,PLC_PRO_ERR
;3[/font]
[font=Times New Roman]
CPL
C
;4[/font]
[font=Times New Roman]
MOV
P4.2,C
;5[/font]
[font=Times New Roman]
MOV
A,INPUT_CNT
;6[/font]
[font=Times New Roman]
JNZ
TMS1
;7[/font]
[font=Times New Roman]
RET
[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]TMS1:
DEC
A
;8 [/font]
[font=Times New Roman]
MOV
DPTR,#XINPUT
;9[/font]
[font=Times New Roman]
ADD
A,DPL
;10[/font]
[font=Times New Roman]
MOV
DPL,A
;11[/font]
[font=Times New Roman]
MOV
A,DPH
;12[/font]
[font=Times New Roman]
ADDC
A,#00H
;13[/font]
[font=Times New Roman]
MOV
DPH,A
;14
[/font]
[font=Times New Roman]
MOV
A,P3
;15[/font]
[font=Times New Roman]
MOVX
@DPTR,A
;16[/font]
[font=Times New Roman]
DEC
INPUT_CNT
;17[/font]
[font=Times New Roman]
RET[/font]
[font=Times New Roman] [/font]
[font=Times New Roman]1.
[/font][font=宋体]第[/font][font=Times New Roman]1~5[/font][font=宋体]句:这[/font][font=Times New Roman]4[/font][font=宋体]条语句是给[/font][font=Times New Roman]RUN/SET[/font][font=宋体]和[/font][font=Times New Roman]ERR[/font][font=宋体]两个灯赋状态的。在原理图中看到,由于[/font][font=Times New Roman]ERR[/font][font=宋体]的[/font][font=Times New Roman]LED[/font][font=宋体]与实际的[/font][font=Times New Roman]P4.2[/font][font=宋体]中间反了一次向,故此在程序中也需要对此端口反向赋值。[/font]
[font=Times New Roman]2.
[/font][font=宋体]第[/font][font=Times New Roman]6[/font][font=宋体]句:取[/font][font=Times New Roman]INPUT_CNT[/font][font=宋体]当前值赋给寄存器[/font][font=Times New Roman]A[/font]
[font=Times New Roman]3.
[/font][font=宋体]第[/font][font=Times New Roman]7[/font][font=宋体]句:这句是一句跳转指令,作用是看对[/font][font=Times New Roman]X[/font][font=宋体]输入端口当前的[/font][font=Times New Roman]8[/font][font=宋体]次采样完成了没有,如果完成了,就直接返回,在[/font][font=Times New Roman]SCAN[/font][font=宋体]用户驱动子程序会对其[/font][font=Times New Roman]X0~X7[/font][font=宋体]寄存器的值进行更新;如果[/font][font=Times New Roman]8[/font][font=宋体]次采样没有完成,则跳转到[/font][font=Times New Roman]TMS1[/font][font=宋体]中进行[/font][font=Times New Roman]X[/font][font=宋体]输入端口采样。[/font]
[font=Times New Roman]4.
[/font][font=宋体]第[/font][font=Times New Roman]8~16[/font][font=宋体]句:将[/font][font=Times New Roman]P3[/font][font=宋体]端口的值(即当前[/font][font=Times New Roman]X[/font][font=宋体]输入端口的采样值)存放到从[/font][font=Times New Roman]#XINPUT+7[/font][font=宋体]开始到[/font][font=Times New Roman]#XINPUT[/font][font=宋体]一共[/font][font=Times New Roman]8[/font][font=宋体]个单元。值得注意的是,完成[/font][font=Times New Roman]8[/font][font=宋体]次采样的时间为[/font][font=Times New Roman]2.5ms * 8 = 20ms[/font][font=宋体]。也就是说,从第[/font][font=Times New Roman]1[/font][font=宋体]次采样开始到[/font][font=Times New Roman]8[/font][font=宋体]次采样结束需要[/font][font=Times New Roman]20ms[/font][font=宋体]的时间。结束时,[/font][font=Times New Roman]#XINPUT~#XINPUT+7[/font][font=宋体]这[/font][font=Times New Roman]8[/font][font=宋体]个单元的地址已被填充,等待[/font][font=Times New Roman]SCAN[/font][font=宋体]用户驱动子程序的处理。这段程序通过改变采样指针[/font][font=Times New Roman]DPTR[/font][font=宋体]的[/font][font=Times New Roman]DPH[/font][font=宋体]和[/font][font=Times New Roman]DPL[/font][font=宋体],使其指向不同的外部寻址单元,这个小技巧希望大家能够熟练掌握。[/font]
[font=Times New Roman]5.
[/font][font=宋体]第[/font][font=Times New Roman]17[/font][font=宋体]句:将[/font][font=Times New Roman]INPUT_CNT[/font][font=宋体]减[/font][font=Times New Roman]1[/font][font=宋体]。不要忘记这句话,否则你的程序将始终填充[/font][font=Times New Roman]#XINPUT+7[/font][font=宋体]这个单元的数据,采样指针永远也指不到[/font][font=Times New Roman]#XINPUT+6[/font][font=宋体]和其以下的单元。[/font]
[font=Times New Roman] [/font]
[font=宋体]好了,[/font][font=Times New Roman]TMS[/font][font=宋体]就给大家分析完了,下一讲笔者将给大家讲述最后一个部分:[/font][font=Times New Roman]SCAN[/font][font=宋体]。[/font]
[font=Times New Roman] [/font]
[font=宋体]本帖首发中国工控网科威[/font][font=Times New Roman]PLC[/font][font=宋体]论坛,转载请声明![/font]
[font=Times New Roman] [/font]
页:
[1]