;************************************************************************ ;*Utført av: Kenneth Romsdal & Arnfinn Berg ;* Microchip Technology Inc. 2002 * ;* Assembler version: 2.0000 * ;* Filename: * ;* p18demo.asm (main routine) * ;* Dependents: * ;* p18lcd.asm * ;* p18math.asm * ;* 16f877.lkr * ;* March 14,2002 * ;* PICDEM 2 PLUS DEMO code. The following functions are included * ;* with this code: * ;* ;Oppg 2 Temperatursensor og skrive til LCD ;Programmet skal starte med å vise temperaturen som avleses fra ;temperatursensoren. Dersom temperaturen stiger og kommer over 25 grader C ;vil lysdiodene på PORTB tennes, under 25 grader slukkes de igjen. ;************************************************************************ 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_OFF_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 #define scroll_dir TRISA,4 #define scroll PORTA,4 ;Push-button RA4 on PCB #define select_dir TRISB,0 #define select PORTB,0 ;Push-button RB0 on PCB EXTERN LCDInit, temp_wr, d_write, i_write, LCDLine_1, LCDLine_2 EXTERN UMUL0808L, UDIV1608L, AARGB0, AARGB1, BARGB0 ssprw macro ;check for idle SSP module routine movlw 0x00 andwf SSPCON2,W sublw 0x00 btfss STATUS,Z bra $-8 btfsc SSPSTAT,R_W bra $-2 endm variables UDATA ptr_pos RES 1 ptr_count RES 1 temp_1 RES 1 temp_2 RES 1 temp_3 RES 1 cmd_byte RES 1 temperature RES 1 LSD RES 1 MsD RES 1 MSD RES 1 NumH RES 1 NumL RES 1 TenK RES 1 Thou RES 1 Hund RES 1 Tens RES 1 Ones RES 1 STARTUP CODE NOP goto start NOP NOP NOP PROG1 CODE stan_table ;table for standard code ; "XXXXXXXXXXXXXXXX" ; ptr: data " Temperature " ;0 start call LCDInit clrf PORTB clrf TRISB bsf TRISC,3 ;initialize MSSP module bsf TRISC,4 movlw B'00101000' movwf SSPCON1 bsf SSPSTAT,SMP movlw .5 movwf SSPADD bcf PIR1,TMR1IF clrf TMR1H ;load Timer1 for 2 sec overflow clrf TMR1L ;************* STANDARD USER CODE ********************************** ;---------------------- Temperature -------------------------------- temp ; This code if for the TC74A5-5.0VAT temperature sensor ; 1st. Check if temperature is ready to be read in config reg. ; 2nd. If ready, retireve temperatute in hex. ; If not ready, check config register again. gamlefar movlw .0 ;temperature movwf ptr_pos call stan_char_1 movlw 0x01 ;config register command byte movwf cmd_byte temp_now bsf SSPSTAT,6 ;SMBUS spec for TC74 bsf SSPCON2,SEN ;write to TC74 btfsc SSPCON2,SEN bra $-2 movlw B'10011010' ;send TC74 ADDRESS (write) movwf SSPBUF ssprw btfsc SSPCON2,ACKSTAT ;ack? bra $-2 movf cmd_byte,W ;send COMMAND byte (config) movwf SSPBUF ssprw btfsc SSPCON2,ACKSTAT ;ack? bra $-2 bsf SSPCON2,RSEN ;send repeated start btfsc SSPCON2,RSEN bra $-2 movlw B'10011011' ;send TC74 ADDRESS (read) movwf SSPBUF ssprw ;module idle? btfsc SSPCON2,ACKSTAT ;ack? bra $-2 bsf SSPCON2,RCEN ;enable receive mode btfsc SSPCON2,RCEN bra $-2 movf SSPBUF,W ;retrieve config reg or temp reg bsf SSPCON2,ACKDT ;send NOT-ACK bsf SSPCON2,ACKEN btfsc SSPCON2,ACKEN bra $-2 bsf SSPCON2,PEN ;stop btfsc SSPCON2,PEN bra $-2 btfss cmd_byte,0 ;config command OR temp command bra convert_temp ;get temperature ready for display andlw 0x40 ;is temp ready ?? sublw 0x40 btfss STATUS,Z bra gamlefar movlw 0x00 ;temp is ready for reading movwf cmd_byte ;send temp register command bra temp_now convert_temp movwf temperature call bin_bcd ;get temp ready for LCD call LCDLine_2 movlw A'T' ;send "Temp=" to LCD movwf temp_wr call d_write movlw A'e' movwf temp_wr call d_write movlw A'm' movwf temp_wr call d_write movlw A'p' 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 movf MSD,W ;send high digit movwf temp_wr call d_write movf MsD,W ;send middle digit movwf temp_wr call d_write movf LSD,W ;send low digit movwf temp_wr call d_write movlw A'C' ;send "C" for celcius movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write movlw 0x20 ;space movwf temp_wr call d_write bcf STATUS, C movf temperature, 0 sublw d'24' ;kritisk verdi = 25 C bc LysAv LysPaa bsf PORTB, RB0 bsf PORTB, RB1 bsf PORTB, RB2 bsf PORTB, RB3 bcf STATUS, C goto gamlefar LysAv bcf PORTB, RB0 bcf PORTB, RB1 bcf PORTB, RB2 bcf PORTB, RB3 goto gamlefar ;******************************************************************* ;************************** ROUTINES ****************************** ;****************************************************************** ;****************************************************************** ;----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 ;---------------------------------------------------------------------- ;------------------ 100ms Delay -------------------------------- delay_100ms movlw 0xFF movwf temp_1 movlw 0x83 movwf temp_2 d100l1 decfsz temp_1,F bra d100l1 decfsz temp_2,F bra d100l1 return ;---------------- 1s Delay ----------------------------------- delay_1s movlw 0xFF movwf temp_1 movwf temp_2 movlw 0x05 movwf temp_3 d1l1 decfsz temp_1,F bra d1l1 decfsz temp_2,F bra d1l1 decfsz temp_3,F bra d1l1 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 return ;********************************************************************* end