News:

;) This forum is the property of Proton software developers

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