CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with 32K crystal and pic24FJ

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Geows



Joined: 05 May 2016
Posts: 11

View user's profile Send private message

Problem with 32K crystal and pic24FJ
PostPosted: Thu Jun 09, 2016 8:48 am     Reply with quote

Hi !

I'm currently working on a pic24FJ128GB106 with ccs compiler with MPLAB v8.92.

I would like to know how to allow my secondary oscillator (32k768 crystal) to run my Timer1.
I checked the datasheet and I didn't find how to do this. It is explained how to use the second oscillator as the main clock of the pic (with the movement in asm) but not how to just use it for a timer (I might have missed something in the datasheet).

I'm using the FRCPLL of the pic as main clock.

I don't think there is a problem with the crystal of the capacitors because it should work with them.

But according to the diagram the bit SOSCEN of the register OSCCON must be activated to allow the crystal to oscillate but since I have to do the move in asm it can't work because if I understand the datasheet well it will change my main clock too.

My code is :
Code:

#include <24FJ128GB106.h>
#DEVICE ICD = 2

#FUSES FRC_PLL, PLL2, NOIOL1WAY, NOPROTECT, NOWDT, NOWRT, NOOSCIO, DEBUG,SOSC
#USE delay(internal=8MHz, clock=32Mhz)

#PIN_SELECT U1RX = PIN_D2
#pin_select U1TX = PIN_D1
#pin_select U2RX = PIN_D5
#pin_select U2TX = PIN_D4
#pin_select U3TX = PIN_D11
#pin_select U3RX = PIN_D8

#pin_select U4RX = PIN_D10   //GPS
#pin_select U4TX = PIN_D9

//#pin_select IC4 = PIN_B2
//#pin_select EXT1 = PIN_E7
//#pin_select IC2 = PIN_B1
#pin_select IC1   = PIN_B9 /
#pin_select T1CK = PIN_C14


#use RS232(BAUD=57600, UART1, STREAM = VarAdapt)
#use RS232(BAUD=57600, UART2, STREAM = ComAdapt) // XMIT = PIN_D4, RCV = PIN_D5
#use RS232(BAUD=57600, UART3, STREAM = TdbAdapt)
#use RS232(BAUD=9600, UART4, STREAM = GPSAdapt)

If you need more of my code, ask me and I'll post it. Same for more information !
If someone has the answer or an idea to how to do this it would be great !
Thanks !
Ttelmah



Joined: 11 Mar 2010
Posts: 19338

View user's profile Send private message

PostPosted: Thu Jun 09, 2016 11:15 am     Reply with quote

It's the CCS version you should tell us..... MPLAB version doesn't matter.

Neat thing in CCS, is you can generate a bset, or access a register directly, using the #bit directive. So:

Code:

#bit SOSCEN=getenv("BIT:SOSCEN")

     //then
     SOSCEN=TRUE; //will set the SOSCEN bit


So:
Code:

#include <24FJ128GB106.h>
#device ICSP=1

#FUSES NOIOL1WAY, NOPROTECT, NOWDT, NOWRT, NOOSCIO, DEBUG, NOJTAG
#use delay(internal=32MHz)
#bit SOSCEN=getenv("BIT:SOSCEN")

void main()
{

   setup_timer1(TMR_EXTERNAL|TMR_DIV_BY_1);
   SOSCEN=TRUE; //will set the SOSCEN bit   

   while(TRUE)
   {
      //TODO: User Code
   }

}

Will run you off the internal oscillator at 32MHz, and enable the secondary, and connect this to timer1.

If you look in the listing file:
Code:

....................    SOSCEN=TRUE; //will set the SOSCEN bit   
0021A:  BSET.B  742.1

Merrily sets the SOSCEN bit.
jeremiah



Joined: 20 Jul 2010
Posts: 1322

View user's profile Send private message

PostPosted: Thu Jun 09, 2016 8:29 pm     Reply with quote

Try

Code:

setup_timer1(TMR_EXTERNAL_RTC,32767);


That should set timer1 up to use the secondary oscillator with a 1 second period.

The assembly generated for the pic24FJ128GB106
Code:

....................    setup_timer1(TMR_EXTERNAL_RTC,32767);
0020C:  CLR     104
0020E:  MOV     #742,W1
00210:  MOV     #2,W4
00212:  MOV     #46,W2
00214:  MOV     #57,W3
00216:  MOV.B   W2L,[W1]
00218:  MOV.B   W3L,[W1]
0021A:  MOV.B   W4L,[W1]
0021C:  MOV     #7FFF,W4
0021E:  MOV     W4,102
00220:  BSET.B  2D1.6
00222:  MOV     #C002,W4
00224:  MOV     W4,104


This works for me on the PIC24FJ64GA004. I'm using PCWHD version 5.056
Geows



Joined: 05 May 2016
Posts: 11

View user's profile Send private message

PostPosted: Mon Jun 13, 2016 5:50 am     Reply with quote

This configuration worked :

setup_timer1(T1_EXTERNAL_RTC,TMR_DIV_BY_1)
PR1=32768

Thanks for all your answers !
jeremiah



Joined: 20 Jul 2010
Posts: 1322

View user's profile Send private message

PostPosted: Mon Jun 13, 2016 9:18 am     Reply with quote

Geows wrote:
This configuration worked :

setup_timer1(T1_EXTERNAL_RTC,TMR_DIV_BY_1)
PR1=32768

Thanks for all your answers !


Your actually doing it a bit wrong. the second parameter should be the period, not the divide by. The PR1 line kinda fixes it (still buggy if you use a different divide by), but the correct way is:

Code:

setup_timer1(T1_EXTERNAL_RTC | TMR_DIV_BY_1, 32767)


Notice the bitwise OR operator between T1_EXTERNAL_RTC and TMR_DIV_BY_1.

Also notice the 32767 versus your 32768. The timer overflow happens on the wrap around to 0. Using 32768 would make your period 32769 ticks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group