;                          

;**************************************************************************
;*                                                                        *
;**************************************************************************
;*                                PIC16F874/877                           *
;*                             --------\/--------                         *
;*                       +5V  -|MCLR     PGD/RB7|-                        *
;*                            -|RA0/AN0  PGC/RB6|-                        *
;*                            -|RA1/AN1      RB5|-                        *
;*                            -|RA2/AN2      RB4|-                        *
;*                            -|RA3/AN3  PGM/RB3|-                        *
;*                            -|RA4          RB2|-                        *
;*                            -|RA5/AN4      RB1|-                        *
;*                            -|RE0/RD   INT/RB0|-                        *
;*                 LCDRW      -|RE1/WR       Vdd|-  +5V                   *
;*                 LCDRES     -|RE2/CS       Vss|-   0V                   *
;*                       +5V  -|Vdd          RD7|-  LCDDB7                *
;*                        0V  -|Vss          RD6|-  LCDDB6                *
;*        Krystal 4,00 Mhz /  -|osc1         RD5|-  LCDDB5                *
;*                         \  -|osc2         RD4|-  LCDDB4                *
;*                            -|RC0       Rx/RC7|-  LCDA0                 *
;*                            -|RC1       Tx/RC6|-  LCDCS1                *
;*                            -|RC2          RC5|-  LCDCS2                *
;*                            -|RC3/SCL  SDA/RC4|-                        *
;*                 LCDDB0     -|RD0          RD3|-  LCDDB3                *
;*                 LCDDB1     -|RD1          RD2|-  LCDDB2                *
;*                             ------------------                         *
;*                                                                        *
;**************************************************************************

list p=16f877A, R=DEC
include "p16f877A.inc"

RAM		equ	20h	; první adresa paměti RAM pro PIC16F877 - 368bajtů (20h - 7Fh)
TMP_W		equ	RAM+01	; temp preruseni
TMP_S		equ	RAM+02	; temp preruseni
TMP_PCL		equ	RAM+03	; temp preruseni

TMP0		equ	RAM+04	; pomocny registr
TMP1		equ	RAM+05	; pomocny registr
TMP2		equ	RAM+06	; pomocny registr
TMP3		equ	RAM+07	; pomocny registr
TMP4		equ	RAM+08	; pomocny registr

ANALOG_H	equ	RAM+09	; pomocny v preruseni
ANALOG_L	equ	RAM+10	; pomocny v preruseni

EE_ADR          equ	RAM+11	; pomocny v preruseni
EE_DATA         equ	RAM+12	; pomocny v preruseni
DATALCD            equ	RAM+13

#define	LCD_RES	PORTE,2		; LCD RES
#define	LCD_RW	PORTE,1		; LCD R/W
#define	LCD_CS1	PORTC,6		; LCD CS1
#define	LCD_CS2	PORTC,5		; LCD CS2
#define	LCD_A0	PORTC,7		; LCD A0
#define	BUSY	DATALCD,7		; registr pro zapis dat do displeje a pro čtení z displeje

	org	0x2007		; adresa nastaveni pojistek pro PIC16F876 a PIC16F877
 	dw _BODEN_OFF & _CP_OFF  & _PWRTE_ON & _WDT_OFF & _XT_OSC & _DEBUG_ON & _CPD_OFF & _LVP_OFF

; typ oscilatoru
;	_LP_OSC                      EQU     H'3FFC'
;	_XT_OSC                      EQU     H'3FFD'
;	_HS_OSC                      EQU     H'3FFE'
;	_RC_OSC                      EQU     H'3FFF'

; Code Protect (ochrana proti precteni)
;	_CP_ALL                      EQU     H'0FCF'
;	_CP_HALF                     EQU     H'1FDF'
;	_CP_UPPER_256                EQU     H'2FEF'
;	_CP_OFF                      EQU     H'3FFF'

; Power Up Timer (casovac pro nabeh napeti 72ms)
;	_PWRTE_ON                    EQU     H'3FF7'
;	_PWRTE_OFF                   EQU     H'3FFF'

