; HYGROMETER program
; 2014.06.19

		list p=PIC16F616
		#include <P16F616.inc>
;-------------------------------------------------------------------
		ORG 0x0000
		goto 0x0020
;-------------------------------------------------------------------
		ORG 0x0004
	 	goto INTRPT
		retfie
;-----------------------------------------------------------------------------------------
		CBLOCK 0x30
		RTMR0; TMR0_preset=06, owerflowing at 0.5 ms (30H)
		RTMR1; 128x0.5ms preset time for ADC reading (31H)
		RPWM; RPWM duty cycle register (32H)
		RPORTC; PORTC status (33H)
		RCNT0; loop counter (34H)
		RCNT1; loop counter (35H)
		RCNT2; loop counter (36H)
		ENDC
;-----------------------------------------------------------------------------------------
		ORG 0x0020
		__CONFIG 0x75; Code protection disable,MCLR enable,Internal oscillator 4MHz enable
		bcf INTCON, GIE; Disable all interrupts
		bsf INTCON, T0IE; Timer0 interrupt enable
		bsf STATUS, RP0; Select register bank 1
		bcf TRISA, 4; CLOCKout enable (others lines are inputs)
		clrf ANSEL; All port lines digital I/O
		bsf ANSEL, 2; Line RA2 analog input 
		bsf ADCON1, ADCS0; ADC conv. clock=Fosc/8
		bsf ADCON1, ADCS2; ADC conv. clock=Fosc/16 (4us)
		movlw b'10000000';
		movwf OPTION_REG; PORTA pull-ups are disabled, TIMER0_ck=Fosc/8
		movlw 0xFF; TMR2 preset value
		movwf PR2; TMR2 preset				
		clrf TRISC; all lines of PORTC are outputs
		bcf STATUS, RP0; Select register bank 0
		movlw b'00001101'; PORTC preset value
		movwf PORTC; ON R6, ON OUT OFF RANGE LED
		movwf RPORTC; PORTC mirror
		bsf ADCON0, CHS1; Analog input AN2 (result format left justifired: 8bit resolution)
		bsf ADCON0, ADON; ADC enable (Vref=Vdd)
		movlw 0x0A;
		movwf RTMR0; TMR0 preset register set-up
		bsf T2CON, T2CKPS0; TMR2 prescaler is 4
		bsf T2CON, TMR2ON; TMR2 start (PWM_freq = 1,22kHz, resolution=8bit)
		clrf CCPR1L; duty_cycle=0
		movlw b'00001100'; simple PWM mode
		movwf CCP1CON; PWM operating
		bsf PWM1CON, PRSEN; PWM auto restart
		bcf PORTC, 3; T2on, T3off
		bcf RPORTC, 3; PORTC mirror
		clrf RPWM; PWM duty cycle register clear
		bcf INTCON, T0IF; TMR0 owerflow flag clear
		bsf INTCON, T0IE; TMR0 owerflow interrupt enable
; ----------------------------------------------------------------------------------------
MAIN	bsf INTCON, GIE; Global interrupt enable
		bsf ADCON0, 1; ADC start
MAIN1	btfss ADCON0, 1; Not skip if there are ADC result
		goto MAIN2; There are ADC result
		nop;
		goto MAIN1; loop
MAIN2	btfsc RTMR1, 7; skip if preset time < 64ms
		goto ADread; Analog-digital converter reading
		goto MAIN; loop
