;----------------------------------------------------------------------
; NAME: Flash Led for 	PIC 12F675
; BY: 	Adrian Bergthaller
; WEB:  http://www.bertys.ro
;----------------------------------------------------------------------

;****************************************************************************
;* 12F675								    *								*
;*                             -----uu-----                                 *
;*                   +5 V ----| vdd    vss |---- Gnd                        *
;*                   led1 <---| gp5    gp0 |---> led4 			    *
;*                   led2 <---| gp4    gp1 |---> led3                       *
;*                 switch --->| gp3    gp2 |<--- NC                         *
;*                             ------------                                 *
;*                                                                          *
;****************************************************************************

; config. GPIO xx543210 = xx00 1100

; vers 0.1	25.03.2006

	list      p=12f675            ; list directive to define processor
	#include <p12f675.inc>        ; processor specific variable definitions

;----------------------------------------------------------------------
; Configurare
	__CONFIG _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF
	ERRORLEVEL -302
;----------------------------------------------------------------------

;----------------------------------------------------------------------
; Constant define
;----------------------------------------------------------------------
#define Led4		GPIO,0		; (o) led yellow / gelb / galben
#define Led3		GPIO,1		; (o) led red / rot / rosu

#define rezerva		GPIO,2		; (i) not connected

#define swNext		GPIO,3		; (i) intrare switch next ( not implemented )
#define	Led2		GPIO,4		; (o) led red / rot / rosu
#define	Led1		GPIO,5		; (o) led yellow / gelb / galben

;----------------------------------------------------------------------
; RAM VARIABLES DEFINITIONS
;----------------------------------------------------------------------
; Bank0 = 20h-5Fh

	CBLOCK	0x020
; -------------------
; delays 
	delayCnt:  1	; contor delay 100uS
	delayCnt1: 1	; contor delay 10mS
	delayCnt2: 1	; contor delay 1S
; -------------------
; alte variabile ..
	cntFlash: 1		; contor flash

	ENDC

;----------------------------------------------------------------------
; MACRO DEFINITIONS
;----------------------------------------------------------------------
; Selects register bank 0
BANK0 MACRO
	BCF	STATUS,RP0	; clear bank select bits
	ENDM
;----------------------------------------------------------------------
; Selects register bank 1
BANK1 MACRO
	BSF	STATUS,RP0	; 
	ENDM

;----------------------------------------------------------------------
; disable global irq 
DISABLE_IRQ MACRO
	LOCAL STOP_INT
STOP_INT
	BCF	INTCON,GIE	; Disable global interrupt
	BTFSC INTCON,GIE	; check if disabled 
	GOTO STOP_INT		; nope, try again
	ENDM
;----------------------------------------------------------------------
; enable global irq 
ENABLE_IRQ MACRO
	LOCAL	START_INT
START_INT	
	BSF	INTCON,GIE	; Enable global interrupt
	BTFSS INTCON,GIE	; check if disabled 
	GOTO START_INT		; nope, try again
	ENDM

;----------------------------------------------------------------------
; macro definitions / comands / comenzi ..

Led1_ON MACRO
	bsf Led1
	nop
	ENDM

Led1_OFF MACRO
	bcf Led1
	nop
	ENDM


Led2_ON MACRO
	bsf Led2
	nop
	ENDM

Led2_OFF MACRO
	bcf Led2
	nop
	ENDM


Led3_ON MACRO
	bsf Led3
	nop
	ENDM

Led3_OFF MACRO
	bcf Led3
	nop
	ENDM

Led4_ON MACRO
	bsf Led4
	nop
	ENDM

Led4_OFF MACRO
	bcf Led4
	nop
	ENDM
;----------------------------------------------------------------------
; END macro definitions
;----------------------------------------------------------------------


;********************************************************************** 
;  PROGRAMUL
;**********************************************************************
	ORG     0x000           	; processor reset vector
	goto    MAIN            	; go to beginning of program

;-----------------------------------------------------------------------------
;	Interrupt service routine
;-----------------------------------------------------------------------------
	ORG     0x004  			; interrupt vector location
	RETFIE				; return from interrupt

;-----------------------------------------------------------------------------
;	 MAIN ROUTINE START HERE
;-----------------------------------------------------------------------------
	ORG     0x010
MAIN:
;-----------------------------------------------------------------------------
; Init_Prog
;  - Initialize program & setups
;  - Initilaize all ports to known state before setup routines are called
;-----------------------------------------------------------------------------
Init_Prog:
; int osc calibration / Calibrarea oscilatorului intern de 4 Mhz
	BANK1
	call	0x3FF		; get the calibration value
	movwf 	OSCCAL
	BANK0
; disable irqs / dezactivare intreruperi
	DISABLE_IRQ
; ports init / initializare porturi
	call Init_Ports
; leds off / initializari leduri
	call Init_Led
; init.end / ok, la munca!

;-----------------------------------------------------------------------------
; Program loop start here (forever_loop)
;-----------------------------------------------------------------------------
Prog_Loop:
	; back to BANK0 / revin la Bank-ul 0
	BANK0

	movlw 0x05
	movwf cntFlash

