;************************************************************************ ;* Microchip Technology Inc. 2002 * ;* Assembler version: 2.0000 * ;* Filename: * ;* p18demo.asm (main routine) * ;* Dependents: * ;* p18lcd.asm * ;* p18math.asm * ;* 16f877.lkr * ;* Utviklet av: Kenneth Romsdal og Arnfinn Berg * ;************************************************************************ list p=18f452 #include p18f452.inc ;Program Configuration Registers __CONFIG _CONFIG1H, _OSCS_OFF_1H & _EC_OSC_1H __CONFIG _CONFIG2L, _BOR_OFF_2L & _PWRT_ON_2L __CONFIG _CONFIG2H, _WDT_OFF_2H __CONFIG _CONFIG3H, _CCP2MX_OFF_3H __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_ON_4L __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L __CONFIG _CONFIG5H, _CPB_OFF_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 ERRORLEVEL -314, -315 EXTERN LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2 variables UDATA ptr_pos RES 1 ptr_count RES 1 cmd_byte RES 1 TALL RES 1 TALL2 RES 1 SVAR RES 1 SVAR2 RES 1 AA1 res 1 BB1 res 1 BYTESTR RES 1 LSD RES 1 MsD RES 1 MSD RES 1 til24bit RES 1 NumH RES 1 NumL RES 1 TenK RES 1 Thou RES 1 Hund RES 1 Tens RES 1 Ones RES 1 #INCLUDE STARTUP CODE NOP org 0x0000 goto Startings org 0x0018 goto interupt org 0x0008 goto overflow PROG1 CODE stan_table ; ptr: data " TULL " ;0 ;**************INIT AV TELLERE****************** Startings CLRF AARGB0 CLRF AARGB1 CLRF AARGB2 CLRF BARGB0 CLRF BARGB1 CLRF BARGB2 CLRF TALL CLRF SVAR CLRF TALL2 CLRF SVAR2 ; CLRF high_bit ; CLRF middle_bit ; CLRF low_bit movlw b'10110000' ;setter timer innstillinger movwf T1CON movlw b'00000000' ;starter telling fra h'0000' movwf TMR1L movlw b'00000000' movwf TMR1H movlw b'00000000' ;UTVIDELSE TIL 24 BIT movwf til24bit bcf T1CON, TMR1ON ;slår av timer call LCDInit clrf PORTB movlw d'00000110' movwf TRISB bcf PIR1, TMR1IF ;Ingen overflow bsf PIE1, TMR1IE ;Enable TMR1 overflow interrupt bsf IPR1, TMR1IP ;høy prioritet bsf RCON, IPEN bsf INTCON2, INTEDG1 ;stigende flanke bsf INTCON2, INTEDG2 bcf INTCON3, INT1IP ;lav prioritet interrupt bcf INTCON3, INT2IP ;lav prioritet interrupt bcf INTCON3, INT1IF ;SLÅR AV INT bcf INTCON3, INT2IF ;flagg bsf INTCON3, INT1IE ;aktiviserer INT1/RB1 bsf INTCON3, INT2IE bsf INTCON, GIEH ;ALL GLOBAL bsf INTCON, GIEL ;*******************************HOVEDPROGRAM******************************** Main goto Main ;***********************START AV KLOKKE SAMT TEST PÅ INTERUPT************************** overflow incf til24bit ;ved overflow =>FFFF->utvidelse av teller bcf PIR1, TMR1IF RETFIE interupt bsf T1CON, TMR1ON ;starter timer btfss PORTB, RB1 ;Hvar det Interupt på RB1? goto startRB2 ;Nei, Interupt på RB2 goto startRB1 ;ja, Interupt på RB1 startRB1 btfss PORTB, RB2 goto $-2 bcf T1CON, TMR1ON ;stopper teller btfsc PORTB, RB2 goto $-2 goto Hva_ER_Hastigheten1 startRB2 btfss PORTB, RB1 goto $-2 bcf T1CON, TMR1ON ;stopper teller btfsc PORTB, RB1 goto $-2 goto Hva_ER_Hastigheten1 ;***********************UTREGNINGER************************** Hva_ER_Hastigheten1 movlw h'44' ;KONSTANTEN ER h'44AA20' movwf AARGB0 ;FLYTTER h'06' INN I AARGB0 movf til24bit,W ;FLYTTER INNHOLDET I til24bit TIL ARB.REGISTERET movwf BARGB0 ;OG VIDERE TIL BARGB0 REGISTERET movlw h'AA' movwf AARGB1 movf TMR1H,W movwf BARGB1 movlw h'20' movwf AARGB2 movf TMR1L,W movwf BARGB2 bcf INTCON3, INT1IF bcf INTCON3, INT2IF CLRF til24bit CLRF TMR1H CLRF TMR1L CALL FXD2424U ;UTFØRER DIVISJON AV h'44AA20' OG INNHOLDET I (BARGB0, BARGB1, BARGB2) ;se side 190 i "embedded design pic 18f452 microcontroller" ;*******************SENDE TEKST OG VERDIER TIL DISPLAYET********************** LCD_LINJE_2 call LCDLine_2 movlw A'F' ;Sender "Fart = " til LCD movwf temp_wr call d_write movlw A'a' movwf temp_wr call d_write movlw A'r' movwf temp_wr call d_write movlw A't' movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw A'=' movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write VERDI_SKRVET_UT movf AARGB1, W ;SENDER VERDIER TIL LCD movwf NumH movf AARGB2, W movwf NumL call bin16_bcd movf Hund,W ;get hunds call bin_bcd movf LSD,W ;send high digit from the LSD #.xx movwf temp_wr call d_write movf Tens,W ;get tens call bin_bcd movf LSD,W ;send low digit x.#x movwf temp_wr call d_write movlw A'.' ;send decimal point "." movwf temp_wr call d_write movf Ones,W ;get ones call bin_bcd movf LSD,W ;send low digit x.x# movwf temp_wr call d_write movlw a' ' movwf temp_wr call d_write movlw a'k' movwf temp_wr call d_write movlw a'm' movwf temp_wr call d_write movlw a'/' movwf temp_wr call d_write movlw a't' movwf temp_wr call d_write ;*******************Test på tall2********************* movlw b'00000001' CPFSGT TALL2 GOTO FORSTE_RUNDE GOTO GJ_SNITT ;*******************Første runde kjøres 1 gang,aldri mer********************* FORSTE_RUNDE movf AARGB1, W MOVWF AA1 MOVWF AARGB0 MOVLW b'00000010' movwf BARGB0 CALL FXD0808U MOVF AARGB0, W MOVWF TALL movf AARGB2, W MOVWF BB1 MOVWF AARGB0 MOVLW b'00000010' movwf BARGB0 CALL FXD0808U MOVF AARGB0, W MOVWF TALL2 ; CLRF AARGB0 ; CLRF AARGB1 ; CLRF AARGB2 ; CLRF BARGB0 ; CLRF BARGB1 ; CLRF BARGB2 RETFIE GOTO Main ;********************* STANDARD USER CODE ************************* ;----Standard code, Place characters on line-1-------------------------- stan_char_1 call LCDLine_1 ;mvoe cursor to line 1 movlw .16 ;1-full line of LCD movwf ptr_count movlw UPPER stan_table movwf TBLPTRU movlw HIGH stan_table movwf TBLPTRH movlw LOW stan_table movwf TBLPTRL movf ptr_pos,W addwf TBLPTRL,F clrf WREG addwfc TBLPTRH,F addwfc TBLPTRU,F stan_next_char_1 tblrd *+ movff TABLAT,temp_wr call d_write ;send character to LCD decfsz ptr_count,F ;move pointer to next char bra stan_next_char_1 return ;----Standard code, Place characters on line-2-------------------------- stan_char_2 call LCDLine_2 ;move cursor to line 2 movlw .16 ;1-full line of LCD movwf ptr_count movlw UPPER stan_table movwf TBLPTRU movlw HIGH stan_table movwf TBLPTRH movlw LOW stan_table movwf TBLPTRL movf ptr_pos,W addwf TBLPTRL,F clrf WREG addwfc TBLPTRH,F addwfc TBLPTRU,F stan_next_char_2 tblrd *+ movff TABLAT,temp_wr call d_write ;send character to LCD decfsz ptr_count,F ;move pointer to next char bra stan_next_char_2 return ;---------------- Binary (8-bit) to BCD ----------------------- ; 255 = highest possible result bin_bcd clrf MSD clrf MsD movwf LSD ;move value to LSD ghundreth movlw .100 ;subtract 100 from LSD subwf LSD,W btfss STATUS,C ;is value greater then 100 bra gtenth ;NO goto tenths movwf LSD ;YES, move subtraction result into LSD incf MSD,F ;increment hundreths bra ghundreth gtenth movlw .10 ;take care of tenths subwf LSD,W btfss STATUS,C bra over ;finished conversion movwf LSD incf MsD,F ;increment tenths position bra gtenth over ;0 - 9, high nibble = 3 for LCD movf MSD,W ;get BCD values ready for LCD display xorlw 0x30 ;convert to LCD digit movwf MSD movf MsD,W xorlw 0x30 ;convert to LCD digit movwf MsD movf LSD,W xorlw 0x30 ;convert to LCD digit movwf LSD retlw 0 ;---------------- Binary (16-bit) to BCD ----------------------- ; xxx = highest possible result bin16_bcd ; Takes number in NumH:NumL ; Returns decimal in ; TenK:Thou:Hund:Tens:Ones swapf NumH,W andlw 0x0F addlw 0xF0 movwf Thou addwf Thou,F addlw 0xE2 movwf Hund addlw 0x32 movwf Ones movf NumH,W andlw 0x0F addwf Hund,F addwf Hund,F addwf Ones,F addlw 0xE9 movwf Tens addwf Tens,F addwf Tens,F swapf NumL,W andlw 0x0F addwf Tens,F addwf Ones,F rlcf Tens,F rlcf Ones,F comf Ones,F rlcf Ones,F movf NumL,W andlw 0x0F addwf Ones,F rlcf Thou,F movlw 0x07 movwf TenK movlw 0x0A ; Ten Lb1: decf Tens,F addwf Ones,F btfss STATUS,C bra Lb1 Lb2: decf Hund,F addwf Tens,F btfss STATUS,C bra Lb2 Lb3: decf Thou,F addwf Hund,F btfss STATUS,C bra Lb3 Lb4: decf TenK,F addwf Thou,F btfss STATUS,C bra Lb4 retlw 0 RETFIE ;*************************MEST SIGNIFIKANTE BIT**************************** GJ_SNITT movf AARGB1, W MOVWF AA1 MOVWF AARGB0 MOVLW b'00000010' MOVWF BARGB0 CALL FXD0808U MOVF AARGB0,W MOVWF AA1 ADDWF TALL, 0 MOVWF SVAR ;WATCH MOVWF AARGB1 MOVWF AA1 ;*************************MINST SIGNIFIKANTE BIT**************************** movf AARGB2, W MOVWF BB1 MOVWF AARGB0 MOVLW b'00000010' MOVWF BARGB0 CALL FXD0808U MOVF AARGB0,W MOVWF BB1 ADDWF TALL2,0 MOVWF SVAR2 ;WATCH MOVWF AARGB2 MOVWF BB1 GOTO SNITT_LCD SNITT_LCD call LCDLine_1 movlw A'S' movwf temp_wr call d_write movlw A'n' movwf temp_wr call d_write movlw A'i' movwf temp_wr call d_write movlw A't' movwf temp_wr call d_write movlw A't' movwf temp_wr call d_write movlw A'=' movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movf SVAR, W ;SENDER VERDIER TIL LCD movwf NumH movf SVAR2, W movwf NumL call bin16_bcd movf Hund,W ;get hunds call bin_bcd movf LSD,W ;send high digit from the LSD #.xx movwf temp_wr call d_write movf Tens,W ;get tens call bin_bcd movf LSD,W ;send low digit x.#x movwf temp_wr call d_write movlw A'.' ;send decimal point "." movwf temp_wr call d_write movf Ones,W ;get ones call bin_bcd movf LSD,W ;send low digit x.x# movwf temp_wr call d_write movlw a' ' movwf temp_wr call d_write movlw a'k' movwf temp_wr call d_write movlw a'm' movwf temp_wr call d_write movlw a'/' movwf temp_wr call d_write movlw a't' movwf temp_wr call d_write ; CLRF AARGB0 ; CLRF AARGB1 ; CLRF AARGB2 ; CLRF BARGB0 ; CLRF BARGB1 ; CLRF BARGB2 RETFIE #INCLUDE #INCLUDE end