View previous topic :: View next topic |
Author |
Message |
newpic
Joined: 09 Aug 2005 Posts: 32
|
How to setup_comparator() in PIC16F917 (mechatronic board) |
Posted: Tue Jan 18, 2011 1:00 pm |
|
|
I am trying to get the CCS work on the mechatronic board. I am following the example project using CCS. I am working on project2.
I am stuck on the comparator setup.
The hardware setup / connection as follows:
Light sensor - RA0
POT1 - RA3
LED - RD7
The main idea is to light up LED when light sensor < POT1
Can anybody explain the following? What do they mean? Which is the right one for me to use? (I got this straight from my device.h file).
Code: |
////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
#define NC_NC_NC_NC 0x00FF07
#define NC_NC 0x00FF07
#define A0_A3_A1_A2 0x30FF04
#define A0_VR_A1_VR 0x30FF02
#define A3_VR_A2_VR 0x30FF0A
#define A0_A2_A1_A2 0x30FF03
#define A0_A2_A1_A2_OUT_ON_A4_A5 0x00CF06
#define NC_NC_C1_C0_OUT_ON_A5 0x00DF05
#define NC_NC_A1_06_OUT_ON_A5 0x00DF0D
#define A3_A2_A1_A2 0x30FF09
|
Here is my code, it does not do anything when run it.
Code: |
#include <16F917.H>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#define LED PIN_D7
#byte LEDPORT = 0x08
main()
{
////////////////////////////////////////////////////////////////// I/O
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),
// PORT_B_PULLUPS(), INPUT(),
// OUTPUT_LOW(), OUTPUT_HIGH(),
// OUTPUT_FLOAT(), OUTPUT_BIT()
// Constants used to identify pins in the above are:
SET_TRIS_A(0x05);
SET_TRIS_D(0x00);
////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparator() are:
Setup_comparator(A0_A3_A1_A2 | COMP_INVERT);
// Constants used in setup_vref() are:
setup_vref(VREF_LOW | 5); // Vref = 5v * (5/24) = 1.04v
// Wait for the Comparator mode change delay, and the Vref
// settling time. (spec = 10 us, so use 20us to be safe).
delay_us(20);
// Continuously poll the Comparator output status bit.
// If it's high, it means the input voltage is greater
// than the Vref voltage. In that case, turn on the LED.
// If not, turn the LED off.
while(1)
{
if(C1OUT == 1)
{
output_high(LED);
}
else
{
output_low(LED);
}
delay_ms(100); // Debounce time
}
}
|
Please help.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2011 1:54 pm |
|
|
Quote: |
I am trying to get the CCS work on the mechatronic board.
|
Post a link to the website for this board. |
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2011 4:19 pm |
|
|
If you install the Mechatronics project files and look at the .ASM source
code for Project2, you can see that they load register CMCON0 with 0x14.
Code: |
; Configure comparators
movlw b'00010100' ; 0x14
movwf CMCON0 ; Turn comparators on as two
; independent comparators,
; invert C1 output
|
To duplicate this same setup with CCS, make a test program, choose
some likely parameters and look at the .LST file to see if you did it
correctly. Here is the test program:
Code: | #include <16F917.H>
#fuses INTRC_IO, NOWDT, BROWNOUT, PUT
#use delay(clock=4000000)
//======================================
void main(void)
{
setup_comparator(A0_A3_A1_A2 | COMP_INVERT);
while(1);
} |
Here is part of the .LST file (compiled with CCS compiler vs. 4.116).
It loads CMCON0 with 0x14, so it matches the .ASM example code.
You can use this setup in your CCS program.
Code: |
......... setup_comparator(A0_A3_A1_A2 | COMP_INVERT);
001C: CLRF CMCON1
001D: MOVLW 14
001E: MOVWF CMCON0
001F: MOVF TRISA,W
0020: IORLW 30
0021: MOVWF TRISA
0022: MOVLW 03
0023: MOVWF @77
0024: DECFSZ @77,F
0025: GOTO 024
0026: MOVF CMCON0,W
0027: BCF STATUS.RP0
0028: BCF PIR2.C1IF |
|
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
Posted: Tue Jan 18, 2011 6:46 pm |
|
|
So I got it right, why my program does not do anything? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2011 7:14 pm |
|
|
What is your CCS compiler version ? |
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
Posted: Tue Jan 18, 2011 7:35 pm |
|
|
PCM 3.249 |
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
Posted: Tue Jan 18, 2011 8:16 pm |
|
|
run your test program and here is what I get
Code: | .................... setup_comparator(A0_A3_A1_A2 | COMP_INVERT);
001E: CLRF 17
001F: MOVLW 14
0020: MOVWF 1C
0021: MOVF 05,W
0022: IORLW 30
0023: MOVWF 05
0024: MOVLW 06
0025: MOVWF 77
0026: DECFSZ 77,F
0027: GOTO 026
0028: NOP
0029: MOVF 1C,W
002A: BCF 03.5
002B: BCF 0D.5 |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 18, 2011 9:05 pm |
|
|
Quote: | SET_TRIS_A(0x05);
SET_TRIS_D(0x00);
|
The TRIS that you have set for PortA is incorrect. That's why it doesn't work. You have pin A3 set an output, but it needs to be an input since
it goes to the comparator.
But, you don't need to set the TRIS. The compiler will do it for you.
That's one of the most useful (but least known - CCS doesn't advertise it)
features of CCS. Just delete both those lines and let the compiler handle
the TRIS. |
|
|
newpic
Joined: 09 Aug 2005 Posts: 32
|
|
Posted: Tue Jan 18, 2011 9:32 pm |
|
|
Thank you. It works. |
|
|
|