


;*********************************************************************
* Definicion del mapa de Memoria del 68HC908QT1
;*********************************************************************

RAM_START       EQU     $0080           ;Tamaņo: 128 Bytes
FLASH_START     EQU     $F800           ;Tamaņo: 4KBytes
VECTORES        EQU     $FFDE           ;Comienzo de la Tabla de Vectores
;extra
TRIM_VALUE      EQU     $FFC0

	        ORG 	TRIM_VALUE
trim_val:
  		db $FF 			;A medida que incremento disminuye el error
					;en 0.2% wn wl oscilador interno
					;12.8 Mhz - bus interno 3.2 Mhz -+ 0.2%

$Include 'C:\pemicro\ics08qtqyz\qtqy_registers.inc'


;*********************************************************************
; Definicion de Valores para el prog
;*********************************************************************
IniTMODH	EQU	$72
IniTMODL	EQU	$73
InitNIVEL	EQU 	!0
Niv_luz_MIN	EQU	!0
Niv_luz_MAX	EQU	!253
PIN_ROJO	EQU	1
Pulso		EQU	!120
RDO_TECLA	EQU 	!7
Usando_teclado  EQU     !0
;*********************************************************************
; Definicion de las variables en RAM
;*********************************************************************
		ORG	RAM_START
Timmer_hab	ds	1		;Reservo un Bit para cada var
Irq_hab		ds	1
nivel_luz	ds	1
temp            ds      1
temp1 		ds	1
temp2 		ds	1
temp3 		ds	1
H_temp          ds      1
X_temp          ds      1
result_H        ds      1
result_L        ds      1
desplaza_hx     ds      2
temp_125us      ds      1
bandera_teclado ds      1
;*********************************************************************
; CARGA DEL PROG EN FLASH
;*********************************************************************

;		Base	Prefix	Suffix
;		02	%	Q
;		10	!	T
;		16	$	H

 		ORG	FLASH_START
main:
		rsp                             ;Reset del stack pointer
		clra                            ;Limpio registros
	        clrh
	        clrx
	        cli                             ;Borro Bit I (habilita interrupciones)
		mov #%00010011,CONFIG1
             	    ; |||||||| CONFIG1 is a write once register
	            ; |||||||+-COPD - 1 disable COP Watchdog
	            ; ||||||+--STOP - 1 enable STOP instruction
	            ; |||||+---SSREC - 0 4096 cycle STOP recovery
	            ; ||||+----LVI5OR3 - 1 LVI operates in 5-V mode
	            ; |||+-----LVIPWRD - 1 disable power to LVI system
	            ; ||+------LVIRSTD - 0 enable reset on LVI trip
	            ; |+-------LVISTOP - 0 disable LVI in STOP mode
	            ; +--------COPRS - 0 long COP timeout

		mov #%11000000,CONFIG2
	            ; |||||||| CONFIG2 is a write once register
	            ; |||||||+-RSTEN -  0  Reset function is inactive in pin
	            ; ||||||+--R - 0 Reserved bit
	            ; |||||+---R - 0 Reserved bit
	            ; ||||+----OSCOPT0 - 0 Set oscillator option as internal
	            ; |||+-----OSCOPT1 - 0 Set oscillator option as internal
	            ; ||+------R - 0 Reserved bit
	            ; |+-------IRQEN -  1  Interrupt request function inactive in pin (para el cruce por cero)
	            ; +--------IRQPUD - 1  Internal pullup is disconnected
		    ; COMENTARIO: 	Si IRQEN se habilita se config solo como entrada

		mov #%01110000,TSC
		    ; |||||||| TIM Status and Control Register (3.2Mhz / 32 = 100000 Hz)
		    ; |||||||+-PS0 - 0 Prescaler select bit
		    ; ||||||+--PS1 - 0 Prescaler select bit
	            ; |||||+---PS2 - 0 Tim clock source int. bus
		    ; ||||+----0 - 0
		    ; |||+-----TRST - 1 TIM reset bit
		    ; ||+------TSTOP - 1 TIM counter stopped
		    ; |+-------TOIE - 1 disable TIM overflow interrupts
		    ; +--------TOF - 0 TIM overflow flag bit

         	mov #IniTMODH,TMODH ;Cargo TMOD con el valor incial
	        mov #IniTMODL,TMODL ;
		mov #InitNIVEL,nivel_luz ;Inicio con un nivel de luz

	        mov #%00000010,DDRA         ; Configuro Puerto A
	            ; ||||||||             Entrada para el led infra
		    ;	|| |+--ddra1 - 1 - Salida para el TRIAC
		    ;   || +---ddra2 - 0 - Entrada IRQ
   		    ;   |+-----ddra4 - 0 - Entrada tecla Subir
		    ;   +------ddra5 - 0 - Entrada tecla Bajar
		 	    	;1 = Corresponding port A pin configured as output
				;0 = Corresponding port A pin configured as input

		mov #%00000110,KBSCR
                    ; |||||||| Keynoard Status and Control Register
	            ; |||||||+- MODEK -  1 Keynoard interrupt request on falling edges only and level
                    ; ||||||+-- IMASKK - 1 Keyboard Interrup requests masked
                    ; |||||+--- ACKK - 1 Keynoard Acknowledge (borra las interupciones pendientes)
                    ; ||||+---- KEYF - 0 No keynoard interrup pending


		mov #%00000001,KBIER
                    ; |||||||| Config del Puerto A como Interrupciones
	            ; |||||||+-  


		;bset ACK,KBSCR		;Borra las

        	;bclr PIN_TRIAC,PTA          ; 
		mov #%00000000,PORTA
		mov #%00000100,INTSCR
	            ; ||||||||
		    ;	   ||+-MODE  - 0 - Solo activa por Flanco
		    ;	   |+--IMASK - 0 - IRQ interrupt requests enabled(habilita este tipo de interrup)
		    ;      +---ACK   - Clears the IRQ latch (por si habia algun pedido de interrup)


                bclr Usando_teclado,bandera_teclado
