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 support@ccsinfo.com

strange result from variable delay

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



Joined: 14 Oct 2005
Posts: 26

View user's profile Send private message Send e-mail

strange result from variable delay
PostPosted: Tue Feb 28, 2006 7:28 am     Reply with quote

Code:
#include <16F877A.H>

#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP

#USE   DELAY( CLOCK=20000000 )

#USE  FAST_IO(A)
#USE  FAST_IO(B)
#USE  FAST_IO(C)
#USE  FAST_IO(D)
#USE  FAST_IO(E)

#byte port_a=5
#byte port_b=6
#byte port_c=7
#byte port_d=8
#byte port_e=9

 int  con_angles[7][5] = {{6,23,17,22,93},{136,53,246,65,50},{7,48,25,41,88},{10,44,23,6,92},{58,111,470,80,28},{2,6,198,62,73},{129,499,203,141,3}};

static byte CONST   UP_MAP0[8] = { 0xe1,0xc3,0x87,0x0f,0x1e,0x3c,0x78,0xf0 };


void main(){int a,b;

int k=1; int c=1;

char cnt=0 ;char tnt=0;

int del =5; int del1=100;


SETUP_ADC_PORTS(NO_ANALOGS);

       set_tris_a(0xfc);
       set_tris_b(0x00);
       set_tris_c(0x00);

       port_a = 0x00,
       port_b = 0xf0;
       port_c = 0xf0;

              for(;;) { restart_wdt(); if (tnt==40) tnt=0; if(tnt%2==1)

              port_a =0x01;else port_a=0x02;for(a=0;a<k;a++){

                    for(b=0;b<con_angles[c][0];b++)

                           delay_us(5);}restart_wdt();

       while(cnt<=3)  {  for(a=0;a<k;a++)

            switch (cnt) {

            case 0:

            {port_b = UP_MAP0[0];

            for(b=0;b<con_angles[c][1];b++)

                        delay_us(5) ;}restart_wdt();

                  break;

            case 1:

            {port_b = UP_MAP0[1];


            for(b=0;b<con_angles[c][2];b++)

                     delay_us(5);}restart_wdt();

                  break;

             case 2:

             {port_b = UP_MAP0[2];

             for(b=0;b<con_angles[c][3];b++)

                     delay_us(5);}restart_wdt();

                  break;

              case 3:

              {port_b = UP_MAP0[3];

              for(b=0;b<con_angles[c][4];b++)

                     delay_us(10);}restart_wdt();

                   break; }cnt++;

                           }cnt=0;

                while(cnt<=3) { for(a=0;a<k;a++)

                     switch (cnt) {

                case 0:

                {port_c = UP_MAP0[0];

                for(b=0;b<con_angles[c][3];b++)

                         delay_us(5);}restart_wdt();

                  break;

                 case 1:

                 {port_c = UP_MAP0[1];

                  for(b=0;b<con_angles[c][2];b++)

                           delay_us(5);}restart_wdt();

                   break;

                 case 2:

                 {port_c = UP_MAP0[2];

                 for(b=0;b<con_angles[c][1];b++)

                            delay_us(5);}restart_wdt();

                   break;

                 case 3:

                { port_c = UP_MAP0[3];

                 for(b=0;b<con_angles[c][0];b++)

                           delay_us(5);}restart_wdt();

                   break;} cnt++;

                       }restart_wdt();

                       if(tnt%2==1)

                  port_a =0x03;else port_a=0x00 ;for(a=0;a<k;a++){

                  for(b=0;b<con_angles[c][0];b++)

                          delay_us(5);} restart_wdt();tnt++;cnt=4;

           while(cnt<=7)  {for( a=0;a<k;a++)

                     switch (cnt) {

                   case 4:

                  { port_b = UP_MAP0[4];

                   for(b=0;b<con_angles[c][1];b++)

                             delay_us(5);}restart_wdt();

                      break;

                    case 5:

                   { port_b = UP_MAP0[5];

                    for(b=0;b<con_angles[c][2];b++)

                             delay_us(5);}restart_wdt();

                       break;

                    case 6:

                    {port_b = UP_MAP0[6];

                    for(b=0;b<con_angles[c][3];b++)

                               delay_us(5);}restart_wdt();

                        break;

                     case 7:

                     {port_b = UP_MAP0[7];

                      for(b=0;b<con_angles[c][4];b++)

                                delay_us(10);}restart_wdt();

                        break;

                     } cnt++;}

                     cnt=4;

            while(cnt<=7) {for(a=0;a<k;a++)

               switch (cnt) {

                      case 4:

                      {port_c = UP_MAP0[4];

                       for(b=0;b<con_angles[c][3];b++)

                                delay_us(5);}restart_wdt();

                         break;

                      case 5:

                      {port_c = UP_MAP0[5];

                      for(b=0;b<con_angles[c][2];b++)

                                delay_us(5);}restart_wdt();

                          break;

                      case 6:

                      {port_c = UP_MAP0[6];

                      for(b=0;b<con_angles[c][1];b++)

                               delay_us(5);}restart_wdt();

                       break;

                      case 7:

                      {port_c = UP_MAP0[7];

                      for(b=0;b<con_angles[c][0];b++)

                                  delay_us(5);}restart_wdt();

                        break; } cnt++;

                           }  cnt=0; } }








