Generating frequencies by CCP1

Generating frequencies by CCP1

Postby include12f675 » Sat May 26, 2018 7:35 am

Hi forum,
a friend of mine asked my to arrange an output on a 12F1822 at 40 kHz on RA5. I've done in Proton Basic
Code: Select all
Device 12F1822
Config1 FOSC_INTOSC, WDTE_OFF, PWRTE_OFF, MCLRE_OFF, CP_OFF, CPD_OFF,_
BOREN_OFF, CLKOUTEN_OFF, IESO_OFF, FCMEN_OFF
Config2 WRT_OFF, PLLEN_ON, STVREN_OFF,LVP_OFF

Symbol TMR1IF = PIR1.0        ' TMR1 Overflow Interrupt Flag
Symbol PEIE = INTCON.6
Symbol GIE = INTCON.7        ' Global Interrupt Enable
Symbol TMR0IF = INTCON.2     ' TMR0 Interrupt Flag
Symbol TMR0IE = INTCON.5     ' TMR0 Interrupt Enable
Symbol TRUE = 1              ' Boolean value
Symbol FALSE = 0             ' Boolean value
Symbol tmrset = 125          ' Setting for internal clock
Symbol FREQ_ON T1CON.0       ' enable timer1

OSCCON = %1111000            '32 MHz
Set APFCON.0                 ' output on RA5 ( DS41413C-page 123)
CCP1CON = %00000010          ' setting Toggle mode (DS41413C-page 228)
T1CON = %00100000            ' timer1 set at 1:4 prescaler
CCPR1H = 255                 ' preset for CCPR1 MSB register
CCPR1L = 206                 ' preset for CCPR1 LSB register

Clear PORTA
Clear LATA                   ' LATCH  off
Clear ANSELA                 ' no analog
OPTION_REG = %10000111       ' timer0 1:256 prescaler. Interrupt every 8 mSec
Set FREQ_ON                  ' start the frequency output
Set TMR0IE                   ' start timer0 interrupt
Set GIE                      ' start general interrupt
While 1 = 1
Wend

Having no 12F1822 I just tried a simulation, but it seems that is not giving more than 8 Hz.
I've searched for an answer about what it should do a Toggle mode, but nothing lead to a clear answer. There are bolgs that mention to obtain high frequencies, but the use to flip the port in a ISR (therefore the CCP1 is configured for interruption).

My question..... Is the Timer1 zeroed once the match has come?
I suppose that it should work as it does with ATmega328. There might be the same technology, after all.
include12f675
 
Posts: 12
Joined: Sat May 26, 2018 6:50 am
PIC experience: Experienced Hobbyist

Re: Generating frequencies by CCP1

Postby jtemples » Sat May 26, 2018 5:29 pm

I don't think you'll find many people who know about the compiler you're using, but it appears you're enabling interrupts with no apparent interrupt handler. And there's no possibility of getting an 8 Hz PWM out of a 32 MHz PIC, so you're not seeing what you think you're seeing. No, TMR1 is not zeroed in toggle mode, only special event trigger mode does that. Use PWM mode for what you're trying to do.
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: Generating frequencies by CCP1

Postby include12f675 » Sat May 26, 2018 10:32 pm

jtemples wrote:I don't think you'll find many people who know about the compiler you're using

That doesn't matter, one could understand how the registry settings are written.
jtemples wrote:No, TMR1 is not zeroed in toggle mode, only special event trigger mode does that. Use PWM mode for what you're trying to do.

This is the clearest answer. Only I'd like to know whether there's a chance to achieve 40 kHz PWM.
include12f675
 
Posts: 12
Joined: Sat May 26, 2018 6:50 am
PIC experience: Experienced Hobbyist

Re: Generating frequencies by CCP1

Postby jtemples » Sun May 27, 2018 12:26 am

Yes, you can do 40 kHz @ 32MHz with almost 10 bits of resolution.
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: Generating frequencies by CCP1

Postby include12f675 » Sun May 27, 2018 7:51 am

I need to ask my friend to try this out.
I set
Code: Select all
APFCON.1 = 1;             // selecting RA4 as output
CCPR1L = 50;                     // unsure for this value
PR2 = 49;                 //32e6 / 4 / (PR2+1)  / 4 (PRESCALER) = 40`000
CCP1CON = 0b00001100;
T2CON = 0b00000001;
T2CON.2 = 1;                 // Start TMR2
TRISA.4 = 0;             // Enable RA4 as output

So I still have the doubt about what could be the duty cycle value to put into CCPR1L.

Forgive my subborn convintion that the compare mode should work in toggle and setting CCP1LH for the wanted frequency, without the duty cycle complications. Anyway, I'll appreciate if you'd show a suitable setting.

The only requirement is to have a fixed frequency out of the micro.
include12f675
 
Posts: 12
Joined: Sat May 26, 2018 6:50 am
PIC experience: Experienced Hobbyist

Re: Generating frequencies by CCP1

Postby jtemples » Sun May 27, 2018 9:56 pm

For 50% duty cycle, you'd want CCPR1L = PR2 / 2.
jtemples
Verified identity
 
Posts: 195
Joined: Sun May 25, 2014 2:23 am
Location: The 805
PIC experience: Professional 5+ years with MCHP products

Re: Generating frequencies by CCP1

Postby include12f675 » Tue May 29, 2018 8:46 am

I found a great helper.
include12f675
 
Posts: 12
Joined: Sat May 26, 2018 6:50 am
PIC experience: Experienced Hobbyist


Return to CCP, ECCP and PWM

Who is online

Users browsing this forum: No registered users and 5 guests

cron