; WatchDog (hlidaci pes)
;	_WDT_ON                      EQU     H'3FFF'
;	_WDT_OFF                     EQU     H'3FFB'

;	_BODEN_ON                    EQU     H'3FFF'
;	_BODEN_OFF                   EQU     H'3FBF'

;	_LVP_ON                      EQU     H'3FFF'
;	_LVP_OFF                     EQU     H'3F7F'

;	_WRT_ENABLE_ON               EQU     H'3FFF'
;	_WRT_ENABLE_OFF              EQU     H'3DFF'

;	_DEBUG_ON                    EQU     H'37FF'
;	_DEBUG_OFF                   EQU     H'3FFF'

;	_CPD_ON                      EQU     H'3EFF'
;	_CPD_OFF                     EQU     H'3FFF'

;************************ Nastavení dat v EEPROM **************************

	org	0x2100		; tabulka v pameti EEPROM (256 bytu)
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF,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xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
;		----------------------------------------------
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
	de	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF

;************************     Reset procesoru     *************************

	org	0x0000		; zacatek programu
	goto	INIT		; skok na pocatecni inicializaci

	org	0x0004		; vektor preruseni
	goto	PRERUS

;**************************************************************************
;*                      preruseni kazdych 8,192ms (122Hz)                 *
;**************************************************************************

PRERUS	movwf	TMP_W		; (ulozeni hodnot registru W a STATUS)
	movf	STATUS,W	; prohodi nibble STATUS a ulozi do W
	clrf	STATUS		; nastaveni banky 0
	movwf	TMP_S		; ulozit STATUS

	movf	PCLATH,W
	movwf	TMP_PCL
	clrf	PCLATH		; nastaveni banky 0
;       ------------------------
	bcf	INTCON,T0IF	; vynulovat priznak preruseni (pretekl casovac TMR0)
;-------------------------------


;	!!! zde napiste co ma preruseni vykonat !!!


;------------------------------- (obnovení hodnot registru W a STATUS)
INT_END movf	TMP_PCL,W
	movwf	PCLATH		; obnova PCLATH

	movf	TMP_S,W		; prohodi nibble TMP_S a ulozi do W
;	swapf	TMP_S,W		; prohodi nibble TMP_S a ulozi do W
	movwf	STATUS		; obnova STATUS

	swapf	TMP_W,F		; prohodi nibble TMP_W a ulozi do W
	swapf	TMP_W,W		; obnova W
	retfie

;************************ Inicializace procesoru  *************************

INIT	bcf	STATUS,RP0	; nastaveni BANKY 0
	bcf	STATUS,RP1	; nastaveni BANKY 0
;	------------------------
	movlw	b'000000'	; prednastaveni PORTu A
	movwf	PORTA
	movlw	b'00000000'	; prednastaveni PORTu B
	movwf	PORTB
	movlw	b'00000000'	; prednastaveni PORTu C
	movwf	PORTC
	movlw	b'00000000'	; přednastavení PORTu D
	movwf	PORTD	
	movlw	b'000'		; přednastavení PORTu E
	movwf	PORTE	
;       ------------------------
	bsf	STATUS,RP0	; nastaveni BANKY 1

	movlw	b'10000110'	; nastaveni A/D vstupu RA0 a ADFM = 1
	movwf	ADCON1
;	movlw	b'00000110'	; A/D vstupy OFF
;	movwf	ADCON1

	movlw	b'000001'	; portA 5-0 1vstupy, 5vystupy
	movwf	TRISA

	movlw	b'11010001'	; PULL-UPy OFF, preddelic=1:4 pripojen k TMR0
	movwf	OPTION_REG	; f:4  :4  :250 = 1000Hz (1ms)
	movlw	b'00000000'	; portB 7-0, 4vstupy, 4vystupy
	movwf	TRISB

	movlw	b'00000000'	; portB 7-0, 0vstupy, 8vystupy
	movwf	TRISC

	movlw	b'11111111'	; portC 7-0, 0vstupy, 8vystupy
	movwf	TRISD		; !!! jen PIC16F874 a PIC16F877 !!!

	movlw	b'00000000'		; portC 7-0, 0vstupy, 8vystupy
	movwf	TRISE		; !!! jen PIC16F874 a PIC16F877 !!!

	bcf	STATUS,RP0	; nastaveni BANKY 0
