
;				JUMBO elettronica

;							Bovolenta (PD), settembre_2001
;		
 	TITLE ' controllo PLL con SDA3202 (Siemens) o TSA5511 (Philips) '

 	list F=INHX8M,P=16F628
	INCLUDE         "PLL.inc"

	__CONFIG _XT_OSC & _PWRTE_ON & _WDT_ON & _BODEN_OFF & _CP_OFF & _LVP_OFF
	
;----------------------------------------------------------------
       	ORG       0             ;
       	goto      START         ; Reset vector

;***************** INTERRUPT *************************    
                                  	
       	ORG     4             	; Interrupt vector
	retfie
	
;***************************** GIALLO_3 e BLEU_3 *************
; sono in questa posizione perchè PCL è un registro di 8 bit, quindi
; è possibile variare solo le prime 255 posizioni.
; 
_giallo_3
	call	_wait1
	movf	numero_mem,w
	addwf	PCL,f
	goto	_leggi_0
	goto	_leggi_1
	goto	_leggi_2
	goto	_leggi_3
	goto	_leggi_4
	goto	_leggi_5
	goto	_leggi_6
	goto	_leggi_7
	goto	_leggi_8
	goto	_leggi_9
	goto	_main

;**************
_bleu_3
	call	_wait1
	movf	numero_mem,w
	addwf	PCL,f
	goto	_store_0
	goto	_store_1
	goto	_store_2
	goto	_store_3
	goto	_store_4
	goto	_store_5
	goto	_store_6
	goto	_store_7
	goto	_store_8
	goto	_store_9
	goto	_main

;*************
_calcolo_canale
	call	_calcolo_canale_1
	movwf	canale_out
	return		
_calcolo_canale_1
	movf	canale,w
	addwf	PCL,f
	retlw	00		; 0
	retlw	b'00000001'	; 1
	retlw	b'00000010'	; 2
	retlw	b'00000100'	; 3
	retlw	b'00001000'	; 4
	retlw	b'00010000'	; 5
	retlw	b'00100000'	; 6
	retlw	b'01000000'	; 7
	retlw	b'10000000'	; 8
	
;***************************************************************************
;                 Trasmette un dato 
;***************************************************************************
_tx_5_byte
	movlw	b'11000010'	; address PLL
	movwf	TxReg
	call   	_bit_start	; bit start
	call	_tx_1
	movf	byte_MSB,w	; 1° byte 
	movwf	TxReg
	call	_tx_1
	movf	byte_LSB,w	; 2° byte
	movwf	TxReg
	call	_tx_1
	movlw	b'11001110'	; 3° byte
	movwf	TxReg
	call	_tx_1
	movf	canale_out,w	; 4° byte: attiva l'uscita desiderata 
	movwf	TxReg
	call	_tx_1
	call	_bit_stop	; bit stop
	return

_tx_1
	movlw   8          	 
        movwf   Count        	 
_tx_2
	bcf     STATUS,CARRY     
        rlf    	TxReg,f		; Rotazione a sinistra

        btfsc   STATUS,CARRY    ; Se il bit di TxReg,7 mette a 1 
       	bsf     SDA	   	; pin di Tx, altrimenti mette a 0
        btfss   STATUS,CARRY     
        bcf     SDA   		

        call    _scl		 
        decfsz  Count,f		 
        goto    _tx_2

	bcf    	SDA		; ACKNOWLEDGE
	call	_scl
	return

_scl
	call	Delay_01ms
	bsf	SCL
	call	Delay_01ms
	bcf	SCL
	call	Delay_01ms
	return
_bit_start
	bsf    	SDA
	call	Delay_01ms
	bsf	SCL
	call	Delay_01ms
	bcf    	SDA
	call	Delay_01ms
	bcf	SCL
	call	Delay_01ms
	return
_bit_stop
	bcf    	SDA
	call	Delay_01ms
	bsf	SCL
	call	Delay_01ms 
	bsf    	SDA
	call	Delay_01ms
	return

_tx_10
	movlw   8          	 
        movwf   Count        	 
