; ; ; This program is an asynchronous MASTER transmission and reception ; through USART pin RC6 and RC7 with a data transfer of 1.2 kbps. ; galing sa master34 na gumagana may powerline modem na ito. ; ENHANCE NA USART!! FINALE!!!!! ; ; ;************************************************************************************************** ; ; PROGRAMMED BY: JOHN PAUL S. AGUILING ; ; DATE PROGRAMMED: JULY 27, 2002 ; ; LAST UPDATED: AUGUST 11, 2002 ; ; PROGRAM DESCRIPTION: ; ; This is the master part of the 3 PIC16F877 using powerline communication ; When the push button is pressed, this master sends value to instruct the slave to start ; acquiring data. The master then receives from the first slave and then the second. ; This will convert the signal into readable data and displays into a seven segment display. ; The values coming from both slave should not be far apart since they acquire synchronously. ; It ranges from 0V to +5V. This project is intended to acquire data synchronously via ; powerline (no need to connect to a network) ; ;************************************************************************************************** LIST P=16F877 ERRORLEVEL -302 #INCLUDE P16F877.INC __CONFIG _CP_OFF & _XT_OSC & _WDT_OFF & _WRT_ENABLE_ON & _PWRTE_ON & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _BODEN_OFF CBLOCK 20H TEMPO_PB LASTPB TEMP PREAMBLE ACQUIRE ADDRESS ADDRESS_A ADDRESS_B ADC_REG RXTEMP DUMMY NaHi NaLo NbHi NbLo D1 D2 GAP COUNT SLAVE_A SLAVE_B WHOLE DECIMAL TIMING TEMPO_WHOLE TEMPO_DECIMAL TEMPA_WHOLE TEMPA_DECIMAL TEMPB_WHOLE TEMPB_DECIMAL ASLV_WHOLE ASLV_DECIMAL BSLV_WHOLE BSLV_DECIMAL ROUNDTWO ENDC ORG 0x0000 ; start address = 0000h INIT ; INITIALISE PORTS ; binary used to see individual pin level ; all port pins = low BCF STATUS, RP0 CLRF PORTA CLRF PORTB CLRF PORTD CLRF PORTE MOVLW b'10010001' MOVWF PORTC ; INITIALISE PORTS ; binary used to see individual pin IO status bsf STATUS,RP0 ; set RAM Page 1 for TRIS registers movlw b'11000000' ; all IO pins = outputs movwf TRISA movlw b'01110000' movwf TRISB movlw b'10100000' movwf TRISC movlw b'00000000' ; input movwf TRISD ; port D is input movlw b'11111000' movwf TRISE movlw b'00001110' ; 1 analog pin rest digital movwf ADCON1 BCF STATUS, RP0 ; INITIALISE USART BSF STATUS, RP0 MOVLW d'207' MOVWF SPBRG movlw b'00100100' ; brgh = 1 movwf TXSTA ; enable Async Transmission, set brgh banksel RCSTA ; bank0 movlw b'10010000' movwf RCSTA ; enable Async Reception movf RCREG,w movf RCREG,w movf RCREG,w ; flush receive buffer MOVLW b'01110001' ; T MOVWF ASLV_WHOLE MOVLW b'01000000' ; DASH MOVWF ASLV_DECIMAL MOVLW b'01001101' ; FOUR MOVWF BSLV_WHOLE MOVLW b'01110110' ; TWO MOVWF BSLV_DECIMAL GOTO DISPLAY START BCF PORTB, 3 bcf STATUS, RP0 bcf STATUS, RP1 movlw b'11111111' movwf PREAMBLE movlw b'11000011' movwf ACQUIRE MOVLW b'00001111' MOVWF ADDRESS_A MOVLW b'11110000' MOVWF ADDRESS_B MOVLW d'10' MOVWF GAP MOVLW d'2' MOVWF ROUNDTWO btfss PORTB, 6 goto START CALL DELAYMS CLRF PORTB BSF PORTB, 3 GOTO MAIN TxLoop NOP btfss PIR1,TXIF ;xmit buffer empty? goto TxLoop ;no, wait return TxData BTFSS PORTC, 5 ; CHECK CD GOTO TxData BCF PORTC, 4 ; TRANSMIT MODE MOVLW d'20' MOVWF TIMING CALL DELAYMS DECFSZ TIMING, F GOTO $-2 MOVF PREAMBLE, W MOVWF TXREG CALL TxLoop MOVF ACQUIRE, W MOVWF TXREG CALL TxLoop MOVF ADDRESS, W MOVWF TXREG CALL TxLoop CLRF TIMING MOVLW d'20' MOVWF TIMING CALL DELAYMS DECFSZ TIMING, F GOTO $-2 BSF PORTC, 4 RETURN RecLoop nop btfsc PIR1, 0 goto timeout btfss PIR1,RCIF ; check for received data goto RecLoop movf RCREG,w return ; 1/2 SEC DELAY ROUTINE WITH 4MHz CLOCK DELAY movlw 01h movwf NbHi movlw 03h movwf NbLo movlw 8Ah movwf NaHi movlw 5Bh movwf NaLo DeLoop CLRWDT decfsz NaLo,F goto DeLoop decfsz NaHi,F goto DeLoop decfsz NbLo,F goto DeLoop decfsz NbHi,F goto DeLoop NOP RETURN TMR_INIT CLRF TMR1L CLRF TMR1H BCF T1CON, 1 BSF T1CON, 0 RETURN DELAYMS ; delay for 1 ms ;993 cycles movlw 0xC6 movwf d1 movlw 0x01 movwf d2 DELAY_0 decfsz d1, f goto $+2 decfsz d2, f goto DELAY_0 ;3 cycles goto $+1 nop ;4 cycles (including call) return DELAYVIEW CLRWDT CALL DELAY DECFSZ GAP, F GOTO DELAYVIEW MOVLW d'20' MOVWF GAP RETURN SEVEN_SEGMENT BCF STATUS, RP0 movlw D'32' movwf count sipoB clrc rlf ASLV_WHOLE, f rlf ASLV_DECIMAL, f rlf BSLV_WHOLE, f rlf BSLV_DECIMAL, f btfss status, c bcf portc, 2 btfsc status, c bsf portc, 2 bsf portc, 3 bcf portc, 3 decfsz count,f goto sipoB bsf portc, 1 bcf portc, 1 RETURN MAIN clrf temp Settle decfsz temp, f goto Settle CALL TxLoop ; clear Tx buffer ; ****************************** ; TRANSMIT TO ACQUIRE ADC ; ADCON MOVLW b'00111100' MOVWF ADDRESS CALL TxData ; MOVF ADDRESS, W ; MOVWF PORTD CLRF TIMING MOVLW d'125' MOVWF TIMING CALL DELAYMS DECFSZ TIMING, F GOTO $-2 ; ***************************** ; Call for Slave A ; POLL_A MOVF ADDRESS_A, W MOVWF ADDRESS CALL TxData CALL DELAY ReceiveA CALL TMR_INIT call RecLoop ; wait and read from PLM-24 movwf RXTEMP movwf PORTD ; show value for diagnostic purpose MOVF ACQUIRE, W XORWF RXTEMP, 1 BTFSS STATUS, Z GOTO ReceiveA CALL RecLoop CLRF RXTEMP MOVWF RXTEMP XORWF ADDRESS_A, W BTFSS STATUS, Z GOTO DISCARDA CALL RecLoop MOVWF ADC_REG MOVWF SLAVE_A ; MOVWF PORTD BSF PORTB, 2 CLRF RCREG CALL DELAY GOTO CNVRT DISCARDA CALL RecLoop MOVWF DUMMY GOTO ReceiveA TIMEOUT BCF T1CON, 0 MOVLW b'01000000' MOVWF WHOLE MOVWF DECIMAL GOTO STORE SECOND ; ******************* ; Call for Slave B ; POLL_B MOVLW d'125' MOVWF TIMING CALL DELAYMS DECFSZ TIMING, F GOTO $-2 MOVF ADDRESS_B, W MOVWF ADDRESS CALL TxData MOVLW d'125' MOVWF TIMING DECFSZ TIMING, F GOTO $-1 ReceiveB CALL TMR_INIT call RecLoop ; wait and read from PLM-24 movwf RXTEMP movwf PORTD ; show value for diagnostic purpose MOVF ACQUIRE, W XORWF RXTEMP, 1 BTFSS STATUS, Z GOTO ReceiveB CALL RecLoop CLRF RXTEMP MOVWF RXTEMP XORWF ADDRESS_B, W BTFSS STATUS, Z GOTO DISCARDB CALL RecLoop MOVWF ADC_REG MOVWF SLAVE_B ; MOVWF PORTD BSF PORTB, 1 CLRF RCREG MOVLW d'125' MOVWF DELAY DECFSZ DELAY, F GOTO $-1 GOTO CNVRT DISCARDB CALL RecLoop MOVWF DUMMY GOTO ReceiveB CNVRT NOP BCF STATUS, RP0 movlw d'255' subwf ADC_REG,w btfsc status,z goto DISPLAY_FIVE_ZERO movlw d'249' subwf ADC_REG,w btfsc status, c goto DISPLAY_FOUR_NINE ; MINUS 5 movlw d'244' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_EIGHT ; MINUS 5 movlw d'239' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_SEVEN ; MINUS 5 movlw d'234' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_SIX ; MINUS 5 movlw d'229' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_FIVE ; MINUS 5 movlw d'224' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_FOUR ; MINUS 5 movlw d'219' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_THREE ; MINUS 5 movlw d'214' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_TWO ; MINUS 5 movlw d'209' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_ONE ; MINUS 5 movlw d'203' subwf ADC_REG,w btfsc status,c goto DISPLAY_FOUR_ZERO ; MINUS 6 movlw d'198' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_NINE ; MINUS 5 movlw d'193' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_EIGHT movlw d'188' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_SEVEN movlw d'183' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_SIX movlw d'178' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_FIVE movlw d'173' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_FOUR movlw d'168' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_THREE movlw d'163' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_TWO movlw d'158' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_ONE movlw d'152' subwf ADC_REG,w btfsc status,c goto DISPLAY_THREE_ZERO movlw d'147' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_NINE movlw d'142' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_EIGHT movlw d'137' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_SEVEN movlw d'132' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_SIX movlw d'127' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_FIVE movlw d'122' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_FOUR movlw d'117' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_THREE movlw d'112' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_TWO movlw d'107' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_ONE movlw d'102' subwf ADC_REG,w btfsc status,c goto DISPLAY_TWO_ZERO movlw d'96' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_NINE movlw d'91' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_EIGHT movlw d'86' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_SEVEN movlw d'81' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_SIX movlw d'76' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_FIVE movlw d'71' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_FOUR movlw d'66' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_THREE movlw d'61' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_TWO movlw d'56' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_ONE movlw d'51' subwf ADC_REG,w btfsc status,c goto DISPLAY_ONE_ZERO movlw d'45' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_NINE movlw d'40' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_EIGHT movlw d'35' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_SEVEN movlw d'30' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_SIX movlw d'25' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_FIVE movlw d'20' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_FOUR movlw d'15' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_THREE movlw d'10' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_TWO movlw d'05' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_ONE movlw d'00' subwf ADC_REG,w btfsc status,c goto DISPLAY_POINT_ZERO CLRW NOP DISPLAY_FIVE_ZERO MOVLW b'00111111' ; ZERO MOVWF DECIMAL MOVLW b'11011011' ; FIVE MOVWF WHOLE GOTO STORE DISPLAY_FOUR_NINE MOVLW b'01011111' ; NINE MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_EIGHT MOVLW b'01111111' ; EIGHT MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_SEVEN MOVLW b'00001110' ; SEVEN MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_SIX MOVLW b'01111011' ; SIX MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_FIVE MOVLW b'01011011' ; FIVE MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_FOUR MOVLW b'01001101' ; FOUR MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_THREE MOVLW b'01011110' ; THREE MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_TWO MOVLW b'01110110' ; TWO MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_ONE MOVLW b'00001100' ; ONE MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_FOUR_ZERO MOVLW b'00111111' ; ZERO MOVWF DECIMAL MOVLW b'11001101' ; FOUR MOVWF WHOLE GOTO STORE DISPLAY_THREE_NINE MOVLW b'01011111' ; NINE MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_EIGHT MOVLW b'01111111' ; EIGHT MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_SEVEN MOVLW b'00001110' ; SEVEN MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_SIX MOVLW b'01111011' ; SIX MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_FIVE MOVLW b'01011011' ; FIVE MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_FOUR MOVLW b'01001101' ; FOUR MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_THREE MOVLW b'01011110' ; THREE MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_TWO MOVLW b'01110110' ; TWO MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_ONE MOVLW b'00001100' ; ONE MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_THREE_ZERO MOVLW b'00111111' ; ZERO MOVWF DECIMAL MOVLW b'11011110' ; THREE MOVWF WHOLE GOTO STORE DISPLAY_TWO_NINE MOVLW b'01011111' ; NINE MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_EIGHT MOVLW b'01111111' ; EIGHT MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_SEVEN MOVLW b'00001110' ; SEVEN MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_SIX MOVLW b'01111011' ; SIX MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_FIVE MOVLW b'01011011' ; FIVE MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_FOUR MOVLW b'01001101' ; FOUR MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_THREE MOVLW b'01011110' ; THREE MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_TWO MOVLW b'01110110' ; TWO MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_ONE MOVLW b'00001100' ; ONE MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_TWO_ZERO MOVLW b'00111111' ; ZERO MOVWF DECIMAL MOVLW b'11110110' ; TWO MOVWF WHOLE GOTO STORE DISPLAY_ONE_NINE MOVLW b'01011111' ; NINE MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_EIGHT MOVLW b'01111111' ; EIGHT MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_SEVEN MOVLW b'00001110' ; SEVEN MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_SIX MOVLW b'01111011' ; SIX MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_FIVE MOVLW b'01011011' ; FIVE MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_FOUR MOVLW b'01001101' ; FOUR MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_THREE MOVLW b'01011110' ; THREE MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_TWO MOVLW b'01110110' ; TWO MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_ONE_ONE MOVLW b'00001100' ; ONE MOVWF DECIMAL MOVLW b'10001100' ; ONE MOVWF WHOLE BCF STATUS, RP0 GOTO STORE DISPLAY_ONE_ZERO MOVLW b'00111111' ; ZERO MOVWF DECIMAL MOVLW b'10111111' ; ONE MOVWF WHOLE GOTO STORE DISPLAY_POINT_NINE MOVLW b'01011111' ; NINE MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_EIGHT MOVLW b'01111111' ; EIGHT MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_SEVEN MOVLW b'00001110' ; SEVEN MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_SIX MOVLW b'01111011' ; SIX MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_FIVE MOVLW b'01011011' MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_FOUR MOVLW b'01001101' ; FOUR MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_THREE MOVLW b'01011110' ; THREE MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_TWO MOVLW b'01110110' ; TWO MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_ONE MOVLW b'00001100' ; ONE MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE DISPLAY_POINT_ZERO MOVLW b'00111111' MOVWF DECIMAL MOVLW b'10111111' MOVWF WHOLE GOTO STORE STORE decfsz ROUNDTWO, F goto STORE_A goto STORE_B STORE_A MOVF WHOLE, W MOVWF ASLV_WHOLE MOVWF TEMPA_WHOLE MOVF DECIMAL, W MOVWF ASLV_DECIMAL MOVWF TEMPA_DECIMAL GOTO SECOND STORE_B MOVF WHOLE, W MOVWF BSLV_WHOLE MOVWF TEMPB_WHOLE MOVF DECIMAL, W MOVWF BSLV_DECIMAL MOVWF TEMPB_DECIMAL DISPLAY CALL SEVEN_SEGMENT BTFSS PORTB, 3 GOTO START BCF PORTB, 3 GOTO START END