Principal:
	        brset 5T,PORTA,Subir
        	brset 4T,PORTA,Bajar
		jmp Principal

Subir:          bset Usando_teclado,bandera_teclado
                jsr retardo                    ;Cuando es manejado por el otro micro no necesito retardo antirebote
	        brclr !5,PORTA,Sub_Sal
		lda #Niv_luz_MAX
		cbeq nivel_luz,Sub_Sal
                inc nivel_luz
Sub_Sal:        bclr Usando_teclado,bandera_teclado
		jmp Principal

Bajar:          bset Usando_teclado,bandera_teclado
		jsr retardo                     ;Cuando es manejado por el otro micro no necesito retardo antirebote
	        brclr !4,PORTA,Baj_sal
		lda #Niv_luz_MIN
		cbeq nivel_luz,Baj_sal
		dec nivel_luz
Baj_sal:        bclr Usando_teclado,bandera_teclado
		jmp Principal

inc_hx:		sthx H_temp
		stx X_temp
		lda X_temp
		add #$01		;incremento en 1
		bcc sale_inc		;salta si carry es cero
		inc H_temp		;si hay acarreo incremento h
sale_inc:	sta X_temp
		ldhx H_temp
		ldx X_temp
		rts



retardo:	mov #RDO_TECLA,temp3
rtdo_3:		mov #$FF,temp2
rtdo_2:		mov #$3F,temp1
rtdo_1:		nop
		dbnz temp1,rtdo_1
		dbnz temp2,rtdo_2
		dbnz temp3,rtdo_3
		rts

