; ; This program is an asynchronous MASTER transmission and reception ; through USART pin RC6 and RC7 with a data transfer of 1.2 kbps. ; PIC16F877 with ST7537 powerline modem ; ;************************************************************************************************** ; ; PROGRAMMED BY: JOHN PAUL S. AGUILING ; ; DATE PROGRAMMED: JULY 28, 2002 ; ; LAST UPDATED: AUGUST 10, 2002 ; ; PROGRAM DESCRIPTION: ; ; This is the second slave of the three powerline communication using PIC16F877. ; This acquires ADC when called by the master and transmits back the value ; to the master after the first slave transmits its data. ; It repeats the process whenever called. Therefore this is part of the ; system which acquires analog signal maximum is +5V. ; ;************************************************************************************************** LIST P=16F877 ERRORLEVEL -302 #INCLUDE P16F877.INC __CONFIG _CP_OFF & _XT_OSC & _WDT_ON & _WRT_ENABLE_ON & _PWRTE_ON & _LVP_OFF & _CPD_OFF & _DEBUG_OFF & _BODEN_OFF CBLOCK 20H TEMP PREAMBLE ACQUIRE ADDRESS ADDRESS_X ADC_REG DUMMY TWENTYMS SAMPLE COUNT RXTEMP NaHi NaLo NbHi NbLo d1 d2 GAP ENDC INIT ORG 0x0000 ; start address = 0000h ; INITIALISE PORTS ; binary used to see individual pin level ; all port pins = low BCF STATUS, RP0 BCF STATUS, RP1 CLRF PORTA CLRF PORTB CLRF PORTC CLRF PORTD CLRF PORTE ; INITIALISE PORTS for Receive ; binary used to see individual pin IO status bsf STATUS,RP0 ; set RAM Page 1 for TRIS registers movlw b'11000001' ; all IO pins = outputs movwf TRISA movlw b'00000000' 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' ; pin 1 analog, the rest digital movwf ADCON1 BCF STATUS, RP0 ; INITIALISE USART for Receive 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 START bcf STATUS, RP0 bcf STATUS, RP1 CLRF ADDRESS CLRF ADC_REG CLRF RCREG movlw b'11111111' movwf PREAMBLE movlw b'11000011' movwf ACQUIRE MOVLW d'10' MOVWF GAP goto MAIN RecLoop nop btfss PIR1,RCIF ; check for received data goto RecLoop movf RCREG,w return 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 CALL DELAYMS MOVF PREAMBLE, W MOVWF TXREG CALL TxLoop MOVF ACQUIRE, W MOVWF TXREG CALL TxLoop MOVF ADDRESS, W MOVWF TXREG MOVWF PORTD CALL TxLoop MOVF ADC_REG, W MOVWF TXREG CALL TxLoop CALL DELAYMS MOVLW b'10010011' MOVWF PORTC 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 decfsz NaLo,F goto DeLoop decfsz NaHi,F goto DeLoop decfsz NbLo,F goto DeLoop decfsz NbHi,F goto DeLoop RETURN Delayms ;19993 cycles movlw 0x9E movwf d1 movlw 0x10 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 CALL DELAY DECFSZ GAP, F GOTO DELAYVIEW MOVLW d'10' MOVWF GAP RETURN ; ; ; ------------------------------------ ; PROVIDE A SETTLING TIME FOR START UP ; ------------------------------------ ; MAIN clrf temp Settle decfsz temp, f goto Settle MOVLW b'10010011' MOVWF PORTC ReceiveADC CLRWDT MOVLW b'00111100' MOVWF ADDRESS_X call RecLoop ; wait and read from PLM-24 movwf RXTEMP movwf PORTD ; show value for diagnostic purpose XORWF ACQUIRE, W BTFSS STATUS, Z GOTO ReceiveADC CALL RecLoop MOVWF ADDRESS XORWF ADDRESS_X, W BTFSS STATUS, Z GOTO DISCARD CLRF RCREG ; ------------------ ; PERFORM ADC NOW!!! ; ------------------ MOVLW b'01000011' MOVWF ADCON0 MOVLW b'00011001' MOVWF TWENTYMS NOP NOP NOP TWENTY DECFSZ TWENTYMS,F GOTO TWENTY BSF ADCON0, GO NOP NOP ANDICO BTFSC ADCON0, GO GOTO ANDICO MOVF ADRESH, W MOVWF SAMPLE MOVWF PORTD BSF PORTC, 2 CALL DELAY ; ---------------------------------- ; Identification for Slave B ; ---------------------------------- MOVLW b'11110000' MOVWF ADDRESS_X ;============================== ReceiveME CLRWDT call RecLoop ; wait and read from PLM-24 movwf RXTEMP movwf PORTD ; show value for diagnostic purpose XORWF ACQUIRE, W BTFSS STATUS, Z GOTO ReceiveME CALL RecLoop MOVWF ADDRESS XORWF ADDRESS_X, W BTFSS STATUS, Z GOTO DISCARD BSF PORTC, 1 CLRF RCREG CALL DELAY GOTO Transmit DISCARD CALL RecLoop MOVWF DUMMY GOTO ReceiveME Transmit CLRWDT CALL DELAY CALL TxLoop MOVF ADDRESS_X, W MOVWF ADDRESS MOVF SAMPLE, W MOVWF ADC_REG CALL TxData CLRF RCREG ; CALL DELAY GOTO MAIN END