View previous topic :: View next topic |
Author |
Message |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
Trouble switching clock speed using Switch Statement |
Posted: Wed Dec 12, 2007 10:29 am |
|
|
I'm trying to adjust the oscillation speed through a menu on the fly using PIC16F690 / PCM 4.060. Here is a piece of the code.
Code: |
printf("\n\rWhat would you like to change the INTERNAL oscillation speed to?\n\r\n\r");
printf("\n\r1)1Mhz\n\r");
printf("\n\r2)2Mhz\n\r");
OscAnswer = getc();
switch ( OscAnswer )
{
case 1:
{
#use delay(clock=1000000,int)
setup_oscillator( OSC_1MHZ );
ShowOscSpeed = getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowOscSpeed); //display Internal oscillation speed
OscSpeed = 1; //OneMhz - Variable saved into eeprom and used to load 1Mhz oscillation speed upon restart of program
break;
}
case 2:
{
#use delay(clock=2000000,int)
setup_oscillator( OSC_2MHZ );
ShowOscSpeed=getenv("CLOCK");
printf("\n\roscillation speed is now %f hertz.", ShowOscSpeed); //display Internal oscillation speed
OscSpeed = 2; //TwoMhz - Variable saved into eeprom and used to load 1Mhz oscillation speed upon restart of program
break;
}
}
|
It will just show the 2Mhz value even if choose case 1 which is supposed to load the 1Mhz value. Any help would be greatly appreciated, and I hope I don't make anyone repeat anything. Thanks. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Wed Dec 12, 2007 10:54 am |
|
|
Try sticking your own test in place (toggle a pin, and see what frequency you get), and see if the frequency is changing, rather than using 'getenv'. I don't think this can really 'cope' with a changeable clock. When I have used it, it always seems to just report the value defined in the 'top' clock definition (at the head of the file).
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Dec 12, 2007 12:05 pm |
|
|
Quote: |
printf("\n\rWhat would you like to change the INTERNAL oscillation speed to?\n\r\n\r");
printf("\n\r1)1Mhz\n\r");
printf("\n\r2)2Mhz\n\r");
OscAnswer = getc();
switch ( OscAnswer )
{
case 1: |
The getc() function is getting an ASCII number, but your switch-case
statement is switching on integer numbers. You need to convert
the ASCII input value to an integer, or you need to change your case
statements so they switch on ASCII values.
Quote: | case 1:
{
#use delay(clock=1000000,int)
setup_oscillator( OSC_1MHZ );
ShowOscSpeed = getenv("CLOCK"); |
The #use delay() statement affects all code that comes after it in
the program. It does not affect any lines of code that come before it.
Also, if you have another #use delay() statement a few lines lower
down (and you do), then that statement now controls all lines
that come after it in the program. |
|
![](templates/subSilver/images/spacer.gif) |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
Posted: Thu Dec 13, 2007 10:48 am |
|
|
PCM Programmer - I'm working out how to switch on ASCII as we speak.
But I have Switch statements here that seem to switch fine.
Code: |
SelectionMenu(); //displays main menu
while(1)
{
c = getc(); //Program's Selection Menu
putc(c); //Displays key pressed
switch(c)
{
case '1':
printf("\n\n\n\r-Input / Output Voltages Menu-");
break;
case '2':
printf("\n\n\n\r-Oscillation Speed Menu-"); //SwitchingFrequencyFunction();
ShowSFrequency=getenv("CLOCK"); //Set Switching Frequency variable equal to clock (oscillation) speed
printf ("\n\r\n\rThe oscillation speed is %f hertz", ShowSFrequency); //Report switching frequency
printf ("\n\rWould you like to...");
printf ("\n\r\n\r 1)Change the oscillation speed.\n\r");
printf ("\n\r\n\r 2)Make no changes and return to the main menu.\n\r");
answer = getc();
putc(answer); //Shows user input
switch(answer)
{
case '1': printf("\n\rWhat would you like to change the INTERNAL oscillation speed to?\n\r\n\r");
printf("\n\r1)1Mhz\n\r");
printf("\n\r2)2Mhz\n\r");
printf("\n\r3)4Mhz\n\r");
printf("\n\r4)8Mhz\n\r");
FreqAnswer = getc();
putc(FreqAnswer);
switch ( FreqAnswer )
{
case 1:
{
setup_oscillator( OSC_1MHZ );
#use delay(clock=1000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 1; //OneMhz;
break;
}
case 2:
{
setup_oscillator( OSC_2MHZ );
#use delay(clock=2000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 2; //TwoMhz;
break;
}
}
|
I noticed that I did not use aposthropes around case 1 and case 2. I placed them and and disabled the lines that switch the clock speed and each statement works. But as soon as I activate the statements it outputs garbage characters. I see why I need to translate the ASCII to integers. But why does it do that? That is strange.
WORKS
Code: |
case '1':
{
printf("Case 1 loaded");
//setup_oscillator( OSC_1MHZ );
//#use delay(clock=1000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 1; //OneMhz;
break;
}
case '2':
{
printf("Case 2 loaded");
//setup_oscillator( OSC_2MHZ );
//#use delay(clock=2000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 2; //TwoMhz;
break;
} |
DOESN'T WORK
Code: |
case '1':
{
printf("Case 1 loaded");
setup_oscillator( OSC_1MHZ );
#use delay(clock=1000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 1; //OneMhz;
break;
}
case '2':
{
printf("Case 2 loaded");
setup_oscillator( OSC_2MHZ );
#use delay(clock=2000000,int)
ShowSFrequency=getenv("CLOCK");
printf("\n\rOscillation speed is now %f hertz.", ShowSFrequency); //display Internal oscillator frequency
SFrequency = 2; //TwoMhz;
break;
} |
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 13, 2007 2:04 pm |
|
|
In this thread, I showed how to write code for a program that switches
between two oscillator speeds.
http://www.ccsinfo.com/forum/viewtopic.php?t=32758
There are more tips in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=32903
The most important thing to know is that the CCS functions do not
automatically adjust for the current oscillator speed at run-time.
Most people don't need this capability and it would add a lot of overhead
to the functions. The ROM usage would increase. So if you want to
write a multi-frequency code, you have to do it yourself. See the
examples in the links above. |
|
![](templates/subSilver/images/spacer.gif) |
ThomasC
Joined: 09 Oct 2007 Posts: 62
|
|
![](templates/subSilver/images/spacer.gif) |
|