ADread	bcf INTCON, GIE; Global interrupt disable
		clrf RTMR1; Starting the 64ms timer (RTMR1) 
		bcf PORTC, 2; Out of range LED off
		bcf RPORTC, 2; PORTC mirror
		movf ADRESH, 1; testing of ADC data invalid?
		btfsc STATUS, Z; Skip if ADRESH > 0
		goto MAIN; Loop starting (ADC result invalid)
		btfss RPORTC, 0; skip if 10kohm (R5) resistor operating
		call R6; 20~50% humidity
		btfss RPORTC, 1; skip if 330kohm (R6) resistor operating
		call R5; 50~95% humidity
		movwf RPWM; ADC~PWM converted data to RPWM register
		movf RPWM, 0; PWM data to W register
		movwf CCPR1L; PWM data to PWM duty register
		btfss RPORTC, 0; skip if R5 (10kohm) operating
		call R6error; invalid result?
		btfss RPORTC, 1; skip if R6 (330kohm) operating
		call R5error; invalid result?
		btfss RPORTC, 0; skip if R5 (10kohm) operating
		call R6test; Need change?
		btfss RPORTC, 1; skip if R6 (330kohm) operating
		call R5test; Need change?
		movf RPORTC, 0; PORTCmirror to W register
		movwf PORTC; PORTC up to date		
 		goto MAIN; loop

; ---------------------------------------INTERRUPT-----------------------------------------
INTRPT	bcf INTCON, T0IF; TMR0 interrupt flag clear
		movf RTMR0, 0; TMR0 preset to W register
		movwf TMR0; TIMER0 preset
		incf RTMR1, 1; AD reading timer increment
		btfsc RPORTC, 4; part of 1kHz generating TOGGLE
 		goto TOGGLEc; part of 1kHz generating TOGGLE
		bsf PORTC, 3; part of 1kHz generating TOGGLE
		bsf RPORTC, 3; PORTC mirror
		bsf PORTC, 4; part of 1kHz generating TOGGLE
		bsf RPORTC, 4; PORTC mirror
		retfie
TOGGLEc	bcf PORTC, 4; part of 1kHz generating TOGGLE
		bcf RPORTC, 4; PORTC mirror
		bcf PORTC, 3; part of 1kHz generating TOGGLE
		bcf RPORTC, 3; PORTC mirror
		retfie
; ------------------------------------SUBROUTINES-----------------------------------------
R6		movlw 0x02; page address
		movwf PCLATH; PCL manipulation preset
		movf ADRESH, 0; ADC result to WREG
		call RADCR6; 20~50% humidity
		return

R5		movlw 0x03; page address
		movwf PCLATH; PCL manipulation preset
		movf ADRESH, 0; ADC result to WREG
		call RADCR5; 50~95% humidity
		return

R6error	movlw 0x6A; (max. legal PWM value) + 1
		subwf RPWM, 0; Carry = 0 if WREG > RPWM
		btfsc STATUS, C; skip if Carry = 0
		bsf RPORTC, 2; Out of range LED operate 
		movlw 0x2C; minimum legal PWM value
		subwf RPWM, 0; Carry = 1 if WREG <= RPWM
		btfss STATUS, C; skip if Carry = 1
		bsf RPORTC, 2; Out of range LED operate
		return

R5error	movlw 0xBF; (max. legal PWM value) + 1
		subwf RPWM, 0; Carry = 0 if WREG > RPWM
		btfsc STATUS, C; skip if Carry = 0
		bsf RPORTC, 2; Out of range LED operate
		movlw 0x64; minimum legal PWM value
		subwf RPWM, 0; Carry = 1 if WREG <= RPWM
		btfss STATUS, C; skip if Carry = 1
		bsf RPORTC, 2; Out of range LED operate 
		return

R6test	movf ADRESH, 0; ADC result in W register
		sublw D'203';
		btfsc STATUS, C; skip if (W>203)
		return
		bcf RPORTC, 1; PORTC mirror R6 off
		bsf RPORTC, 0; PORTC mirror R5 on
		return
		
R5test	movf ADRESH, 0; ADC result in W register
		sublw D'31';
		btfss STATUS, C; skip if (W<=31)
		return
		bcf RPORTC, 0; PORTC mirror R5 off
		bsf RPORTC, 1; PORTC mirror R6 on
		return

		ORG 0x200; (20~50% humidity)		
