Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 5th November 2009, 08:07 AM   #1
Default Changing Fosc on the go.

Hey guys..
Below are some of the code to read ADC on both AN0 and AN1 at 2Hz using PIC12F615.
The default FOSC is 4 MHz using internal oscillator.
There is a switch connect to GP2 and when the switch is pressed, GP2 -> HIGH.

What I'm trying to do is when the switch is pressed, I want to PIC to run at FOSC = 8MHz. When the switch is released, I want the PIC to run back at Fosc = 4MHz. As in, the PIC will just change its FOSC on the go.. Below is the rough code. As you can see, when GP2 -> HIGH, ANSEL and IOSCFS will change accordingly so that the PIC will sample at 4Hz instead of 2Hz.

Hmm.. will it work..? Is there any thing I missed..?
How long will it need for the internal oscillator to stabilize to its new Fosc..?


Code:
#include <pic.h>

/***** CONFIGURATION *****/
// Config: 4MHz int clock, no watchdog, ext reset disbled, no code protect
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS);


void adc_read(unsigned char channel)
{
	ADON = 1;
	ADCON0 = (channel << 2) + 0b00000001;	// 
	GODONE = 1;
	while(GODONE)
		continue;	// wait for conversion complete
	ADON = 0;
}


void interrupt isr(void)
{
	static int cntTMR0 = 0;

	TMR0 += 256 - 250 + 3;
	T0IF = 0;
	
	
	if (++cntTMR0 == 2000) 	// loop 2000 times to make 0.5s (for 4 MHz)
	{
		cntTMR0 = 0;	// interrupt occur every 250us. 2000 * 250us = 0.5s
		ReadADC = 1;
	}		
	
}

void main()
{
    	// Initialisation
    	// ports
    	TRISIO = 0b00011111;
    
  	// Timer0
  	OPTION = (OPTION & 0xC0) + 0x08;
                                    //  Bit 3 - Prescaler assigned to WDT
                                    //  Bit 2:0 - WDT Rate = 1:1
  	
  	// Interrupt
  	T0IE = 1;	// Enable TMR0 interrupt
  	ei();		// Enable global interrupt

	// configure ADC:           
    	CMCON0 = 7;                 //  Turn off Comparators
	ANSEL = 0b00010011;	    //  Set GP0 and GP1 as analog
				    // 	Select the Clock as Fosc/8
								
    	ADCON0 = 0b00000000;        //  Turn on the ADC
        	                    //  Bit 7 - Left Justified Sample
                                    //  Bit 6 - Use VSS
                                    //  Bit 4:2 - Channel 0
                                    //  Bit 1 - Do not Start
                                    //  Bit 0 - Turn on ADC

	while(1)
	{
	    	if (ReadADC == 1)
		{
			ReadADC = 0;
			adc_read(0);
			ADC1 = ADRESH;
		    	adc_read(1);
		    	ADC2 = ADRESH;
		}

		if (GP2 == 1)
		{
			// 8MHz
			ANSEL = (ANSEL & 0x0F) + 0x50; // Change TAD for 8 MHz
			IOSCFS = 1;  //Internal Oscillator Frequency Select bit
		}
		else
		{
			// 4MHz
			ANSEL = (ANSEL & 0x0F) + 0x10 // Change TAD for 4 MHz
			IOSCFS = 0;  //Internal Oscillator Frequency Select bit
		}
	}
}
MrNobody is offline  
Old 5th November 2009, 08:33 AM   #2
Default

You can't write to the config register in code and so this will not work.

Mike.
Pommie is offline  
Old 5th November 2009, 08:38 AM   #3
Default

Quote:
Originally Posted by Pommie View Post
You can't write to the config register in code and so this will not work.

Mike.
Oh.. bummer.. thanks anyway..
MrNobody is offline  
Reply

Tags
changing, fosc

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
confusion about FOSC of PIC18F4550 set up tuanvoi Micro Controllers 1 20th March 2009 07:31 AM
PIC: Fosc and Fcy UTMonkey Micro Controllers 0 26th March 2008 11:54 AM
Fosc in PICs mora Micro Controllers 5 2nd September 2005 11:30 PM
Changing your username.... solidhelix Chit-Chat 5 8th June 2005 12:07 PM
changing dc polarity janetsmith2000@yahoo.com Electronic Projects Design/Ideas/Reviews 1 8th March 2003 11:36 PM



All times are GMT. The time now is 01:20 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker