Hi Tabsoft, okay had another read at first it was just going over my head. Am I right in saying I need to read bit 6 of CMCON0? So CMCON0.6?
Thanks,
Rob
Hi Tabsoft, okay had another read at first it was just going over my head. Am I right in saying I need to read bit 6 of CMCON0? So CMCON0.6?
Thanks,
Rob
Exactly!
CMCON0 Register:
bit 6 COUT: Comparator Output bit.
This is the logic bit output of the Comparator.
CM: Comparator Mode = 100
With this setup the comparator is comparing VIN- (CIN-/GP1) to VIN+ (which is not an external pin but it is "From CVREF Module", internal voltage reference)
So you need to look at the description of COUT in the CMCON0 register listed below AND you need to look at Table 8-1 in the DS.
This will help you work out what value CMCON0.6 will be based upon a given input on GP1.
Take note that CINV is a configuration bit within the CMCON0 register that determines the logic polarity of COUT.
COUT: Possible values
When CINV = 0:
1 = VIN+ > VIN-
0 = VIN+ < VIN
When CINV = 1:
1 = VIN+ < VIN-
0 = VIN+ > VIN
Regards,
TABSoft
Brilliant! Thanks for the info again your the best! I should be able to get this working how I want it to now, then & only then will I move onto interupt with it.
Thanks again,
Rob
Hi Tabsoft, I got the comparator working perfectlyso I decided to move onto adding an interrupt, which believe it or not hasn't worked. Here is the code I have fully written, maybe you can help where I have gone wrong?
Thanks,
Rob
You will probably have a laugh at this, as its probably completely wrong lol
Code:'PIC 12F683 #CONFIG __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF #ENDCONFIG PAUSE 20 'wait for hardware to settle DEFINE OSC 4 '4mhz ocsillator ANSEL = %00000010 'pin 6 analog CMCON0 = %00000100 'comparator mode VRCON = %10101000 'voltage reference TRISIO = %00000010 'pin 6 input ON INTERRUPT GOTO SLSELECT 'interrupt handler is slselect INTCON = %11001000 'enable GIE and GPIE; clear GPIF IOC = %00000010 ' enable IOC1 (GPIO.1 Interrupt on change) PIE1 = %00001000 'enable comparator interrupt POT1 VAR CMCON0.6 'read potentiometer LED VAR GPIO.2 'led pin 5 ENABLE main: HIGH LED 'light led GOTO main 'repeat DISABLE 'disable interrupts in handler SLSELECT: IF POT1 = 1 THEN 'if <1.1v sleep NAP 1 ELSE ENDIF INTCON = %10001000 'enable GIE and GPIE; clear GPIF PIR1 = %00000000 'reset CMIF RESUME 'where left off ENABLE 'enable interrupts
Glad to hear if Rob.
You did not say what is happening only that it is not working.
I took a look at your code and several things jump out at me.
1. You are jumping into the deep end of the pool with testing "nap"
If would take a smaller step first. Set a flag in the ISR and resume. Then in the main act on the flag you set in the ISR.
This way you can see the effects of the ISR logic.
2. You have IOC.1 enabled, which is GP1, which you are using for the comparator. You need to comment out IOC.1.
3. You are not testing for which Interrupt is firing inside of your ISR. It is almost always a good practice to determine what interrupt caused the jump to the ISR.
4. At the end of your ISR you are not setting the INTCON register they way you think. I believe you have a typo in the value you are assigning to it.
I changed your code to the following which shows the changes.
See if this gets you along further.Code:'PIC 12F683 #CONFIG __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF #ENDCONFIG PAUSE 20 'wait for hardware to settle DEFINE OSC 4 '4mhz ocsillator ANSEL = %00000010 'pin 6 analog CMCON0 = %00000100 'comparator mode VRCON = %10101000 'voltage reference TRISIO = %00000010 'pin 6 input ON INTERRUPT GOTO SLSELECT 'interrupt handler is slselect INTCON = %11001000 'enable GIE and GPIE; clear GPIF 'IOC = %00000010 ' enable IOC1 (GPIO.1 Interrupt on change) PIE1 = %00001000 'enable comparator interrupt POT1 VAR CMCON0.6 'read potentiometer LED VAR GPIO.2 'led pin 5 bitTest var bit 'Debug test bit. Set/Clear in ISR and Check in maiin bitTest = 0 ENABLE main: 'HIGH LED 'light led LED = bitTest GOTO main 'repeat DISABLE 'disable interrupts in handler SLSELECT: 'IF POT1 = 1 THEN 'if <1.1v sleep 'NAP 1 'ELSE 'ENDIF if PIR1.3 = 1 then 'Comparator Interrupt fired (must be cleared before resume) bitTest = POT1 endif 'INTCON = %10001000 'enable GIE and GPIE; clear GPIF INTCON =%11001000 PIR1 = %00000000 'reset CMIF RESUME 'where left off ENABLE 'enable interrupts
Regards,
TABSoft
Hi Tabsoft, okay thanks for all that. I will go back to flagging. I think I was
Getting a bit ahead of myself lol.
Let you know how it goes![]()
Hi, okay I've been playing about with comparator interrupt now & think I have got the hang of it using flags. The code below is something little I came up with![]()
Now I think I would like to implement the nap command now, or do you think I should learn something else before jumping to the nap command?
Thanks again,
Rob
Code:'PIC 12F683 #CONFIG __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF #ENDCONFIG PAUSE 50 'wait for hardware to settle DEFINE OSC 4 '4mhz ocsillator ANSEL = %00000010 'pin 6 analog CMCON0 = %00000100 'comparator mode VRCON = %10101000 'voltage reference TRISIO = %00000010 'pin 6 input ON INTERRUPT GOTO int1 'interrupt handler is int1 INTCON = %11001000 'enable GIE and GPIE; clear GPIF PIE1 = %00001000 'enable comparator interrupt POT1 VAR CMCON0.6 'read potentiometer LED VAR GPIO.2 'led pin 5 loop1 var word 'loop1 counter bitTest var bit 'Debug test bit. Set/Clear in ISR and Check in main bitTest = 0 Loop1 = 0 ENABLE main: IF bittest = 1 THEN GOTO FLASH ENDIF LOW LED GOTO main ENABLE FLASH: do for loop1 = 1 to 1000 HIGH LED PAUSE 1 NEXT LOOP1 for loop1 = 1 to 1000 LOW LED PAUSE 1 NEXT LOOP1 IF bittest = 0 THEN GOTO main ENDIF LOOP DISABLE 'disable interrupts in handler int1: if PIR1.3 = 1 then 'Comparator Interrupt fired (must be cleared before resume) BITtest = pot1 ENDIF INTCON =%11001000 PIR1 = %00000000 'reset CMIF RESUME 'where left off ENABLE 'enable interrupts
Bookmarks