8051 C Programming Bug

Discussion in 'Embedded Systems and Microcontrollers' started by Seehat, Jun 10, 2014.

  1. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    Hello Everyone
    I'm trying to get a servo motor turn from 0 to 180degree. and back for the same it requires me to send a a different pulse for different positions of the motor. for 0 position it requires 600us and for 180 position requires 2400us approx. and their should be a 20ms gap between two pulses.
    For that i have created a 0.1ms timer delay and counting for 200 times before the swithes. so it creates a 20ms gap between pulses.

    but it is not working. i had it working perfectly but now it's messed up. and i dont know why.


    Code ( (Unknown Language)):
    1. #ifndef A3_XX_h
    2. #define A3_XX_h
    3.  
    4. //--------------------------------------------------------------------------------------------------------------------
    5. //                              Global Variables
    6. //--------------------------------------------------------------------------------------------------------------------
    7. sbit PB1 = P1 ^ 0;                           // Pushbutton PB1
    8. sbit PB2 = P1 ^ 1;                           // Pushbutton PB2
    9. sbit PB3 = P1 ^ 2;                           // Pushbutton PB3  
    10. sbit PB4 = P1 ^ 3;                           // Pushbuttom PB4
    11. sbit PB5 = P1 ^ 4;                           // Pushbutton PB5
    12. sbit PB6 = P1 ^ 5;                           // Pushbutton PB6
    13. sbit PB7 = P1 ^ 6;                           // Pushbutton PB7  
    14. sbit PB8 = P1 ^ 7;                           // Pushbuttom PB8
    15.  
    16. sbit SMODE = P3 ^ 7;                                                // Pushbutton on F120 development board
    17.  
    18. sbit LD0 = P2 ^ 0;                         // LD0  
    19. sbit LD1 = P2 ^ 1;                         // LD1
    20. sbit LD2 = P2 ^ 2;                         // LD2
    21. sbit LD3 = P2 ^ 3;                         // LD3  
    22. sbit LD4 = P2 ^ 4;                         // LD4  
    23. sbit LD5 = P2 ^ 5;                         // LD5
    24. sbit LD6 = P2 ^ 6;                         // LD6
    25. sbit LD7 = P2 ^ 7;                         // LD7    
    26.  
    27.  
    28. sfr     LCD = 0xB0;
    29.  
    30. sbit DB4     = P3 ^ 0;                                                    // LCD Pins
    31. sbit DB5     = P3 ^ 1;
    32. sbit DB6     = P3 ^ 2;
    33. sbit DB7     = P3 ^ 3;
    34. sbit RS     = P3 ^ 4;
    35. sbit RW     = P3 ^ 5;
    36. sbit E      = P3 ^ 6;
    37. sbit BL     = P3 ^ 7;
    38.  
    39. sbit USonicTX        = P0 ^ 2;                                            // TX for ultrasonic
    40. sbit USonicRX        = P0 ^ 3;                                            // RX
    41. sbit Servo_Ctrl = P0 ^ 4;                                            // Servo control pin
    42.  
    43.  
    44. //--------------------------------------------------------------------------------------------------------------------
    45. //                              Function prototypes
    46. //--------------------------------------------------------------------------------------------------------------------
    47. void main(void);
    48. void General_Init(void);
    49. void Timer2_Init();
    50. void Interrupts_Init();
    51. void Timer2_ISR (void);
    52. void PCA_Init(void);
    53. void delay(unsigned int);
    54. void Wavegenerator();
    55. void lcd_init();
    56. void toggle();
    57. void sweep();
    58. void delaytimer2();
    59. #endif  
    Code ( (Unknown Language)):
    1. #include <c8051f120.h>     // SFR declarations
    2. #include "A3_XX.h"
    3.  
    4. int angle;
    5. int count;
    6. int k=0;
    7. int i;
    8. int j;
    9. /*--------------------------------------------------------------------------------------------------------------------
    10.         Function:         Main Routine
    11. --------------------------------------------------------------------------------------------------------------------*/
    12. void main(void)
    13. {  
    14.     //USonicTX  = 0xff;
    15.     //P2 = 0xff;
    16.     SFRPAGE   = CONFIG_PAGE;
    17.     //OSCICN    = 0x83;                    // Need a faster clock....24.5MHz selected
    18.     General_Init();
    19.     Timer2_Init();    
    20.     Interrupts_Init();
    21.  
    22.     while(1)
    23.     {  
    24.       sweep();
    25.      }
    26. }
    27.  
    28. /*--------------------------------------------------------------------------------------------------------------------
    29.         Function:         General_Init
    30. --------------------------------------------------------------------------------------------------------------------*/
    31. void General_Init()
    32. {
    33.     WDTCN = 0xde;
    34.     WDTCN = 0xad;
    35.     SFRPAGE = CONFIG_PAGE;
    36.     XBR2 = 0x40;
    37.     XBR0 = 0x80;
    38.     XBR1 = 0x03;
    39.     P0MDOUT = 0x10;        // NOTE: Pushpull required for Servo control OTHERWISE TOO WEAK TO DRIVE PROPERLY SINCE ONLY 3.3V!!!!
    40.     P1MDOUT = 0x00;        // Ensure not pushpull outputs....this could damage microcontroller...
    41.     P2MDOUT = 0xff;        // Need to make pushpull outputs to drive LEDs properly
    42.  
    43.  
    44.     Servo_Ctrl = 0;    // Initialise servo control pin to 0
    45.  
    46.    
    47. }
    48.  
    49. /*--------------------------------------------------------------------------------------------------------------------
    50.         Function:         Timer_Init
    51. --------------------------------------------------------------------------------------------------------------------*/
    52. /*void Timer2_Init()
    53. {   //Timer 2 Initialisation For Servo motor control//
    54.     SFRPAGE   = TMR2_PAGE;
    55.     TMR2CN    = 0x04;
    56.     RCAP2L    = 0x3C;
    57.     RCAP2H    = 0xF6;
    58.    
    59. }*/
    60.  
    61. /*--------------------------------------------------------------------------------------------------------------------
    62.         Function:         Interrupts_Init
    63. --------------------------------------------------------------------------------------------------------------------*/
    64. void Interrupts_Init()
    65. {
    66.     IE        = 0xA0;
    67.     EIE2      = 0x04;
    68. }
    69.  
    70. /*--------------------------------------------------------------------------------------------------------------------
    71.         Function:         Timer2_ISR
    72. --------------------------------------------------------------------------------------------------------------------*/
    73. void Timer2_ISR (void) interrupt 5
    74. {          
    75.  
    76.            unsigned char SFRPAGE_SAVE = SFRPAGE;        // Save Current SFR page
    77.            
    78.             count++;
    79.  
    80.                                    if(count==angle)
    81.                                    Servo_Ctrl=0;
    82.                                            
    83.                                 if(count==200)
    84.                                   {
    85.                                        Servo_Ctrl=1;
    86.                                        count=0;
    87.                                    }
    88.  
    89.                               TF2 = 0;    // Reset interrupt flag
    90.                
    91.              SFRPAGE = SFRPAGE_SAVE;
    92.                                                            // Restore SFR page
    93. }
    94.  
    95. /*--------------------------------------------------------------------------------------------------------------------
    96.         Function:         Delay_0.1s
    97. --------------------------------------------------------------------------------------------------------------------*/
    98. void delay(unsigned int time)
    99.  {
    100.      
    101.    unsigned int y,j;
    102.    for(y=0;y<time;y++)
    103.     for(j=0;j<141;j++);
    104.  }
    105.  
    106.  
    107.  /*--------------------------------------------------------------------------------------------------------------------
    108.         Function: Lcd_init
    109. --------------------------------------------------------------------------------------------------------------------*/
    110. /*void lcd_init()
    111. {
    112. RS = 0; RW = 0; DB7 = 0; DB6 = 0; DB5 = 1; DB4 = 0;
    113. toggle();
    114. delay(5);
    115.  
    116. RS = 0; RW = 0; DB7 = 0; DB6 = 0; DB5 = 1; DB4 = 0;
    117. toggle();
    118. delay(5);
    119.  
    120. RS = 0; RW = 0; DB7 = 0; DB6 = 0; DB5 = 1; DB4 = 0;
    121. toggle();
    122. delay(5);
    123.  
    124. RS = 0; RW = 0; DB7 = 0; DB6 = 0; DB5 = 0; DB4 = 0;
    125. toggle();
    126. delay(5);
    127.  
    128. RS = 0; RW = 0; DB7 = 1; DB6 = 1; DB5 = 1; DB4 = 0;
    129. toggle();
    130. delay(5);
    131.  
    132. RS = 0; RW = 0; DB7 = 0; DB6 = 0; DB5 = 0; DB4 = 0;
    133. toggle();
    134. delay(5);
    135.  
    136. RS = 0; RW = 0; DB7 = 0; DB6 = 1; DB5 = 1; DB4 = 0;
    137. toggle();
    138. delay(5);
    139.  
    140. RS = 1; RW = 0; DB7 = 0; DB6 = 1; DB5 = 0; DB4 = 1;
    141. toggle();
    142. delay(5);
    143.  
    144. RS = 1; RW = 0; DB7 = 0; DB6 = 0; DB5 = 1; DB4 = 1;
    145. toggle();
    146. delay(5);
    147.  
    148. }*/
    149.  
    150. /*--------------------------------------------------------------------------------------------------------------------
    151.         Function: toggleE
    152. --------------------------------------------------------------------------------------------------------------------*/
    153.  
    154. /*void toggle()
    155. {
    156.  
    157.  if(E==0){
    158.  E=1;}
    159.  
    160.  else
    161.  E=0;
    162.  
    163. }*/
    164.  
    165. /*--------------------------------------------------------------------------------------------------------------------
    166.         Function: Sweep
    167. --------------------------------------------------------------------------------------------------------------------*/
    168.  
    169. void sweep()
    170. {          
    171.             int x=6;
    172.             angle=15;
    173.             delaytimer2();
    174.            
    175.  
    176.          while(1){                  
    177.            
    178.             angle=x;
    179.             delaytimer2();
    180.  
    181.             x=x+i;
    182.  
    183.             if(angle==24){
    184.                           i=-1;}
    185.             if(angle==6){
    186.                          i=1;}
    187.                   }
    188. }
    189.  
    190. /*--------------------------------------------------------------------------------------------------------------------
    191.         Function: Sweep 0.1ms
    192. --------------------------------------------------------------------------------------------------------------------*/
    193.  
    194. void delaytimer2()
    195. {
    196.  
    197. TR2 = 1;
    198. while (TF2 == 0);
    199. TR2 = 0;
    200. TF2 = 0;
    201. }
    202.  
     
  2. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    Bump ^ Bump
     
  3. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You are that impatient that you must bump after only 26 minutes?

    This is an international forum... The sun isn't even up yet here, yet you expect an answer? Bumping your thread makes some people not want to respond as it seems insistent and demanding of attention.

    With that said, how is it messed up? What bug have you made? What have you tried to debug the situation?
     
  4. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    You have made changes to your code and now it's messed up. Try to recall what changes you have made.


    Code ( (Unknown Language)):
    1.  
    2. void delaytimer2()
    3. {
    4.   TR2 = 1;
    5.   while (TF2 == 0);
    6.   TR2 = 0;
    7.   TF2 = 0;
    8. }
    9.  
    This code is not correct. Maybe you made some changes here.
     
  5. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    Sorry if that bump hit you mate. :D:D

    the Sweep() function i have made in this code with an interrupt is not working how i designed it to be. on the hardware it is stuck on the angle 6 position i.e. the to the left most on the servo motor.

    can you please have a look on the sweep function coming out of the main and then going into interrupt after each timer overflow.
     
  6. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    @Mrchips
    Yes bro..somewhere here....as i tried to omit the timer delay i messed it up and put in a software delay but as i tried to reverse it into original form i am not able to find the bug i have created.

    Lesson learned - don't touch a code that is working. put it as comments and then try something new. :mad:
     
  7. MrChips

    Moderator

    Oct 2, 2009
    12,442
    3,361
    Try replacing the delaytimer2( ) function with software delay loop.
     
  8. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You are using timer 2 as your interrupt source, yet you disable the timer in delaytimer2().

    Does the servo actively stay at the angle corresponding to 6, or does it move there and stop moving?
     
  9. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    @Mrchips
    It's not even working with soft delay mate. Anyway i need to use the timer delay acc. To the requirments of the program.

    @tshuck
    Hmm.. Does giving a zero value to TR2 bit will disable the interrupt there only in delaytimer2 loop ?
    Yeah it goes to angle corresponding to 6 and stop moving. I can feel the motor pushing more towards less than 6 angles.
     
  10. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    According to the datasheet I'm looking at, setting TR2=0 means the counter will not count, so no overflow/interrupt could happen, it shouldn't disable the interrupt, just the interrupt will never come.

    So the servo is actively attempting to move beyond mechanical limitations? If so, your controller is sending something, just not a 1-2ms. pulse every 20ms. Do you have an oscilloscope at all? Perhaps you should try one of those sound card based oscilloscope programs to check your signals if you don't have one...
     
  11. Seehat

    Thread Starter New Member

    Oct 14, 2013
    12
    0
    Sorry man yes i have an oscilloscope and it is a weird signal with a peak to peak gap of 4.9ms. Yeah you might be correct. But the signal on oscilloscoscope is weird surely
     
Loading...