Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Based on the following circuit, use Bit Banging methods to write a program (A

ID: 3719785 • Letter: 1

Question

1. Based on the following circuit, use Bit Banging methods to write a program (A acts as a Master PIC (RB2 is the CLK/Clock and RB3 is the Data Out/(Dout/SDO) to Transmit a set of data bytes to a Slave PIC 1 (RAO is th this transmission. (30 Points) Note: (1) Do NOT return fowchart lectured in class, (2) Configure l/O pins properly. (3) Use The data are pre-stored at DM oxoC - oX3A on the PIC16F84A Master PIC for Bit Banning on the Master PIC, (4) Use Index mode to transmit the data at DM: OXOC-0X3A, (5) START and STOP are optional. +5V Slave PIC 1 RAO, CLK RBO, Din/SDI Master PIC RB2, CLK RB3, Dout/SDO RA4, Din/SDIRA2, D Slave PIC 2 RA1, CLK RB1, Din/SDI RA2, Dout/SDO

Explanation / Answer

Answer::

list p=16lf1509
#include <p16lf1509.inc>

__CONFIG _CONFIG1, _FOSC_ECH & _WDTE_OFF & _PWRTE_OFF & _CLKOUTEN_OFF & _BOREN_OFF & _CP_OFF
__CONFIG _CONFIG2, _WRT_OFF & _LVP_OFF & _STVREN_ON


TMR2IF equ 0x01
ADIE equ 0x06
ADIF equ 0x06
PEIE equ 0x06
GIE equ 0x07

counter equ 0x70 ;
nextSPIbyte equ 0x71 ;
incomingSPIbyte equ 0x72 ;
dataPointerISR equ 0x73 ;
sendAnotherFF equ 0x74 ;

#define ledON bsf PORTC, 5
#define ledOFF bcf PORTC, 5

ORG 0x00 ; reset vector
resetvect
nop
goto main


ORG 0x04 ;
BANKSEL PIR1
bcf PIR1, 3 ;

BANKSEL SSP1BUF ;
movf SSP1BUF, w
movwf incomingSPIbyte

  
movf nextSPIbyte, w ;
movwf SSP1BUF ;


movlw 0x7E
movwf nextSPIbyte

movf dataPointerISR, w
subwf FSR0L, w
btfsc STATUS, Z
goto exitISR


;Load nextSPIbyte data from the circular buffer
movf dataPointerISR, w
movwf FSR1L ;
movf INDF1, w
movwf nextSPIbyte
incf dataPointerISR

exitISR
retfie

init

initialise_ports
BANKSEL ANSELA
clrf ANSELA ;
clrf ANSELC

BANKSEL TRISC
bcf TRISC, 5 ;

; BANKSEL OSCCON
; movlw 0x78
; movwf OSCCON ;

; Configure timer 2
initialise_timer2
BANKSEL PR2 ;
movlw 0x61 ;
movwf PR2 ;

BANKSEL T2CON
clrf T2CON
bcf T2CON, 1
bsf T2CON, 0 ;
bsf T2CON, 2 ;

initialise_ADC
Configure Port:
Disable pin output driver (Refer to the TRIS register)
Configurepinasanalog(RefertotheANSEL register)
BANKSEL TRISA
BSF TRISA, 2 ;
BSF TRISA, 1 ;

BANKSEL ANSELA
bsf ANSELA, 2 ;


Configure the ADC module:
SelectADCconversionclock ? Configurevoltagereference ? SelectADCinputchannel
TurnonADCmodule
BANKSEL ADCON1
movlw B'11010000' ;
movwf ADCON1

BANKSEL ADCON2
movlw B'01010000' ;
movwf ADCON2

BANKSEL ADCON0
movlw B'00001001' ;
movwf ADCON0

Configure ADC interrupt (optional): ? ClearADCinterruptflag
EnableADCinterrupt
Enableperipheralinterrupt
Enableglobalinterrupt(1)

BANKSEL PIE1
bcf PIE1, ADIE ;

BANKSEL PIR1
bcf PIR1, ADIF ;

BANKSEL INTCON
bsf INTCON, PEIE ;
bsf INTCON, GIE;


clrf nextSPIbyte;

BANKSEL TRISB
bsf TRISB, 4 ;
bsf TRISB, 6 ;

BANKSEL TRISC
bsf TRISC, 6 ;
bcf TRISC, 7 ;

BANKSEL SSP1STAT
bcf SSP1STAT, 7 ;
bsf SSP1STAT, 6 ;

BANKSEL SSP1CON1
movlw B'00100100' ;
movwf SSP1CON1
  
clrf dataPointerISR; ;

BANKSEL PIE1
bsf PIE1, 3 ;

return

main

initalise
banksel TRISC
bcf TRISC, 5 ;

call init ;

fsrtest

movlw 0x20 ;
movwf FSR0H ;
movwf FSR1H ;
clrf FSR0L ;
clrf FSR1L ;

;Main loop checks for A/D result and enters results into buffer
checkAD

BANKSEL PIR1
btfss PIR1, ADIF
cd goto checkAD

;A/D interrupt flag set, so clear it
BANKSEL PIR1
bcf PIR1, ADIF

BANKSEL ADRESH
;Write A/D result and increment
movf ADRESH, w
movwf INDF0
incf FSR0L


;If AD result low = 0x7E then insert sequence 0x7D 0x5E
movf ADRESL, w
sublw 0x7E
btfss STATUS, Z
goto checkfor0x7D


escapeSequenceWrite0x7E
movlw 0x7D
movwf INDF0
incf FSR0L
movlw 0x5E
movwf INDF0
incf FSR0L
goto checkAD

checkfor0x7D
movf ADRESL, w
sublw 0x7D
btfss STATUS, Z
goto normalwrite

escapeSequenceWrite0x7D
movlw 0x7D
movwf INDF0
incf FSR0L
movlw 0x5D
movwf INDF0
incf FSR0L
goto checkAD


goto checkAD
normalwrite
movf ADRESL, w
movwf INDF0
incf FSR0L
goto checkAD

end