_tx_20
	bcf     STATUS,CARRY     
        rlf    	TxReg,f		; Rotazione a sinistra

        btfsc   STATUS,CARRY    ; Se il bit di TxReg,7 mette a 1 
       	bsf     SDA	   	; pin di Tx, altrimenti mette a 0
        btfss   STATUS,CARRY     
        bcf     SDA   		

        call    _scl		 
        decfsz  Count,f		 
        goto    _tx_20
	return
;************************ RICEVE DATO DAL PLL **********
_rx
	call	_bit_start

	movlw	b'11000011'
	movwf	TxReg
	call	_tx_10

	BCF 	STATUS,6
	BSF 	STATUS,5 	
	movlw	b'00010010'	; SDA diventa ingresso   
	movwf   TRISA                 
	BCF 	STATUS,5
	
_rx_1
	call	Delay_01ms
	
	movlw   8          	 
        movwf   Count    
	clrf	RxReg    	 
_rx_3
	call	Delay_01ms
	bsf	SCL
	call	Delay_01ms
        btfsc   SDA
       	bsf     RxReg,0
        btfss   SDA     
        bcf     RxReg,0
	call	Delay_01ms
	bcf	SCL
	call	Delay_01ms
	rlf    	RxReg,f		; Rotazione a sinistra   		
	 
        decfsz  Count,f		 
        goto    _rx_3
	
	call	_bit_stop
	
	BCF 	STATUS,6
	BSF 	STATUS,5 	
	movlw	b'00010000'	; SDA ritorna uscita   
	movwf   TRISA                 
	BCF 	STATUS,5

	return

;************************ Sub delay del sistema ********

Delay_1ms	movlw  	.125		; 1ms
        	goto   	save
Delay_01ms 	
		movlw  	.1		; 0,1ms (.13)
        	goto   	save
save    	movwf   DlyCnt
redo_1  
		clrwdt
		nop
		nop
		nop
		nop
		nop
		decfsz  DlyCnt,f
        	goto    redo_1
        	retlw   0
;
;*******************************************************

	INCLUDE         "lcd.inc"
	INCLUDE         "tasti.inc"

;******************** START PROGRAM  ****************
 
START
	BCF 	STATUS,6
	BCF 	STATUS,5 	

	MOVLW 	0X07 		
	MOVWF 	CMCON 		

	BCF 	STATUS,6
	BSF 	STATUS,5 	
	
	movlw	b'00010000'	;  RA,4= in  
	movwf   TRISA           

	movlw   b'11110000'   	; RB 0-3=out ; 4-7=in
	movwf   TRISB          

	clrf    INTCON  	; Disabilita interrupt
	movlw   b'01000111'   	
	movwf   OPTREG         	
	bcf     STAT,5        	

	clrf	PORTA
	clrf	PORTB
	clrf	TMR0         	

	call	_leggi_frequenza

; ***********************  main program ***************************    
_main
	call  	DELA2M        	; Attesa 200ms  

	call    RESLCD        	; Reset LCD
	movlw   'F'           	 
	call    WRITE         	 
	movlw   'r'           	  
	call    WRITE         	 
	movlw   'e'           	 
	call    WRITE         	 
	movlw   'q'           	 
	call    WRITE         	 
	movlw   'u'           	 
	call    WRITE         	 
	movlw	'e'
	call	WRITE		

	btfss	buff4,0
	goto	_main_a
	call	_calcolo_divisori_2
	goto	_main_b
_main_a
	call	_calcolo_divisori
_main_b
	call	_visualizza_frequenza
	call	_calcolo_canale
	call	_tx_5_byte	

	movlw   'M'           	 
	call    WRITE         	 
	movlw   'H'           	 
	call    WRITE         	 
	movlw	'z'
	call	WRITE	
	
_main_1
	call	_seconda_riga	; 0c0h= 2°riga	
	
	call	DELA2M
	call	DELA2M
	call	DELA2M	

	call	_rx		; leggi status del PLL
	bcf	led_PLL
	btfsc	RxReg,6		; accendi il led se il PLL è agganciato	
	bsf	led_PLL		

	call	_leggi_status