;*********************************************************************
; TABLA DE RETARDOS
;*********************************************************************
TABLA:
               ; DW $7D01        ;nivel: 0
                DW $7273        ;nivel: 1
                DW $6FAB        ;nivel: 2
                DW $6DB4        ;nivel: 3
                DW $6C21        ;nivel: 4
                DW $6ACA        ;nivel: 5
                DW $699C        ;nivel: 6
                DW $688D        ;nivel: 7
                DW $6795        ;nivel: 8
                DW $66B0        ;nivel: 9
                DW $65DB        ;nivel: 10
                DW $6512        ;nivel: 11
                DW $6454        ;nivel: 12
                DW $639F        ;nivel: 13
                DW $62F3        ;nivel: 14
                DW $624E        ;nivel: 15
                DW $61B0        ;nivel: 16 *
                DW $6117        ;nivel: 17
                DW $6083        ;nivel: 18
                DW $5FF5        ;nivel: 19
                DW $5F6A        ;nivel: 20
                DW $5EE4        ;nivel: 21
                DW $5E61        ;nivel: 22
                DW $5DE2        ;nivel: 23
                DW $5D65        ;nivel: 24
                DW $5CEC        ;nivel: 25
                DW $5C75        ;nivel: 26
                DW $5C01        ;nivel: 27
                DW $5B90        ;nivel: 28
                DW $5B20        ;nivel: 29
                DW $5AB3        ;nivel: 30
                DW $5A47        ;nivel: 31
                DW $59DE        ;nivel: 32
                DW $5976        ;nivel: 33
                DW $5910        ;nivel: 34
                DW $58AB        ;nivel: 35
                DW $5848        ;nivel: 36
                DW $57E7        ;nivel: 37
                DW $5786        ;nivel: 38
                DW $5727        ;nivel: 39
                DW $56CA        ;nivel: 40
                DW $566D        ;nivel: 41
                DW $5612        ;nivel: 42
                DW $55B8        ;nivel: 43
                DW $555E        ;nivel: 44
                DW $5506        ;nivel: 45
                DW $54AF        ;nivel: 46
                DW $5459        ;nivel: 47
                DW $5403        ;nivel: 48
                DW $53AF        ;nivel: 49
                DW $535B        ;nivel: 50
                DW $5308        ;nivel: 51
                DW $52B6        ;nivel: 52
                DW $5264        ;nivel: 53
                DW $5213        ;nivel: 54
                DW $51C3        ;nivel: 55
                DW $5174        ;nivel: 56
                DW $5125        ;nivel: 57
                DW $50D7        ;nivel: 58
                DW $5089        ;nivel: 59
                DW $503C        ;nivel: 60
                DW $4FF0        ;nivel: 61
                DW $4FA4        ;nivel: 62
                DW $4F58        ;nivel: 63
                DW $4F0D        ;nivel: 64
                DW $4EC3        ;nivel: 65
                DW $4E79        ;nivel: 66
                DW $4E2F        ;nivel: 67
                DW $4DE6        ;nivel: 68
                DW $4D9E        ;nivel: 69
                DW $4D55        ;nivel: 70
                DW $4D0E        ;nivel: 71
                DW $4CC6        ;nivel: 72
                DW $4C7F        ;nivel: 73
                DW $4C38        ;nivel: 74
                DW $4BF2        ;nivel: 75
                DW $4BAC        ;nivel: 76
                DW $4B66        ;nivel: 77
                DW $4B21        ;nivel: 78
                DW $4ADC        ;nivel: 79
                DW $4A97        ;nivel: 80
                DW $4A52        ;nivel: 81
                DW $4A0E        ;nivel: 82
                DW $49CA        ;nivel: 83
                DW $4987        ;nivel: 84
                DW $4943        ;nivel: 85
                DW $4900        ;nivel: 86
                DW $48BD        ;nivel: 87
                DW $487A        ;nivel: 88
                DW $4838        ;nivel: 89
                DW $47F6        ;nivel: 90
                DW $47B3        ;nivel: 91
                DW $4772        ;nivel: 92
                DW $4730        ;nivel: 93
                DW $46EE        ;nivel: 94
                DW $46AD        ;nivel: 95
                DW $466C        ;nivel: 96
                DW $462B        ;nivel: 97
                DW $45EA        ;nivel: 98
                DW $45AA        ;nivel: 99
                DW $4569        ;nivel: 100
                DW $4529        ;nivel: 101
                DW $44E9        ;nivel: 102
                DW $44A8        ;nivel: 103
                DW $4469        ;nivel: 104
                DW $4429        ;nivel: 105
                DW $43E9        ;nivel: 106
                DW $43A9        ;nivel: 107
                DW $436A        ;nivel: 108
                DW $432A        ;nivel: 109
                DW $42EB        ;nivel: 110
                DW $42AC        ;nivel: 111
                DW $426D        ;nivel: 112
                DW $422E        ;nivel: 113
                DW $41EF        ;nivel: 114
                DW $41B0        ;nivel: 115
                DW $4171        ;nivel: 116
                DW $4132        ;nivel: 117
                DW $40F3        ;nivel: 118
                DW $40B5        ;nivel: 119
                DW $4076        ;nivel: 120
                DW $4037        ;nivel: 121
                DW $3FF9        ;nivel: 122
                DW $3FBA        ;nivel: 123
                DW $3F7C        ;nivel: 124
                DW $3F3D        ;nivel: 125
                DW $3EFF        ;nivel: 126
                DW $3EC0        ;nivel: 127
                DW $3E81        ;nivel: 128
                DW $3E43        ;nivel: 129
                DW $3E04        ;nivel: 130
                DW $3DC6        ;nivel: 131
                DW $3D87        ;nivel: 132
                DW $3D49        ;nivel: 133
                DW $3D0A        ;nivel: 134
                DW $3CCC        ;nivel: 135
                DW $3C8D        ;nivel: 136
                DW $3C4E        ;nivel: 137
                DW $3C10        ;nivel: 138
                DW $3BD1        ;nivel: 139
                DW $3B92        ;nivel: 140
                DW $3B53        ;nivel: 141
                DW $3B14        ;nivel: 142
                DW $3AD5        ;nivel: 143
                DW $3A96        ;nivel: 144
                DW $3A57        ;nivel: 145
                DW $3A18        ;nivel: 146
                DW $39D9        ;nivel: 147
                DW $3999        ;nivel: 148
                DW $395A        ;nivel: 149
                DW $391A        ;nivel: 150
                DW $38DA        ;nivel: 151
                DW $389A        ;nivel: 152
                DW $385B        ;nivel: 153
                DW $381A        ;nivel: 154
                DW $37DA        ;nivel: 155
                DW $379A        ;nivel: 156
                DW $3759        ;nivel: 157
                DW $3719        ;nivel: 158
                DW $36D8        ;nivel: 159
                DW $3697        ;nivel: 160
                DW $3656        ;nivel: 161
                DW $3615        ;nivel: 162
                DW $35D3        ;nivel: 163
                DW $3591        ;nivel: 164
                DW $3550        ;nivel: 165
                DW $350D        ;nivel: 166
                DW $34CB        ;nivel: 167
                DW $3489        ;nivel: 168
                DW $3446        ;nivel: 169
                DW $3403        ;nivel: 170
                DW $33C0        ;nivel: 171
                DW $337C        ;nivel: 172
                DW $3339        ;nivel: 173
                DW $32F5        ;nivel: 174
                DW $32B1        ;nivel: 175
                DW $326C        ;nivel: 176
                DW $3227        ;nivel: 177
                DW $31E2        ;nivel: 178
                DW $319D        ;nivel: 179
                DW $3157        ;nivel: 180
                DW $3111        ;nivel: 181
                DW $30CB        ;nivel: 182
                DW $3084        ;nivel: 183
                DW $303D        ;nivel: 184
                DW $2FF5        ;nivel: 185
                DW $2FAE        ;nivel: 186
                DW $2F65        ;nivel: 187
                DW $2F1D        ;nivel: 188
                DW $2ED4        ;nivel: 189
                DW $2E8A        ;nivel: 190
                DW $2E40        ;nivel: 191
                DW $2DF6        ;nivel: 192
                DW $2DAB        ;nivel: 193
                DW $2D5F        ;nivel: 194
                DW $2D13        ;nivel: 195
                DW $2CC7        ;nivel: 196
                DW $2C7A        ;nivel: 197
                DW $2C2C        ;nivel: 198
                DW $2BDE        ;nivel: 199
                DW $2B8F        ;nivel: 200
                DW $2B40        ;nivel: 201
                DW $2AF0        ;nivel: 202
                DW $2A9F        ;nivel: 203
                DW $2A4D        ;nivel: 204
                DW $29FB        ;nivel: 205
                DW $29A8        ;nivel: 206
                DW $2954        ;nivel: 207
                DW $2900        ;nivel: 208
                DW $28AA        ;nivel: 209
                DW $2854        ;nivel: 210
                DW $27FD        ;nivel: 211
                DW $27A5        ;nivel: 212
                DW $274B        ;nivel: 213
                DW $26F1        ;nivel: 214
                DW $2696        ;nivel: 215
                DW $2639        ;nivel: 216
                DW $25DC        ;nivel: 217
                DW $257D        ;nivel: 218
                DW $251C        ;nivel: 219
                DW $24BB        ;nivel: 220
                DW $2458        ;nivel: 221
                DW $23F3        ;nivel: 222
                DW $238D        ;nivel: 223
                DW $2325        ;nivel: 224
                DW $22BC        ;nivel: 225
                DW $2250        ;nivel: 226
                DW $21E3        ;nivel: 227
                DW $2173        ;nivel: 228
                DW $2102        ;nivel: 229
                DW $208E        ;nivel: 230
                DW $2017        ;nivel: 231
                DW $1F9E        ;nivel: 232
                DW $1F21        ;nivel: 233
                DW $1EA2        ;nivel: 234
                DW $1E1F        ;nivel: 235
                DW $1D99        ;nivel: 236
                DW $1D0E        ;nivel: 237
                DW $1C80        ;nivel: 238
                DW $1BEC        ;nivel: 239
                DW $1B53        ;nivel: 240
                DW $1AB5        ;nivel: 241
                DW $1A10        ;nivel: 242
                DW $1964        ;nivel: 243
                DW $18AF        ;nivel: 244
                DW $17F1        ;nivel: 245
                DW $1728        ;nivel: 246
                DW $1653        ;nivel: 247
                DW $156E        ;nivel: 248
                DW $1476        ;nivel: 249
                DW $1367        ;nivel: 250
                DW $1239        ;nivel: 251
                DW $10E2        ;nivel: 252
                DW $0F4F        ;nivel: 253
                DW $0D58        ;nivel: 254
                ;DW $0A90        ;nivel: 255		;Pongo 10 y no 00 para que la interupcion del TIM no se libere
		                 			;dentro de la int IRQ
