Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

math equation for time delay

Status
Not open for further replies.

chibi41

New Member
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 and i am using PIC C compiler..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); 
      
   } 
    
    
}
 
Frankly, you don´t really help us with the intended purpose of this code, especially with no comments what so ever in the code, and I don´t really feel like going through the code to figure everything out.

It looks like you want to generate some sinewaves. What strikes me are t0..t5 terms, why are you chaining them like that? What waveform are you actually trying to generate?
 
ohh...sorry about that..

i am manually generate PWM signal through the output port.Usually, people generate pwm signals through the CCP pins but the PWM that will be generated is Sinusoidal PWM(SPWM) isn't it?

But my pwm is dual slope pwm(DSPWM) which is i can modified for every time switching (t0...t5).the time switching here would be high and low. If the ordinary pwm such as SPWM the time switching can't be modified isn't it but DSPWM is possible. That is why i used output port to generate PWM because as we know at output port can produce digital signal and it is almost the same characteristic with pwm signal..

But my problem is that the code above doesnt work out and the oscilloscope did not display any signals.
 
make sure the port is set as digital not analogue. also check if internal pull ups are on or off. do you get any output on the pins? is pic c the hitech compiler? i dont know it i use C18,
 
Last edited:
yes, it was already set in digital output. But i did not understand how to check if internal pull up are on or off? i didn;t get any ouput at the pins,that was problem now.before this i am manually calculate the values of 't' and write it in time delay and it is work. But when i programme the algorithm to make it automatically calculated, it doesn't work.
 
i dont know that compiler but looking at the datasheet PORTB pins 0-4 start up as analogue inputs i see you have set tris as output but have you set theese as digital pins
 
sorry you beat me to post
 
port b is a bad port lol it even has the usb on it!! i think you will need to read the DS on port and check each register in the watch window to see if its set properly its start from page 116
 
i am not sure about that but if i am not mistaken, if we didn't set it as the analog output or input then the pins will automatically set as digital output.:)
 
port b is a bad port lol it even has the usb on it!! i think you will need to read the DS on port and check each register in the watch window to see if its set properly its start from page 116

has usb on it?bad port?isn't the usb port is on pins 18 and i think all the port is same as long as they were input/ouptut port.
 
yeah what i meant was port B has alot going on, some the port b pins are set as analogue input on start up .
Each of the PORTB pins has a weak internal pull-up. A
single control bit can turn on all the pull-ups. This is
performed by clearing bit, RBPU (INTCON2<7>).

On a Power-on Reset, RB4:RB0 are
configured as analog inputs by default and
read as ‘0’; RB7:RB5 are configured as
digital inputs.
By programming the Configuration bit,
PBADEN (CONFIG3H<1>), RB4:RB0 will
alternatively be configured as digital inputs
on POR.

thats all from the data sheet
so check in the watch window
 
How can 't0' start at zero?..... Every calculation from that point will result in zero..... Anything * 0 = 0...

Code:
   t0=0; 
   t1=t0+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t0))))));



t1 will = 0.

yes,you are right anything *0=0..but cos 0 =1..am i right? t0 is start with zero because it start at origin which is t=0.the it follows with another 't' which are t1,t2,t3,t4,t5. the value of t will be determine the period of the switching pulses..
 
yeah what i meant was port B has alot going on, some the port b pins are set as analogue input on start up .
Each of the PORTB pins has a weak internal pull-up. A
single control bit can turn on all the pull-ups. This is
performed by clearing bit, RBPU (INTCON2<7>).

On a Power-on Reset, RB4:RB0 are
configured as analog inputs by default and
read as ‘0’; RB7:RB5 are configured as
digital inputs.
By programming the Configuration bit,
PBADEN (CONFIG3H<1>), RB4:RB0 will
alternatively be configured as digital inputs
on POR.

thats all from the data sheet
so check in the watch window

okay,i will try as you suggest then..thank you..:)
 
Should you increase the variable i somewhere? If it is incremented in an interrupt, you should define it as "volatile".
 
Should you increase the variable i somewhere? If it is incremented in an interrupt, you should define it as "volatile".

i just know that "volatile". Thus, then should define it like this.."volatile int i,x;"..?but i need "i" to be always positive..
 
i just know that "volatile". Thus, then should define it like this.."volatile int i,x;"..?but i need "i" to be always positive..

You can define it:
volatile unsigned int i, x;
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top