View previous topic :: View next topic |
Author |
Message |
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
Case statements :( |
Posted: Fri Aug 22, 2008 5:28 pm |
|
|
How does a case statement work i have looked in the ccs manual but still a bit confused . I need to ask the user to perform an action with printf then press any key to continue. Perform an ADC then continue on to the next state ment.
*is this right*
Code: | for (ctr=0;ctr<9;ctr++) {
switch (ctr) {
case 0:
printf("Move up and press any key\n\r");
getc();
*perform action* and go to next case
break;
case 1:
printf("Move Down and press any key\n\r");
getc;
*perform action* and go to next case
break;
...........
....... |
_________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Aug 22, 2008 6:13 pm |
|
|
The case statement is a basic C language construct. If you want to learn on how to use it I suggest you buy a good book on the C language. These books are much better in explaining the language than the CCS compiler manual and you will use them a lot. Much better and quicker than asking basic questions here in the forum.
What you want to do in your program is a technique called 'state machine'. A good explanation of this technique can be found at http://www.aqdi.com/state.htm |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Fri Aug 22, 2008 6:51 pm |
|
|
I solved it. But I'm getting an error:
The state machine is to calibrate an accelerometer.
Quote: | Executing: "C:\Program files\Picc\CCSC.exe" +FM "main.C" +DF +LN +T +A +M +Z +Y=9 +EA
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 15(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 28(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 42(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 52(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 64(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 76(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 87(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 99(20,29): Expecting function name
*** Error 53 "C:\Users\BETTIE\Documents\Work\ADC_CAL.C" Line 110(20,29): Expecting function name
9 Errors, 0 Warnings. |
My code :
Code: | void calibrate(void) {
for (cnt1=0;cnt1<9;cnt1++) {
switch (cnt1) {
case 0:
printf("Orientate Xaxis UP and press any key to continue.\n\r");
getc();
X1 = Y1 = Z1 =X2 = Y2 = Z2 = X3 = Y3 = Z3 = 0;
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(1); // Select the A/D channel
delay_us(20); // Aquisition time
X1 += adc_value(); // read the raw adc value
}
X1 /= 8;
break;
case 1:
printf("Orientate Xaxis Down and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(1); // Select the A/D channel
delay_us(20); // Aquisition time
X2 += adc_value(); // read the raw adc value
}
X2 /= 8;
break;
case 2:
printf("Orientate Xaxis Horizontal and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(1); // Select the A/D channel
delay_us(20); // Aquisition time
X3 += adc_value(); // read the raw adc value
}
X3 /= 8;
case 3:
printf("Orientate Yaxis UP and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(2); // Select the A/D channel
delay_us(20); // Aquisition time
Y1 += adc_value(); // read the raw adc value
}
Y1 /= 8;
break;
case 4:
printf("Orientate Yaxis Down and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(2); // Select the A/D channel
delay_us(20); // Aquisition time
Y2 += adc_value(); // read the raw adc value
}
Y2 /= 8;
break;
case 5:
printf("Orientate Yaxis Horizontal and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(1); // Select the A/D channel
delay_us(20); // Aquisition time
Y3 += adc_value(); // read the raw adc value
}
Y3 /= 8;
break;
case 6:
printf("Orientate Zaxis UP and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(4); // Select the A/D channel
delay_us(20); // Aquisition time
Z1 += adc_value(); // read the raw adc value
}
Z1 /= 8;
break;
case 7:
printf("Orientate Zaxis Down and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(4); // Select the A/D channel
delay_us(20); // Aquisition time
Z2 += adc_value(); // read the raw adc value
}
Z2 /= 8;
break;
case 8:
printf("Orientate Zaxis Horizontal and press any key to continue.\n\r");
getc();
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(4); // Select the A/D channel
delay_us(20); // Aquisition time
Z3 += adc_value(); // read the raw adc value
}
Z3 /= 8;
break;
}
}
}
|
_________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Fri Aug 22, 2008 11:43 pm |
|
|
Should be read_adc() instead of adc_value() perhaps? |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Sat Aug 23, 2008 2:46 am |
|
|
Thanks that makes total sense wrote this code at 3 this morning.
Kind Regards _________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Sat Aug 23, 2008 3:13 am |
|
|
Thw victory was short lived
Getting same error Code: | void calibrate() {
for (cnt1=0;cnt1<9;cnt1++) {
switch (cnt1) {
case 0:
printf("Orientate Xaxis UP and press any key to continue.\n\r");
getc();
X1 = Y1 = Z1 =X2 = Y2 = Z2 = X3 = Y3 = Z3 = 0;
for(cnt2 = 0; cnt2 < 8; cnt2++)
{
set_adc_channel(1); // Select the A/D channel
delay_us(20); // Aquisition time
adc_value = read_adc(); // Read from specified channel
X1 += adc_value(); // read the raw adc value
}
X1 /= 8;
break;
....... |
_________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
RayJones
Joined: 20 Aug 2008 Posts: 30 Location: Melbourne, Australia
|
|
Posted: Sat Aug 23, 2008 3:37 am |
|
|
you still have brackets after adc_value ! |
|
|
jacqueskleynhans
Joined: 10 Apr 2008 Posts: 109 Location: Cape Town, South Africa
|
|
Posted: Sat Aug 23, 2008 3:43 am |
|
|
Yea i noticed its working now Thanks _________________ "THE ONLY EASY DAY WAS YESTERDAY" |
|
|
|