;*****************************************************************************
;                                                                            *
;    NOM:	M2K2					                     *
;    Date:      13/02/10                                                     *
;    Version:   1.0                                                          *
;    Circuit:   M2K2 + PIC12C508			                     *
;    Auteur:    F4AVI Fabrice                                                *
;                                                                            *
;*****************************************************************************
;                                                                            *
;    Fichier requis: P12F508.inc                                             *
;                                                                            *
;                                                                            *
;                                                                            *
;*****************************************************************************
;                                                                            *
;    Notes:                                                                  *
;		Pilotage de la synthese d'une sonde M2K2		     *
;		PLL LMX2316, PIC alimente en 3V3, Oscillateur interne RC     *
;                                                                            *
;                                                                            *
;                                                                            *
;*****************************************************************************

        LIST    p=12F508
        #INCLUDE  "P12F508.INC"

        __config b'11010'	; bit4 : 1 -> MCLR enabled
				; bit3 : 1 -> CP off
				; bit2 : 0 -> WDT disabled
				; bit1:bit0 : 10 -> INTRC oscillator 
          
;************************************************************************
;	Registres Processeur						*
;************************************************************************

indf	equ	00h
tmr0	equ	01h
pcl	equ	02h
stat	equ	03h
fsr	equ	04h
osccal	equ	05h
gpio	equ	06h

;************************************************************************
;	Variables en RAM						*
;************************************************************************
	CBLOCK	0x07

; routines TEMPORISATION
savW0		; sauvegarde W
savW1		; sauvegarde W
savW2		; sauvegarde W
temp0		; compteur
temp1		; compteur
temp2		; compteur

;routine LMX2316
regh		; poids fort donnees a transmettre
regm		; poids milieu donnees a transmettre
regl		; poids faible donnees a transmettre
cnt_spi		; compteur de bits

ENDC


;************************************************************************
;	Definition des ALIAS et constantes				*
;************************************************************************

; ALIAS systeme
#define	gp0	gpio,0
#define	gp1	gpio,1
#define	gp2	gpio,2
#define	gp3	gpio,3
#define	gp4	gpio,4
#define	gp5	gpio,5
#define	z	stat,2
#define	c	stat,0

; ALIAS personnels
#define CLK	gp0
#define DATA	gp1
#define LE	gp2

; Constantes


        org     0
;************************************************************************
;	Vecteur RESET							*
;************************************************************************

	movlw	b'01000111'	; bit7 : wakeup pin on change
				; bit6 : pullups
				; bit5 : tmr0 clock source
				; bit4 : tmr0 source edge
				; bit3 : prescaler assignement
				; bit2-0 : prescaler value
	option			; ecriture registre option

	movlw	b'11111000'	; GP0, GP1, GP2 out; GP3, GP4, GP5 in
	tris	gpio

	clrw
      	movwf   gpio		; mise à 0 des sorties
	call	ini_pll		; Initialisation PLL
 	goto	main		; saut programme principal


;*************************************************************************
;	Programme Principal						 *
;*************************************************************************
main
	goto	main


;*************************************************************************
;	ROUTINES LMX2316							*
;*************************************************************************

;************************************************
; Emission SPI 						*
; Entree : regh:regm:regl contiennent les 21  	*
;		bits a transmettre			*
;************************************************
tx_spi
	movlw   .5			; chargement compteur 5 bits  
	movwf   cnt_spi
	rlf	regh,1		; suppression bits inutiles
	rlf	regh,1
	rlf	regh,1
bclspi1 
	rlf   regh,1		; decalage gauche buffer emission
    	btfsc   c
    	bsf     DATA		; transfert bit vers DATA
    	btfss   c
    	bcf     DATA
    	call    retard_spi	; temporisation
    	bsf     CLK           	; creation front horloge CLK
    	call    retard_spi	; temporisation
	bcf     CLK			; retombee CLK
	call    retard_spi	; temporisation
	decfsz  cnt_spi,1		; decrementation compteur de bits
	goto    bclspi1		; bouclage tant que tous les bits non transmis

	movlw   .8			; chargement compteur 8 bits  
	movwf   cnt_spi
bclspi2	
	rlf   regm,1		; decalage gauche buffer emission
    	btfsc   c
    	bsf     DATA		; transfert bit vers DATA
    	btfss   c
    	bcf     DATA
    	call    retard_spi	; temporisation
    	bsf     CLK           	; creation front horloge CLK
    	call    retard_spi	; temporisation
	bcf     CLK			; retombee CLK
	call    retard_spi	; temporisation
	decfsz  cnt_spi,1		; decrementation compteur de bits
	goto    bclspi2		; bouclage tant que tous les bits non transmis

	movlw   .8			; chargement compteur 8 bits  
	movwf   cnt_spi
