|
|
View previous topic :: View next topic |
Author |
Message |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
Setting Up External Clock |
Posted: Fri May 02, 2008 1:50 pm |
|
|
Can someone show me an example of setting up an external clock? I tried searching.
I know I'm supposed to setup_oscillator(OSC_TIMER1), IESO, etc....
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 02, 2008 1:55 pm |
|
|
1. Post what you mean by "external clock".
(Just to be sure we are talking about the same thing).
2. Post your PIC and compiler version. |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Fri May 02, 2008 2:39 pm |
|
|
Whups sorry. I should know the drill. PCM / 4.060 / ICD2 / PIC16F690
I have a 20.480mhz crystal oscillator hooked up to the RA5/t1ck/osc1/clkin (Pin 2) of the PIC16F690.
I would like to set it up so it runs off this "external oscillator" at the start of the program, and if it fails, it should run off the internal oscillator.
Thanks! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 02, 2008 4:28 pm |
|
|
Quote: | I have a 20.480mhz crystal oscillator hooked up to the
RA5/t1ck/osc1/clkin (Pin 2) of the PIC16F690. |
The 16F690.h file lists the EC_IO fuse for this.
To select the FSCM mode, the 16F690 data sheet says:
Quote: | 3.8 Fail-Safe Clock Monitor
The Fail-Safe Clock Monitor (FSCM) allows the device
to continue operating should the external oscillator fail.
The FSCM can detect oscillator failure any time after
the Oscillator Start-up Timer (OST) has expired. The
FSCM is enabled by setting the FCMEN bit in the
Configuration Word register (CONFIG). The FSCM is
applicable to all external Oscillator modes (LP, XT, HS,
EC, RC and RCIO). |
The 16F690.h file lists the FCMEN fuse for this.
Regarding the internal oscillator frequency, it says:
Quote: | 3.8.2 FAIL-SAFE OPERATION
The internal clock source chosen by the FSCM is
determined by the IRCF<2:0> bits of the OSCCON
register. This allows the internal oscillator to be
configured before a failure occurs. |
The setup_oscillator() function can be used to select the frequency
of the internal oscillator, which will be used after the external
oscillator fails. Example:
Code: | setup_oscillator(OSC_8MHZ | OSC_NORMAL); |
I have not made a test program to check all of this.
But the information given above should allow you to start testing. |
|
|
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Sat May 03, 2008 11:13 am |
|
|
So far it compiles without errors. Does what I have, with your input, look correct or is there something blatantly wrong with my code. Thx for the help.
Code: | #include <16F690.h> //Include PIC16F690 header file
#device adc=10 //Configures the read_adc return size. 10 will return the full A/D reading of 10 bits. Can change to 8 to fit int8.
#fuses INTRC,EC_IO,FCMEN,IESO,NOWDT,NOPROTECT,BROWNOUT,NOMCLR,BROWNOUT,PUT
// TIMER1 IS EXTERNAL CLOCK
// SET TO USE EXTERNAL CLOCK, AND TO ACTIVATE INTERNAL CLOCK IF THE EXTERNAL CLOCK FAILS
//////// Fuses: LP,XT,HS,EC_IO,INTRC_IO,INTRC,RC_IO,RC,PROTECT,NOPROTECT
//////// Fuses: NOBROWNOUT,BROWNOUT,MCLR,NOMCLR,CPD,NOCPD,WDT,NOWDT,PUT
//////// Fuses: NOPUT,IESO,NOIESO,FCMEN,NOFCMEN,BROWNOUT_SW,BROWNOUT_NOSL
#use delay(clock=2mhz,int) //int means internal //maybe able to change here?
#use rs232(baud=9600, xmit=PIN_B7, rcv=PIN_B5, enable=PIN_C1, ERRORS, stream = Network) //Set Slave address here. Initializing the hardware UART, the PIC refers to it as EUSART, TX=xmit=PIN_B7, RX=rcv=PIN_B5, Enable Pin is for direction control,FORCE_SW
//The enable pin C1 controls the direction of data transfer. It is needed for RS485 Two-Wire.
#rom 0x2100 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}//0x2100 is for 16F-series PICs
#byte RCREG = 0x1A //RCREG = Receive register address //Courtesy of PCM Programmer
#byte RCSTA = 0x18 // Receive Status and Control //Courtesy of PCM Programmer
#define SPEN_BIT 7 // Bit position of SPEN within RCSTA //Courtesy of PCM Programmer
#byte TXREG = 0x19 // Transmission Register-Transmission is initiated by writing to TXREG then it gets sent to the TSR, if TSR is clear.
#byte PIR1 = 0x0C
#bit TXIF = PIR1.4 //for Transmit Interrupt Flag - 0x0C.4
/* bit 4 TXIF: USART Transmit Interrupt Flag bit - READ ONLY //PROTECTS AGAINST BUFFER OVERRUNS
1 = The USART transmit buffer, TXREG, is empty (cleared when TXREG is written)
0 = The USART transmit buffer is full */
#include <stdlib.h> //Include general purpose standard library
#include <stdio.h> //Include standard input/output header
#include <stdlibm.h>
#include <string.h>
//----Include these modules----
#include <variables.h>
#include <ClearUsart.c>
//========MAIN========
unsigned int32 XTAL_FREQUENCY = 2000000; //if clock speed changes, divisor changes
unsigned int8 divisor = 4;
void main()
{
setup_oscillator(OSC_2MHZ | OSC_NORMAL);
}
|
|
|
|
|
|
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
|