;       ------------------------
	clrf	TMR0		; nulovat TMR0 (i jeho preddelic)

	movlw	b'00100000'	; povolit preruseni od preteceni TMR0
	movwf	INTCON
;       ------------------------
	movlw	b'10000000'	; fosc/32, kanal 0, A/D on
	movwf	ADCON0

	bcf	PIR1,ADIF	; neni kompletni A/D prevod

bsf     LCD_A0
bsf     LCD_CS1
bsf     LCD_CS2
bsf     LCD_RW
bsf     LCD_RES


;************************     Hlavní program      *************************

MAIN	

call    INIT_LCD

;zápis znaku 's' na displej
movlw   b'00100110'     ;zapis znaku 's' do displeje
movwf   DATALCD
call    write_DATA
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'01001001'     
movwf   DATALCD
call    write_DATA
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'01001001'     
movwf   DATALCD
call    write_DATA
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'01001001'     
movwf   DATALCD
call    write_DATA
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'00110010'     
movwf   DATALCD
call    write_DATA
call    read_DATA
btfsc   BUSY
goto    $-2

goto    $-0
;********************   Inicializace LCD   *******************************

INIT_LCD

bsf     LCD_RES



movlw   b'10101110'     ;displej OFF
movwf   DATALCD
call  write_CMD  
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'10100100'     ;static drive OFF
movwf   DATALCD
call  write_CMD 
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'00000000'     ;column adress 0
movwf   DATALCD
call  write_CMD 
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'10111000'     ;page 0
movwf   DATALCD
call  write_CMD 
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'10101001'     ;DUTY 1/32
movwf   DATALCD
call  write_CMD 
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'10100000'     ;ADC
movwf   DATALCD
call  write_CMD
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'11101110'     ;END
movwf   DATALCD
call  write_CMD
call    read_DATA
btfsc   BUSY
goto    $-2

movlw   b'10101111'     ;displej ON
movwf   DATALCD
call  write_CMD
call    read_DATA
btfsc   BUSY
goto    $-2


return



;zápis příkazu do DISPLEJE
write_CMD
bcf     LCD_CS1
bcf     LCD_CS2
nop
bcf     LCD_RW
bcf     LCD_A0
nop
bsf     LCD_CS1
bsf     LCD_CS2

bsf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
clrf    TRISD
bcf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
movf    DATALCD,0       ; data do displeje
movwf   PORTD
nop
bcf     LCD_CS1
bcf     LCD_CS2
nop
;bsf     LCD_RW
;bsf     LCD_A0

bsf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
movlw   b'11111111'
movwf   TRISD
bcf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0

return




;zápis dat do DISPLEJE

write_DATA

bcf     LCD_CS1
bcf     LCD_CS2
nop
bcf     LCD_RW
bsf     LCD_A0
nop
bsf     LCD_CS1
bsf     LCD_CS2

bsf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
clrf    TRISD
bcf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
movf    DATALCD,0       ;data do displeje
movwf   PORTD
nop
bcf     LCD_CS1
bcf     LCD_CS2
nop
;bsf     LCD_RW
;bcf     LCD_A0

bsf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
movlw   b'11111111'
movwf   TRISD
bcf	STATUS,RP0	; nastaveni BANKY 0
bcf	STATUS,RP1	; nastaveni BANKY 0
return



;čtení registru STATUS
read_DATA

bcf     LCD_CS1
bcf     LCD_CS2
nop
bsf     LCD_RW
bcf     LCD_A0
nop
bsf     LCD_CS1
bsf     LCD_CS2


nop
bcf     LCD_CS1
bcf     LCD_CS2
nop
movf   PORTD,0
movwf  DATALCD  ;data z displeje
;bcf     LCD_RW
;bsf     LCD_A0

return

;************************      Konec programu     *************************

	end






