News:

Let's find out together what makes a PIC Tick!

Main Menu

CRYSTAL Division for PIC 16F684

Started by Abdullah, Today at 05:11 AM

Previous topic - Next topic

Abdullah

Device = 16F684
Config  HS_OSC,WDTE_OFF,PWRTE_OFF,MCLRE_OFF,CP_OFF,CPD_OFF,BOREN_OFF,IESO_OFF,FCMEN_OFF
Declare Xtal             = 10  ' 10 MHZ for 9.76KHZ   20 MHZ for 19.53KHZ   both are 10 bit resolutions
Declare Optimiser_Level  = 2
Declare Dead_Code_Remove = 1
Declare Create_Coff      On
Declare Watchdog         Off

Dim duty       As Word
Dim dutya      As Word
Dim vin        As Word
TRISA = %00000110       ' RA1=AN1, RA2=AN2 analog inputs
TRISC = %11001111       ' RC5=PWM output, RC2=LED
PORTA = 0
PORTC = 0

Proc ADC_Setup()
ADCON0 = %10000001
ADCON1 = %01100000      ' FOSC/64 â†' TAD=3.2μs
ANSEL  = %00000110      ' AN1 (RA1), AN2 (RA2) analog
EndProc

Proc ADC_Read10(pChan As Byte), Word
pChan  = pChan << 2
ADCON0 = ADCON0 & %11000011
ADCON0 = ADCON0 | pChan
DelayUS 20
ADCON0bits_GO = 1
Repeat : Until ADCON0bits_GO = 0
Result.Byte1 = ADRESH
Result.Byte0 = ADRESL
EndProc

Proc PWM_Setup()
TRISC.5 = 0
PR2     = 255
T2CON   = %00000100
CCPR1L  = 0
CCP1CON = %00001100
EndProc

Proc PWM_Write(pDuty As Word)
CCP1CON.4 = pDuty.0
CCP1CON.5 = pDuty.1
CCPR1L    = pDuty >> 2
EndProc

Main:
ADC_Setup()
PWM_Setup()
Do   
    duty = ADC_Read10(2)
    vin      = ADC_Read10(1)
   
    If duty <> dutya Then
        dutya = duty
        PWM_Write(duty)
    EndIf

Loop
Hello everyone,

I have a question. I want to obtain two PWM frequencies with 10-bit resolution from the PIC16F684. One frequency should be 19.53 kHz and the other should be approximately 9.76 kHz, and both must have 10-bit resolution.

I have tried all possible methods, but I have not found a solution. If I use a 20 MHz crystal, I can get 19.53 kHz PWM with 10-bit resolution using prescaler = 1 and PR2 = 255. For approximately 9.76 kHz PWM, if I use a 10 MHz crystal, then with prescaler = 1 and PR2 = 255, I can also achieve 10-bit resolution.

My question is: Is there any way to divide the crystal frequency (or use some clock division method) so that I can obtain both frequencies, 19.53 kHz and about 9.76 kHz, with full 10-bit resolution from the same setup?

Thank you very much, everyone.
Abdullah

RGV250

Hi,
PWM is not something I look at generally but one thought springs to mind, can you not use timer 1 which has a prescaler of 1:2?

Regards,
Bob

Stephen Moss

The datasheet indicates that you only get 10-bit resolution with PR2 set to 255, so to get 9.76Khz with a 20MHz crystal you would have to set the Prescaler to 2, but that is not an available options.

My brian doesn't function well in this heat (not much better without it) so this may be wrong, but the closest I can get using the ECCP module is that with a 20MHz crystal if you set the Prescaler to 1:4 and then compensate for the increased time between ticks by setting PR2 to 128 you should get 9.765Khz, but at a 9-bit resolution.

If it absolutely has to be 10-bit, have you tried using the forums search facility to find a solution? I have a feeling this, or something very similar has been asked before, if so then the solution you seek may have already been posted.

Abdullah

Quote from: RGV250 on Today at 07:55 AMHi,
PWM is not something I look at generally but one thought springs to mind, can you not use timer 1 which has a prescaler of 1:2?

Regards,
Bob
Hi
In this chip
1:1
1:4
1:16
1:2 is not available in this chip
Abdullah

Abdullah

Quote from: Stephen Moss on Today at 08:21 AMThe datasheet indicates that you only get 10-bit resolution with PR2 set to 255, so to get 9.76Khz with a 20MHz crystal you would have to set the Prescaler to 2, but that is not an available options.

My brian doesn't function well in this heat (not much better without it) so this may be wrong, but the closest I can get using the ECCP module is that with a 20MHz crystal if you set the Prescaler to 1:4 and then compensate for the increased time between ticks by setting PR2 to 128 you should get 9.765Khz, but at a 9-bit resolution.

If it absolutely has to be 10-bit, have you tried using the forums search facility to find a solution? I have a feeling this, or something very similar has been asked before, if so then the solution you seek may have already been posted.
Hi
Thanks sir it's much better I can try
Abdullah

Gamboa

#5
Abdullah,

From Chat GPT:

No. Not on the PIC16F684 hardware PWM, if both frequencies must keep full 10-bit duty resolution.

The PIC16F684 PWM period is set by Timer2 + PR2, with:

F
PWM
    �

=
4⋅(PR2+1)⋅Timer2 prescaler
F
OSC
    �

    �


Microchip also states that the maximum PWM resolution is 10 bits only when PR2 = 255; the resolution is:

Resolution=log
2
    �

(4⋅(PR2+1))

With a 20 MHz crystal:

F
PWM
    �

=
4⋅256⋅1
20MHz
    �

=19.53125kHz

So this gives:

Fosc    Prescaler    PR2    PWM frequency    Resolution
20 MHz    1    255    19.531 kHz    10-bit
10 MHz    1    255    9.765 kHz    10-bit
20 MHz    4    127    9.765 kHz    9-bit
20 MHz    4    255    4.883 kHz    10-bit

The problem is that, from 20 MHz, getting 9.765 kHz with 10-bit resolution would require either:

PR2=511

which is impossible because PR2 is only 8-bit, or:

Timer2 prescaler=2,PR2=255

which is also impossible because the PIC16F684 Timer2 prescaler options are only 1:1, 1:4, and 1:16. There is no 1:2 Timer2 prescaler.

The Timer2 postscaler also does not solve it. It can divide the Timer2 interrupt rate, but Microchip explicitly notes that the Timer2 postscaler is not used to determine the PWM frequency.

So the practical options are:

Use 20 MHz and accept 19.53 kHz at 10-bit plus 9.76 kHz at only 9-bit using PR2 = 127, prescaler 1:4.
Use 10 MHz and get 9.76 kHz at 10-bit, but then 19.53 kHz at 10-bit is not available from the same PWM setup.
Use another PIC with two independent PWM time bases, for example a newer PIC16/PIC18 with multiple PWM/NCO/CLC peripherals or multiple timers assignable to PWM modules.
Use an external divider to divide 19.53 kHz by 2, but that will not give you an independent 10-bit PWM output at 9.76 kHz. It only helps if the second signal can be a divided carrier or fixed-shape signal.

Therefore, for the PIC16F684 specifically: there is no clock-division method that gives both 19.53 kHz and 9.76 kHz with full 10-bit PWM resolution from the same hardware PWM configuration.

Regards,
Gamboa
Long live for you