View previous topic :: View next topic |
Author |
Message |
hobgoblin
Joined: 10 Sep 2003 Posts: 7
|
pic16f887 - how get timer1 gate to work without comparator? |
Posted: Tue Oct 16, 2007 10:31 am |
|
|
Hi,
I can get timer1 to count an external clock on T1CKI using T1GATE on RB5 to enable it *ONLY* if I directly write the following bits:
//T1 Gate control in CM2CON1
#byte cm2_con1 = 0x109 //comparator 2 control register
#bit t1g_enable = 0x109.1 //T1 Gate bit = 1 = enabled for T1G RB5
//T1 Gate polarity in T1CON
#byte T1_con = 0x10 //T1 control register
#bit t1g_polarity = 0x10.7 //T1 Gate bit 7 polarity 0:lo/1:hi
t1g_enable = 1; //T1GATE enabled on RB5
t1g_polarity = 0; //T1GATE inverted
I am not using the comparator and have turned it off as I assume COMP_T1_GATE in setup_comparator() is only used to declare the comparator as the source for T1GATE.
However if I attempt to set up the gate using C library fns as below, the above bits are ignored and the gate is functionless UNTIL I force them as above:
setup_vref(false);
setup_comparator(NC_NC_NC_NC);
setup_timer_1(T1_EXTERNAL | T1_PRESCALE_1 | T1_GATE_INVERTED);
What am I doing wrong? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 16, 2007 11:40 am |
|
|
Quote: | setup_timer_1(T1_EXTERNAL | T1_PRESCALE_1 | T1_GATE_INVERTED); |
This isn't real code. That constant doesn't exist.
Also, if you have a problem with a CCS function, you should post your
compiler version. |
|
|
Ttelmah Guest
|
|
Posted: Tue Oct 16, 2007 2:51 pm |
|
|
COMP_T1_GATE
T1_DIV_BY_1
T1_EXTERNAL
T1_GATE_INVERTED
It is the 'comp_t1_gate' setting for the comparator that is missing, and the unity prescaler uses a different name to the one you have shown.
Best Wishes |
|
|
Guest
|
|
Posted: Wed Oct 17, 2007 4:27 am |
|
|
Hi,
Thank you for your replies.
I'm sorry, I forgot to mention that I have a statement in my code:
#define T1_PRESCALE_1 T1_DIV_BY_1 so that isn't part of the problem.
The CCS compiler version I'm using is V4.020b. It was supplied by Microchip with the PICkit 2 Debug Express (DV164121) that I am using it on and it has a code size limit of 2k but I am only using 500 words. There is nothing to indicate that it is not fully functional other than having this code size limit.
According to the 16F887.h file, COMP_T1_GATE is a parameter of setup_comparator() and not setup_timer_1(), so it can only go in setup_comparator().
I have already confirmed that it does not work for the following pair of statements:
setup_comparator(NC_NC_NC_NC | COMP_T1_GATE);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_GATE_INVERTED);
But as I said, I think COMP_T1_GATE just assigns T1GATE to the comparator output, which is why the code I have now just says:
setup_comparator(NC_NC_NC_NC );
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_GATE_INVERTED);
Any more ideas what I have to do to make it work? |
|
|
Ttelmah Guest
|
|
Posted: Wed Oct 17, 2007 8:01 am |
|
|
Look at the data sheet diagram 6.1
Note what signal controls where the gate signal comes from - 'T1GSS'.
Where is his signal implementd in the chip?.
CM2CON1.
You have the choice of using the C2OUT signal, or the T1G signal.
The pattern I give sets up to use the C2OUT signal, which matches what you are doing by directly writing the registers.
The alternative (which is what you post), will directly use T1G. To use this, you need to first ensure that this pin is set for use as digital I/O, not as an analog input, and note also that this signal is inverted (this actually inverts the sense of the TIGINV selection).
What you post will owrk, provided the pin is selected for digital I/O.
4.020, is a _buggy_ compiler release. Anything before the 4.03x releases is unlikely to work.
Best Wishes |
|
|
Guest
|
|
Posted: Wed Oct 17, 2007 9:51 am |
|
|
Hi Ttelmah,
Thanks for coming back. I know which registers control this, otherwise I would not have been able to declare and write the 2 bits to make it work!
This my full setup, so you can see I do not have any analog inputs:
port_b_pullups(0xff);
setup_ccp1(CCP_OFF);
setup_ccp2(CCP_OFF);
setup_spi(SPI_SS_DISABLED);
setup_vref(false);
setup_comparator(NC_NC_NC_NC);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_oscillator(OSC_8MHZ);
setup_WDT(WDT_OFF);
set_timer1(0);
setup_timer_1(T1_EXTERNAL | T1_DIV_BY_1 | T1_GATE_INVERTED);
The gate polarity is correct. If I directly write the two bits (CM2CON1 bit 1 = 1 to select T1GATE on RB5 and T1CON bit 7 = 0 to invert T1GATE) it works perfectly but the setup I have above does not work.
I think you have confirmed that I have done everything correctly and
the problem is Microchip supplied a buggy version of the compiler...
Regards, |
|
|
|