set 0 bit set 1 bit tog iobit
常用指令介紹 set 0 bit set 1 bit tog iobit call addr goto addr delay x ceqsn x, y t 0 sn bit t 1 sn bit dzsn x wait 0 iobit wait 1 iobit clear x mov x, y inc x ; 位元設 0 ; 位元設 1 ; 位元0/1切換 ; 呼叫副程式 ; 跳躍到標籤 ; 延時 x+1 指令周期 ; if x=y skip ; if bit=0 skip ; if bit=1 skip ; x-=1, if x=0 skip ; 等待直到 bit=0 ; 等待直到 bit=1 ; x=0 ; x=y ; x+=1
輸入控制-按鍵 /// ---- PORTA ------0 b 76543210 SET_PORTA equ 0 b 11111110; SET_PORTA_PH equ 0 b 1111; BTN 0 equ pa. 0; LED 0 equ pa. 1; … mov a, SET_PORTA; /// PA IO direction mov pac, a; mov a, SET_PORTA_PH; /// PA pull-high mov paph, a; button. Loop: wait 0 BTN 0; // TODO goto button. Loop; MCU Wait Button Press TODO 程式碼: 4 FPPA程式設計-1 -1按鍵. asm
按鍵防彈跳 button. Loop: wait 0 BTN 0; call Do. Button 0; goto button. Loop; Do. Button 0: call Delay 4 ms; t 0 sn BTN 0; ret; // TODO wait 1 BTN 0; ret; Delay 4 ms: mov delay. Loop: delay dzsn goto ret; a, 16; counter, a; 250; counter; delay. Loop; 程式碼: 4 FPPA程式設計-1 -2按鍵彈跳. asm
程式架構 MCU Wait Button Press Condition TODO
實作: 按鍵防彈跳 Delay 4 ms: mov delay. Loop: delay dzsn goto ret; a, 16; counter, a; // T=1 // +1+16 x( 250; counter; delay. Loop; // +250+1 // +1) // -1+1 T=1+1+16 x(250+1+1+1)-1+1=4050 T=4050 us 1 1 253 253 253 253 252 1
多按鍵輪詢 button. Loop: t 1 sn call … goto BTN 0 Do. Button 0 BTN 1 Do. Button 1 button. Loop Do. Button 0: call Delay 4 ms t 0 sn BTN 0 ret // TODO 0 wait 1 BTN 0 ret Do. Button 1: call Delay 4 ms t 0 sn BTN 1 ret // TODO 1 wait 1 BTN 1 ret 程式碼: 4 FPPA程式設計-1 -3多按鍵輪詢. asm
程式架構 MCU Check Button 0 Press Condition TODO Check Button 1 Press Condition TODO
按鍵狀態細分 button. Loop: t 1 sn call … t 0 sn call … goto BTN 0 Do. Button 0 BTN 1 Do. Button 1 BTN 0 Do. Button 0 Release BTN 1 Do. Button 1 Release button. Loop Do. Button 0: call Delay 4 ms t 0 sn BTN 0 ret // TODO ret Do. Button 0 Release: // TODO ret 程式碼: 4 FPPA程式設計-1 -4按鍵事件細分. asm
程式架構 MCU Check Button 0 Press Condition TODO Check Button 1 Press Condition TODO Check Button 0 Release TODO Check Button 1 Release TODO
事件觸發 event. Loop: t 0 sn call … t 1 sn call … goto EVENT 0 Active. Event 0 EVENT 1 Active. Event 1 EVENT 0 Stop. Active. Event 0 EVENT 1 Stop. Active. Event 1 event. Loop Active. Event 0: // TODO 0 ret Active. Event 1: // TODO 1 ret 程式碼: 4 FPPA程式設計-1 -5事件觸發. asm
狀態控制-事件集中處理 Get. Event. Loop: clear t 1 sn set 1 … t 1 sn set 1 t 0 sn set 1 … mov goto SET_EVENT BTN 0 SET_EVENT 0 BTN 1 SET_EVENT 1 IR 0 SET_EVENT 0 IR 0 SET_EVENT 1 a, SET_EVENT, a Get. Event. Loop 按鍵輸入 IR輸入 其它控制方式
程式架構 FPP 1 Get Event 0 Get Event 1 Get Event 2 Get Event 3 FPP 0 Check Event 1 Check Event 2 Check Event 3 Event 0 Active Event 1 Active Event 2 Active Event 3 Active
程式架構 FPP 2 FPP 1 Process Button Get Event 0 FPP Get Event 1 Process IR Get Event 2 Get Event 3 FPP 0 Check Event 1 Check Event 2 Check Event 3 Event 0 Active
傳統程式架構 Get IO Process IO Check IO Event 0 Active Get IO Process IO Check IO Event 1 Active interrupt ISR Event 1 Active Get IO Process IO Check IO Event 2 Active
FPPA 觀點 Get IO Process IO Check IO Event 0 Active Get IO Process IO Check IO Event 1 Active interrupt ISR Get IO Process IO Check IO Event 2 Active
事件觸發-多核的應用 event. Loop: t 1 sn call … goto Active. Event 0: set 1 ret FPP-0 EVENT 0 Active. Event 0 EVENT 1 Active. Event 1 event. Loop TRIGGER 0 trigger 0 Loop: t 1 sn TRIGGER 0 goto trigger 0 Loop set 0 TRIGGER 0 // TODO goto trigger 0 Loop FPP-1 Active. Event 1: … ret EVENT 1 //TODO FPP-2 … … FPP-N 程式碼: 4 FPPA程式設計-1 -8&9多核的應用-蜂鳴器. asm
程式架構 FPP 2 FPP 1 FPP 0 FPP Event 0 Triggered Process Button FPP Process IR Get Event 0 Get Event 1 Get Event 2 Get Event 3 Check Event 0 Check Event 1 Check Event 2 Check Event 3 Event 0 Active FPP Event 1 Triggered Event 1 Active Event 2 Active Event 3 Active
實作: 多核的應用-蜂鳴器 mov BZ 0 Delay. Loop: set 1. repeat delay set 0. repeat delay dzsn goto BZ 0_DUTY x 1 BZ 0_DUTY x 9 a, BZ 0_TIME; bz 0_delay, a; BZ 0; 1; BZ 0_DUTY; BZ 0; 9; BZ 0_DUTY; bz 0_delay; BZ 0 Delay. Loop; BZ 0_DUTY 影響嗶聲頻率 Repeat 影響佔空比 BZ 0_TIME 影響聲音持續時間 370 us, F = 2. 7 KHZ BZ 0_DUTY x 10 x BZ 0_TIME
事件觸發-觸發條件控制 event. Loop: t 0 sn call … goto EVENT 0 Active. Event 0 EVENT 1 Active. Event 1 EVENT 2 Active. Event 2 EVENT 3 Active. Event 3 event. Loop Active. Event 0: t 0 sn ret set 1 EVENT_LOCK 0 Active. Event 1: set 0 EVENT_LOCK 0 // TODO 0 ret EVENT_LOCK 0 // TODO 1 ret 程式碼: 4 FPPA程式設計-1 -10單一事件觸發. asm
多核的應用-CPU控制 Active. Event 2: set 1 ret; LED 1; BZ 1_CPU; Active. Event 3: set 0 ret; FPP-0 LED 1; BZ 1_CPU; Pause CPU Resume CPU set 0 BZ 1_CPU; BZ 1 Loop: set 1 BZ 1; . repeat 9 delay BZ 1_DUTY; set 0 BZ 1; . repeat 1; delay BZ 1_DUTY; goto BZ 1 Loop; BZ 1_CPU 程式碼: 4 FPPA程式設計-1 -9單一事件觸發. asm
多核的應用-PC控制 Active. Event 0: mov mov pushw popw ret; a, ha@Restart. BZ 0 Loop; Waitting for start hb@tmp. PC, a; a, la@Restart. BZ 0 Loop; lb@tmp. PC, a; tmp. PC; goto $; BZ 0_PC; Restart. BZ 0 Loop: mov a, BZ 0_TIME; FPP-0 mov bz 0_delay, a; BZ 0 Delay. Loop: set 1 BZ 0; Restart Program Counter. repeat 9 delay BZ 0_DUTY; set 0 BZ 0; . repeat 1; delay BZ 0_DUTY; dzsn bz 0_delay; Waitting for restart goto BZ 0 Delay. Loop; goto $; BZ 0_CPU
程式架構 FPP 2 Process Button FPP Process IR FPP 1 Get Event 0 Get Event 1 Get Event 2 Get Event 3 FPP 0 Check Event 1 Check Event 2 Check Event 3 T AR T S OP T S RE S FPP Event 0 Active FPP TA RT Event 1 Active Event 2 Active Event 3 Active
實作: 多核的應用-PC控制 ROM位址 行數 指令碼 334 335 336 0044 c 044 337 338 0045 1 ffa 339 0046 5 c 06 ||FPP 3 Main. Loop: || || goto $; ||Restart. BZ 0 Loop: || mov a, BZ 0_TIME; || mov bz 0_delay, a; 420 007 e 1 f 00 421 007 f 5 c 01 422 0080 1 f 45 423 0081 5 c 00 424 0082 0401 425 0083 0063 || || || mov mov pushw popw a, ha@Restart. BZ 0 Loop; hb@tmp. PC, a; a, la@Restart. BZ 0 Loop; lb@tmp. PC, a; tmp. PC; BZ 0_PC; tmp. PC 00 45 word Label標籤,虛指令 不佔指令碼 。 Compiler 在編譯 時就會去查找 Label所代表的位 址。
蜂鳴器更多的應用方式 BZ-FPP PLAY Sound Effect #1 Other-FPP PLAY Sound Effect #2 PLAY Sound Effect #3 PLAY Sound Effect #4 單音、連續二聲、長音、一 連串的旋律…各種不同的音 效。
TIMER
自動控制-timer-循序 timer. Loop: call Delay 2 sec // TODO goto timer. Loop: call Delay 1 sec // TODO 0 // TODO 1 goto timer. Loop
程式架構 MCU Delay 2 sec Delay 1 sec TODO 0 Delay 2 sec TODO 0 TODO 1
自動控制-timer-Time. Based clear timer 0_counter clear timer 1_counter timer. Loop: call Delay 1 s inc timer 0_counter ceqsn timer 0_counter, 1 goto no. Timer 0 clear timer 0_counter // TODO 0 no. Timer 0: inc timer 1_counter ceqsn timer 1_counter, 2 goto no. Timer 1 clear timer 1_counter // TODO 1 no. Timer 1: goto timer. Loop
程式架構 MCU Delay base time Check Counter 0 TODO 0 Check Counter 1 TODO 1
自動控制-timer-事件處理 clear timer. Loop: call inc ceqsn goto clear set 1 no. Timer 0: inc ceqsn goto clear set 1 no. Timer 1: goto FPP-0 timer 0_counter timer 1_counter Delay 1 s timer 0_counter, 1 no. Timer 0 timer 0_counter TIMER 0 timer 1_counter, 2 no. Timer 1 timer 1_counter TIMER 1 timer. Loop Get. Event. Loop: clear t 0 sn set 1 t 1 sn set 1 … t 0 sn set 1 … mov goto FPP-1 SET_EVENT BUTTON 0 SET_EVENT 0 BUTTON 0 SET_EVENT 1 TIMER 0 SET_EVENT 0 TIMER 1 SET_EVNET 1 a, SET_EVENT, a Get. Event. Loop
程式架構 TIMER HANDLE Delay base time Check Counter 0 Check Counter 1 CPU 0 CPU 1 TODO 0 TODO 1
程式架構 FPP 2 FPP 1 FPP 0 FPP Event 0 Triggered Process Button FPP 3 Process Timer Get Event 0 Get Event 1 Get Event 2 Get Event 3 Check Event 0 Check Event 1 Check Event 2 Check Event 3 Event 0 Active FPP Event 1 Triggered Event 1 Active Event 2 Active Event 3 Active
自動控制-timer-動作觸發 clear timer. Loop: call inc ceqsn goto clear set 1 no. Timer 0: inc ceqsn goto clear set 1 no. Timer 1: goto FPP-0 timer 0_counter timer 1_counter Delay 1 s timer 0_counter, 1 no. Timer 0 timer 0_counter TRIGGER 0 timer 1_counter, 2 no. Timer 1 timer 1_counter TRIGGER 1 timer. Loop trigger 0 Loop: t 1 sn TRIGGER 0 goto trigger 0 Loop set 0 TRIGGER 0 clear trigger 0_counter // TODO 0 goto trigger 0 Loop FPP-1 trigger 1 Loop: t 1 sn TRIGGER 1 goto trigger 1 Loop set 0 TRIGGER 1 clear trigger 1_counter // TODO 1 goto trigger 1 Loop FPP-2
消除時序上誤差的方法 clear timer. Loop: delay inc ceqsn goto clear set 1 no. Timer 0: goto timer 0_counter 199 // 200 us timer 0_counter // +1 timer 0_counter, 1 // +1 no. Timer 0 // +1 timer 0_counter // +1 TIMER 0 // +1 timer. Loop ? // +1 205 us or 204 us ? 1 us 常數 202 us 常數 條件判斷式 1 us A 2 us B 變數 1 us 常數
消除時序上誤差的方法 clear timer. Loop: delay inc ceqsn goto is. Timer 0: clear set 1 goto no. Timer 0: nop goto end. Timer 0: goto timer 0_counter 199 // 200 us timer 0_counter // +1 timer 0_counter, 1 // +1 no. Timer 0 // +1 is. Timer 0 // +1 timer 0_counter TIMER 0 end. Timer 0 // +1 // +1 timer. Loop // +1 ? 1 us 常數 202 us 常數 條件判斷式 4 us A 207 us 4 us B 常數 1 us 常數
自動控制-timer-使用內部timer Initial_ISR: mov mov engint; a, 0 b 0000; intrq, a; /// clear interrpt reqest register a, 0 b 00000100; inten, a; /// initial interrupt 0=Disable 1=Enable /// Enable Interrupt intrq & inten: /// Bit Description bit-0: PA. 0 bit-1: PA. 1 bit-2: Timer 16 bit-3: General 0 bit-4: General 1 bit-5: General 2 bit-6: General 3 bit-7: General 4 FPP 0 intrq!=0 Interrupt Subroutine
Interrupt 運作示意圖. romadr 0 x 010 FPP 0 ISR engint & inten 7 6 5 4 3 2 1 0 intrq 7 6 5 4 3 2 1 0 General Timer PA 1 PA 0 user define time’s up toggled 當 intrq. n=1 而且 inten. n=1 時,FPP 0 會自動進入ISR,也就是程式碼 0 X 010 的位址。(*會用掉 2 bytes stack ) 程式設計時必須負責作好狀態保存(有可能干擾FPP 0的變數),在處理 完中斷後須要將 intrq. n 重設為 0 並且作狀態回復。
自動控制-timer-使用內部timer Initial_TIMER: mov mov mov stt 16 a, 0 b 10011111; /// 使用內部 OSC - 16 MHZ t 16 m, a; a, 0 x 1 E; hb@t 16 reset, a; a, 0 x 58; lb@t 16 reset, a; t 16 reset; /// Restart timer tick t 16 m: /// Bit Description Timer Clock Source = 7 ~ 5 000: NONE 001: SYS 100: IHRC 101: OSC 110: ILRC 111: PA 0 Timer Clock Prescale = 4 ~ 3 00: Clock/1 01: Clock/4 10: Clock/16 11: Clock/64 Interrupt sorce = 2 ~ 0 000: bit 8 ~ 111: bit 15 carray interrupt Interrup. Time = Clock. Source/Prescale*Counter, Counter is base on sorce ie. 想要產生每 100000 us 一次的中斷 OSC=16 MHz, Prescale=1/64, 1 -tick = 64/16 = 4 us count#=100000/4=25000, Sorce = 111(32768), stt 16=32768 -25000=0 x 1 E 58
Timer 運作示意圖 engint & inten 7 6 5 4 3 2 1 0 intrq 7 6 5 4 3 2 1 0 Score t 16 0 0 0 0 16384 32768 8192 4096 1024 2048 256 512 /Prescale Clock Source 7 6 5 4 3 2 1 0 t 16 m OSC PA 0
自動控制-timer-使用內部timer. romadr 0 x 0010 Interrupt: set 0 intrq. 2; /// clear interrpt reqest register stt 16 reset ; /// Restart timer tick inc timer 0_counter; mov a, timer 0_counter; ceqsn a, 10; /// = 100000 us*10=1 sec goto no. Timer 0; clear timer 0_counter; // TODO 0 /// set trigger or event no. Timer 0: inc timer 1_counter; mov a, timer 1_counter; ceqsn a, 20; /// = 100000 us*20=2 sec goto no. Timer 1; clear timer 1_counter; // TODO 0 /// set trigger or event no. Timer 1: reti;
自動控制-timer-使用內部timer-自行處理中斷 disgint; ISRLoop: t 1 sn intrq. 2; goto ISRLoop; set 0 intrq. 2; /// clear interrpt reqest register stt 16 reset ; /// Restart timer tick inc timer 0_counter; mov a, timer 0_counter; ceqsn a, 10; goto no. Timer 0; clear timer 0_counter; // TODO 0 /// set trigger or event no. Timer 0: inc timer 1_counter; mov a, timer 1_counter; ceqsn a, 20; goto no. Timer 1; clear timer 1_counter; // TODO 0 /// set trigger or event no. Timer 1: goto ISRLoop;
程式架構 TIMER HANDLE timer Check Counter 0 Check Counter 1 CPU 0 CPU 1 TODO 0 TODO 1
傳統程式架構 Get IO Process IO Check IO Event 0 Active Get IO Process IO Check IO Event 1 Active interrupt ISR Event 1 Active Get IO Process IO Check IO Event 2 Active
多核程式架構 FPP 2 FPP 1 FPP 0 FPP Event 0 Triggered Process Button Get Event 0 I Get Event 1 Process Timer Get Event 2 FPP 3 Check Event 0 Check Event 1 Check Event 2 Check Event 3 EVENT Get Event 3 Event 0 Active FPP O Event 1 Triggered Event 1 Active Event 2 Active Event 3 Active
- Slides: 56