CCS PCM C COMPILER VERSION 3.181


i DO NO UN EXPECTED RESULT COME OUT EVETHOGH I CALCULATE THE LOOK UP TABLE TO GIVE 50 Hz ,EACH SET DIFFERENT FREQUENCY.

AND STRANGE RESULT

THANK U FOR HELP
Ttelmah
Guest







PostPosted: Tue Feb 28, 2006 8:16 am     Reply with quote

First comment. An 'int' in CCS C, is 8 bits. It can't hold a value above 255. The numbers higher than this in the array, will return values mod 256.
Second comment. Computers take time to do things.
For instance, accessing a variable in an array, involves taking one array index, multiplying by the number of elements in a line, adding the second array index, putting this into the indirect addressing registers, and then accessing the required value. A typical array access, will probably take perhaps 50 instructions. With this happening every time round the loop, timings will be appalling. Add the time needed for the loops themselves, and the two switches, and everything will take a lot longer than you think...

Best Wishes
trento



Joined: 14 Oct 2005
Posts: 26

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 28, 2006 8:53 am     Reply with quote

i cosider all the delay of instruction in my calculation and i chnge (int) to (int16) bu still strange result
Ttelmah
Guest







PostPosted: Tue Feb 28, 2006 8:56 am     Reply with quote

Remember the counter variable is also going to need to be an int16.

Best Wishes
trento



Joined: 14 Oct 2005
Posts: 26

View user's profile Send private message Send e-mail

PostPosted: Tue Feb 28, 2006 10:58 am     Reply with quote

i already did ,but still strange result
Guest








PostPosted: Tue Feb 28, 2006 12:07 pm     Reply with quote

I'd also take a close look at your curly braces. There should be a closing brace for every opening brace. If for no other reason then do it for better readability.

Each of your cases has an opening brace but not all have a corresponding closing brace.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Feb 28, 2006 2:37 pm     Reply with quote

I looked at the code for about 5 minutes but then had to stop from headaches. I'm sorry to say so, but I have never seen code with such terrible formatting before.

A few tips:
- Human brains are not very good in remembering all details. Make life easier to yourself and others looking at your code by limiting the number of lines in a function to what fits on a single screen, let's say about 50 lines maximum. Functions of more than 50 lines can always be split into several smaller functions, the only exception I know of is the large switch in a state machine.
Your code with the multiple included loops can easily be split into 3 or 4 functions by creating a function for each loop.

- Use spaces instead of the tab-character for indenting lines. This helps to preserve the layout between editors as the number of spacings for a tab-character is different everywhere. Most modern editors can be configured to insert spaces i.s.o. a tab-character.

- Don't put multiple commands on a single line.
Especially a line like
Code:
while(cnt<=7)  {for( a=0;a<k;a++)
is terrible because it messes up the indentation of your layout.

- Try to use meaningful names for your variables. For a simple loop iterator you can get away with a single letter name but for the other variables please use longer more descriptive name. For example, your 'cnt' is this a Count, Counter or Control variable? And what does it count/control?

- Use curly braces only when needed.
Code:
                  case 4:

                  { port_b = UP_MAP0[4];

                   for(b=0;b<con_angles[c][1];b++)

                             delay_us(5);}restart_wdt();

                      break;
The curly braces are not required here. They will not trouble the compiler, but a human will look at them and then start to search for where the corresponding counter part is located. A waste of time.
Also, the restart_wdt command is outside the braces. Why? It looks like you broke your own design guidelines here. So easy to make mistakes with the braces. So leave them out when not required.


Writing code that is easy to read is not easy and is something we all had to learn, but in the end your code will be easier to maintain and contain less errors and save you time.
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