CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Can't get Timer 1 to count RC0 input

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
picnic



Joined: 09 Oct 2008
Posts: 15

View user's profile Send private message

Can't get Timer 1 to count RC0 input
PostPosted: Wed Nov 23, 2011 4:54 am     Reply with quote

I'm using a PIC18F46K22 and trying to count the RC0 input using Timer1 but it doesn't count. Changing the code to use T1_INTERNAL does create a count so the program seems sound except for actually talking to the outside world.

Code:
// Test1
//
//

#include <18F46K22.h>

#fuses INTRC_IO
#fuses NOWDT
#fuses NOMCLR
#fuses NOPROTECT
#fuses NOCPD
#fuses NOBROWNOUT

#use delay(clock=4000000)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, errors)

#byte PMD0 = 0xF3F
#byte T1CON = 0xFCD
#byte T1GCON = 0xFCC

#define LED   PIN_C2
#define CLOCK PIN_D2

#INT_TIMER2
void isr_t2() {
   output_toggle( CLOCK );
}

void main() {
   printf( "Test1\r\n" );
   output_high( LED );
   output_high( CLOCK );
   output_float( PIN_C0 );
   output_float( PIN_A4 );

   set_timer0(0);
   setup_timer_0( T0_EXT_L_TO_H | T0_DIV_1 );
   set_timer1(0);
   setup_timer_1( T1_EXTERNAL | T1_DIV_BY_1 );
   setup_timer_2(T2_DIV_BY_4, 62, 4);        // 1ms Interrupts
   enable_interrupts(INT_TIMER2);            // enable timer interrupt
   enable_interrupts(INT_RDA);               // UART receive

   enable_interrupts(GLOBAL);                // enable all interrupts

   while ( 1 ) {
      delay_ms( 500 );
      output_toggle( LED );
      printf( "T0=%Lu\r\n", get_timer0() );
      printf( "t1=%Lu\r\n", get_timer1() );
      printf( "t2=%d\r\n", get_timer2() );
      printf( "T1CON=%02x\r\n", T1CON );
      printf( "T1GCON=%02x\r\n", T1GCON );
      printf( "PMD0=%02x\r\n", PMD0 );
   }
}



Timer 2 is used to generate an edge every 1ms on RD2, this output is connected to RC0 (T1CKI) and RA4 (T0CKI). Timer 0 counts the pulses but Timer1 does not.

I'm sure I must be doing something stupid but I can't see it. Can anyone shed any light on why Timer 1 won't count the RC0 input?

TIA
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 8:37 am     Reply with quote

Look at figure 12.1 in the data sheet. Note 5. FCMEN is enabled by default, which means the secondary oscillator is also enabled.

Best Wishes
picnic



Joined: 09 Oct 2008
Posts: 15

View user's profile Send private message

PostPosted: Wed Nov 23, 2011 10:43 am     Reply with quote

You are a gentleman and a scholar Smile Little bit of reading around the FCMEN and playing around and now T1 is now counting for me. Thank you very much.

The trick for anyone else who stumbles across this problem was to add these fuses

#fuses NOFCMEN
#fuses IESO

Again thanks.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group