;**********************************************************************
;                               PEDAL                                 *
;**********************************************************************

;                                                                     *
;    Filename:	    Vysilac_v2.asm                                    *
;    Date:          24. 2.2009                                        *
;    File Version:  2.0                                               *
;                                                                     *
;    Author:        Vlastimil Vlcek                                   *
;    Company:       Policna 79, Valasske Mezirici                     *
;                                                                     * 
;                                                                     *
;**********************************************************************
;**********************************************************************
;Program cte spinace nozniho pedalu a po seriove lince (USART) posila *
;povely pro prepnuti kanalu kytaroveho komba (CLEAN, SHARP, BROWN),   *
;pro prepnuti funkci VOLUME a prepina rele pro zapnuti/vypnuti        *
;efektoveho kanalu (EFFECT/BYPAS)                                     *
;Vsechny funkce jsou navic indikovany LED diodami.                    *
;V teto verzi je komunikace s prijimacem v kytarovem kombu prozatim   *
;jednosmerna. Pokud se vyskytnouo problemy se spolehlivosti prenosu   *
;dat, bude vytvorena verze s obousmernou komunikaci s potvrzovanim    *
;spravnosti prijeti. Hardware s takovou moznosti pocita.              *
;                                                                     *
;Pro kazdy datovy byte je vypocteno kontrolni cislo (dvojkovy komple- *
;ment), ktere se vysila ihned za datovym byte. Prijimac vypocte z     *
;prijatych dat kontrolni cislo stejnym zpusobem a pokud se obe        *
;kontrolni cisla shoduji, povazuje data za platna.                    *
;**********************************************************************


	list      p=16f628            ; list directive to define processor
	__CONFIG	0X2150	;osc=INT (PINY=I/O),WDT=OFF,PWRTE=ON,
	;MCLRE=OFF (DIGITAL I/O), BOREN=ON,
				;LVP=OFF, CODE PROTECTION=OFF

;DEFINICE SFR:
;********************
INDF                         EQU     H'0000'
TMR0                         EQU     H'0001'
PCL                          EQU     H'0002'
STATUS                       EQU     H'0003'
FSR                          EQU     H'0004'
PORTA                        EQU     H'0005'
PORTB                        EQU     H'0006'
PCLATH                       EQU     H'000A'
INTCON                       EQU     H'000B'
PIR1                         EQU     H'000C'
TMR1L                        EQU     H'000E'
TMR1H                        EQU     H'000F'
T1CON                        EQU     H'0010'
TMR2                         EQU     H'0011'
T2CON                        EQU     H'0012'
CCPR1L                       EQU     H'0015'
CCPR1H                       EQU     H'0016'
CCP1CON                      EQU     H'0017'
RCSTA                        EQU     H'0018'
TXREG                        EQU     H'0019'
RCREG                        EQU     H'001A'
CMCON                        EQU     H'001F'

OPTION_REG                   EQU     H'0081'
TRISA                        EQU     H'0085'
TRISB                        EQU     H'0086'
PIE1                         EQU     H'008C'
PCON                         EQU     H'008E'
PR2                          EQU     H'0092'
TXSTA                        EQU     H'0098'
SPBRG                        EQU     H'0099'
EEDATA                       EQU     H'009A'
EEADR                        EQU     H'009B'
EECON1                       EQU     H'009C'
EECON2                       EQU     H'009D'
VRCON                        EQU     H'009F'

;DEFINICE UZIVATELSKYCH REGISTRU:
RAM		EQU	0x20	;pocatek uzivatelske oblasti pameti RAM
TEMP		EQU	RAM	;pomocny registr
FLAGS		EQU	RAM+1	;priznaky spinacu s prepinaci funkci:
				;vyznam jednotlivych bitu:
				;0 = BYPAS
				;1 = VOLUME

DELAY		EQU	RAM+2	;pomocna promenna (casova prodleva)
citac1		equ	RAM+3
citac2		equ	RAM+4
W_TEMP		EQU	RAM+5
STATUS_TEMP	EQU	RAM+6
CHECK		EQU	RAM+7


;DEFINICE BITU:
#DEFINE		TMR1CS		T1CON,1		;Volba rezimu Timer/Counter
#DEFINE		TMR1ON		T1CON,0		;Aktivace TIMER1 (Enable)		
#DEFINE		T1CKPS0		T1CON,4		;Preddelicka - LSB
#DEFINE		T1CKPS1         T1CON,5         ;Preddelicka - MSB
						;00 = 1:1
						;01 = 1:2
						;10 = 1:4
						;11 = 1:8
#DEFINE		TMR1IF		PIR1,0		;Flag preteceni TIMER1

