I generate a constant voltage , called "KEY" between 0 and 4,5V using a potentiometer, and I send it on A0, set as input. This is OK.
I use the Vref generator to produce a triangle wave, with rising and falling edges. I output this wave, called "VRef" on A2, set as input. This is OK.
I program the comparators to compare these two voltages, using the the mode defined by CMCON : "CM<2:0> = 010", with CIS set properlly. So the comparator C1 receives KEY and the triangle signal VRef. Its output must change according the value of KEY, which can be higher or lower than VRef. The outputs of the comparators are supposed to appear on A3 and A4.
All interrupts are disabled. Both timers are OFF.
The program compiles and runs when downloaded in the 16F128A. But the comparisons do not work. I can't output any result of the comparisons on A3 or A4. I tried many configurations of the registers TRISA, VRCON, CMCON.
An other test is to use the mode "CM<2:0> = 101" and to input 2 different voltages on A1 and A2. Reading output of the comparator C2 on A4 shows no activity. Where is the trick ?
Image does not appear. How to make it shown on the post ?
Code: Select all
main:
'initialisations
PortA=%00011110
PortB=%11000000 'LEDs on PortB on
TRISA=%00100111 'all Out, excepted inputs A0=Key, A5=Etal (MCLR). A2=input (to allow Vref output)
TRISB=%11000001 'all Out, excepted B0, B6 and B7.
CMCON=%00000111 'comparators Off
OPTION_REG=%01000000 'with pull-up on PORTB. 4 MHz
'timer0 on internal Clock ; Prescaler = 2 (internal osc. 4 Mhz)
INTCON=%00000000 'interrupts by RB0 and by TMR0 overflow disabled
PIE1=0
PIR1=0
T2CON= %00000000 'timer 2 : OFF, and prescaler = 1
T1CON= %00000000 'timer 1 : osc OFF, and prescaler = 1
' CAUTION : if Timer1 used, B6 et B7 become inputs !
VRCON = %00000000 'VRef generator Off
CMCON= %00010101 'comparators On, in config 101. C2 inputs on A1 and A2, output on A3
while true
LED0=0 'LED0 on B4
Delay_ms(250)
'Generating the rising edge
for V =0 to 21
VRCON = tabVref[V]-192
Delay_us(200)
next V
'Generating the falling edge
for V =21 to 0 step -1
VRCON = tabVref[V]-192
Delay_us(200)
next V
LED0=1
Delay_ms(250)
wend
end.
One question remains : how to post an image ? The one I posted does not appear.