Phase sequence detection

Discussion in 'Programmer's Corner' started by mitko89, Nov 14, 2014.

  1. mitko89

    Thread Starter Member

    Sep 20, 2012
    123
    19
    Hello guys,
    I was asked to design a phase sequence detector, which utilizes low end uC (pic10f200). Below is my code:
    Code (Text):
    1.  
    2. /*#############################################################################
    3. * Phase Sequence Detector
    4. ############################################################################ */
    5. #include <xc.h>
    6. #include <stdint.h>
    7.  
    8. /***** CONFIGURATION *****/
    9. #pragma config MCLRE=OFF
    10. #pragma config CP=OFF
    11. #pragma config WDTE=OFF
    12.  
    13. /*#############################################################################
    14. * Functions
    15. ############################################################################ */
    16.  
    17. /*#############################################################################
    18. * timer0_init ()
    19. * Usage - Initializes the TMR0 module for future use.
    20. ############################################################################ */
    21. void timer0_init (void){
    22.     OPTION = 0b11010110; // Timer mode with Prescaler 64
    23.                          // Timer period = 4MHz/(4*128) = 7812Hz
    24.                          // Period = 128us
    25.                          // 20ms/128ms = 156,25 for 1 period
    26.                          // 156,25/20 = 7,8125 ticks for 1ms
    27. }
    28.  
    29. /*#############################################################################
    30. * timer0_start ()
    31. * Usage - Clears and starts the timer.
    32. ############################################################################ */
    33. void timer0_start (void){
    34.     TMR0 = 0b00000000;  // Reset and start the Timer
    35. }
    36.  
    37. /*#############################################################################
    38. * Debounce  ()
    39. * Usage - Filters the edge detections.
    40. ############################################################################ */
    41. uint8_t debounce (uint8_t PortMask){
    42.     uint8_t counter = 0;
    43.     while( PortMask == (GPIO & PortMask) && counter<15 )
    44.     {
    45.         counter++;
    46.     }
    47.     return (counter > 10)?(1):(0);
    48. }
    49. /*#############################################################################
    50. * div_uint_by5  ()
    51. * Usage - Optimized division by constant for the particular task. (max divident
    52. * is expected to be less than 80)
    53. ############################################################################ */
    54. uint8_t udiv (uint8_t divider){
    55.     uint8_t divident = TMR0;
    56.     uint8_t counter = 0;  
    57.     while(divident)
    58.     {
    59.         divident -= divider;
    60.         counter++;
    61.     }
    62.     return counter;
    63. }
    64.  
    65.  
    66. void main(){
    67.  
    68.     volatile uint8_t Margin = 0; // varriable to store the duration of the pulse on GP1
    69.                                      // and the 20% margin
    70.     volatile uint8_t PortCurrentState = 0;
    71.     volatile uint8_t PortPreviousState = 0;
    72.     volatile uint8_t Flag = 0;
    73.     const uint8_t Phase2 = 51;
    74.     const uint8_t Phase3 = 102;
    75.     const uint8_t Phase1 = 153;
    76.     volatile uint8_t temp2 = 0;
    77.  
    78.     OPTION = 0b11010110;
    79.     TRISGPIO = 0b11111011;// Initialize GPIO port
    80.  
    81.     GPIO = _GPIO_GP2_MASK;// Turn the LED on for error;
    82.  
    83.     // Get the pulse width for initial calculations
    84.     do{
    85.          PortCurrentState = GPIO;
    86.          if( (PortPreviousState & _GPIO_GP0_MASK)==0x00 && (PortCurrentState & _GPIO_GP0_MASK)!=(PortPreviousState & _GPIO_GP0_MASK) )
    87.          {
    88.             if( debounce(_GPIO_GP0_MASK) )
    89.             {
    90.                 TMR0 = 0b00000000;
    91.                 if(Margin)
    92.                 {
    93.                     break;
    94.                 }
    95.             }
    96.          }
    97.          if( (PortPreviousState & _GPIO_GP0_MASK)==_GPIO_GP0_MASK && (PortCurrentState & _GPIO_GP0_MASK)!=(PortPreviousState&_GPIO_GP0_MASK) )
    98.          {
    99.             if( debounce(_GPIO_GP0_MASK) )
    100.             {
    101.                Margin = udiv(5); // calculate the 20% margin
    102.             }
    103.          }
    104.         PortPreviousState = PortCurrentState;
    105.     }while(1);
    106.  
    107.     while(1)
    108.     {
    109.         switch( udiv(Phase2)+1 )
    110.         {
    111.             case 1 :
    112.             {
    113.                 temp2 = Phase2 + Margin;
    114.                 while( TMR0<temp2 )
    115.                 {
    116.                     if( debounce(_GPIO_GP1_MASK) )
    117.                     {
    118.                         Flag |= _GPIO_GP1_MASK;
    119.                         break;
    120.                     }
    121.                 }
    122.             }
    123.             break;
    124.             case 2 :
    125.             {
    126.                 temp2 = Phase3 + Margin;
    127.                 while( TMR0<temp2 )
    128.                 {
    129.                     if( debounce(_GPIO_GP3_MASK) )
    130.                     {
    131.                         Flag |= _GPIO_GP3_MASK;
    132.                         break;
    133.                     }
    134.                 }
    135.             }
    136.             break;
    137.             case 3 :
    138.             {
    139.                 temp2 = Phase1 + Margin;
    140.                 while( TMR0<temp2 )
    141.                 {
    142.                     if( debounce(_GPIO_GP0_MASK) )
    143.                     {
    144.                          Flag |= _GPIO_GP0_MASK;                      
    145.                          if( Flag == 0x0B )
    146.                          {
    147.                             TMR0 = 0b00000000;
    148.                             Flag = 0x00;
    149.                             GPIO = ~_GPIO_GP2_MASK;
    150.                          }
    151.                          break;
    152.                     }
    153.                 }
    154.             }
    155.             break;
    156.             case 4 :
    157.             {
    158.                 temp2 = Phase1 + Margin;
    159.                 if(TMR0>temp2)
    160.                 {
    161.                     GPIO = _GPIO_GP2_MASK;
    162.                     Flag = 0x00;
    163.                     TMR0 = 0b00000000;
    164.                 }
    165.             }
    166.             break;
    167.         }
    168.     }
    169. }
    170.  
    But I can't quite find where's my mistake. The code compiles and the logic seems right to me, but failed on the proteus test. Can you help me out with some suggestions on where the problem is or how to improve the algorith? Thank you!
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,797
    Can you tell us what the device is supposed to do, and can you elaborate on your criteria for failing the proteus test?
     
  3. mitko89

    Thread Starter Member

    Sep 20, 2012
    123
    19
    The device is supposed to provide indication (GP2 will be on in case of error) if the phases of a 3 phase power supply (50Hz) are swapped/removed (the assignment of the phases is GP0 - P1, GP1-P2,GP3-P3). The input signals are pulses generated by MOSFETs.
    The algorith is: Initialize (set IOs & Prescaler for the timer and set the led to error state (on) ) -> in the do-while I measure the pulse width of the 1st phase and then calculate the 20% margin for the phases -> Main loop, which uses switch statement, which is branched according to the value in the timer ( I reset the timer before entering the main loop on the rising edge of P1, which means I expect the rising edge of the 2nd phase to arrive in 6ms (51 ticks of the timer, but no later than 51+ the 20% of pulse width). The signal is debounced and if it happens to pass the debounce test I set a bit in the Flag variable. This happens for phases 2,3 and 1. At case 3 (the case for phase 1), if the phase passes the debounce test I check the value of the Flag variable. If all the phases have arrived (and arrived in time) I clear the error (turn the led off), clear the Flag variable and reset the timer. If the phases arrive at time, the program should never branch to case 4, but I added it in case phase 1 doesn't arrive in time (it turns on the error led, clears the Flag and resets the timer).
    By failing the Proteus test I mean: I drawed the schematic in proteus, loaded the code in the simulation model of the pic and used pulse signal generators to feed the pic the correct phase sequence. Although the signals were in the correct sequence, the device shows there is an error (led is on). I'm currently debugging it by removing pieces of the code, but yet I haven't found where is the problem. There is no debugger for this processor and Proteus has no debugger for XC8... It's quite frustrating to debug such tiny devices.
     
  4. Papabravo

    Expert

    Feb 24, 2006
    10,163
    1,797
    I'm guessing you have no spare pins to help with the debugging process. My long experience in embedded systems suggests that you must increase the observability of your process. Since you are still at the prototype stage, my recommendation is to debug your code on a larger (more pins), but similar processor. A serial port connected to a PC with a terminal emulator will do wonders for insight into the possible causes. When you have working code you can return to the limited part with a higher degree of confidence.

    At least you don't have to put up with the ECB ( Edit, Compile, Burn) cycle, where each iteration produces a chiclet.*

    *chiclet -- 1. A throwaway one-time programmable chip. Also, 2. A small piece of gum or candy.

    You need to make certain that edges are detected when they are there, and not detected when they are absent. You need to make sure that after detecting an event that all of your hardware is configured to detect the next event. You must ensure that you can recover from various error conditions including unexpected timer overflow. Since you are not using interrupts I would also check for loops that require a single specific condition to exit. It should be a rule that all loops require 2 or more conditions to exit and that none of the loops take longer than some critical period to execute.
     
    Last edited: Nov 14, 2014
  5. mitko89

    Thread Starter Member

    Sep 20, 2012
    123
    19
    Ok, I'll test it on something like pic16 (that's what I have available). Does my algorith seem correct to you? Can you give me any suggestions on how to improve it?
     
Loading...