;*********************************************************************
; SUBRUTINAS DE INTERRUPCIONES
;*********************************************************************


;*********************************************************************
; ADC_ISR - Rutina de Servicio de Conversion Analogico Digital Completada
;*********************************************************************
adc_isr:
        rti






retard_125us:	mov #!78,temp_125us
rtdo_125us 	nop
		dbnz temp_125us,rtdo_125us	;para Cristal 9,8304 Mhz
		rts

retard_250us:   jsr retard_125us
                jsr retard_125us
                rts

retard_500us:   jsr retard_250us
                jsr retard_250us
                rts


;*********************************************************************
; TIM_OV_ISR - Rutina de Servicio de Interrupcion por Overflow del Timer
;*********************************************************************
tim_ov_isr:

	bset PIN_ROJO,PORTA	; Pendo led
        lda #Pulso 		;  
RTDO:   nop
        dbnza RTDO
	bclr PIN_ROJO,PORTA	; Apago PIN
	;bclr IMASK,INTSCR	; Habilito las interrupciones IRQ
	bset TSTOP,TSC		; Detengo el contador del timmer
	lda TSC 		; Secuencia requerida para limpiar el TOF flag: (1) Leo el TSC reg.
	bclr TOF,TSC 		; (2) Escribo "0" en el TOF flag


        rti