__main_1
	call	_rx		; leggi status del PLL
	bcf	led_PLL
	btfsc	RxReg,6		; accendi il led se il PLL è agganciato	
	bsf	led_PLL		

	call	_leggi_status

	call	_tasti		; controlla se tastiera libera
	movwf	FR03		
	movf	FR03,w		; controlla se premuto GRIGIO
	xorlw	"g"		
	btfsc	zero
	goto	_grigio
	movf	FR03,w		; controlla se premuto ROSSO
	xorlw	"r"		 
	btfsc	zero
	goto	_rosso
	movf	FR03,w		; controlla se premuto GIALLO
	xorlw	"G"		
	btfsc	zero
	call	_giallo
	movf	FR03,w		; controlla se premuto BLEU
	xorlw	"b"		
	btfsc	zero
	goto	_bleu
	movf	FR03,w
	xorlw	"R"
	btfss	zero		
	goto	__main_1	
	call	_programm

	goto	_main
	
_visualizza_frequenza
	bcf	RS		; posiziona il 1° numero
	movlw	087H		 	
	movwf	BUFF	
	call	CLOCK2			
	bsf	RS		

	movf	f_giga,w	 
	addlw	30H
	call	WRITE		
	movf	f_centinaia,w	 
	addlw	30H
	call	WRITE		
	movf	f_decine,w	 
	addlw	30H
	call	WRITE		
	movf	f_unità,w	 
	addlw	30H
	call	WRITE		
	movlw	','
	call	WRITE		
	movf	f_decimale,w	 
	addlw	30H
	call	WRITE		
	return

_leggi_status
	call	_seconda_riga
	movf	RxReg,w
	call	WRITE
	movf	RxReg,w
	addlw	30H
	call	WRITE	
	return	

;****************** gestione tastiera ********************
_pro1
	call      RESLCD        ; Reset LCD

	movlw     'P'            
	call      WRITE          
	movlw     'R'            
	call      WRITE          
	movlw     'O'            
	call      WRITE          
	movlw     'G'           
	call      WRITE          
	movlw     'R'            
	call      WRITE          
	movlw     'A'           
	call      WRITE          
	movlw     'M'            
	call      WRITE         
	return
_programm
	bsf	buff4,0
	call	_pro1
	call	_wait1
	call	_lpatt3
	call	_f_giga
	call	_wait1
	call	_lpatt3
	call	_f_centinaia
	call	_wait1
	call	_lpatt3
	call	_f_decine
	call	_wait1
	call	_lpatt3
	call	_f_unità
	call	_wait1
	call	_lpatt3
	call	_f_decimale
	call	_wait1
	goto	_conferma	
_lpatt3
	call	_tasti	
	movwf	FR03		; Buffer pulsante premuto
	movf	FR03,w
	xorlw   "H"             
       	btfsc   zero            
	goto	_lpatt3
	movf	FR03,w		; controlla se premuto RESET
	xorlw	"P"		
	btfsc	zero
	goto	_main
	nop
	movf	FR03,w		; controlla se premuto GRIGIO
	xorlw	"g"		
	btfsc	zero
	goto	_lpatt3
	movf	FR03,w		; controlla se premuto ROSSO
	xorlw	"r"		
	btfsc	zero
	goto	_lpatt3
	movf	FR03,w		; controlla se premuto GIALLO
	xorlw	"G"		 
	btfsc	zero
	goto	_giallo
	movf	FR03,w		; controlla se premuto BLEU
	xorlw	"b"		
	btfsc	zero
	goto	_lpatt3
	movf	FR03,w		; controlla se premuto BLEU
	xorlw	"R"		
	btfsc	zero
	goto	_lpatt3
	return
_f_giga
	movf	FR03,w
	movwf	f_giga
	bcf	RS		; posiziona il 1° numero
	movlw	0c0H		 	
	movwf	BUFF		
	call	CLOCK2			
	bsf	RS		

	movf	f_giga,w	 
	addlw	30H
	call	WRITE
	return		
_f_centinaia
	movf	FR03,w
	movwf	f_centinaia
	bcf	RS		; posiziona il 1° numero
	movlw	0c1H		 	
	movwf	BUFF		
	call	CLOCK2		
	bsf	RS		

	movf	f_centinaia,w	 
	addlw	30H
	call	WRITE
	return		
_f_decine
	movf	FR03,w
	movwf	f_decine
	bcf	RS		; posiziona il 2° numero
	movlw	0c2H		 	
	movwf	BUFF		
	call	CLOCK2			
	bsf	RS		

	movf	f_decine,w	 
	addlw	30H
	call	WRITE		
	return
_f_unità
	movf	FR03,w
	movwf	f_unità
	bcf	RS		; posiziona il 3° numero
	movlw	0c3H		 	
	movwf	BUFF		
	call	CLOCK2		
	bsf	RS		

	movf	f_unità,w	 
	addlw	30H
	call	WRITE		
	movlw	','
	call	WRITE		
	return
_f_decimale
	movf	FR03,w
	movwf	f_decimale
	bcf	RS		; posiziona il 4° numero
	movlw	0c5H		 	
	movwf	BUFF		
	call	CLOCK2			
	bsf	RS		

	movf	f_decimale,w	 
	addlw	30H
	call	WRITE		
	return	
_conferma
	call	_tasti
	movwf	FR03
	movf	FR03,w		; controlla se premuto ' CONFERMA '
	xorlw	"R"		
	btfsc	zero
	goto	_conferma_1
	movf	FR03,w		; controlla se premuto RESET
	xorlw	"P"		
	btfss	zero
	goto	_conferma
	return
_conferma_1
	call	_salva_frequenza
	return	
_wait1
	bsf	led_tasti
	call    DELA2M     
	call	_tasti		; controlla tasto premuto
	movwf	FR03		
	movf	FR03,w		
	xorlw	"H"
	btfss	zero		
      	goto    _wait1		
	bcf	led_tasti
	return
;+++++++++++++++++++++++++++++++++ INCREMENTA FREQUENZA ++++
_grigio
	incf	f_decimale,f	; incrementa frequenza
	movf	f_decimale,w
	sublw	.10
	btfss	zero	
	goto	_grigio_1	
      	clrf	f_decimale
	incf	f_unità,f
	movf	f_unità,w
	sublw	.10
	btfss	zero	
	goto	_grigio_1	
	clrf	f_unità
	incf	f_decine,f
	movf	f_decine,w
	sublw	.10
	btfss	zero	
	goto	_grigio_1	
      	clrf	f_decine
	incf	f_centinaia,f
	movf	f_centinaia,w
	sublw	.10
	btfss	zero	
	goto	_grigio_1	
	clrf	f_centinaia
	incf	f_giga,f
_grigio_1	
	call	_visualizza_frequenza
	call	_calcolo_divisori_2
	call	_tx_5_byte
	call	DELA2M
	call	_tasti		
	movwf	FR03		
	movf	FR03,w		
	xorlw	"g"		
	btfsc	zero
	call	_grigio
	goto	__main_1
;+++++++++++++++++++++++++++++++ DECREMENTA FREQUENZA +++++++
_rosso
	movf	f_decimale,w	; decrementa frequenza
	sublw	00
	btfss	zero	
	goto	_rosso_1	
	movlw	.9
      	movwf	f_decimale

	movf	f_unità,w
	sublw	00
	btfss	zero	
	goto	_rosso_2
	movlw	.9	
	movwf	f_unità
	
	movf	f_decine,w
	sublw	00
	btfss	zero	
	goto	_rosso_3
	movlw	.9	
      	movwf	f_decine
	
	movf	f_centinaia,w
	sublw	00
	btfss	zero	
	goto	_rosso_4
	movlw	.9	
	movwf	f_centinaia
	movf	f_giga,w
	sublw	.1
	btfss	zero	
	goto	_rosso_5
	clrf	f_giga
	goto	_rosso_10
	
_rosso_1
	decf	f_decimale,f
	goto	_rosso_10
_rosso_2
	decf	f_unità,f
	goto	_rosso_10
_rosso_3
	decf	f_decine,f
	goto	_rosso_10
_rosso_4
	decf	f_centinaia,f
	goto	_rosso_10
_rosso_5
	clrf	f_decimale
	clrf	f_unità
	clrf	f_decine
	clrf	f_centinaia
	clrf	f_giga
