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

math equation for time delay
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

math equation for time delay
PostPosted: Tue May 29, 2012 7:07 am     Reply with quote

hello,

I am calculating several values for time delay for input output port. From the coding below, I set two conditions which are if the x is even number, s=4500 and if x=odd, s =5000. There is no error when i'm run the code programming, but when i load it into pic simulation it doesn't show any result (the led isn't blinking). Besides, I also want to know how can I display the value of t1,t2,t3,t4,t5 in my simulation instead of using the lcd. Anyone please help me. Thank you very much.

Code:

#include <18F4550.h>
#fuses XT,NOWDT,NOLVP,NOPROTECT
#use delay (clock=4M)
#include <math.h>


#define LED1   PIN_B0
#define LED2   PIN_B1
#define LED3   PIN_B2
#define LED4   PIN_B3
#define LED5   PIN_B4
#define LED6   PIN_B5

unsigned int i,x;
int s,w,fm,Vm,dv,t0,t1,t2,t3,t4,t5;

void main()
{
   set_tris_b(0x00);
   output_b(0x00);
   
 while(TRUE);
 {
   x=i%2;
 
  if(x==0)
  {
   s=4500;
  }
  else if(x!=0)
  {
   s=5000;
  }
   
  dv=1;
  Vm=10;
  fm=30;
  w=2*3.142*fm;
  t0=0;
   
  t1=t0+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t0))))));
  t2=t1+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t1))))));
  t3=t2+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t2))))));
  t4=t3+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t3))))));
  t5=t4+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t4))))));
 
       output_high(PIN_B0);
      delay_ms(t0);
      output_low(PIN_B0);
      delay_ms(t1);
      output_high(PIN_B0);
      delay_ms(t2);
      output_low(PIN_B0);
      delay_ms(t3);
      output_high(PIN_B0);
      delay_ms(t4);
      output_low(PIN_B0);
      delay_ms(t5);
     
      output_high(PIN_B1);
      delay_ms(t0);
      output_low(PIN_B1);
      delay_ms(t1);
      output_high(PIN_B1);
      delay_ms(t2);
      output_low(PIN_B1);
      delay_ms(t3);
      output_high(PIN_B1);
      delay_ms(t4);
      output_low(PIN_B1);
      delay_ms(t5);
     
      output_high(PIN_B2);
      delay_ms(t0);
      output_low(PIN_B2);
      delay_ms(t1);
      output_high(PIN_B2);
      delay_ms(t2);
      output_low(PIN_B2);
      delay_ms(t3);
      output_high(PIN_B2);
      delay_ms(t4);
      output_low(PIN_B2);
      delay_ms(t5);
     
     
     
      output_high(PIN_B3);
      delay_ms(t0);
      output_low(PIN_B3);
      delay_ms(t1);
      output_high(PIN_B3);
      delay_ms(t2);
      output_low(PIN_B3);
      delay_ms(t3);
      output_high(PIN_B3);
      delay_ms(t4);
      output_low(PIN_B3);
      delay_ms(t5);
   
      output_high(PIN_B4);
      delay_ms(t0);
      output_low(PIN_B4);
      delay_ms(t1);
      output_high(PIN_B4);
      delay_ms(t2);
      output_low(PIN_B4);
      delay_ms(t3);
      output_high(PIN_B4);
      delay_ms(t4);
      output_low(PIN_B4);
      delay_ms(t5);
     
      output_high(PIN_B5);
      delay_ms(t0);
      output_low(PIN_B5);
      delay_ms(t1);
      output_high(PIN_B5);
      delay_ms(t2);
      output_low(PIN_B5);
      delay_ms(t3);
      output_high(PIN_B5);
      delay_ms(t4);
      output_low(PIN_B5);
      delay_ms(t5);
   }
   
}
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Tue May 29, 2012 7:31 am     Reply with quote

So this works in real hardware, but not in your simulator? It sounds like a simulator problem. I don't see where you tell us which simulator you are using. You would probably do better to ask at a web site for the simulator. I gave up using simulators years ago.
_________________
The search for better is endless. Instead simply find very good and get the job done.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue May 29, 2012 7:33 am     Reply with quote

You don't need to set TRIS in CCS, the compiler sorts that out for you.

For debugging use MPLAB SIM.

It's free.

Set up a watch window for all your variables, and single step through the code. No need for the LEDs or the delay_ms(xx).

As you step through, you SHOULD be able to see where you're going wrong.

All your variables are ints, but cos & sin require fp, could be a problem. You may have to use casts to get the results you're looking for.

Mike
ckielstra



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

View user's profile Send private message

PostPosted: Tue May 29, 2012 7:40 am     Reply with quote

Code:
x=i%2;
i never gets a value, so the result will be random.

Code:
s=4500;
In the 8-bit PIC an integer is by default 8-bits too. What is the maximum value that will fit into an unsigned 8-bit variable?