flash1		; flash mode 1
	Led1_ON
	call Delay_100mS
	Led1_OFF
	call Delay_100mS

	Led3_ON
	call Delay_100mS
	Led3_OFF
	call Delay_100mS

	Led2_ON
	call Delay_100mS
	Led2_OFF
	call Delay_100mS

	Led4_ON
	call Delay_100mS
	Led4_OFF
	call Delay_100mS


	Led1_ON
	call Delay_100mS
	Led1_OFF
	call Delay_100mS


	Led3_ON
	call Delay_100mS
	Led3_OFF
	call Delay_100mS


	Led2_OFF
	call Delay_100mS
	Led2_OFF
	call Delay_100mS


	Led4_OFF
	call Delay_100mS
	Led4_OFF
	call Delay_100mS

	decfsz cntFlash, f
	goto flash1
; end 1

	goto Prog_Loop

;-----------------------------------------------------------------------------
; Library / PROCEDURI
;-----------------------------------------------------------------------------

;-----------------------------------------------------------------------------
; Delay 1S
;-----------------------------------------------------------------------------
Delay_1S:					; Call = 2 cycles
	movlw	d'100'				; 1 cycles
	movwf	delayCnt2			; 1 cycles
Delay_1S_1:
	call	Delay_10mS			; 2 cycles
	decfsz	delayCnt2,f			; 
	goto	Delay_1S_1			; 1+2 cycles
	return

;-----------------------------------------------------------------------------
; Delay 100 mS
;-----------------------------------------------------------------------------
Delay_100mS:					; Call = 2 cycles
	movlw	d'10'				; 1 cycles
	movwf	delayCnt2			; 1 cycles
Delay_100mS_1:
	call	Delay_10mS			; 2 cycles
	decfsz	delayCnt2,f			; 
	goto	Delay_100mS_1			; 1+2 cycles
	return

;-----------------------------------------------------------------------------
; Delay 10 mS
;-----------------------------------------------------------------------------
Delay_10mS:						; Call = 2 cycles
	movlw	d'100'				; 1 cycles
	movwf	delayCnt1			; 1 cycles
Delay_10mS_1:
	call	Delay_100uS			; 2 cycles
	decfsz	delayCnt1,f			; 
	goto	Delay_10mS_1		; 1+2 cycles
	return

;-----------------------------------------------------------------------------
; Delay 100 uS = (5+(3*Constant))*OSC/4
;-----------------------------------------------------------------------------
Delay_100uS:					; Call = 2 cycles
	movlw	d'31'				; 1 cycle
	movwf	delayCnt			; 1 cycle
	decfsz	delayCnt,f			; 
	goto	$-1					; 1+2 cycles
    return						; 2 cycles

;-----------------------------------------------------------------------------
; Port init / Initialize Port
;-----------------------------------------------------------------------------
Init_Ports:
	BANK0				; Bank 0
	clrf	GPIO			; 1)Status of pins:  GPIO (GPIO=0 <ViL, =1 >ViH)
	movlw	b'00000111'		; 2)Comparator configuration
	movwf	CMCON			; Set GP<2:0> digital IO (comparator Off)
	BANK1				; Bank 1
	bcf     OPTION_REG,5		; 3)TMR0 clock source select:  Internal instruction cycle clock (CLKOUT)
	bsf	OPTION_REG,7		; 4)GPIO pull-ups: is disabled
	clrf	WPU			; 5)Clear weak pull-up register
	clrf	IOC			; 6)Clear Interrupt-on-change register

;-----------------------------------------------------------------------------
; Port setup GP0-GP5
;-----------------------------------------------------------------------------
;GP0 (Led Y)		act as output
;GP1 (Led R)		act as output (eventual rezerva)
;GP2 (NC)			act as input - rezerva
;GP3 (switch)		can be input only
;GP4 (Led R)		act as output
;GP5 (Led Y)		act as output
;-----------------------------------------------------------------------------

	movlw   b'00001100'		; 7) data direction TRISO 0: output, 1: input (Hi-Z mode)
	movwf   TRISIO         	; {0,0,GPIO<5:0>} 0-Output 1-Input
; * 12F675 Only *			; initializaz registrul de selectare a intrarilor analogice
	clrf ANSEL				; 8)* xxxx ANS<3..0> =0 digital I/O, =1 analog input	
	;
	BANK0
	return
;-----------------------------------------------------------------------------

;-----------------------------------------------------------------------------
; Initialize Leduri
;-----------------------------------------------------------------------------
Init_Led:
	Led1_OFF
	Led2_OFF
	Led3_OFF
	Led4_OFF
	return
;-----------------------------------------------------------------------------
; calibrarea oscilator intern
;-----------------------------------------------------------------------------
	org 3FFh
; [34]9Ch for  pic 12F675
	retlw 0x9C	;
;-----------------------------------------------------------------------------
	END                    ; directive 'end of program'
;-----------------------------------------------------------------------------
