【正文】
PLS、 DIN、 SFT、 END等基本邏輯指令,建立基于單片機(jī)的 PLC源程序語句標(biāo)準(zhǔn)集合。 表 1. 6中入口符號(hào)地址 THTL處為 TO中斷服務(wù)子程序。等編譯程序中執(zhí)行“掃描源程序文件,逐條翻譯... 時(shí),得到源程序 提供的定時(shí)器初值,再在用正式代碼 (以 MOV指令目標(biāo)代碼的形式,并含定時(shí)器初值 ) 代替相應(yīng)位置上的盲碼。 0066H. 00C5H單元共 96個(gè)字節(jié)用于定時(shí)器初值設(shè)定子程序預(yù)留空間。 表 1. 6 基于單片機(jī)的 PLC目標(biāo)程序框架結(jié)構(gòu) 在表 1. 6所示的基于單片機(jī)的 PLC目標(biāo)程序框架結(jié)構(gòu)中,專用地址矢量、延時(shí) 20ms子程序、 CPU和接口初始化子程序、定時(shí)器初值設(shè)定子程序、 TO中斷服務(wù)子程序、主程序開始初始化設(shè)定、輸入刷新,這些均屬于固定目標(biāo)代碼。而活動(dòng)代碼的 裝配則涉及到對(duì)源程序語句的識(shí)別、查錯(cuò)、 17 翻譯生成目標(biāo)代碼等過程,是編譯程序主要需要解決的問題。固定代碼和活動(dòng)代碼均采用單片機(jī)的機(jī)器指令代碼。其中,固定代碼的編譯結(jié)果基本是固定的。在基于單片機(jī)的 PLC編譯問題中,目標(biāo)程序框架和單片機(jī)的芯片編程相關(guān),諸如 CPU初始化、內(nèi)部硬件定時(shí)器設(shè)定、接口的初始化等。 DIN Mi指令的單片機(jī)程序模塊如下: MOV Gi, C ;僅僅一條單片機(jī)指令。其功能是將 輸入端的邏輯值暫存在 Gi中,供 SFT指令使用;第二個(gè)是對(duì)應(yīng)于 SFT Mi的移位操作 程序模塊,其功能是依據(jù)已知的當(dāng)前邏輯運(yùn)算值 (在累加器 C中 ),以及 Hi的值,判斷 16 移位脈沖是否到;當(dāng)移位脈沖到時(shí),將輸入暫存位 Gi中的值移入移位寄存器 Mi。移位類 運(yùn)算程序由三個(gè)程序模塊組成。 1. 5 移位類語句的單片機(jī)程序模塊的構(gòu)建 共提供了 4個(gè) 8位移位寄存器。程序中通過對(duì)本次輸入 (累加器 C)和前次 輸入 (Pi)進(jìn)行判斷,當(dāng) C=I同時(shí) Pi=0時(shí)才產(chǎn)生脈沖微分信號(hào) (Yi=1)。它們分別是:①脈沖微分寄存器的邏輯位 Yi,位地址 08H. 0FH;②用于寄存前次輸入的寄存器 Pi,位地址 18H. 1FH。 CNT Ci計(jì)數(shù)指令的單片機(jī)程序模塊如下: 14 1. 4 PLS. 8 Yi 脈沖微分語句的單片機(jī)程序模塊的構(gòu)建 本系統(tǒng)共設(shè)有 8個(gè)脈沖微分寄存器。 CNT指令的單片機(jī)程序模塊用 Ci表示計(jì)數(shù)器的邏輯位,位地 址 30H. 3FH;用 ni表示計(jì)數(shù)器的計(jì)數(shù)脈沖前沿識(shí)別位,位地址 50H一 5FH;用 Di(雙字節(jié)寄存器 )表示軟加 1計(jì)數(shù)器, Di由 Di, 0和 Di, 1組成;用 D’i表示計(jì)數(shù)器的 預(yù)置值寄存器 (雙字節(jié)寄存器 ), D39。 圖 1. 2 TIM Ti邏輯指令的單片機(jī)程序模塊流程圖 13 1. 3 CNT Ci 語句的單片機(jī)程序模塊的構(gòu)建 計(jì)數(shù)器和計(jì)時(shí)器使用相同的片內(nèi)資源。這是一個(gè)對(duì)第 i個(gè)延時(shí)計(jì)時(shí)器進(jìn)行邏輯運(yùn)算處理的模塊,運(yùn)算的依據(jù)是已知的當(dāng)前邏輯運(yùn)算值 (在累加器 C中 )。其流程見圖 2. 1。 1. 2. 2 TIM Ti所需的 To中斷服務(wù)程序 單片機(jī)初始化時(shí),將 To設(shè)定成定時(shí)方式 l,并開放 To中斷。.., 4EH,4FH。., 6EH, 6FH。實(shí)現(xiàn) TIM指令所需要的片內(nèi)資源有:①延時(shí)計(jì)時(shí)器的邏輯位 Ti,位地址 30H. 3FH;②延時(shí)計(jì)時(shí)器的狀態(tài)標(biāo)志位 Si,位地址 40H. 4FH;⑨延時(shí)計(jì)時(shí)器的軟加 1計(jì)數(shù)器 Zi, Zi由兩字節(jié)寄存器組成,由 TO中斷服務(wù)程序?qū)ζ鋵?shí)行加 1計(jì)數(shù),可記錄 4位 BCD數(shù),字節(jié) 地址 50H, 51H, 52H, 53H, TO設(shè) 定成定時(shí) 0. 1s,每隔O. 1sT0中斷一次。 基于單片機(jī)的 PLC的 TIM Ti語句的單片機(jī)程序模塊構(gòu)建 1. 2. 1 圍繞 TIM Ti片內(nèi)硬件資源的配置 梯形圖中的延時(shí)計(jì)時(shí)器通??梢杂卸鄠€(gè),它們需要有一個(gè)統(tǒng)一的時(shí)鐘。 (如延時(shí)計(jì)時(shí)器類、脈沖微分類、計(jì)數(shù)器類、移位類等 )的單片機(jī)程序模塊需十幾條或 幾十條單片機(jī)指令組成。表 1. 1 基于單片機(jī)的 PLC 10 中常用 PLC邏輯指令的單片機(jī)程序模塊序號(hào) PLC邏輯指令單片機(jī)程序模塊 (指令 ) 目標(biāo)程序代碼 序號(hào) PLC邏輯指令 單片機(jī)模塊(指令) 目標(biāo)程序代碼 1 LD bit MOV C, bit A2, bit 2 LDI bit MOV C, bit CPL C A2, bit B3 3 AND bit ANL C, bit 82, bit 4 ANI bit ANL C, bit B0, bit 5 OR bit ORL C, bit 72, bit 6 ORI bit ORL C, bit A0, bit 7 STORE bit MOV C, bit 92, bit 8 OUT bit CPL C MOV bit, C B3 92, bit 表 1. 1 基于單片機(jī)的 PLC中常用 PLC邏輯指令的單片機(jī)程序模塊 在表 1. 1的邏輯指令中,運(yùn)算都圍繞位累 加器 C進(jìn)行。 AND、 ANI、 OR、 ORI、 LD、 LDI、 OUT、 STORE指令的單片機(jī)程序 (指令 )如表 2. 1所示。但是,這些基本邏輯指令所對(duì) 應(yīng)的單片機(jī)程序模塊的程序長短不一,有的只有 l~2條單片機(jī)指令組成,有的則需十幾條或幾十條單片機(jī)指令組成。為了設(shè)計(jì)出基于單片機(jī)的 PLC編譯程序,首先要構(gòu)建出每一條源程序指令所 對(duì)應(yīng)的單片機(jī)程序模塊。此外在設(shè)計(jì)中采用盲碼預(yù)填及修正技術(shù)。設(shè)定目標(biāo)程序框架結(jié)構(gòu),是本課題的另一項(xiàng)主要工作任務(wù)。因此在編譯程序編寫前,必須根據(jù)單片機(jī)芯片編程的特點(diǎn)設(shè)定目標(biāo)程序框架結(jié)構(gòu)。 (2)其次,目標(biāo)程序框架結(jié)構(gòu)也不同。 9 (1)首先,當(dāng)從市場上選來新~代的單片機(jī)芯片作為執(zhí)行符合梯形圖邏輯控制的 邏輯控制器,那么從總體上來看,基于該單片機(jī)芯片的 PLC編譯程序的設(shè)計(jì)就和其他 PLC的編譯程序設(shè)計(jì)不同,沒有一個(gè)現(xiàn)成的編譯程序可以套用,必須做創(chuàng)造性的工作, 獨(dú)立完成基于該單片機(jī)的編譯程序設(shè)計(jì)。為了大大降低成本,實(shí)現(xiàn)如前所述的,在 機(jī)電一體化產(chǎn)品和自動(dòng)化裝置的設(shè)計(jì)中直接用單片機(jī)芯片作為符合梯形圖邏輯的邏輯控制器, 就必須設(shè)計(jì)出可以將 PLC指令源程序翻譯成單片機(jī)目標(biāo)程序的編譯程序。世界上現(xiàn)成的 PLC產(chǎn)品很多,每一 個(gè) PLC產(chǎn)品都有一個(gè) PLC編譯程序。 indicated that the piler can translate correctly the source program of PLC based on monolithic microputer into the target program which is enable to be executed by monolithic microputer and tallies the request of ladder diagram logic contr01. The experimental result proved that the piler designed for PLC based on the monolithic microputer conforms to the design requirement pletely,may use in the project development. Because the PLC based on the monolithic microputer is extremely inexpensive in cost, it may bring the good economic efficiency when practical application, thus has the strong usability. Key words: Monolithic microputer,programmable logic controller,piler,blind code, source program, target program 前 言 前人在單片機(jī)應(yīng)用研究和 PLC應(yīng)用研究方面已經(jīng)取得了豐碩的成果。 關(guān)鍵詞 : 單片機(jī),可編程邏輯控制器,編譯程序,盲碼,源程序,目標(biāo)程序 7 Abstract Sometimes, one kind of extremely inexpensive logic controller is needed in design and the development of the automation device and the integration oelectro—mechanical product. In order to meet this kind of needs. the author carried Oll the hardware and the sofhvare research of having directly realized the ladder diagram logic control with the monolithic microputer. In which. most essential technology is how to design the piler for PLC based on monolithic microputer,and is how to translate PLC instruction source program which conf01Tn ladder diagram logic into monolithic microputer targetprogram. This paper elaborated design theory and the method of the piler for PLC based on the monolithic microputer,has studied the monolithic microputer program module and the target program frame for PLC based on the monolithic microputer,has c ried on the analysis to . elaborated design method for the checking mistaken module as well as the assembly method for goal code, has carried on the analysis to expedmental result and debugging experiment system position for the piler for PLC based on the monolithicmicroputer. First,the research on the monolithic microputer program module for PLC based on the monolithic microputer has been conducted. The alone characteristic frame construction for targetprogram has been constructed. Then. the paper has defined the standard set of the source program sentence for PLC based on the monolithic microputer,proposed the array expression method for the standard setof the source program sentence. The goal code set has been defined. Establishing the relations between the source program sentence standard set and the goal code set with sigma the mapping has been proposed, so that the condition and the method for realization of the pile structure has been provided. A thought ofthe filling blind code in advance and the revising blind code has been proposed。實(shí)驗(yàn)結(jié)果表明,本文所設(shè)計(jì)的基于單片機(jī)的 PLC編譯程序完全符合設(shè)計(jì)要求,可以用于項(xiàng)目開發(fā)。建立了基于單片機(jī)的 PLC的編譯程序設(shè)計(jì)研究的實(shí)驗(yàn)系統(tǒng),對(duì)該編譯程序的各項(xiàng)功能進(jìn)行了驗(yàn)證和比對(duì)。首先,進(jìn)行了基于單片機(jī)的 PLC的單片機(jī)程序模塊研究,構(gòu)建了獨(dú)具特點(diǎn)的目標(biāo)程序框架結(jié)構(gòu)。其中最為關(guān)鍵的一項(xiàng)技術(shù)是如何設(shè)計(jì)出基 于單片機(jī)的 PLC編譯程序,將符合梯形圖邏輯的 PLC指令源程序翻譯成單片機(jī)目標(biāo)程序。 1