Hello
I want to measure a voltage from a sensor 0.5V to 4.5V
and convert it to a specific pulse..
12uS 12uS 12uS
zero = 0.5V = ||____________________||_____________________||
180uS 180uS
12uS 12uS 12uS
fullScale = 4.5V= ||_______________________________||_______________ _________________||
342uS 342uS
I want to use a pic12f675 at 20MHZ use the adc to read the voltage , and output repeatteadly a 12uS pulse to GPIO.2
Time between pulses is is 180uS for zero (adc read 0.5V) and 342uS fulscale (adc read 4.5V)
I have done it with the following code using a small delay routine in assembler.
Code:
------------------------------------------------------------------------------------------------------
@ DEVICE PIC12F675, HS_OSC, MCLR_OFF, PROTECT_ON, WDT_OFF,BOD_OFF
DEFINE OSC 20 'cristal 20 Mhz 2x 22pf
ADCON0 = 131 '000011 Set AN0 ON pino GPIO.0
ANSEL = 101000 'set Fosc/64 'Cristal 20Mhz OBRIGA
CMCON = 7 ' Comparators OFF
TRISIO = �0001 ' GPIO.0 = Input outros output
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_SAMPLEUS 30 ' Set sampling time in uS
'*** Definir Pinos e Variaveis
PULSO var GPIO.2
GPIO = 0
adval Var Word ' adval to store analog reading 0 to 1023 (102 to 920)
Sensor var word ' adval minus offset (0 to 818)
n var word
Tempo var word
Zero var word 'store (180us delay) minus instruccion time of adc lines
pulso = 1
Pause 100 ' da tempo para tudo acordar
Zero = 16 ' 16uS added to the lines below equal 180us
Ciclo:
Pulso =0 'Inicio impulso negativo 12 uS
pauseus 16
PULSO = 1 'Fim impulso negativo 12 uS
ADCIN 0, adval ' Read channel AN0 to adval
If adval > 102 then
Sensor =adval -102 'ofset 1024/10 = 102,4 +/- 0,5V sensor zero
else
sensor =1 ' overflow dá valores perto de 65000
endif
' Adval voltage 0,5 V 180 uS + Widht Sensor Zero
' Adval Voltage 4,5 V 340 uS + Widht Sensor Full scale
pauseus zero ' 180 uS including above instruction time
sensor = (sensor /4) +1 ' the assembler delay overflows if sensor = 0
'sensor = 84 ' for testing
'-------------
Delay: ' Adds the sensor reading to fixed 180uS delay
@ nop
@ decfsz _Sensor,F
@ goto _Delay
Goto Ciclo ' Do it forever
End
--------------------------------------------------------------------------------------------------------
Problem is that i have small resolution
the assembler delay is only 8bits (256 steps
adval variable ranges 102 to 920
sensor variable ranges 0 to 818
sensor /4 ranges 0 to 204
actualy only 204 steps
My assembler knowledge is very limited. actualy i learned a little bit only for this project.
I searched the web tried to have a 16bit delay routine in assembler but could not do it.
I would like to rewrite everything to read analog pin then use timer1 and interrupt
i never used interrupts i searched the web and the forum for examples and tried some code
without sucess.
As a newbie this what i think i have to do
Cristal 20Mhz / 4 = 5Mhz clock = 0.2uS step. As sensor ranges from 0 to 818 . 818 * 0.2uS = 163.6 uS
very close to 162 uS variation i want
After reading the datasheet these are the registers i think i must tweak.
set bits 6 and 7 of INTCON register
clear PIR1 bit 0 = TMR1IF
Set T1CON = �000001 ' to turn on timer1
i think timer 1 counts up and i want to delay 1 to 818 clock cicles
so i must preset timer1 to 65535 - sensor or (0 to 818)
so sensor = 65535 - sensor
TMR1H = Sensor.Highbyte
TMR1L = Sensor.Lowbyte
i realy do not understand how interrupts acts here
i think i must
read the adc in a loop
load TMRH1 and TMR1L with the 64717 to 65535 value
turn on timer1 ' T1CON = �000001
then on IsR stop timer1,pause 180uS , output the 12uS pulse on gpio.2, clear interrupt flag
I think somthing like this
------------------------------------------------------------------------------------------------
Code:
Sensor = 0
INTCON = 0
PIR1 = 0
PIE1 = �000001
ON INTERRUPT GOTO Isr
Ciclo:
ADCIN 0, adval
If adval > 102 then
Sensor = adval -102
Else
Sensor =1
Endif
Sensor = 65535 - Sensor
TMR1H = Sensor.HighByte
TMR1L = Sensor.LowByte
T1CON = �000001
Goto Ciclo
Isr:
T1CON = 0
pauseus 180
GPIO.2 = 1
pauseus 12
GPIO.2 = 0
PIR1 = 0
RESUME
ENABLE
END
---------------------------------------------------------------------------------------------
Can anyone point me the right direction ?
Thanks
Fernando Carvalho
Bookmarks