;****************************************************************************************** ; PIC18F452 Sample Program ; Project : ; Program Name : ; Programer : ; Create date : Version 1.0 H27.06.20 ; History : ; Laungage : MPLAB MPASM ;****************************************************************************************** ; Description ; OSC : External OSC 8MHz ; This program is used by setting the 32Hz frequency of the system in the PLL ; Assign I/O pins ; Rb[O] : PL9823(Shift register programmable RGB Lamp) DIN pin ;****************************************************************************************** LIST P=18F452,ST=OFF,R=DEC INCLUDE "P18F452.inc" ; ; __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H __CONFIG _CONFIG2L, _BOR_OFF_2L & _PWRT_OFF_2L & _BORV_20_2L __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_128_2H __CONFIG _CONFIG3H, _CCP2MX_OFF_3H __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_ON_5H & _CPD_OFF_5H __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L __CONFIG _CONFIG7H, _EBTRB_OFF_7H ; ; Define Macro ; WAIT10M MACRO #WAT MOVLW #WAT CALL WAT10M ENDM NOP2 MACRO NOP NOP ENDM NOP10 MACRO NOP NOP NOP NOP NOP NOP NOP NOP NOP NOP ENDM ; ; Define Parameter ; MLED_P EQU 0 ; ; Define Memory Area ; TMP_C EQU 00H WAT1M1 EQU 01H TMP_LP1 EQU 02H TMP_LP2 EQU 03H ; ; ; ORG 0h GOTO START ORG 8h GOTO START ORG 18h GOTO START START ; ***************************** ; 1.Initialize ; ***************************** ; 1.Define ADCON MOVLW B'00000111' ; All Degital Port MOVWF ADCON1 ; 2.Define I/O Port ; (1)PortA CLRF PORTA CLRF LATA MOVLW B'00011110' MOVWF TRISA ; (2)PortB CLRF PORTB CLRF LATB MOVLW B'11111110' MOVWF TRISB ; (3)PortC CLRF PORTC CLRF LATC MOVLW B'11111111' MOVWF TRISC ; (4)PortD CLRF PORTD CLRF LATD MOVLW B'11111111' MOVWF TRISD ; (5)PortE CLRF PORTE CLRF LATE MOVLW B'11111111' MOVWF TRISE ; ; 3.Reset LED Module CALL RES_MLED ; ; ; MOVLW B'0000' MOVWF BSR ; MAIN ; ***************************** ; 2.Program main ; ***************************** ; 1.Pattan 1 CALL RES_MLED CALL COL_R CALL COL_G CALL COL_B CALL COL_W CALL COL_R CALL COL_G CALL COL_B CALL COL_W WAIT10M 100 ; ; 2.Pattan 2 CALL COL_G CALL COL_B CALL COL_W CALL COL_R CALL COL_G CALL COL_B CALL COL_W CALL COL_R WAIT10M 100 ; ; 3.Pattan 3 CALL COL_B CALL COL_W CALL COL_R CALL COL_G CALL COL_B CALL COL_W CALL COL_R CALL COL_G WAIT10M 100 ; ; 4.Pattan 4 CALL COL_W CALL COL_R CALL COL_G CALL COL_B CALL COL_W CALL COL_R CALL COL_G CALL COL_B WAIT10M 100 GOTO MAIN ;****************************************************************************************** ; Program : COL_F ; Title : Lights off ; Input : ; Output : ;****************************************************************************************** COL_F ; *************************************** ; Set the color data ; *************************************** ; (1)Set RED MOVLW H'8' MOVWF TMP_C COL_F_R BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_F_R ; (2)Set GREEEN MOVLW H'8' MOVWF TMP_C COL_F_G BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_F_G ; (3)Set BLUE MOVLW H'8' MOVWF TMP_C COL_F_B BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_F_B RETURN ;****************************************************************************************** ; Program : COL_R ; Title : Lights red ; Input : ; Output : ;****************************************************************************************** COL_R ; *************************************** ; Set the color data ; *************************************** ; (1)Set RED MOVLW H'8' MOVWF TMP_C COL_R_R BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_R_R ; (2)Set GREEEN MOVLW H'8' MOVWF TMP_C COL_R_G BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_R_G ; (3)Set BLUE MOVLW H'8' MOVWF TMP_C COL_R_B BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_R_B RETURN ;****************************************************************************************** ; Program : COL_G ; Title : Lights green ; Input : ; Output : ;****************************************************************************************** COL_G ; *************************************** ; Set the color data ; *************************************** ; (1)Set RED MOVLW H'8' MOVWF TMP_C COL_G_R BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_G_R ; (2)Set GREEEN MOVLW H'8' MOVWF TMP_C COL_G_G BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_G_G ; (3)Set BLUE MOVLW H'8' MOVWF TMP_C COL_G_B BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_G_B RETURN ;****************************************************************************************** ; Program : COL_B ; Title : Lights blue ; Input : ; Output : ;****************************************************************************************** COL_B ; *************************************** ; Set the color data ; *************************************** ; (1)Set RED MOVLW H'8' MOVWF TMP_C COL_B_R BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_B_R ; (2)Set GREEEN MOVLW H'8' MOVWF TMP_C COL_B_G BSF PORTB,MLED_P NOP2 BCF PORTB,MLED_P NOP10 DECFSZ TMP_C,F GOTO COL_B_G ; (3)Set BLUE MOVLW H'8' MOVWF TMP_C COL_B_B BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_B_B RETURN ;****************************************************************************************** ; Program : COL_W ; Title : Lights white ; Input : ; Output : ;****************************************************************************************** COL_W ; *************************************** ; Set the color data ; *************************************** ; (1)Set RED MOVLW H'8' MOVWF TMP_C COL_W_R BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_W_R ; (2)Set GREEEN MOVLW H'8' MOVWF TMP_C COL_W_G BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_W_G ; (3)Set BLUE MOVLW H'8' MOVWF TMP_C COL_W_B BSF PORTB,MLED_P NOP10 BCF PORTB,MLED_P NOP2 DECFSZ TMP_C,F GOTO COL_W_B RETURN ;****************************************************************************************** ; Program : RES-MLED ; Title : Reset PL9823 ; Input : ; Output : ; Description : Wait more than 50 microseconds (more than 400 step when the 32MHz) ;****************************************************************************************** RES_MLED BCF PORTB,MLED_P ; MOVLW H'64' MOVWF TMP_LP1 RES_MLED1 MOVLW H'64' MOVWF TMP_LP2 RES_MLED2 DECFSZ TMP_LP2,F GOTO RES_MLED2 DECFSZ TMP_LP1,F GOTO RES_MLED1 RETURN ;************************************************************************************ ; Function Name : WAT10M ; Title : Wait 10[m] ; Input : W-reg ; Output : ;************************************************************************************ WAT10M ; MOVLW H'64' MOVWF WAT1M1 WAT10MA CALL TIMER DECFSZ WAT1M1,F GOTO WAT10MA RETURN ;************************************************************************************ ; Function Name : TIMER ; Title : Wait 0.01[S] ; 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) Unimplemented: Read as '0' ; 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' 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:10MHz, 4X PLLON, 1:1 Prescale value, 0.001[S];1000Hz ) ; 1mS(1000Hz): 10MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d10,000(Count) ==> 0x2710(Count) ; TMR1 = 0xFFFF - 0x2710 ==> 0xD8EF ; TMR1 data ( OSC:32MHz, FOSC/4, 1:8 Prescale value, 0.01[S];100Hz ) ; 10mS(100Hz): 32MHz / 4(FOSC/4) / 8(Prescale) / 100(100Hz)==> 0d10,000(Count) ==> 0x2710(Count) ; TMR1 = 0xFFFF - 0x2710 ==> 0xD8EF MOVLW H'D8' MOVWF TMR1H MOVLW H'EF' MOVWF TMR1L ; ******************************************************* ; 3.Timer ; ****************************************************** BCF PIR1,TMR1IF TIMER_1 BTFSS PIR1,TMR1IF GOTO TIMER_1 RETURN END