Code:
if(x==0)
{
    ...
}
else if(x!=0)
{
    ...
}
When x is not zero in the first if-statement, then it will always be !=0 in the second if statement. You are making your code more complex then necessary and can remove the second if-statement:
Code:
if(x==0)
{
    ...
}
else
{
    ...
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue May 29, 2012 8:28 am     Reply with quote

Quote:
There is no error when i'm run the code programming,but when i load it into pic simulation it doesn't show any result (the led isn't blinking).


By this, do you mean that your code COMPILES without error, but does not work in your anonymous simulator?

Mike
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Tue May 29, 2012 8:41 am     Reply with quote

Quote:
You don't need to set TRIS in CCS, the compiler sorts that out for you.


if i don't use tris, then how come the port B could set as output port??

Quote:
By this, do you mean that your code COMPILES without error, but does not work in your anonymous simulator?


yes,this what i mean.the problem like this is more difficult when there is no error but the code doesn't work.. Sad


Quote:
i never gets a value, so the result will be random.


i could be any numbers,in order to determine if there is balance if any number is divided by 2..if there is no balance,then it will be even,if not it will be odd number..however, if this line is wrong, what should i change to make it better?

Quote:

Code:
s=4500;
In the 8-bit PIC an integer is by default 8-bits too. What is the maximum value that will fit into an unsigned 8-bit variable?


i didn't understand what do you mean by this..if you can make it more clear i am very thankful.. Smile


Quote:
So this works in real hardware, but not in your simulator? It sounds like a simulator problem. I don't see where you tell us which simulator you are using. You would probably do better to ask at a web site for the simulator. I gave up using simulators years ago.


As you mentioned it probably because of the simulator problem nut this is also does not work in real hardware..i am currently using proteus simulator.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue May 29, 2012 9:04 am     Reply with quote

Lesson number 1.

FORGET PROTEUS. It's so full of bugs you're wasting your time with it.


With the CCS, the compiler automatically works out from your code what your TRIS states need to be. YOU don't have to do it.

If there are no syntax (etc.) mistakes in your code, it will compile without errors. When programmed the PIC will do what it is told to do. You give it nonsense code, it will do exactly that, nonsense actions.

Something like
Code:
i = 0;

will force i to be zero.

The maximum value for an unsigned 8 bit variable is 2^8 - 1.


Mike
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Tue May 29, 2012 9:29 am     Reply with quote

I had try as you told which is by eliminating the tris in coding. I try it in common blinking LED code programming (not the above code programming) and of course it doesn't have the error after compile it. But the LED do the nonsense actions which doesn't blinking (I try it in real hardware not in full bugs Proteus simulator). So, I don't understand when you said the compiler will automatically do what the TRIS states need to be. Thank you Mike for your lesson. Smile

Quote:
All your variables are ints, but cos & sin require fp, could be a problem. You may have to use casts to get the results you're looking for.


what is fp as you mentioned above?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue May 29, 2012 10:34 am     Reply with quote

fp is short for floating point arithmetic, as opposed to integer arithmetic.

I've just tried this code out on my PICDEM 2 PLUS board
Code:
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

void main()
{
   while (1)
   {
     output_high(PIN_B0);
     delay_ms(250);
     output_low(PIN_B0);
     delay_ms(250);
   }
}


I've used a PIC16F877 because I don't have your device, and that's what happened to be plugged in at the time.

The LED flashes at 2Hz.

I'm using MPLAB on REAL hardware, the code is complete, nothing hidden, nothing up my sleeves.

Mike
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 29, 2012 5:45 pm     Reply with quote

Mike's program, with appropriate changes (PIC and fuses) does work on an 18F4550...on,off,on,off...etc...
Jay
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Wed May 30, 2012 5:06 am     Reply with quote

Ok mike, i had try it again this morning in my lab. Yes, you are right. Even with no tris the code for common led blinking is work out. Perhaps last night i did not burn my micro-c properly. By the way, thanks for the new knowledge.Smile

Isn't the PIC C compiler compatible with MPLAB? If yes, how can i open a watch window to see variables?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed May 30, 2012 6:01 am     Reply with quote

Pleased to help, and be aknowledged.

When you install MPLAB it should find the appropriate complier files.

OR

If you install a compiler after MPLAB the compiler install searches for MPLAB.

If neither works you may have to tell MPLAB where to find the compliers, (near the bottom of the 'Project' pull down menu).

It's such a long time since I did mine I've forgotten how it's done.

Watch windows are in the pull down 'View' menu.

Mike
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Wed May 30, 2012 10:57 am     Reply with quote

I am very not familiar with MPLAB. I don't know how to debug with MPLAB and also if I can open a watch window but how to use it? How it function?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed May 30, 2012 1:09 pm     Reply with quote

This is not an MPLAB forum. Google for this, and you will find many
webpages that explain how to do it:
Quote:

MPLAB watch window tutorial
chibi41



Joined: 16 Apr 2012
Posts: 12

View user's profile Send private message

PostPosted: Wed May 30, 2012 1:27 pm     Reply with quote

Quote:
This is not an MPLAB forum. Google for this, and you will find many
webpages that explain how to do it:
Quote:

MPLAB watch window tutorial


LOL..actually MPLAB is not the main issues here..the issue is i didn't understand what is the mistake in my programming that i post earlier.. i don't how the MPLAB is being dragged..LOL..sorry about that. Confused
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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