; Chapter 13 6812 assembly language programs ; Jonathan W. Valvano, 2/26/07 ; This software accompanies the book, ; Embedded Microcomputer Systems: Real Time Interfacing, Second Edition ; published by Thomson Engineering, 2006 ; Program 13.17. Global variables for fuzzy controller in 6812 assembly. org $3800 ; crisp inputs speed: ds 1 acceleration: ds 1 ; input membership variables fuzvar: ds 0 ; inputs EPL: ds 1 ; speed way too fast EPS: ds 1 ; speed too fast EZE: ds 1 ; speed OK ENS: ds 1 ; speed too slow ENL: ds 1 ; speed way too slow DPL: ds 1 ; speed decreasing a lot DPS: ds 1 ; speed decreasing DZE: ds 1 ; speed constant DNS: ds 1 ; speed increasing DNL: ds 1 ; speed increasing a lot fuzout: ds 0 ; outputs OPL: ds 1 ; add a lot of power to system OPS: ds 1 ; add some power to system OZE: ds 1 ; leave power as is ONS: ds 1 ; subtract some power from system ONL: ds 1 ; subtract a lot of power from system BREAK: ds 1 ; apply break? ; input membership variables relative offsets epl: equ 0 ; speed way too fast eps: equ 1 ; speed too fast eze: equ 2 ; speed ok ens: equ 3 ; speed too slow enl: equ 4 ; speed way too slow dpl: equ 5 ; speed decreasing a lot dps: equ 6 ; speed decreasing dze: equ 7 ; speed constant dns: equ 8 ; speed increasing dnl: equ 9 ; speed increasing a lot ;output membership variables opl: equ 10 ; add alot of power to system ops: equ 11 ; add some power to system oze: equ 12 ; leave power as is ons: equ 13 ; subtract some power from system onl: equ 14 ; subtract alot of power from system break: equ 15 ; apply break? ;crisp outputs dpower: ds 1 ; Program 13.18. Definition of the error input function in 6812 assembly. org $F000 ; format is Point1,Point2,Slope1,Slope2 s_tab: dc.b 192,255,8,0 ;EPL dc.b 128,244,5,8 ;EPS dc.b 112,144,16,16 ;EZE dc.b 32,128,8,5 ;ENS dc.b 0,64,0,8 ;ENL ; Program 13.19. Definition of the acceleration input function in 6812 assembly. a_tab: dc.b 244,255,16,0 ;DPL dc.b 160,255,16,4 ;DPS dc.b 64,192,8,8 ;DZE dc.b 0,96,4,16 ;DNS dc.b 0,32,0,16 ;DNL ; Program 13.20. Definition of the fuzzy rules in 6812 assembly. rules: dc.b enl,dnl,$FE,opl,$FE ; if ENL and DNL then OPL dc.b ens,dnl,$FE,opl,$FE ; if ENS and DNL then OPL dc.b eze,dnl,$FE,opl,$FE ; if EZE and DNL then OPL dc.b enl,dns,$FE,opl,$FE ; if ENL and DNS then OPL dc.b ens,dns,$FE,ops,$FE ; if ENS and DNS then OPS dc.b eze,dns,$FE,ops,$FE ; if EZE and DNS then OPS dc.b enl,dze,$FE,opl,$FE ; if ENL and DZE then OPL dc.b ens,dze,$FE,ops,$FE ; if ENS and DZE then OPS dc.b eze,dze,$FE,oze,$FE ; if EZE and DZE then OZE dc.b eps,dze,$FE,ons,$FE ; if EPS and DZE then ONS dc.b epl,dze,$FE,onl,break,$FE ; if EPL and DZE then ONL dc.b eze,dps,$FE,ons,$FE ; if EZE and DPS then ONS dc.b eps,dps,$FE,ons,$FE ; if EPS and DPS then ONS dc.b eps,dps,$FE,onl,break,$FE ; if EPL and DPS then ONL dc.b eze,dpl,$FE,onl,break,$FE ; if EZE and DPL then ONL dc.b eps,dpl,$FE,onl,break,$FE ; if EPS and DPL then ONL dc.b epl,dpl,$FE,onl,break,$FE ; if EPL and DPL then ONL dc.b $FF ; Program 13.21. Ritual and main program for fuzzy controller in 6812 assembly. addsingleton: dc.b 255,138,128,118,0 ; 128 subtracted, +127,10,0,-10,-128 ritual: sei ;make atomic bset #$20,TIOS ;OC5 movb #$80,TSCR1 ;enable, no fast clr movb #$03,TSCR2 ;1us clk bset #$20,TIE ;Arm OC5 ldaa #$20 ;clear C5F staa TFLG1 ldd TCNT ;current time addd #10000 ;first in 10 ms std TC5 cli ;enable rts main: lds #$4000 ldd #100 ; initial duty cycle 1% is off std dutycycle jsr ritual ; initialize OC interrupt bra * ; Program 13.22. Interrupt handler for fuzzy controller in 6812 assembly. timehan: ldaa #$20 ;clear C5F staa TFLG1 ;Acknowledge ldd TC5 addd #10000 ;next in 10 ms std TC5 jsr measurespeed ; crisp input speed ;reg A is speed 0 to 255 ldx #s_tab ldy #fuzvar mem ; calculate EPL mem ; calculate EPS mem ; calculate EZE mem ; calculate ENS mem ; calculate ENL jsr measureacceleration ; crisp input acceleration ;reg A is acceleration 0 to 255 ldx #a_tab mem ; calculate DPL mem ; calculate DPS mem ; calculate DZE mem ; calculate DNS mem ; calculate DNL ldab #6 cloop: clr 1,y+ ; clear OPL,OPS,OZE,ONS,ONL,BREAK dbne b,cloop ldx #rules ldy #fuzvar ldaa #$FF rev ldy #fuzout ldx #addsingleton ldab #5 wav ediv tfr y,d subb #128 stab dpower change ldab dpower sex b,d addd dutycycle ; 200 Hz squarewave is 10000 cycles ; correct range is 100 to 9600 cycles cpd #100 bhs nolow low: ldd #100 ; underflow bra set notlow: cpd #9600 bls set high: ldd #9600 ; overflow set: std dutycycle rti ; Program 13.23. Fuzzification function for the temperature input in 6812 assembly. ; format is Point1,Point2,Slope1,Slope2 T_tab: dc.b 160,255,16,0 ;Hot dc.b 64,192,8,16 ;Normal dc.b 0,64,0,8 ;Cold ; Program 13.24. Fuzzification function for the temperature error input in 6812 assembly. E_tab: dc.b 144,255,16,0 ;TooHot dc.b 64,192,8,16 ;OK dc.b 0,96,0,16 ;TooCold ; Program 13.25. Global variables in 6812 assembly. ; input membership variables fuzvar: ds 0 ; inputs Hot: ds 1 Normal: ds 1 Cold: ds 1 TooHot: ds 1 OK: ds 1 TooCold: ds 1 ; output membership variables fuzout: ds 0 ; outputs NoHeat: ds 1 ; turn off heater SomeHeat: ds 1 ; apply some heat MaxHeat: ds 1 ; turn on heater NoFan: ds 1 ; turn off fan SomeFan: ds 1 ; apply some fan MaxFan: ds 1 ; turn on fan ; input membership variables relative offsets hot: equ 0 normal: equ 1 cold: equ 2 toohot: equ 3 ok: equ 4 toocold: equ 5 ;output membership variables noheat: equ 6 ; turn off heater someheat: equ 7 ; apply some heat maxheat: equ 8 ; turn on heater nofan: equ 9 ; turn off fan somefan: equ 10 ; apply some fan maxfan: equ 11 ; turn on fan ; Program 13.26. Fuzzy rules in 6812 assembly. rules: dc.b hot,toohot,$FE,noheat,maxfan,$FE ; use fan for max cooling dc.b hot,ok,$FE,noheat,somefan,$FE ; use fan for some cooling dc.b normal,toohot,$FE,noheat,nofan,$FE ; no fan for normal temps dc.b normal,ok,$FE,noheat,nofan,$FE ; perfect dc.b normal,toocold,$FE,someheat,nofan,$FE ; a little heat dc.b cold,ok,$FE,noheat,nofan,$FE ; cold but perfect dc.b cold,toocold,$FE,maxheat,maxfan,$FE ; fast warmup dc.b $FF ; Program 13.27. Fuzzy controller in 6812 assembly. Heatsingleton: dc.b 0,50,255 Fansingleton: dc.b 0,128,255 timehan: ldaa #$20 ;clear C5F staa TFLG1 ;Acknowledge ldd TC5 addd #10000 ;next in 10 ms std TC5 jsr MeasureTemperatire ; crisp input temperature, T ;reg A is temperature 0 to 255 (units 0.5ûF) ldx #T_tab ldy #fuzvar mem ; calculate Hot mem ; calculate Normal mem ; calculate Cold jsr MeasureError ; crisp input error, E ;reg A is error 0 to 255 (128 means no error) (units 0.125ûF) ldx #E_tab mem ; calculate TooHot mem ; calculate OK mem ; calculate TooCold ldab #6 cloop: clr 1,y+ ; clear NoHeat, SomeHeat, MaxHeat, NoFan, SomeFan, MaxFan dbne b,cloop ldx #rules ldy #fuzvar ldaa #$FF rev ldy #NoHeat ; pointer to NoHeat, SomeHeat, MaxHeat ldx #Heatsingleton ldab #3 wav ediv tfr y,d stab Heat ldy #NoFan ; pointer to NoFan, SomeFan, MaxFan ldx #Fansingleton ldab #3 wav ediv tfr y,d stab Fan rti