math equation for time delay

Discussion in 'Embedded Systems and Microcontrollers' started by ayu_ayfa, May 29, 2012.

  1. ayu_ayfa

    Thread Starter New Member

    Sep 26, 2011
    14
    0
    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 ( (Unknown Language)):
    1. #include <18F4550.h>
    2. #fuses XT,NOWDT,NOLVP,NOPROTECT
    3. #use delay (clock=4M)
    4. #include <math.h>
    5.  
    6.  
    7. #define LED1   PIN_B0
    8. #define LED2   PIN_B1
    9. #define LED3   PIN_B2
    10. #define LED4   PIN_B3
    11. #define LED5   PIN_B4
    12. #define LED6   PIN_B5
    13.  
    14.  
    15. unsigned int i,x;
    16. int s,w,fm,Vm,dv,t0,t1,t2,t3,t4,t5;
    17.  
    18. void main()
    19. {
    20.    
    21.    set_tris_b(0x00);
    22.    output_b(0x00);
    23.    
    24.    
    25.    while(TRUE);
    26.    
    27.    {
    28.    
    29.    x=i%2;
    30.  
    31.   if(x==0)
    32.   {
    33.  
    34.    
    35.    s=4500;
    36.   }
    37.  
    38.   else if(x!=0)
    39.   {
    40.    s=5000;
    41.   }
    42.  
    43.  
    44.    
    45.   dv=1;
    46.   Vm=10;
    47.   fm=30;
    48.   w=2*3.142*fm;
    49.   t0=0;
    50.  
    51.    
    52.    
    53.    
    54.   t1=t0+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t0))))));
    55.   t2=t1+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t1))))));
    56.   t3=t2+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t2))))));
    57.   t4=t3+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t3))))));
    58.   t5=t4+((2*dv)/(s+(((-1)^x)*(w)*(Vm)*(cos((w)*(t4))))));
    59.  
    60.    
    61.  
    62.  
    63.  
    64.        output_high(PIN_B0);
    65.       delay_ms(t0);
    66.       output_low(PIN_B0);
    67.       delay_ms(t1);
    68.       output_high(PIN_B0);
    69.       delay_ms(t2);
    70.       output_low(PIN_B0);
    71.       delay_ms(t3);
    72.       output_high(PIN_B0);
    73.       delay_ms(t4);
    74.       output_low(PIN_B0);
    75.       delay_ms(t5);
    76.    
    77.      
    78.      
    79.       output_high(PIN_B1);
    80.       delay_ms(t0);
    81.       output_low(PIN_B1);
    82.       delay_ms(t1);
    83.       output_high(PIN_B1);
    84.       delay_ms(t2);
    85.       output_low(PIN_B1);
    86.       delay_ms(t3);
    87.       output_high(PIN_B1);
    88.       delay_ms(t4);
    89.       output_low(PIN_B1);
    90.       delay_ms(t5);
    91.  
    92.      
    93.      
    94.      
    95.       output_high(PIN_B2);
    96.       delay_ms(t0);
    97.       output_low(PIN_B2);
    98.       delay_ms(t1);
    99.       output_high(PIN_B2);
    100.       delay_ms(t2);
    101.       output_low(PIN_B2);
    102.       delay_ms(t3);
    103.       output_high(PIN_B2);
    104.       delay_ms(t4);
    105.       output_low(PIN_B2);
    106.       delay_ms(t5);
    107.      
    108.      
    109.      
    110.       output_high(PIN_B3);
    111.       delay_ms(t0);
    112.       output_low(PIN_B3);
    113.       delay_ms(t1);
    114.       output_high(PIN_B3);
    115.       delay_ms(t2);
    116.       output_low(PIN_B3);
    117.       delay_ms(t3);
    118.       output_high(PIN_B3);
    119.       delay_ms(t4);
    120.       output_low(PIN_B3);
    121.       delay_ms(t5);
    122.      
    123.    
    124.    
    125.       output_high(PIN_B4);
    126.       delay_ms(t0);
    127.       output_low(PIN_B4);
    128.       delay_ms(t1);
    129.       output_high(PIN_B4);
    130.       delay_ms(t2);
    131.       output_low(PIN_B4);
    132.       delay_ms(t3);
    133.       output_high(PIN_B4);
    134.       delay_ms(t4);
    135.       output_low(PIN_B4);
    136.       delay_ms(t5);
    137.      
    138.      
    139.       output_high(PIN_B5);
    140.       delay_ms(t0);
    141.       output_low(PIN_B5);
    142.       delay_ms(t1);
    143.       output_high(PIN_B5);
    144.       delay_ms(t2);
    145.       output_low(PIN_B5);
    146.       delay_ms(t3);
    147.       output_high(PIN_B5);
    148.       delay_ms(t4);
    149.       output_low(PIN_B5);
    150.       delay_ms(t5);
    151.      
    152.    }
    153.    
    154.    
    155. }
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    If the "PIC C compiler" is compatible with MPLAB you can open a watch window to see variables. You can even "mouse over" and see them in a tooltip.

    You may also need stimulus file to set input pins to get a response.
     
    ayu_ayfa likes this.
  3. upand_at_them

    Active Member

    May 15, 2010
    246
    29
    One of the best lessons to learn in debugging something is to simplify, simplify, simplify. Get rid of the blinky code and just turn an LED on to see if it gets to that point.

    Also, your 'x' variable isn't initialized. Does this cause problems with your compiler?
     
    ayu_ayfa likes this.
  4. ayu_ayfa

    Thread Starter New Member

    Sep 26, 2011
    14
    0
    i am not familiar with MPLAB actually..but i will try it..by the way, what is it mean by stimulus file?
     
  5. ayu_ayfa

    Thread Starter New Member

    Sep 26, 2011
    14
    0
    Actually the main issues it not the LED blinking,but the digital signal produced on that pins.LED is just additional accessories act as indicator.but it seems like there is no signal display on oscilloscope and of course the LED doesn't tun ON. i am manually produce the PWM signals instead of using CCP PWM pins because i do not generate sinusoidal PWM but dual slope PWM. this PWM is not built in micro-c like sinusoidal PWM.

    is it 'unsigned int x' is not consider as initialize?if not, please tell me the better way..thank you :)
     
Loading...