Can an INTERNAL and EXTERNAL oscillator run at the same time?

MrChips

Joined Oct 2, 2009
34,919
If I understand you correctly, you are saying that you want TIMER1 to interrupt every 1ms. And you don't think you can clock it to do that with the oscillator being an 8 MHz crystal x 4 PLL or 32MHz.

I don't see the problem. You don't even need the prescalar.

Fosc is 32 Mhz.
TIMER1 is clocked by Fosc / 4 which is 8 MHz.


Load TIMER1 with (65536 - 8000) and it will interrupt after 1 msec. It is tricky to do this continuously because you have to allow for the instruction cycles from when the interrupt occurred to when it is reloaded. But I have done this before on PIC16s and it works just fine.
No. You don't have to do that.
Configure your timer for capture compare. Have the timer run continuously at 8MHz without ever stopping.
Every time you receive a capture compare interrupt, add 8000 to the capture compare register. Done!

If you want to run at 32MHz, just add 32000 to the capture compare register.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Hi,
I have used crystals for a long time, as I was getting problems with internal oscillators, it's no problem.

Im afraid I don't understand why 32mHz is incorrect?

I need between 1000uS and 20000uS in 1xuS steps.

I accept, I'm not a good programmer and have great difficulty with D/S, but I'll get there ;)
C.
 

MrChips

Joined Oct 2, 2009
34,919
Hi,
I have used crystals for a long time, as I was getting problems with internal oscillators, it's no problem.

Im afraid I don't understand why 32mHz is incorrect?

I need between 1000uS and 20000uS in 1xuS steps.

I accept, I'm not a good programmer and have great difficulty with D/S, but I'll get there ;)
C.
m = milli, i.e. 1/1000, e.g. 1mΩ = 0.001Ω
M = mega, i.e. 1,000,000, e.g. 1MΩ = 1,000,000Ω
 

BobTPH

Joined Jun 5, 2013
11,566
No. You don't have to do that.
Configure your timer for capture compare. Have the timer run continuously at 8MHz without ever stopping.
Every time you receive a capture compare interrupt, add 8000 to the capture compare register. Done!

If you want to run at 32MHz, just add 32000 to the capture compare register.
TIMER1 does not have a compare register, and cannot be clocked at 32MHz. It is clocked by the processor clock / 4 (or external clock)
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
TIMER1 does not have a compare register, and cannot be clocked at 32MHz. It is clocked by the processor clock / 4 (or external clock)
Hi B,
I have Timer1 to compare CCP1 in 1uS, by FOSC/4 and prescale 1:8.

The way I program, is push buttons until I move forward a step, and I'm hoping that the above settings haven't slowed the system clock, but I won't know till, I've tested.
I'm sure you all know, but I won't know till later.

P.S. I have a mate who is wrting flow charts, for the 4xprograms here, and while I stumble along getting stuff to work, he will correct the CODEs later, hopefully not breaking my wonderful work :)
C
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
3,842
Hi,
I think it is working!

Here's a 1000US LED flash in the MAIN loop, and a 1000US INTERRUPT shon in the OSCILLOSCOPE and CCP1 PIN.

Why I asked the question was in order to have a 1:4 PRESCALE and a 1:8 POSTSCALE so dividing the 32mHz by 32. One of the above suggestions showed a FOSC/4 which served as one of the SCALES.
I've now got to use the above to get 6x TIMES.

I don't think this PIC will 'do' what my question asked, but if anyone wants me to test there BASIC CODE in my SIM, in order to find out, I'll do that.

Thanks for all your help.
C.
 

Attachments

nsaspook

Joined Aug 27, 2009
16,359
Hi,
I think it is working!

Here's a 1000US LED flash in the MAIN loop, and a 1000US INTERRUPT shon in the OSCILLOSCOPE and CCP1 PIN.

Why I asked the question was in order to have a 1:4 PRESCALE and a 1:8 POSTSCALE so dividing the 32mHz by 32. One of the above suggestions showed a FOSC/4 which served as one of the SCALES.
I've now got to use the above to get 6x TIMES.

I don't think this PIC will 'do' what my question asked, but if anyone wants me to test there BASIC CODE in my SIM, in order to find out, I'll do that.

Thanks for all your help.
C.
Glad you're making progress.
 

nsaspook

Joined Aug 27, 2009
16,359
I snagged a few 4331 samples to test locally.
They are limited to one source OR the other.

A very simple interrupt timer works just fine using XC8. You just don't have MCC support with this old chip, so it's a total manual config.
1711139640108.png
1711139661307.png
1711139928927.png
Timer1 ISR toggles RB5.
 

Thread Starter

camerart

Joined Feb 25, 2013
3,842
I snagged a few 4331 samples to test locally.
They are limited to one source OR the other.

A very simple interrupt timer works just fine using XC8. You just don't have MCC support with this old chip, so it's a total manual config.
View attachment 318176
View attachment 318177
View attachment 318179
Timer1 ISR toggles RB5.
Hi N,
Thanks for your effort.
Scanning your CODE, it is very similar to mine, apart from I need to toggle the CCPR1 RC2 PIN.
I've been experimenting with inputting CCPR1 with the times, after trying TMR1, for comparison, and it looks like it may be easier for me.
It looks good in the Simulator, but so far not 'live'.
I'll post any good results.
Cheers, C.
 

nsaspook

Joined Aug 27, 2009
16,359
Hi N,
Thanks for your effort.
Scanning your CODE, it is very similar to mine, apart from I need to toggle the CCPR1 RC2 PIN.
I've been experimenting with inputting CCPR1 with the times, after trying TMR1, for comparison, and it looks like it may be easier for me.
It looks good in the Simulator, but so far not 'live'.
I'll post any good results.
Cheers, C.
A bit of init code.
C:
void config_interrupt(void){
    RCONbits.IPEN = 1;        //Interrupt Priority (In this case: high and low priorities are Enabled)    
    INTCONbits.GIE_GIEH = 1;
    INTCONbits.PEIE_GIEL = 1;
    INTCONbits.INT0IE = 1;
    INTCON2bits.INTEDG0 = 1;  //Counter Switch Setting for RISING EDGE DETECTION
}

void config_timer0(void){    // Register 10-1: INTCON
    T0CONbits.T016BIT   = 1; // 16-bit or 8-bit counter (In this case: 8-bit)
    T0CONbits.T0CS      = 0; // Use Internal Clock(FOSC/4)    
    T0CONbits.PSA       = 0; // Enable Pre-scale (In this case: Enabled)
    // Prescaler bits  
    T0CONbits.T0PS2     = 1; // In this case 1:128
    T0CONbits.T0PS1     = 1;
    T0CONbits.T0PS0     = 0; 
    //NOTE: READ USART TIMEOUT
}

void config_timer1(void){  
    T1CONbits.RD16  = 1; //Enables read/write in one 16-bit operation    

    // Tout = Ticks * (4*Prescale/FOSC)
    TMR1H = 0b11011000;
    TMR1L = 0b11101111;
        
    // Prescale Bits
    T1CONbits.T1CKPS1 = 0; // In this case 1:1
    T1CONbits.T1CKPS0 = 0;
    T1CONbits.T1OSCEN = 0; // Oscillator off
    T1CONbits.TMR1CS  = 0; // Internal Clock (FOSC/4)
    
    PIE1bits.TMR1IE = 1;   // TIMER1 Flag Enable
    IPR1bits.TMR1IP = 0;   // Timer0 has Low Priority
    T1CONbits.TMR1ON = 1;  // Timer1 Enabled
    
}
 
Top