【正文】
建立固定代碼數(shù)組 向目標(biāo)文件中裝配固定代碼 32 4. 1. 2 目標(biāo)文件 (. Bin)的建立 先用 fopen,以 rb的方式打開源程序文件。目標(biāo)文件中存放的是翻譯的結(jié)果,為二進(jìn)制的目標(biāo)代碼。 LDll 08 當(dāng)上述語句語法檢查發(fā)現(xiàn)錯誤時,就將錯誤標(biāo)記寫入列表文件。依照 3. 1. 1知, Mi中的元素 mi k的值等于源語句應(yīng)有的長度的 ASCII值,所以元素 mi k是語句長度核對 的標(biāo)準(zhǔn)。 3. 4. 2 核對關(guān)鍵詞是否相等 在 M數(shù)組的逐行搜索過程中,核對關(guān)鍵詞是否相等是語句語法檢查的重要環(huán)節(jié)。若能在 M數(shù)組中找到相同的,則該源程序語句語法正確。整句識別完成后,得到的整句存放在數(shù) 組 C中,供語句語法檢查用 。整句識別由讀入源語句字符、源程序文件尾判斷、語句尾判斷、源語句字符送C數(shù)組,這樣四部分組成。再用 fopen語句,以 wb 的方式,利用 fopen語句以 wb的方式工作的一個特性 :如果原來不存在該文件,則在 打開時新建一個以指定名字命名的文件,從而建立列表文件。(i=36) {i++。讀入源程序語句、整句識別、語句語法檢查,這些過程在循環(huán)進(jìn)行,直到出現(xiàn)源程序結(jié)尾標(biāo)記才結(jié)束。例 如,前面曾提到,對于源程序語句 PLS 09,其目標(biāo)代碼子集為: F i={50H, ODH, 20H, 24 P, 06H, D2H, Y, D2H, P, 80H, 08H, C2H, Y, 80H, 04It, C2H, Y, C2H, P, 00H,00H, 00H},式中的 P、 Y位置為盲碼位置,符號用變量 P和 Y表示,值為: P=19H。 2. 5 盲碼的預(yù)先填入及修正技術(shù) 定義 5:將目標(biāo)代碼各模塊模板中字節(jié)數(shù)已定但內(nèi)容暫未確定的代碼稱為盲碼。 再根據(jù) 2. 1. 1中的性質(zhì) 4,各 Mi沒有一個重復(fù),故由 F i仃 (Mi)知, F i也沒 有重復(fù),因此 F為 M的一個 1— 1映射成立。對不同的基于單片機(jī)的 PLC源語句,其目標(biāo)代碼的字節(jié)數(shù)不同,并且差別很大。 建立基于單片機(jī)的 PLC源程序語句標(biāo)準(zhǔn)集合的目的是為了確定一個進(jìn)行源程序查錯時的對照標(biāo)準(zhǔn),以及進(jìn)行翻譯時的判別標(biāo)準(zhǔn)。 性質(zhì) 2:根據(jù)定義 2,有: M數(shù)組的每一行對應(yīng)于不同的基于單片機(jī)的 PLC的源語 句定義。 例如:基于單片機(jī)的 PLC的 LD語句為: LD 20,其長度值 =5,標(biāo)識符串為 String Mi_{h, D, ),故 LD源語句標(biāo)準(zhǔn)子集為 Mi={L, D, , 5}。先用盲碼 值 00H填充?;顒哟a是和源程序語句對應(yīng)的目標(biāo)程序代碼。一個是對應(yīng)于 DIN Mi的單片機(jī)程序模塊。脈沖微分類運算的單片機(jī)程序模塊所占的片內(nèi)資源較少。 1. 2. 3 TIM Ti模塊的流程分析和建立 與梯形圖中延時計時器所對應(yīng)的 TIM Ti邏輯指令的單片機(jī)程序模塊流程見圖 1. 2。表中的 bit表示位地址,它 們都是單片機(jī)中片內(nèi) RAM中可直接位尋址的位 (位地址 )。 總之,基于單片機(jī)的 PLC編譯程序設(shè)計研究是一項具有創(chuàng)造性的工作,需要充分吸 收前人 經(jīng)驗,開創(chuàng)創(chuàng)新,建立新的模型,新的結(jié)構(gòu),新的方法 1 基于單片機(jī)的 PLC的單片機(jī)程序模塊研究及其目標(biāo)程序框架的構(gòu)建 基于單片機(jī)的 PLC編譯程序的功能是將基于單片機(jī)的 PLC源程序翻譯成單片機(jī)目 標(biāo)程序。編寫基于該單片機(jī)的 PLC編譯程序,是本課題的主要工作任務(wù)之 一。A mentality of checking mistaken to the source program according to the Marray has been proposed. The processing of the checking mistaken to the source program has been realized through a series of procedure such as the establishment of list filename and list documenL the recognition to entire sentence. the search by line using M array in sentence grammar inspection, the checkup to key word whether equal, the checkup to sentence length whether correct and so on. The process of assembling the fixed code into the goal document during the translation has been elaborated. Overall mentality and the procedure 8 of the translation and the assembly code have been elaborated. The prelocalization of the sigma mapping bycarrying on the type recognition to source program sentence depend on the array M has initially beenrealized. The character transformation function for quotation name(bit address)has been definedeffectively, e first kind of procedure structure and the second kind of procedure structure for realizationsigma mapping has separately been given, so that the translation of the simple active code and the plex active code has effective been pleted, and that the assembly of the goal document has been pleted. The procedure realization method for the first kind of blind code revision and the second kind of blind code revision has been proposed and elaborated. The experimental system which using the research of piler design for PLC based on monolithic microputer has been established. We had carried on the confirmation and pare to the each function of the piler. After the severaI procedure trials。本文論述了基于單片機(jī)的 PLC編譯程序設(shè)計的理論和方法,研究了基于單片機(jī)的 PLC的單片機(jī)程序模塊和目標(biāo)程序框架,對編譯程序結(jié)構(gòu)進(jìn)行了分析,闡述了查錯模塊設(shè)計方法以及目標(biāo)代碼裝配方法,對基于單片機(jī)的 PLC的編譯程序的調(diào)試實驗系統(tǒng)組成和實驗結(jié)果進(jìn)行了分析。首先,進(jìn)行了基于單片機(jī)的 PLC的單片機(jī)程序模塊研究,構(gòu)建了獨具特點的目標(biāo)程序框架結(jié)構(gòu)。 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)取得了豐碩的成果。 (2)其次,目標(biāo)程序框架結(jié)構(gòu)也不同。為了設(shè)計出基于單片機(jī)的 PLC編譯程序,首先要構(gòu)建出每一條源程序指令所 對應(yīng)的單片機(jī)程序模塊。 (如延時計時器類、脈沖微分類、計數(shù)器類、移位類等 )的單片機(jī)程序模塊需十幾條或 幾十條單片機(jī)指令組成。., 6EH, 6FH。這是一個對第 i個延時計時器進(jìn)行邏輯運算處理的模塊,運算的依據(jù)是已知的當(dāng)前邏輯運算值 (在累加器 C中 )。它們分別是:①脈沖微分寄存器的邏輯位 Yi,位地址 08H. 0FH;②用于寄存前次輸入的寄存器 Pi,位地址 18H. 1FH。其功能是將 輸入端的邏輯值暫存在 Gi中,供 SFT指令使用;第二個是對應(yīng)于 SFT Mi的移位操作 程序模塊,其功能是依據(jù)已知的當(dāng)前邏輯運算值 (在累加器 C中 ),以及 Hi的值,判斷 16 移位脈沖是否到;當(dāng)移位脈沖到時,將輸入暫存位 Gi中的值移入移位寄存器 Mi。固定代碼和活動代碼均采用單片機(jī)的機(jī)器指令代碼。等編譯程序中執(zhí)行“掃描源程序文件,逐條翻譯... 時,得到源程序 提供的定時器初值,再在用正式代碼 (以 MOV指令目標(biāo)代碼的形式,并含定時器初值 ) 代替相應(yīng)位置上的盲碼。 又如: PLS語句為: PLS 09, 其長度值 =6,標(biāo)識符串為 String Mi=(P, L, S, }, 20 故 PLS源語句標(biāo)準(zhǔn)子集為 Mi={P, L, S, , 6) 定義 2:在 Mi cM的條件下, Mi是一種源語句標(biāo)準(zhǔn)子集。 性質(zhì) 3: M數(shù)組的行數(shù)等于基于單片機(jī)的 PLC語句的條數(shù)。為此,在基于單片機(jī)的 PLC編譯程序中用 C語言實現(xiàn) M數(shù)組所表達(dá)的基于單片機(jī)的 PLC源程序語句標(biāo)準(zhǔn)集合。 fi。 例如,對于源程序語句 LD 20,其目標(biāo)代碼子集為: F i={A2H, 20H}, (實際編譯程序中引進(jìn)變量 q,為: LD 20; Fi={A2H, q), q=20H) 而對于源程序語句 PLS 09,其目標(biāo)代碼子集為: F i={50H, 0DH, 20H, P, 06II, D2H, Y, D2H, P, 80H, 08H, C2H, Y, 80H, 04H, C2H, Y, C2H, P, 00H, 00H, 00H} 其中: P=19H, y=09H。定理 1證畢。有兩種情況需要采用 盲碼技術(shù)。 y=09H, 其中, 09H值是從源語句 PLS 09中的參數(shù) 09得到, 19H是計算得到。 3. 1 編譯過程中列表文件名和列表文件的建立 建立列表文件的目的是為了用來存放查錯結(jié)果。} If(i36) {b1=2。 if((fp=fopen(a,” rb” ))==0) {printf(” cannot open file% s!\ n”, a): exit(0): } i f((fpl=fopen(b,” wb” ))==0) {printf(” cannot open or create file% s, disk error.\ n”,b): exit(0): } 經(jīng)過上面的程序以后,列表文件便已建立。當(dāng)識別出一個整句時,進(jìn)行一次語句語法檢查。 3. 4 語句語法檢查 在整句識別程序中,當(dāng)識別出一個整句時,便轉(zhuǎn)向語句語法檢查程序。 若找不到相同的,則該源程序語句語法不正確。 29 從 C數(shù)組和 M數(shù)組行的對應(yīng)關(guān)系可以找到核對的方法。上例中的標(biāo)準(zhǔn)有: mE4][5]=’\ x06’。 在. 1st文件的相應(yīng)行的末尾作標(biāo)記“術(shù) 。 列表文件中的 LDll 08*表示源程序中的 LDll 08語句出現(xiàn)語法錯誤。所以,目標(biāo)文件的擴(kuò)展名為“. bin。再用 fopen語句,以 wb的方式,利 用 fopen語句以 wb的方式工作的一個特性:如果原來不存在該文件,則在打開時新建 一個以指定名字命名的文件,從而建立目標(biāo)文件。結(jié)果,實現(xiàn)了用 b數(shù)組記錄一個文件名和源程序文件名相同,但擴(kuò)展