View previous topic :: View next topic |
Author |
Message |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
PIC16F18857 external timer_0 setup help needed |
Posted: Mon Mar 31, 2025 9:06 am |
|
|
Hello all,
I am using MPLAB X IDE v6.20, CCS PCM Compiler version 5.118 on a pic16f18857.
I have written a very simple program that toggles the output for LED1 every time timer_0 interrupts.
It will also toggle the output for LED2 ten times slower.
For some reason I cannot get the timer to function with a 4MGHz external Ceramic Resonator connected to the OSC1/OSC2 pins.
Any help would be appreciated.
Code: | #include <16f18857.H>
#fuses XT, NOWDT, NOPROTECT, NOPUT
#use delay(clock = 4M)
#PIN_SELECT T0CK=PIN_A7
#define LED1 PIN_C0
#define LED2 PIN_C1
int8 interrupt_counter;
int8 Timer0Preset;
#separate
#int_TIMER0
void TIMER0_Interrupt_Servicing(void)
{
SET_TIMER0(Timer0Preset);
delay_cycles(1);
output_toggle(LED1);
interrupt_counter++;
}
void main()
{
// Initialize variables
Timer0Preset = 141;
output_low(LED1);
output_low(LED2);
interrupt_counter = 0;
SETUP_TIMER_0(T0_EXT_L_TO_H|T0_DIV_32|T0_8_BIT);
SET_TIMER0(Timer0Preset);
enable_interrupts(global);
enable_interrupts(INT_TIMER0);
// Initial state for LEDS
output_low(LED1);
output_low(LED2);
// Main
while(true)
{
if(interrupt_counter >= 10) // Toggle LED every ~500ms
{
delay_cycles(1);
output_toggle(LED2);
interrupt_counter = 0;
}
}
} |
|
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19763
|
|
Posted: Mon Mar 31, 2025 10:32 am |
|
|
OK.
I hope you mean 4MHz, you have inserted an extra G in your figure.
Try using HS.
Ceramic resonators typically need a higher drive current than crystals.
It is fairly common to need to use the higher power oscillator output
with these.
Do you know what capacitor values are integrated into the resonator
you have?. You can get ones with different capacitor values and these
affect the drive needed. |
|
 |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
|
Posted: Mon Mar 31, 2025 12:29 pm |
|
|
Yes, I did mean 4MHz.
I am using a Murata Electronics CSTLS4M00G530B0 resonator.
It has 15pf capacitors.
I have used this resonator for decades with no problem. Maybe the newer Microchips are more finicky.
I tried switching it to HS but this did not have any effect. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 325
|
|
Posted: Mon Mar 31, 2025 2:25 pm |
|
|
What are you using to drive Timer 0 input? |
|
 |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
|
Posted: Mon Mar 31, 2025 3:01 pm |
|
|
gaugeguy:
I am not using anything to drive the resonator. I just have it connected to Pins RA7 and RA6. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9445 Location: Greensville,Ontario
|
|
Posted: Mon Mar 31, 2025 3:13 pm |
|
|
curious...kinda obvious things but...
1) can you put scope on the 'clkout' pin and see squiggles ??
2) tried another resonator ?
3) is the PIC actually running ? Maybe blink another LED ( I'm alive ?) |
|
 |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
|
Posted: Mon Mar 31, 2025 3:21 pm |
|
|
temtronic:
I just put a scope on the clkout pin and I don't get anything.. Just flatlined.
Not sure why I don't see anything.
I have tried multiple resonators from a production batch that we use daily.
When I switch to internal clock I do get a signal on the clkout pin that corresponds to the internal clock I select. |
|
 |
temtronic
Joined: 01 Jul 2010 Posts: 9445 Location: Greensville,Ontario
|
|
Posted: Tue Apr 01, 2025 5:46 am |
|
|
hmm. so PIC is good, rez is good..
I see 'pin select is used on that PIC.
Perhaps you need some other 'setup' code ?
Maybe the compiler has a bug ?
I'd print the listing and check how all the registers are being setup.
Actually print 2 , one using internal clock( you KNOW that works) and the rez version.
I don't have that PIC just trying to think what 'quirk' is causing this. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 325
|
|
Posted: Tue Apr 01, 2025 6:01 am |
|
|
Why are you setting pin A7 as the T0CK pin? This is one of the pins the ceramic resonator should be connected to. |
|
 |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
|
Posted: Tue Apr 01, 2025 6:43 am |
|
|
gaugeguy:
I thought this was necessary since this is a Peripheral Pin Select (PPS) chip.
Did I do this wrong?
I have never used a PPS chip before.
It's tough to teach an old dog new tricks. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 325
|
|
Posted: Tue Apr 01, 2025 7:01 am |
|
|
Timer0 can be clocked from the internal instruction clock or from an external pin. You are choosing an external pin and then trying to set that external pin as the ceramic resonator oscillator input pin.
I imagine this is either overriding the oscillator setting or causing excessive loading. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 19763
|
|
Posted: Tue Apr 01, 2025 10:53 am |
|
|
I thought you were clocking the chip off the resonator, not the timer.
The timer oscillator does not support frequencies like this.
Data sheet.
The secondary oscillator, is designed for a 32KHz crystal, and is rated
for 32.4 to 33.1KHz operation. It just will nor work at 4MHz. |
|
 |
edbfmi1
Joined: 18 Jul 2006 Posts: 110
|
|
Posted: Tue Apr 01, 2025 12:21 pm |
|
|
I am trying to use an external resonator for the timer_0 clock.
I guess I am not setting up the PPS correctly.
Can anyone look at my original code and let me know what I need to change to use an external 4Mhz resonator?
The data sheet shows the external resonator being connected to:
OSC1 (PIN_A7) and OSC2 (PIN_A6).
Thanks again for all the help. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 325
|
|
Posted: Tue Apr 01, 2025 12:45 pm |
|
|
Don't use T0CK at all.
Don't clock timer0 from an external pin. |
|
 |
gaugeguy
Joined: 05 Apr 2011 Posts: 325
|
|
Posted: Tue Apr 01, 2025 12:49 pm |
|
|
If you need to provide an external signal to clock Timer0 then pick a pin that is not being used. The default pin for T0CK is RA4. Feed that with a TTL/CMOS level signal from some external source. |
|
 |
|