_rosso_10	
	call	_visualizza_frequenza
	call	_calcolo_divisori_2
	call	_tx_5_byte
	call	DELA2M
	call	_tasti		
	movwf	FR03		
	movf	FR03,w		
	xorlw	"r"		
	btfsc	zero
	call	_rosso
	goto	__main_1
;+++++++++++++++++++++++++++++ CARICA MEMORIA +++++++++++++++
_giallo
	call	_seconda_riga
	movlw     'C'            
	call      WRITE          
	movlw     'a'           
	call      WRITE         
	movlw     'r'           
	call      WRITE          
	movlw     'i'            
	call      WRITE          
	movlw     'c'           
	call      WRITE          
	movlw     'a'            
	call      WRITE          
	movlw     ' '           
	call      WRITE    
	
	call	_wait1
_giallo_1
	call	_bleu_4
	call	_tasti		
	movwf	FR03		
	movf	FR03,w		; controlla che non ritorni un valore diverso da 0-9
	addlw	.246
	btfsc	carry	
	goto	_giallo_1
	movf	FR03,w	
	movwf	numero_mem	
	movf	FR03,w
	addlw	30H
	call	WRITE	
	call	_wait1	
_giallo_2
	call	_tasti
	movwf	FR03
	movf	FR03,w		; controlla se premuto ' CONFERMA '
	xorlw	"R"		 
	btfsc	zero
	goto	_giallo_3
	movf	FR03,w		; controlla se premuto RESET
	xorlw	"P"		 
	btfss	zero
	goto	_giallo_2
	call	_wait1
	goto	_main

;+++++++++++++++++++++++++++++ MEMORIZAZIONE FREQUENZE ++++++
_bleu
	call	_seconda_riga 
	movlw     'S'           
	call      WRITE         
	movlw     't'            
	call      WRITE         
	movlw     'o'           
	call      WRITE        
	movlw     'r'          
	call      WRITE          
	movlw     'e'            
	call      WRITE         
	movlw     ' '           
	call      WRITE          
	
	call	_wait1
_bleu_1
	call	_bleu_4
	call	_tasti		
	movwf	FR03		
	movf	FR03,w		; controlla che non ritorni un valore diverso da 0-9
	addlw	.246
	btfsc	carry	
	goto	_bleu_1
	movf	FR03,w	
	movwf	numero_mem	
	movf	FR03,w
	addlw	30H
	call	WRITE	
	call	_wait1	

	movlw     ' '           
	call      WRITE  
	movlw     'C'           
	call      WRITE         
	movlw     'a'            
	call      WRITE         
	movlw     'n'           
	call      WRITE        
	movlw     ' '          
	call      WRITE    

_bleu_1a
	bcf	RS		
	movlw	0cdH		 	
	movwf	BUFF		
	call	CLOCK2			
	bsf	RS		
	call	_tasti		
	movwf	FR03		
	movf	FR03,w		; controlla che non ritorni un valore diverso da 0-8
	addlw	.247
	btfsc	carry	
	goto	_bleu_1a
	movf	FR03,w	
	movwf	numero_can	
	movf	FR03,w
	addlw	30H
	call	WRITE	
	call	_wait1	
	swapf	numero_can,f	; usa il secondo nible di f_giga per memorizzare il canale
	movf	numero_can,w
	addwf	f_giga
_bleu_2
	call	_tasti
	movwf	FR03
	movf	FR03,w		; controlla se premuto ' CONFERMA '
	xorlw	"R"		 
	btfsc	zero
	goto	_bleu_3
	movf	FR03,w		; controlla se premuto RESET
	xorlw	"P"		 
	btfss	zero
	goto	_bleu_2
	call	_wait1
	goto	_main

_bleu_4
	bcf	RS		; posiziona il 1° numero
	movlw	0c7H		 	
	movwf	BUFF		
	call	CLOCK2			
	bsf	RS		
	return

;************** CALCOLO DIVISORI PLL *******************

	INCLUDE		"calc_div.inc"
	
;************** SALVATAGGIO DATI IN EEPROM ************** 

	INCLUDE         "eeprom.inc"
	
;-----------------------------------------
       END
