'DEVICE PIC16F887
Define CONFIG1 = 0x23e4 '23e4
Define CONFIG2 = 0x3eff
Define CLOCK_FREQUENCY = 8
'OSCCON = %01110101 'Oscillator control (datasheet page 66)
AllDigital 'alle ports digital
'defines the parameters of the LCD interfacing
Define LCD_BITS = 4
Define LCD_DREG = PORTD
Define LCD_DBIT = 4
Define LCD_RSREG = PORTD
Define LCD_RSBIT = 2
Define LCD_EREG = PORTD
Define LCD_EBIT = 3
Define LCD_RWREG = PORTD
Define LCD_RWBIT = 0
'Defines the parameters of the PSI interfacing
Define SPI_SCK_REG = PORTC
Define SPI_SCK_BIT = 3
Define SPI_SDI_REG = PORTC
Define SPI_SDI_BIT = 4
Define SPI_SDO_REG = PORTC
Define SPI_SDO_BIT = 5
Define SPI_CS_REG = PORTA
Define SPI_CS_BIT = 5
SPIPrepare
'defines the SPI serial port
SSPCON.SSPOV = 1 'In slave mode, the user must read the SSPBUF
SSPCON.SSPEN = 1 'Enables serial port and configures SCK, SDO, SDI and SS as the source of the serial port pins
SSPCON.CKP = 1 'Clock polarity select bit: idle state for clock is a high level
SSPCON.SSPM0 = 0 'SPI slave mode, clock = SCK pin, SS pin control enabled
SSPCON.SSPM1 = 0
SSPCON.SSPM2 = 1
SSPCON.SSPM3 = 0
SSPSTAT.CKE = 0 'SPI Clock Edge select bit: data transmitted on falling edge of SCK
SSPSTAT.SMP = 0 'SPI Master mode: 1 = input data sampled at end of data output time 0 = input data sampled at middle of data output time
'port names
Symbol ledred = PORTA.4 'control LED for version number and alarm signals
Symbol ledgreen = PORTC.6 'control LED for power and microcontroller
'declaring all kinds off variable
Dim n As Byte
Dim waarde As Byte 'voltage as variable "byte"(the measured analog value on input AN7)
'A gate set to the desired value
'76543210
PORTA = %00000000 'make all ports "PORTA" low
TRISA = %11101111 '1 = input,RA7,RA6,RA5,RA3,RA2,RA1,RA0 are set for input. 0 = output, RA4(LEDred), is set for output!
ANSEL = %11000000 '1 = Analog input, RA6 and RA7 are set for analog input, 0 = digital input, RA0,RA1,RA2,RA3,RA4,RA5 are set digital I/O input.
'B gate set to the desired value
'76543210
PORTB = %00000000 'make all ports "PORTB" low
TRISB = %11111111 '1 = input, 0 = output
WPUB = %11001010 'weak pull-up portB register. 1 = pull-up enabled 0 = pull-up disabled (datasheet page 51)
'C gate set to the desired value
'76543210
PORTC = %00000000 'make all ports "PORTC" low
TRISC = %10011001 '1 = input, Port RC0(Save),RC3(SCKspi),RC4(SDIspi) are set for input, 0 = output, RC1(CCP2),RC2(CCP1),RC5(SDOspi),RC6(LEDgreen), are set for output
'D gate set to the desired value
'76543210
PORTD = %00000000 'make all ports "PORTD" low
TRISD = %11111111 '1 = input, 0 = output
'E gate set to the desired value
'3210
PORTE = %0000 'make all ports "PORTE"low
TRISE = %1111 '1 = input, 0 = output
Lcdinit 0 'no cursor
ledred = 1 'turn on the red led!
waarde = 0
main: 'endless loop
Lcdcmdout LcdClear
Lcdout "Pot1="
For n = 1 To 10
SPICSOn
SPIReceive waarde
SPICSOff
Lcdcmdout LcdLine1Pos(6)
Lcdout #waarde
WaitMs 50
Next n
Goto main
End 'end program