bclspi3	
	rlf     regl,1		; decalage gauche buffer emission
    	btfsc   c
    	bsf     DATA		; transfert bit vers DATA
    	btfss   c
    	bcf     DATA
    	call    retard_spi	; temporisation
    	bsf     CLK           	; creation front horloge CLK
    	call    retard_spi	; temporisation
	bcf     CLK			; retombee CLK
	call    retard_spi	; temporisation
	decfsz  cnt_spi,1		; decrementation compteur de bits
	goto    bclspi3		; bouclage tant que tous les bits non transmis

    	call    retard_spi	; temporisation
	bsf	  LE			; creation front Latch Enable
	call	  retard_spi	; temporisation
	bcf	  LE			; retombee LE
    	return

;************************************************
; Initialisation PLL 					*
; 								*
; Sequence initialisation et transfert des 	*
; valeurs R=160, B=90 et A=16				*
;************************************************
ini_pll
	movlw	0x00
	movwf	regh			; poids fort mot commande function init
	movlw	0x00
	movwf	regm			; poids moyen mot commande
	movlw	0xD3			
	movwf	regl			; poids faible mot commande
	call	tx_spi		; envoi les 21 bits

	movlw 0x00
	movwf	regh			; poids fort mot commande R
	movlw	0x02
	movwf	regm			; poids moyen mot commande
	movlw	0x80			
	movwf	regl			; poids faible mot commande
	call	tx_spi		; envoi les 21 bits

	movlw 0x10
	movwf	regh			; poids fort mot commande N
	movlw	0x2D
	movwf	regm			; poids moyen mot commande
	movlw	0x41			
	movwf	regl			; poids faible mot commande
	call	tx_spi		; envoi les 21 bits

	return


;************************************************************************
;	ROUTINES TEMPORISATION						*
;************************************************************************

;************************
; Retard court 4us	*
;************************
retard_spi
    nop
    nop
    return

;****************
; Routine 100us	*
;****************
rt_1
	movwf   savW0	; sauvegarde valeur W
	movlw   .25     ; W = 25
	movwf   temp0   ; temp0 = W  
brt_1
    nop
    decfsz  temp0,1     ; temp0 = temp0 - 1  temp0 = 0 ?          
    goto    brt_1       ; non, boucle
    movf    savW0,0     ; oui, restaure W
    return

;****************
; Routine 500us	*
;****************
rt_5
	movwf   savW0	; sauvegarde valeur W
	movlw   .125    ; W = 25
	movwf   temp0   ; temp0 = W  
brt_5
    nop
    decfsz  temp0,1     ; temp0 = temp0 - 1  temp0 = 0 ?          
    goto    brt_5       ; non, boucle
    movf    savW0,0     ; oui, restaure W
    return

;****************
; Routine 1ms	*
;****************
rt1
    movwf   savW0       ; sauvegarde valeur W
    movlw   .248        ; W = 248
    movwf   temp0       ; temp0 = W  
brt1
    nop
    decfsz  temp0,1     ; temp0 = temp0 - 1  temp0 = 0 ?
    goto    brt1        ; non, boucle
    movf    savW0,0     ; oui, restaure W
    return

;****************
; Routine 10ms	*
;****************
rt10
    movwf   savW1       ; sauvegarde valeur W
    movlw   .10		; W = 10
    movwf   temp1       ; temp1 = W  
brt10
    call    rt1		; appel routine tempo 1ms  
    decfsz  temp1,1     ; temp1 = temp1 - 1  temp1 = 0 ?
    goto    brt10       ; non, boucle
    movf    savW1,0     ; oui, restaure W
    return

;****************
; Routine 100ms	*
;****************
rt100
    movwf   savW1	; sauvegarde valeur W
    movlw   .100	; W = 100
    movwf   temp1   	; temp1 = W  
brt100
    call    rt1         ; appel routine tempo 1ms  
    decfsz  temp1,1     ; temp1 = temp1 - 1  temp1 = 0 ?          
    goto    brt100      ; non, boucle
    movf    savW1,0     ; oui, restaure W
    return

;****************
; Routine 1s	*
;****************
rt1s
    movwf   savW2	; sauvegarde valeur W
    movlw   .100        ; W = 100
    movwf   temp2       ; temp2 = W  
brt1s
    call    rt10        ; appel routine tempo 10ms  
    decfsz  temp2,1     ; temp2 = temp2 - 1  temp2 = 0 ?          
    goto    brt1s       ; non, boucle
    movf    savW2,0     ; oui, restaure W
    return

	END
