PDA

View Full Version : external clock input to RC0/T1CKI on pic18f252 troubles



dazp85
- 27th March 2014, 18:10
hi all

been having some ups and down with a frequency counter. i had it working taking an audio signal through a NPN pre-amp and then to a 741 config as a comparator. the op amp was powered by +/-4.5v from a 9v cell tapped with a gnu at the middle. this created a nice square wave output. when connected to the pic my code counted the frequency perfectly, using TMR1 as an asynchronous counter and TMR0 to create a 1 second delay in which i count the number of tmr1 overflows.

basically, i wanted to make the fixtures a little more permanent and so replaced the 9v cell with a 7660S CPAZ to give my -ve rail and then integrated a dual op amp, config as a 20 gain amp and comparator. result - a nice -5v/+5v swing square wave. however the pic now does not recognise this as an external clock. and i scoped it before when it was connected. i can see a square wave at the RC0/T1CKI pin on the pic, the correct frequency, but it only runs from -0.6v to +4v now. i don't know if this is the problem. is there a certain format the ext clk is required to be for the input? I've even tried a ttl from a function generator but it doesn't like that either.

i checked code in the xc8 sim and worked every time.

below is code extract:
#include <pic18f252.h>
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <htc.h>
#include <delays.h>
#include <math.h>
#pragma config WDT = OFF


unsigned int tmr1_ovflow;
unsigned int freq;


/*****************************Timer1 overflow count**********************************/


void high_ISR (void)
{
PIR1bits.TMR1IF = 0; //checks for Timer 1 rollover interrupt flag
tmr1_ovflow=tmr1_ovflow + 1;
}
/*****************************1 Second Delay**********************************/
void delay (void)
{
int i;
i=0;
do{


T0CON = 0x87;
// configure Timer 0 - select 16-bit mode,
// internal clock source, enable 1:256 prescaler
TMR0H = 0xA4;
// load Timer 0 registers for 1 second delay
TMR0L = 0x6D;
INTCONbits.TMR0IF = 0;
// make sure interrupt flag is reset
while(!(INTCONbits.TMR0IF)); // wait until TMR0 rolls over
i=i+1;
}while(i<1);
}
/*****************************Frequency Counter**********************************/


void freqcount ()
{
tmr1_ovflow = 0;
freq = 0;
TMR1H =0;// force Timer1 to count from 0
TMR1L =0;
PIR1bits.TMR1IF = 0;// clear Timer1 interrupt flag
RCONbits.IPEN = 1;// enable priority interrupt scheme
IPR1bits.TMR1IP = 1;// set Timer1 interrupt to high priority
PIE1bits.TMR1IE = 1;// enable Timer1 roll-over interrupt
T1CON = 0x87;// enable Timer1 with external clock, prescaler 1
INTCON = 0xC0;// enable global and peripheral interrupts
delay ();// start one-second delay and wait for interrupts
INTCONbits.GIE = 0;// disable global interrupt
freq = tmr1_ovflow * 65536 + TMR1H * 256 + TMR1L;
// get the total Timer 1 count, i.e. calculate the frequency
}

any ideas? its driving me up the wall haha!

HenrikOlsson
- 27th March 2014, 18:39
Hi,
This forum isn't really about discussing issues with compilers/languages other than PBP but a quick glance at your code shows

T0CON = 0x87;
// configure Timer 0 - select 16-bit mode,
// internal clock source, enable 1:256 prescaler
The value of 0x87 seems to correspond with the comment which probably isn't what you want to do if using an external clock source connected to T0CKI.

/Henrik.

dazp85
- 27th March 2014, 19:50
oh is it i didn't know the forum wasn't a generic one.

oh thats fine its timer1 that is configured for ext clk. timer 0 still needs internal to give my 1 second delay. thanks for the reply though!:smile:

HenrikOlsson
- 27th March 2014, 20:00
Argh, I read the thread title several times and STILL T0CKI.... Must have made a mixup with RC0....
Sorry about the confusion, good luck!

/Henrik.