View previous topic :: View next topic |
Author |
Message |
chibi41
Joined: 16 Apr 2012 Posts: 12
|
math equation for time delay |
Posted: Tue May 29, 2012 7:07 am |
|
|
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
|
|
Posted: Tue May 29, 2012 7:31 am |
|
|
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
|
|
Posted: Tue May 29, 2012 7:33 am |
|
|
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
|
|
Posted: Tue May 29, 2012 7:40 am |
|
|
i never gets a value, so the result will be random.
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
|
|
Posted: Tue May 29, 2012 8:28 am |
|
|
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
|
|
Posted: Tue May 29, 2012 8:41 am |
|
|
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..
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..
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
|
|
Posted: Tue May 29, 2012 9:04 am |
|
|
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
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
|
|
Posted: Tue May 29, 2012 9:29 am |
|
|
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.
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
|
|
Posted: Tue May 29, 2012 10:34 am |
|
|
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
|
|
Posted: Tue May 29, 2012 5:45 pm |
|
|
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
|
|
Posted: Wed May 30, 2012 5:06 am |
|
|
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.
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
|
|
Posted: Wed May 30, 2012 6:01 am |
|
|
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
|
|
Posted: Wed May 30, 2012 10:57 am |
|
|
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
|
|
Posted: Wed May 30, 2012 1:09 pm |
|
|
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
|
|
Posted: Wed May 30, 2012 1:27 pm |
|
|
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. |
|
|
|