Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- list p=18f452
- include "p18f452.inc"
- variable databit
- databit set b'000000000'
- potused set b'000000000'
- totalresmul set d'0'
- UDATA_ACS
- DN res 1
- RA res 1 ;Wiper A
- RB res 1 ;Wiper B
- RAB res 2 ;Total Res
- ; Declare a code section at 0x0 named 'RST'. This instruction sends the
- ; program to the actual start of the program.
- RST code 0x0000
- GOTO Setup
- ; Declare a code section at 0x0030. This is where the actual program logic will start
- SRT code 0x0060
- Setup:
- totalres = 720
- ;Load the Total Res into an easier to locate ram loco
- MOVLW HIGH totalres ;We dont worry about full number, it is divided by 256
- MOVWF RAB
- RLNCF ;Save first two bits /256 = first two bits+1
- RLNCF
- MOVLW b'11'
- ANDWF RAB
- MOVLW b'1'
- ADDWF RAB ;Total Resistance has been setup
- call SetupPortB
- call SetupSPI
- potused = b'10'
- databit = d'86' ;Set N a percentage of the Pot Value
- call convertN ;The argument N is now placed in W before the subroutine is called
- call VSetPotBW
- ;Converts N a percentage to a 255 bit number
- convertN:
- MOVLW databit
- MULLW 0xFF ; percentage * 255 = 16 bit number. First 8 bits are the new number
- MOVFF PRODH, W ;Move the high byte of the multiplication to the W
- VSetPotBW:
- ;Sets the resistance between B and W (wiper to N% of the potentionmeters value)
- MOVWF DN ;Store the value W in a better location
- potused = b'01' ;Set the Wiper A first
- selectP
- databit = DN
- call WriteToP
- potused = b'10'
- selectP
- databit = DN
- call WriteToP
- SetupPortB:
- BCF PORTB, 3
- BSF TRISB, 3 ;
- RETURN
- SetupSPI:
- BSF SSPSTAT, CKE
- BCF SSPSTAT, SMP ;Sampled in the middle
- BSF SSPCON1, SSPEN ;Enables SCK, SDO, SDI and SS
- MOVWF b'0001' ;Set the SPI Master mode to FOSC/16 (Pot works on 16)
- IORWF SSPCON1
- BSF TRISC, 4 ;Serial Data In (SDI) - Connected to
- BCF TRISC, 3 ;Serial Clock (SCK)
- BCF TRISC, 5 ;Serial Data Out (SDO)
- RETURN
- selectP:
- BCF PORTB, 3 ;Set CS to LOW
- MOVLW b'00100000' ;Set to shutdown for selection
- MOVWF SSPBUF ;Send First byte (the command byte)
- BSF PORTB, 3
- call WaitforSSPConversion
- BCF PORTB, 3 ;Set CS to LOW
- MOVWF potused ;Selects Pot
- IORWF SSPBUF
- BSF PORTB, 3
- WriteToP:
- ;SETTING UP THE COMMAND BYTE
- BCF PORTB, 3 ;Set CS to LOW
- ;Write in Values
- MOVLW b'00010000' ;Turn on the write byte, Select Potentiometer
- MOVWF SSPBUF ;Send First byte (the command byte)
- BSF PORTB, 3 ;Set CS to High to execute the command
- call WaitforSSPConversion
- ;SETTING UP THE DATA BYTE
- BCF PORTB, 3 ;Set CS to LOW
- MOVLW databit ;The Data Byte is created
- MOVWF SSPBUF ;Send the data to the Pot
- BSF PORTB, 3 ;Set CS to High to execute the command
- call WaitforSSPConversion
- RETURN
- WaitforSSPConversion:
- btfss SSPSTAT, BF ; Wait for the Buffer to become full
- goto WaitforSSPConversion
- RETURN
- ;Set CS Low
- ;clocking in a command byte and then a data byte into the 16 bit shift regist
- ;;Executed when CS is raised
- ;Clocked in on the rising edge of the clock andf out the SO pin on the falling edge
- ;Command Byte - two command select bytes and two potenitometer select bits
- ;c1 and c0 (bits 4 and 5) - 01 write command will be executed to the 8 bits
- ; 10 shutdown
- ; SSPBUF = 0xFF; // Sends data in order to read from the buffer
- ; while ( !SSPSTATbits.BF ); // wait until cycle complete
- ;
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement