;****************************************************************************************** ; PIC18F14K50 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H22.11.02 ; History : ; Laungage : MPASM ;****************************************************************************************** ; (1)Description ; 1)Define I/O PORT ; PORTC(0) : DCM16117A(E) ; PORTC(1) : DCM16117A(RS) ; PORTC(7-4) : DCM16117A(DB7-4) ; ; PORTC(2) : Terminal(0) ; PORTB(4) : Terminal(1) ; PORTB(6) : Terminal(2) ; ; PORTB(5) : Serial PORT(Rx) ; PORTB(7) : Serial PORT(Tx) ; ; 2)OSC ; 48MHz (12MHz Crystal * PLL4) ; ;****************************************************************************************** LIST P=PIC18F14K50,ST=OFF,R=DEC INCLUDE "P18F14K50.inc" config CPUDIV = NOCLKDIV ;CPU System Clock Selection bit(No CPU System Clock divide) config USBDIV = OFF ;USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) config FOSC = HS ;Oscillator Selection bits(HS oscillator) config PLLEN = ON ;X PLL Enable bit(Oscillator multiplied by 4) config PCLKEN = OFF ;Primary Clock Enable Bit(Primary clock is under software control) config FCMEN = OFF config IESO = OFF ;Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) config PWRTEN = OFF,BOREN = OFF,BORV = 19 config WDTEN = OFF,WDTPS = 1 config MCLRE = OFF ;MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF ; Define MACRO MNOP MACRO NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP ENDM ; Define Parameter LCD_E EQU H'0000' LCD_RS EQU H'0001' LCD_DB0 EQU H'0004' LCD_DB1 EQU H'0005' LCD_DB2 EQU H'0006' LCD_DB3 EQU H'0007' LCD_DB4 EQU H'0004' LCD_DB5 EQU H'0005' LCD_DB6 EQU H'0006' LCD_DB7 EQU H'0007' ; Define Memory Area LP0 EQU 01H LP1 EQU 02H LP2 EQU 03H DB_TMP EQU 04H TMP_WDSP EQU 05H ORG 0h GOTO START ORG 8h GOTO START ORG 18h GOTO START START ; ***************************** ; 1.Initialized Segment ; ***************************** ; (1)Define I/O port CLRF PORTA CLRF PORTB CLRF PORTC ; MOVLW B'11111111' MOVWF TRISA MOVLW B'11111111' MOVWF TRISB MOVLW B'00000000' MOVWF TRISC ; MOVLW B'00000000' ;All Degital PORT MOVWF ANSEL MOVLW B'00000000' ;All Degital PORT MOVWF ANSELH ; (2)Initialized LCD CALL INTLCD ; (3)Demo Program; CALL DEMO1 ; MAIN ; GOTO MAIN ;****************************************************************************************** ; Function name : DEMO1 ; Title : ; Input : ; Output : ;****************************************************************************************** DEMO1 ; ******************* ; Start Pic 18 表示 ; ******************* ; (1)領域0を選択 CALL INTPOS0 ; (2)表示 MOVLW 'S' CALL LCDDSP MOVLW 't' CALL LCDDSP MOVLW 'a' CALL LCDDSP MOVLW 'r' CALL LCDDSP MOVLW 't' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW 'P' CALL LCDDSP MOVLW 'I' CALL LCDDSP ; ; (3)領域1を選択 CALL INTPOS1 ; (4)表示 MOVLW 'C' CALL LCDDSP MOVLW '1' CALL LCDDSP MOVLW '8' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP ; (5)少し待つ CALL WATS1 ; ******************* ; 18F14K50 OK> 表示 ; ******************* ; (0)画面クリア CALL LCDCLR ; (1)領域0を選択 CALL INTPOS0 ; (2)表示 MOVLW '1' CALL LCDDSP MOVLW '8' CALL LCDDSP MOVLW 'F' CALL LCDDSP MOVLW '1' CALL LCDDSP MOVLW '4' CALL LCDDSP MOVLW 'K' CALL LCDDSP MOVLW '5' CALL LCDDSP MOVLW '0' CALL LCDDSP ; ; (3)領域1を選択 CALL INTPOS1 ; (4)表示 MOVLW ' ' CALL LCDDSP MOVLW 'O' CALL LCDDSP MOVLW 'K' CALL LCDDSP MOVLW '>' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP MOVLW ' ' CALL LCDDSP ; (5)少し待つ CALL WATS1 ; RETURN ;****************************************************************************************** ; Function name : INTPOS0 ; Title : LCDの1〜8に書き込む ; Input : ; Output : ;****************************************************************************************** INTPOS0 MOVLW B'10000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer RETURN ;****************************************************************************************** ; Function name : INTPOS1 ; Title : LCDの9〜16に書き込む ; Input : ; Output : ;****************************************************************************************** INTPOS1 MOVLW B'11000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer RETURN ;****************************************************************************************** ; Function Name : INTLCD ; Title : インストラクションによるLCDの初期化 ; Input : ; Output : ;****************************************************************************************** INTLCD ; 1.ファンクションセット ; (1)15mS以上待つ CALL WATS01 ; (2)ファンクションセット(1) MOVLW B'00110000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal ; (3)4.1mS以上待つ CALL Timer ; (4)ファンクションセット(2) MOVLW B'00110000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal ; (5)100μS以上待つ CALL Timer ; (6)ファンクションセット(3) MOVLW B'00110000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; 2.表示設定 ; ; (1)ファンクションセット MOVLW B'00100000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00100000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'10000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; ; (2)表示OFF MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'10000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; ; (3)表示クリア MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00010000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; ; (4)エントリーモードセット MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'01100000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; ; (5)表示ON MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'11000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; ; ; (6)DD RAM アドレスセット MOVLW B'10000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00000000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer ; RETURN ;****************************************************************************************** ; Function Name : LCDDSP ; Title : LCDに文字を表示する ; Input : ; Output : ;****************************************************************************************** LCDDSP MOVWF TMP_WDSP CALL LCDDBH BSF PORTC,LCD_RS ; RS MNOP ; 48MHz mode Only BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal BCF PORTC,LCD_RS ; RS MNOP MOVF TMP_WDSP,W CALL LCDDBL BSF PORTC,LCD_RS ; RS MNOP ; 48MHz mode Only BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal MNOP ; 48MHz mode Only BCF PORTC,LCD_RS ; RS CALL Timer ; RETURN ;****************************************************************************************** ; Function Name : LCDCLR ; Title : LCDの画面クリア ; Input : ; Output : ;****************************************************************************************** LCDCLR ; (1)表示クリア MOVLW B'00000000' ; MOVWF PORTC MNOP ; 48MHz mode Only BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer MOVLW B'00010000' ; MOVWF PORTC BCF PORTC,LCD_RS ; RS BSF PORTC,LCD_E ; Enable-Signal MNOP BCF PORTC,LCD_E ; Enable-Signal CALL Timer RETURN ;****************************************************************************************** ; Function Name : LCDDBH ; Title : LCDのDB(7-4)にデータ設定 ; Input : W-reg ; Output : ;****************************************************************************************** LCDDBH MOVWF DB_TMP BTFSS DB_TMP,4 BCF PORTC,LCD_DB4 BTFSC DB_TMP,4 BSF PORTC,LCD_DB4 BTFSS DB_TMP,5 BCF PORTC,LCD_DB5 BTFSC DB_TMP,5 BSF PORTC,LCD_DB5 BTFSS DB_TMP,6 BCF PORTC,LCD_DB6 BTFSC DB_TMP,6 BSF PORTC,LCD_DB6 BTFSS DB_TMP,7 BCF PORTC,LCD_DB7 BTFSC DB_TMP,7 BSF PORTC,LCD_DB7 RETURN ;****************************************************************************************** ; Function Name : LCDDBL ; Title : LCDのDB(3-0)にデータ設定 ; Input : W-reg ; Output : ;****************************************************************************************** LCDDBL MOVWF DB_TMP BTFSS DB_TMP,0 BCF PORTC,LCD_DB0 BTFSC DB_TMP,0 BSF PORTC,LCD_DB0 BTFSS DB_TMP,1 BCF PORTC,LCD_DB1 BTFSC DB_TMP,1 BSF PORTC,LCD_DB1 BTFSS DB_TMP,2 BCF PORTC,LCD_DB2 BTFSC DB_TMP,2 BSF PORTC,LCD_DB2 BTFSS DB_TMP,3 BCF PORTC,LCD_DB3 BTFSC DB_TMP,3 BSF PORTC,LCD_DB3 RETURN ;****************************************************************************************** ; Function Name : WATS1 ; Title : wait function (1S) ; Input : ; Output : ;****************************************************************************************** WATS1 MOVLW H'64' MOVWF LP0 WATS1A CALL Timer DECFSZ LP0 GOTO WATS1A RETURN ;****************************************************************************************** ; Function Name : WATS01 ; Title : wait function (0.1S) ; Input : ; Output : ;****************************************************************************************** WATS01 MOVLW H'0A' MOVWF LP0 WATS01A CALL Timer DECFSZ LP0 GOTO WATS01A RETURN ;****************************************************************************************** ; Function Name : Timer ; Title : wait function (10mS) ; Input : ; Output : ;****************************************************************************************** Timer: ; ******************************************************* ; 1.Define T1CON(TIMER1 CONTROL REGISTER) Register ; T1CON(7) RD16: 16-bit Read/Write Mode Enable bit ; 1 = Enables register read/write of TImer1 in one 16-bit operation ; 0 = Enables register read/write of Timer1 in two 8-bit operations ; T1CON(6) T1RUN: Timer1 System Clock Status bit ; 1 = Main system clock is derived from Timer1 oscillator ; 0 = Main system clock is derived from another source ; T1CON(5-4)T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits ; 11 = 1:8 Prescale value ; 10 = 1:4 Prescale value ; 01 = 1:2 Prescale value ; 00 = 1:1 Prescale value ; T1CON(3) T1OSCEN: Timer1 Oscillator Enable bit ; 1 = Timer1 oscillator is enabled ; 0 = Timer1 oscillator is shut off ; The oscillator inverter and feedback resistor are turned off to eliminate power drain. ; T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit ; When TMR1CS = 1: ; 1 = Do not synchronize external clock input ; 0 = Synchronize external clock input ; When TMR1CS = 0: ; This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. ; T1CON(1) TMR1CS: Timer1 Clock Source Select bit ; 1 = External clock from the T13CKI pin (on the rising edge) ; 0 = Internal clock (FOSC/4) ; T1CON(0) bit 0 TMR1ON: Timer1 On bit ; 1 = Enables Timer1 ; 0 = Stops Timer1 ; ******************************************************* MOVLW B'10110001' ; 16-bit Read/Write Mode, 1:8 Prescale value MOVWF T1CON ; ******************************************************* ; 2.Define TMR1 Register ; Caution(16-bit Read/Write Mode) !!!!: ; (a)Writing to TMR1H does not directly affect Timer1. ; Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. ; This allows all 16 bits of Timer1 to be updated at once. ; (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. ; This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to ; determine whether a read of the high byte, followed by a read of the low byte, has become invalid due ; to a rollover or carry between reads. ; ******************************************************* ; TMR1 data ( OSC:12MHz, 4X PLLON, 1:8 Prescale value, 0.01[S];100Hz ) ; 10mS(100Hz): 12MHz * 4(PLLON) / 4(PIC) / 8(Prescale) / 100(100Hz)==> 0d15,000(Count) ==> 0x3A98(Count) ; TMR1 = 0xFFFF - 0x3A98 ==> 0xC567 MOVLW H'C5' MOVWF TMR1H MOVLW H'67' MOVWF TMR1L ; ******************************************************* ; 3.Timer ; ******************************************************* BCF PIR1,TMR1IF Timer_1 BTFSS PIR1,TMR1IF GOTO Timer_1 RETURN END