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

variable delay

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







variable delay
PostPosted: Fri Jan 13, 2006 6:00 am     Reply with quote

Hi every body

I would like to aske about how I can control the value of the delay when I tried to put any variable inside the bracktes the programme give strange result

I triad to use fixed value of delay and make variable loop but also not work also I traid the follwoing function which suggested by PCM programmer but also not work when I put any variable for the value variable it give srange result any help pleas
Code:

#include <16F877.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//----------------------------------

void long_delay_us(long count)
{
char i;

i = (char)(count >> 8);

while(i >= 1)
{
i--;
delay_us(253);
restart_wdt();
}

delay_us((char)count);
}

//---------------------------

main ()
{
long value;
long k;

value = 50000;

printf("Press Enter to start\n\r");
getc();

// Do a 10 second delay ( 200 * 50 ms = 10 seconds)
for(k = 0; k < 200; k++)
long_delay_us(value);

printf("Done\n\r");

while(1);
}

[/quote]
ckielstra



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

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 6:58 am     Reply with quote

I compiled your program for my PIC18F458 and it worked fine.

Please describe in more details the 'strange result' you see.
Also add a link to the other thread you are referring to, or better, sign in to this forum so we can search on your author name.

Which compiler version are you using?
Guest








PostPosted: Fri Jan 13, 2006 8:05 am     Reply with quote

when I want to generate square wave using this programe with certain period, simply when I put for example

[/code]int k=10;

value=50000+k
Code:

 the squre wave will generat with strange result but if I value=50000 every thing become fine
Guest








PostPosted: Fri Jan 13, 2006 8:10 am     Reply with quote

my user name is PIC
ckielstra



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

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 8:43 am     Reply with quote

Anonymous wrote:
my user name is PIC
No, it is Guest.

No wonder I couldn't find the error, you gave us a working program! Mad

I tried your program again with your suggested changes, but it still works perfectly. Please post a complete program showing the strange results you see.

You still haven't described what the strange results are and please, again, mention your compiler version!
PIC



Joined: 13 Jan 2006
Posts: 3
Location: Malaysia

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

PostPosted: Fri Jan 13, 2006 8:49 am     Reply with quote

I will give u full description of my compiler:CCS PCM C compiler version 3.181

as I said when I put value=A+50000 they give wrong result which mean I use variable A for the value when I use value =50000 it work.
wrong result mean they will not give u the delay as u adjust by changing the value of A
fine Exclamation
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 5:25 pm     Reply with quote

You need to post a program which uses the variable 'A', and
also shows the declaration of 'A'.

Don't tell us about it in words. Post a short program that shows
the problem.
PIC



Joined: 13 Jan 2006
Posts: 3
Location: Malaysia

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

PostPosted: Fri Jan 13, 2006 8:28 pm     Reply with quote

this is sample from my program
Code:
#include <16F877A.H>
#fuses HS,NOWDT,NOPROTECT, PUT, BROWNOUT, NOLVP
#USE   DELAY( CLOCK=20000000 )
#use rs232(baud=115200, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#USE  FAST_IO(A)
#USE  FAST_IO(B)
#byte port_a=5
#byte port_b=6
static byte CONST   UP_MAP0[8] = { 0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff };
static byte CONST   UP_MAP1[8] = { 0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00 };

//----------------------------------

void long_delay_us(long count)
{
char i;
i = (char)(count >> 8);
while(i >= 1)
{
i--;
delay_us(253);
restart_wdt();
}
delay_us((char)count);
}
void main(){
char k=10;
long value1,value2,value3,value4,value5;
char cnt=0;char tnt=0;
SETUP_ADC_PORTS(NO_ANALOGS);
       set_tris_a(0xfc);
       set_tris_b(0x00);
       port_a = 0x00,
       port_b = 0x00,
       value1=(6588*k)/180;
       value2=(20628*k)/180;
       value3=(17982*k)/180;
       value4=(17118*k)/180;
       value5=(55368*k)/180;
              for(;;) { if (tnt==40) tnt=0; if(tnt%2==1)
              port_a =0x01;else port_a=0x02;
                    long_delay_us(value1);
       while(cnt<=3)  {
                      switch (cnt) {
            case 0:
            port_b = UP_MAP0[0];
            long_delay_us(value2) ;
                  break;
            case 1:
            port_b = UP_MAP0[1];
            long_delay_us(value3);
                  break;
           case 2:
           port_b = UP_MAP0[2];
           long_delay_us(value4);
                 break;
           case 3:
           port_b = UP_MAP0[3];
            long_delay_us(value5);
                 break;    }cnt++;}}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jan 13, 2006 10:03 pm     Reply with quote

The problem is that you need to tell the compiler to use a data type
that is large enough to hold the intermediate result of your equations.

In CCS, a "long" data type is a 16-bit unsigned value. The largest
value it can hold is 65535. If you look at your equations, all of
the results of the multiplication are larger than 65535. So, the
results will be truncated.

Run the following program. Notice what the output values are.
Then change the declaration of "k" to be an "int32". Re-compile
and run the program again. See the improvement.
Code:
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

//===============================
void main()
{
char k=10;

long value1,value2,value3,value4,value5;

value1=(6588*k)/180;
printf("value1 = %LU\n\r", value1);

value2=(20628*k)/180;
printf("value2 = %LU\n\r", value2);

value3=(17982*k)/180;
printf("value3 = %LU\n\r", value3);

value4=(17118*k)/180;
printf("value4 = %LU\n\r", value4);

value5=(55368*k)/180;
printf("value5 = %LU\n\r", value5);

while(1);
}


----------
Edited to change 65536 to the actual max value of 65535.


Last edited by PCM programmer on Fri Jan 13, 2006 11:15 pm; edited 1 time in total
PIC



Joined: 13 Jan 2006
Posts: 3
Location: Malaysia

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

PostPosted: Fri Jan 13, 2006 10:51 pm     Reply with quote

thank you so much ,I grateful for your help ,it work fine thank you so much again

regards
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