;*********************************************************************
; TIM_0_ISR - Rutina de Servicio de Interrupcion del Canal 0 del Timer
;*********************************************************************
tim_0_isr:
        rti                     ;retorna


;*********************************************************************
; TIM_1_ISR - Rutina de Servicio de Interrupcion del Canal 1 del Timer
;*********************************************************************
tim_1_isr:
        rti

;*********************************************************************
; IRQ_ISR - Interrupt Request Service Routine
;*********************************************************************
irq_isr:
        	bset IMASK,INTSCR		;Deshabilito las interrupciones IRQ
	        lda #!02			;cargo A = 2
                ldx nivel_luz			;cargo X = nivel_luz
	        mul 				;multiplica A*X (MSB en X y LSB en A)
                sta result_L
                stx result_H
                ldhx result_H
                ldx result_L
                lda Tabla,x
                sta TMODH
                jsr inc_hx
                lda Tabla,x
                sta TMODL
                bset TRST,tsc			;reseteo TIM Counter Registers antes de liberar contador
                bclr TSTOP,TSC			;Libero el contador del timmer (empieza a contar)
               	bset ACK,INTSCR			;Escribo un 1 en ACK para borrar IRQ Flag
                ;inc nivel_luz
                ;jmp irq_isr
                rti


;*********************************************************************
* DUMMY_ISR - Dummy Interrupt Service Routine
;*********************************************************************
dummy_isr:
        rti                     ; vuelve sin hacer nada

;*********************************************************************
; DEFINICION DE VECTORES
;*********************************************************************
		ORG 	VECTORES

	        dw      adc_isr         ; ADC Conv.Complete Vector      ($FFDE-$FFDF)
	        dw      keyb_isr        ; Keyboard Vector               ($FFE0-$FFE1)
        	dw      dummy_isr       ; (No Vector Assigned)          ($FFE2-$FFE3)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFE4-$FFE5)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFE6-$FFE7)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFE8-$FFE9)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFEA-$FFEB)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFEC-$FFED)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFEE-$FFEF)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFF0-$FFF1)
	        dw      tim_ov_isr      ; TIM1 Overflow Vector          ($FFF2-$FFF3)
	        dw      tim_1_isr       ; TIM1 Channel 1 Vector         ($FFF4-$FFF5)
	        dw      tim_0_isr       ; TIM1 Channel 0 Vector         ($FFF6-$FFF7)
	        dw      dummy_isr       ; (No Vector Assigned)          ($FFF8-$FFF9)
	        dw      irq_isr         ; ~IRQ1                         ($FFFA-$FFFB)
	        dw      dummy_isr       ; SWI Vector                    ($FFFC-$FFFD)
	        dw      main           ; Reset Vector                  ($FFFE-$FFFF)







