'DEVICE PIC16F883
Define CONFIG1 = 0x23e4
Define CONFIG2 = 0x3eff
Define CLOCK_FREQUENCY = 8
OSCCON = %01110101 'Oscillator control (datasheet page 66)
Define LCD_BITS = 4
Define LCD_DREG = PORTB
Define LCD_DBIT = 0
Define LCD_RSREG = PORTB
Define LCD_RSBIT = 4
Define LCD_EREG = PORTB
Define LCD_EBIT = 5
Define LCD_RWREG = PORTB
Define LCD_RWBIT = 0
AllDigital 'alle ports digital
'port names
Symbol led1 = PORTA.4 'control LED for microcontroller
Symbol select_sw2 = PORTC.2 'pushbutton for function "Select"
Symbol start_sw3 = PORTC.1 'pushbutton for function "Start"
'declaring variable
Dim an0 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN0)
Dim an1 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN1)
Dim an2 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN2)
Dim an3 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN3)
Dim an4 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN4)
Dim an12 As Word 'voltage as variable "word (0 to 65535)"(here the measured analog value on input AN12)
'declaring variable for the TIMER
Dim var_timer As Byte 'variable "TIMER" value "As Byte"
Dim timer_limite As Word '"TIMER" limite "As Byte"
Dim timer1 As Byte 'variable timer1 setting "As byte 0 to 255"
OPTION_REG.7 = 0 'RBPU must be cleared for individual pull-up 'PORTB' pull-ups
OPTION_REG.INTEDG = 0 'Interrupt on falling edge of GP2/INT pin
OPTION_REG.T0CS = 0 'Internal instruction cycle clock (CLKOUT)
OPTION_REG.T0SE = 0 'Increment on low-to-high transition on GP2/T0CKI pin
OPTION_REG.PSA = 0 'Prescaler is assigned to the TIMER0 module
OPTION_REG.PS2 = 1 'Set the prescaler
OPTION_REG.PS1 = 1 'to increment TMR0
OPTION_REG.PS0 = 0 'every 128th instruction cycle
'A gate set to the desired value
'76543210
PORTA = %00000000 'make all ports "PORTA" low
TRISA = %11101111 '1 = set for input RA5,RA3,RA2,RA1,RA0. 0 = set for output RA4,!
ANSEL = %00011111 '1 = Analog input. AN0,AN1,AN2,AN3,AN4 are assigned as analog input.
'B gate set to the desired value
'76543210
PORTB = %00000000 'make all ports "PORTB" low
TRISB = %00000000 '0 = set for output / all ports are set for output!
'C gate set To the desired value
'76543210
PORTC = %00000000 'make all ports "PORTC" low
TRISC = %11111111 'port RC1 & RC2 (CCP1 & CCP2) are set for output
'converts the analog input to the desired value
'76543210
ADCON0 = %01001011 'Channel 02 (AN0/AN3) as a/d selected (CHS2:CHS0), ADCS1&ADCS0 bit are selected as Fosc/8
ADCON1 = %00000000 'configured as analog inputs (see datasheet page 103)
'The INTCON register is a readable and writable register, which contains the various Enable And flag bits For TMR0 register overflow,
'GPIO port change And external GP2 / int pin interrupts.
INTCON.GIE = 1 'Enables all unmasked interrupts
INTCON.T0IE = 1 'Enables the TMR0 interrupt (TMR0 Overflow Interrupt Enable bit)
INTCON.T0IF = 1 'TMR0 register has overflowed (must be cleared in software)
TMR0 = 0 'Clear TMR0 initially, (reset de timer0 module register)
Lcdinit 0 'no cursor
WaitMs 250
led1 = 0 'PCB control LED green
an0 = 0
an1 = 0
an2 = 0
an3 = 0
an4 = 0
an12 = 0
timer1 = 0 'set timer1 to 0
var_timer = 0 'variable waarde van de timer is 0
timer_limite = 12 'intern klok 8mhz/4 = 2mhz (2mhz/(128*128)) = 122 Hz pulse every 1 second
main: 'endless loop
Adcin 4, an4 'read AN4 from port-A and write value to "DIM AN4 AS WORD"
ANSELH = %00010000 'Setting the ANSELH bit high wil make an analog input
TRISB = %00000001 'Setting the TRISB high wil make it set as a analog input
WaitMs 500
Adcin 12, an12 'read AN12 from port-B and write value to "DIM AN12 AS WORD"
ANSELH = %00000000 'setting the ANSELH bit low so it will be an digital I/O
TRISB = %00000000 'Setting the TRISB bit low so it will be an output
Lcdcmdout LcdLine1Pos(5) 'write from position 5
Lcdout #an4 'just to see on the screen what the digital number is!
Lcdcmdout LcdLine2Pos(5) 'write from position 5
Lcdout #an12 'just to see on the screen what the digital number is!
WaitMs 100
Goto main
End 'end program
On Interrupt 'Interrupt timer
Save System 'Save
TMR0 = 128 'set "TMR0" on 0 (start counting from 128 to 255)
var_timer = var_timer + 1 'increment var_timer
INTCON.T0IF = 0 'clear Interrupt flag
Resume 'jump back where it started
'*************************************************
'* subroutin TIMER will increase the timer every second!
'*************************************************
timer:
If var_timer > timer_limite Then 'when var_timer is greater than timer_limite (122) then....
var_timer = 0 'set "var_timer" back to 0
timer1 = timer1 + 1 'increase "timer1" by 1
Endif
Return