#DEFINE		LED_CLEAN	PORTA,2		;indikacni LED diody
#DEFINE		LED_SHARP       PORTA,1
#DEFINE		LED_BROWN       PORTA,0
#DEFINE		LED_BYPAS       PORTA,7
#DEFINE		LED_VOLUME      PORTA,4
#DEFINE		RELE		PORTA,3		;prepinani RELE EFFECT/BYPAS

#DEFINE		TL_CLEAN	PORTB,4		;nozni spinace pedalu
#DEFINE		TL_SHARP        PORTB,3
#DEFINE		TL_BROWN        PORTB,0
#DEFINE		TL_BYPAS        PORTB,6
#DEFINE		TL_VOLUME       PORTB,5

#DEFINE		TX		PORTB,2		;USART - vystup
#DEFINE		RX		PORTB,1         ;USART - vstup

#define RP0     status,5
#define RP1	status,6
#define T0IF    intcon,2        ;Indikator preteceni TMR0
#define C       status,0
#define Z       status,2
#define	BRGH	TXSTA,2		;high-speed/low speed prenos
#define	SYNC	TXSTA,4		;synchronni/asynchronní rezim
#define	SPEN	RCSTA,7		;Serial Port Enable
#define	TXEN	TXSTA,5		;Transmit Enable
#define	TXIF	PIR1,4		;priznak vyprazdneni TXREG         
#define	TRMT	TXSTA,1		;priznak vyprazdneni TSR (vsechno odeslano)
#define	GIE	INTCON,7	;Global Interrupt (banka 0)
#define	TXIE	PIE1,4		;USART Transmit Interrupt Enable bit (banka 1)
#define	PEIE	INTCON,6	;Periferni preruseni (banka 0)

	ORG	0
     	GOTO	START

;************************************************************************
;                                PODPROGRAMY                            *
;************************************************************************
bank0	macro   		;Nastaveni banky 0
	bcf	RP0		;(pro 16F628 nutne 2 bity!)
	bcf	RP1
	endm
bank1	macro                   ;Nastaveni banky 1
	bsf	RP0     	
	bcf	RP1
	endm

;Init: Inicializace mikroradice:
Init    movlw	b'00000111'	;Vypnuti analogovych komparatoru.
	movwf	CMCON
	bank0
	movlw	.255		;Výchozí nastavení LED před inicializací
	movwf	porta
	bank1                   
        movlw   b'11111111'	;Nastaveni portu:	
        movwf   trisb           ;RB0 - RB7 = IN
        movlw   b'00100000'	
        movwf   trisa           ;RA0,1,2,3,4,6 = OUT, RA5 = IN.                
	BCF	OPTION_REG,7	;zapnuti pull-up rezistoru na PORTu B.
	bank0
	bank1
;Nastaveni parametru USART:
	BSF	BRGH		;High-speed prenos
	MOVLW	.25		;Baud Rate = 9600
	MOVWF	SPBRG
	BCF	SYNC		;asynchronni rezim	
	BANK0
	BSF	SPEN		;Serial Port Enable
	BANK1
	BSF	TXEN		;Vysilani povoleno!	
;Pocatecni nastaveni indikacnich LED diod a rele:
	BANK0	
	BCF	TMR1ON		;Vypnuti TIMER1
	BCF	TMR1CS		;TIMER1 - interni hodiny
	BCF	LED_CLEAN	;nastaveni LED diod a rele:
	BSF	LED_SHARP       ;CLEAN, BYPAS = ON
	BSF	LED_BROWN       ;SHARP, BROWN, VOLUME, RELE = OFF
	BCF	LED_BYPAS
	BSF	LED_VOLUME
	BCF	RELE
	MOVLW	B'00000001'	;nastaveni priznaku spinacu:
	MOVWF	FLAGS		;BYPAS=ON, VOLUME=OFF
	return
;---------------------------------------------------------------
;Zpozdovaci smycka 100ms:
t_100ms	movlw	.198
	movwf	citac1
	movlw	.167
	movwf	citac2
	decfsz	citac2,1
	goto	$-1
	decfsz	citac1,1
	goto	$-5
	return

;***************************************************************
;                      HLAVNI PROGRAM                          *
;***************************************************************
START	CALL	INIT
		
ST	BTFSS	TL_CLEAN	;testy spinacu a volani jednotlivych
	CALL	CLEAN           ;podprogramu
        BTFSS	TL_SHARP
	CALL	SHARP
	BTFSS	TL_BROWN
	CALL	BROWN
	BTFSS	TL_BYPAS
	CALL	BYPAS
	BTFSS	TL_VOLUME
	CALL	VOLUME
	GOTO	ST
;********************************************************************
;Podprogramy hlavniho programu (krome inicializace a casovych smycek)
;********************************************************************
CLEAN   MOVLW	b'00000001'	;Stlacen spinac CLEAN - vysila se 1
	MOVWF	TXREG
	bank1
	btfss	trmt
	goto	$-1
	bank0
	MOVLW	b'11111110'	;CHECKSUM - dvojkovy doplnek dat. byte
	MOVWF	TXREG
		
	BSF	LED_SHARP	;Zhasnuti a rozsviceni indikacnich LED diod
	BSF	LED_BROWN
	BCF	LED_CLEAN
	CALL	T_100ms
	BTFSS	TL_CLEAN
	GOTO	$-1
	CALL	T_100ms
	RETURN

;----------------------------------------------------------------------
SHARP	MOVLW	b'00000010'	;Stlacen spinac SHARP - vysila se 2
	MOVWF	TXREG
	bank1
	btfss	trmt
	goto	$-1
	bank0
	MOVLW	b'11111101'	;CHECKSUM
	MOVWF	TXREG

	BSF	LED_CLEAN       ;Zhasnuti a rozsviceni indikacnich LED diod
	BSF	LED_BROWN
        BCF	LED_SHARP
	CALL	T_100ms
	BTFSS	TL_SHARP
	GOTO	$-1
	CALL	T_100ms
	RETURN

;----------------------------------------------------------------------
BROWN	MOVLW	b'00000100'	;Stlacen spinac BROWN - vysila se 4
	MOVWF	TXREG
	bank1
	btfss	trmt
	goto	$-1
	bank0
	MOVLW	b'11111011'	;CHECKSUM
	MOVWF	TXREG

	BSF	LED_CLEAN       ;Zhasnuti a rozsviceni indikacnich LED diod
	BSF	LED_SHARP
	BCF	LED_BROWN
	CALL	T_100ms
	BTFSS	TL_BROWN
	GOTO	$-1
	CALL	T_100ms
	RETURN
;--------------------------------------------------------------------
BYPAS	BTFSS	FLAGS,0 	;stlacen spinac BYPAS => prepnuti rele
	GOTO	BYP_ON		;a rozsviceni/zhasnuti indikacni LED
	GOTO	BYP_OFF         

BYP_OFF	BSF	RELE		;BYPAS neaktivni - rele pritazeno, cesta pres efekt
	NOP			;prodleva nutna kvuli vystupu RA4 (otevreny
	NOP                     ;kolektor). Pricina nejasna, zjisteno experimentalne.
	NOP
	NOP
        BSF	LED_BYPAS
	BCF	FLAGS,0         ;inverze puvodni hodnoty flagu (prepnuti)
	CALL	T_100ms
	BTFSS	TL_BYPAS
	GOTO	$-1
	CALL	T_100ms
	RETURN

BYP_ON	BCF	RELE		;BYPAS aktivni - rele nepritazeno, cesta mimo efekt
	NOP
	NOP
	NOP
	NOP
	BCF	LED_BYPAS
	BSF	FLAGS,0		;inverze puvodni hodnoty flagu (prepnuti)
	CALL	T_100ms
	BTFSS	TL_BYPAS
	GOTO	$-1
	CALL	T_100ms
	RETURN
	GOTO	BYP_OFF

;---------------------------------------------------------------------
VOLUME	BTFSC	FLAGS,1 	;stlacen spinac VOLUME => vysila se:
	GOTO	VOL1		;8 = VOLUME1, pokud FLAG = 1
	GOTO	VOL2            ;16 = VOLUME2, pokud flag = 0

VOL1	MOVLW	b'00001000'     ;hlasitost 1
	MOVWF	TXREG
	bank1
	btfss	trmt
	goto	$-1
	bank0
	MOVLW	b'11110111'	;CHECKSUM
	MOVWF	TXREG

	BSF	LED_VOLUME	;zhasnutí LED VOLUME
	BCF	FLAGS,1		;inverze puvodni hodnoty flagu (prepnuti)
	CALL	T_100ms
	BTFSS	TL_VOLUME
	GOTO	$-1
	CALL	T_100ms
	RETURN

VOL2	MOVLW	b'00010000'     ;hlasitost 2
	MOVWF	TXREG
	bank1
	btfss	trmt
	goto	$-1
	bank0
	MOVLW	b'11101111'	;CHECKSUM
	MOVWF	TXREG

	BCF	LED_VOLUME	;rozsvícení LED VOLUME
	BSF	FLAGS,1		;inverze puvodni hodnoty flagu (prepnuti)
	CALL	T_100ms
	BTFSS	TL_VOLUME
	GOTO	$-1
	CALL	T_100ms
	RETURN                  

	END                       ; directive 'end of program'