RADCR6	addwf PCL, 1; TABLAT zero position not data!
		retlw 0x0E;  1 
		retlw 0x0F;  2
		retlw 0x10;  3
		retlw 0x11;  4
		retlw 0x12;  5
		retlw 0x13;  6
		retlw 0x14;  7
		retlw 0x15;  8
		retlw 0x16;  9
		retlw 0x17;  10
		retlw 0x18;  11
		retlw 0x19;  12
		retlw 0x1A;  13
		retlw 0x1B;  14
		retlw 0x1C;  15
		retlw 0x1D;  16	
		retlw 0x1E;  17
		retlw 0x1F;  18
		retlw 0x20;  19
		retlw 0x21;  20
		retlw 0x22;  21
		retlw 0x23;  22
		retlw 0x24;  23
		retlw 0x25;  24
		retlw 0x26;  25
		retlw 0x27;  26
		retlw 0x27;  27
		retlw 0x28;  28
		retlw 0x28;  29
		retlw 0x29;  30
		retlw 0x29;  31
		retlw 0x2A;  32
		retlw 0x2B;  33
		retlw 0x2B;  34
		retlw 0x2C;  35
		retlw 0x2C;  36
		retlw 0x2D;  37
		retlw 0x2E;  38
		retlw 0x2E;  39
		retlw 0x2F;  40
		retlw 0x2F;  41
		retlw 0x30;  42
		retlw 0x30;  43
		retlw 0x31;  44
		retlw 0x31;  45
		retlw 0x32;  46
		retlw 0x32;  47
		retlw 0x33;  48
		retlw 0x33;  49
		retlw 0x34;  50
		retlw 0x34;  51
		retlw 0x34;  52
		retlw 0x35;  53
		retlw 0x35;  54
		retlw 0x36;  55
		retlw 0x36;  56
		retlw 0x37;  57
		retlw 0x37;  58
		retlw 0x37;  59
		retlw 0x38;  60
		retlw 0x38;  61
		retlw 0x39;  62
		retlw 0x39;  63
		retlw 0x39;  64
		retlw 0x3A;  65
		retlw 0x3A;  66
		retlw 0x3A;  67
		retlw 0x3B;  68
		retlw 0x3B;  69
		retlw 0x3C;  70
		retlw 0x3C;  71
		retlw 0x3C;  72
		retlw 0x3D;  73
		retlw 0x3D;  74
		retlw 0x3D;  75
		retlw 0x3E;  76
		retlw 0x3E;  77
		retlw 0x3E;  78
		retlw 0x3F;  79
		retlw 0x3F;  80
		retlw 0x3F;  81
		retlw 0x40;  82
		retlw 0x40;  83
		retlw 0x40;  84
		retlw 0x41;  85
		retlw 0x41;  86
		retlw 0x41;  87
		retlw 0x88;  88
		retlw 0x88;  89
		retlw 0x88;  90
		retlw 0x43;  91
		retlw 0x43;  92
		retlw 0x43;  93
		retlw 0x44;  94
		retlw 0x44;  95
		retlw 0x44;  96
		retlw 0x45;  97
		retlw 0x45;  98
		retlw 0x45;  99
		retlw 0x46; 100
		retlw 0x46; 101
		retlw 0x46; 102
		retlw 0x47; 103
		retlw 0x47; 104
		retlw 0x47; 105
		retlw 0x47; 106
		retlw 0x48; 107
		retlw 0x48; 108
		retlw 0x48; 109
		retlw 0x49; 110
		retlw 0x49; 111
		retlw 0x49; 112
		retlw 0x4A; 113
		retlw 0x4A; 114
		retlw 0x4A; 115
		retlw 0x4A; 116	
		retlw 0x4B; 117
		retlw 0x4B; 118
		retlw 0x4B; 119
		retlw 0x4C; 120
		retlw 0x4C; 121
		retlw 0x4C; 122
		retlw 0x4C; 123
		retlw 0x4D; 124
		retlw 0x4D; 125
		retlw 0x4D; 126
		retlw 0x4E; 127
		retlw 0x4E; 128
		retlw 0x4E; 129
		retlw 0x4E; 130
		retlw 0x4F; 131
		retlw 0x4F; 132
		retlw 0x4F; 133
		retlw 0x50; 134
		retlw 0x50; 135
		retlw 0x50; 136	
		retlw 0x51; 137
		retlw 0x51; 138
		retlw 0x51; 139
		retlw 0x51; 140
		retlw 0x52; 141
		retlw 0x52; 142
		retlw 0x52; 143
		retlw 0x53; 144
		retlw 0x53; 145
		retlw 0x53; 146
		retlw 0x53; 147
		retlw 0x54; 148
		retlw 0x54; 149
		retlw 0x54; 150
		retlw 0x55; 151
		retlw 0x55; 152
		retlw 0x55; 153
		retlw 0x56; 154
		retlw 0x56; 155
		retlw 0x56; 156
		retlw 0x56; 157
		retlw 0x57; 158
		retlw 0x57; 159
		retlw 0x57; 160
		retlw 0x58; 161
		retlw 0x58; 162
		retlw 0x58; 163
		retlw 0x59; 164
		retlw 0x59; 165
		retlw 0x59; 166	
		retlw 0x5A; 167
		retlw 0x5A; 168
		retlw 0x5A; 169
		retlw 0x5B; 170
		retlw 0x5B; 171
		retlw 0x5B; 172
		retlw 0x5C; 173
		retlw 0x5C; 174
		retlw 0x5C; 175
		retlw 0x5D; 176
		retlw 0x5D; 177
		retlw 0x5D; 178
		retlw 0x5E; 179
		retlw 0x5E; 180
		retlw 0x5E; 181
		retlw 0x5F; 182
		retlw 0x5F; 183
		retlw 0x5F; 184
		retlw 0x60; 185
		retlw 0x60; 186	
		retlw 0x61; 187
		retlw 0x61; 188
		retlw 0x61; 189
		retlw 0x62; 190
		retlw 0x62; 191
		retlw 0x62; 192
		retlw 0x63; 193
		retlw 0x63; 194
		retlw 0x64; 195
		retlw 0x64; 196
		retlw 0x65; 197
		retlw 0x65; 198
		retlw 0x65; 199
		retlw 0x66; 200
		retlw 0x66; 201
		retlw 0x67; 202
		retlw 0x67; 203
		retlw 0x68; 204
		retlw 0x68; 205
		retlw 0x69; 206	
		retlw 0x69; 207
		retlw 0x69; 208
		retlw 0x6A; 209
		retlw 0x6A; 210
		retlw 0x70; 211
		retlw 0x70; 212
		retlw 0x71; 213
		retlw 0x71; 214
		retlw 0x72; 215
		retlw 0x72; 216	
		retlw 0x73; 217
		retlw 0x73; 218
		retlw 0x74; 219
		retlw 0x74; 220
		retlw 0x75; 221
		retlw 0x75; 222
		retlw 0x76; 223
		retlw 0x76; 224
		retlw 0x77; 225
		retlw 0x77; 226	
		retlw 0x78; 227
		retlw 0x78; 228
		retlw 0x79; 229
		retlw 0x79; 230
		retlw 0x7A; 231
		retlw 0x7A; 232
		retlw 0x7B; 233
		retlw 0x7B; 234
		retlw 0x7C; 235
		retlw 0x7C; 236	
		retlw 0x7D; 237
		retlw 0x7D; 238
		retlw 0x7E; 239
		retlw 0x7E; 240
		retlw 0x7F; 241
		retlw 0x7F; 242
		retlw 0x7F; 243
		retlw 0x7F; 244
		retlw 0x7F; 245
		retlw 0x80; 246	
		retlw 0x80; 247
		retlw 0x80; 248
		retlw 0x80; 249
		retlw 0x80; 250
		retlw 0x80; 251
		retlw 0x80; 252
		retlw 0x80; 253
		retlw 0x80; 254
		retlw 0x80; 255

		ORG 0x0300; (50~95% humidity)
