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 CCS Technical Support

Case statements :(

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



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

Case statements :(
PostPosted: Fri Aug 22, 2008 5:28 pm     Reply with quote

How does a case statement work i have looked in the ccs manual but still a bit confused 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

View user's profile Send private message

PostPosted: Fri Aug 22, 2008 6:13 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Aug 22, 2008 6:51 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Aug 22, 2008 11:43 pm     Reply with quote

Should be read_adc() instead of adc_value() perhaps?
jacqueskleynhans



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

PostPosted: Sat Aug 23, 2008 2:46 am     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Aug 23, 2008 3:13 am     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Sat Aug 23, 2008 3:37 am     Reply with quote

you still have brackets after adc_value !
jacqueskleynhans



Joined: 10 Apr 2008
Posts: 109
Location: Cape Town, South Africa

View user's profile Send private message

PostPosted: Sat Aug 23, 2008 3:43 am     Reply with quote

Yea i noticed its working now Thanks
_________________
"THE ONLY EASY DAY WAS YESTERDAY"
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