RADCR5	addwf PCL, 1; TABLAT zero position not data!
		retlw 0x47;  1
		retlw 0x48;  2
		retlw 0x49;  3
		retlw 0x4A;  4
		retlw 0x4B;  5
		retlw 0x4C;  6
		retlw 0x4D;  7
		retlw 0x4E;  8
		retlw 0x4F;  9
		retlw 0x50;  10
		retlw 0x51;  11
		retlw 0x52;  12
		retlw 0x53;  13
		retlw 0x54;  14
		retlw 0x55;  15
		retlw 0x56;  16	
		retlw 0x57;  17
		retlw 0x58;  18
		retlw 0x59;  19
		retlw 0x5A;  20
		retlw 0x5B;  21
		retlw 0x5C;  22
		retlw 0x5D;  23
		retlw 0x5E;  24
		retlw 0x60;  25
		retlw 0x62;  26
		retlw 0x63;  27
		retlw 0x64;  28
		retlw 0x65;  29
		retlw 0x66;  30
		retlw 0x67;  31
		retlw 0x67;  32
		retlw 0x68;  33
		retlw 0x69;  34
		retlw 0x6A;  35
		retlw 0x6A;  36
		retlw 0x6B;  37
		retlw 0x6C;  38
		retlw 0x6C;  39
		retlw 0x6D;  40
		retlw 0x6E;  41
		retlw 0x6E;  42
		retlw 0x6F;  43
		retlw 0x6F;  44
		retlw 0x70;  45
		retlw 0x71;  46
		retlw 0x71;  47
		retlw 0x72;  48
		retlw 0x72;  49
		retlw 0x73;  50
		retlw 0x73;  51
		retlw 0x74;  52
		retlw 0x75;  53
		retlw 0x75;  54
		retlw 0x76;  55
		retlw 0x76;  56
		retlw 0x77;  57
		retlw 0x77;  58
		retlw 0x78;  59
		retlw 0x78;  60
		retlw 0x79;  61
		retlw 0x79;  62
		retlw 0x7A;  63
		retlw 0x7A;  64
		retlw 0x7B;  65
		retlw 0x7B;  66
		retlw 0x7C;  67
		retlw 0x7C;  68
		retlw 0x7D;  69
		retlw 0x7D;  70
		retlw 0x7D;  71
		retlw 0x7E;  72
		retlw 0x7E;  73
		retlw 0x7F;  74
		retlw 0x7F;  75
		retlw 0x80;  76
		retlw 0x80;  77
		retlw 0x80;  78
		retlw 0x81;  79
		retlw 0x81;  80
		retlw 0x82;  81
		retlw 0x82;  82
		retlw 0x82;  83
		retlw 0x83;  84
		retlw 0x83;  85
		retlw 0x84;  86
		retlw 0x84;  87
		retlw 0x84;  88
		retlw 0x85;  89
		retlw 0x85;  90
		retlw 0x85;  91
		retlw 0x86;  92
		retlw 0x86;  93
		retlw 0x86;  94
		retlw 0x87;  95
		retlw 0x87;  96
		retlw 0x88;  97
		retlw 0x88;  98
		retlw 0x88;  99
		retlw 0x89; 100
		retlw 0x89; 101
		retlw 0x8A; 102
		retlw 0x8A; 103
		retlw 0x8A; 104
		retlw 0x8B; 105
		retlw 0x8B; 106
		retlw 0x8B; 107
		retlw 0x8C; 108
		retlw 0x8C; 109
		retlw 0x8C; 110
		retlw 0x8D; 111
		retlw 0x8D; 112
		retlw 0x8D; 113
		retlw 0x8E; 114
		retlw 0x8E; 115
		retlw 0x8F; 116	
		retlw 0x8F; 117
		retlw 0x8F; 118
		retlw 0x90; 119
		retlw 0x90; 120
		retlw 0x90; 121
		retlw 0x91; 122
		retlw 0x91; 123
		retlw 0x91; 124
		retlw 0x92; 125
		retlw 0x92; 126
		retlw 0x92; 127
		retlw 0x93; 128
		retlw 0x93; 129
		retlw 0x93; 130
		retlw 0x94; 131
		retlw 0x94; 132
		retlw 0x94; 133
		retlw 0x95; 134
		retlw 0x95; 135
		retlw 0x96; 136	
		retlw 0x96; 137
		retlw 0x96; 138
		retlw 0x97; 139
		retlw 0x97; 140
		retlw 0x97; 141
		retlw 0x98; 142
		retlw 0x98; 143
		retlw 0x98; 144
		retlw 0x99; 145
		retlw 0x99; 146
		retlw 0x99; 147
		retlw 0x9A; 148
		retlw 0x9A; 149
		retlw 0x9B; 150
		retlw 0x9B; 151
		retlw 0x9B; 152
		retlw 0x9C; 153
		retlw 0x9C; 154
		retlw 0x9C; 155
		retlw 0x9D; 156
		retlw 0x9D; 157
		retlw 0x9D; 158
		retlw 0x9E; 159
		retlw 0x9E; 160
		retlw 0x9F; 161
		retlw 0x9F; 162
		retlw 0x9F; 163
		retlw 0xA0; 164
		retlw 0xA0; 165
		retlw 0xA1; 166	
		retlw 0xA1; 167
		retlw 0xA1; 168
		retlw 0xA2; 169
		retlw 0xA2; 170
		retlw 0xA2; 171
		retlw 0xA3; 172
		retlw 0xA3; 173
		retlw 0xA4; 174
		retlw 0xA4; 175
		retlw 0xA5; 176
		retlw 0xA5; 177
		retlw 0xA5; 178
		retlw 0xA6; 179
		retlw 0xA6; 180
		retlw 0xA7; 181
		retlw 0xA7; 182
		retlw 0xA7; 183
		retlw 0xA8; 184
		retlw 0xA8; 185
		retlw 0xA9; 186	
		retlw 0xA9; 187
		retlw 0xAA; 188
		retlw 0xAA; 189
		retlw 0xAB; 190
		retlw 0xAB; 191
		retlw 0xAC; 192
		retlw 0xAC; 193
		retlw 0xAD; 194
		retlw 0xAD; 195
		retlw 0xAE; 196
		retlw 0xAE; 197
		retlw 0xAF; 198
		retlw 0xAF; 199
		retlw 0xB0; 200
		retlw 0xB0; 201
		retlw 0xB1; 202
		retlw 0xB1; 203
		retlw 0xB2; 204
		retlw 0xB2; 205
		retlw 0xB3; 206	
		retlw 0xB3; 207
		retlw 0xB4; 208
		retlw 0xB4; 209
		retlw 0xB5; 210
		retlw 0xB6; 211
		retlw 0xB6; 212
		retlw 0xB7; 213
		retlw 0xB7; 214
		retlw 0xB8; 215
		retlw 0xB9; 216	
		retlw 0xB9; 217
		retlw 0xBA; 218
		retlw 0xBB; 219
		retlw 0xBB; 220
		retlw 0xBC; 221
		retlw 0xBD; 222
		retlw 0xBE; 223
		retlw 0xBF; 224
		retlw 0xBF; 225
		retlw 0xC0; 226	
		retlw 0xC1; 227
		retlw 0xC2; 228
		retlw 0xC3; 229
		retlw 0xC4; 230
		retlw 0xC5; 231
		retlw 0xC6; 232
		retlw 0xC7; 233
		retlw 0xC7; 234
		retlw 0xC8; 235
		retlw 0xC8; 236	
		retlw 0xC8; 237
		retlw 0xC8; 238
		retlw 0xC8; 239
		retlw 0xC8; 240
		retlw 0xC8; 241
		retlw 0xC8; 242
		retlw 0xC8; 243
		retlw 0xC8; 244
		retlw 0xC8; 245
		retlw 0xC8; 246	
		retlw 0xC8; 247
		retlw 0xC8; 248
		retlw 0xC8; 249
		retlw 0xC8; 250
		retlw 0xC8; 251
		retlw 0xC8; 252
		retlw 0xC8; 253
		retlw 0xC8; 254
		retlw 0xC8; 255

end

		

