+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Member level 5
    Points: 673, Level: 5

    Join Date
    Aug 2018
    Posts
    81
    Helped
    0 / 0
    Points
    673
    Level
    5

    Repeat Cycle using timer application

    I am using 4Mhz crystal with pic16F886 using timer interrupt 1ms. In below

    Here i could able to display set value and process value.During ontime Setvalue displayed on bottom Dissect(value) & process value on top Dissect_2(value) For Off time Setvalue displayed on TOp Dissect_2(value) & process value on bottomDissect(value)

    First cycle where first ontime with process value and second offtime value with process value work properly. Now this cycle has to repeat But in third cycle it will display on time and offtime Set value.But wont process the value.

    My working should be I have 2 time on time and Off time.It has been set initial stage. First on time come into picture. where bottom display show set value and top display show process value. when counter reached then top display show off time value as setted and bottom value as process value. cycle keep repeat

    My question is how can i solve below issue.

    Code C# - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    
    #include <htc.h>
    #include <stdio.h>
    #include <math.h>
    #include "delay.h"
    #define _XTAL_FREQ 4000000
     
    __CONFIG(FOSC_HS & WDTE_ON& PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF  & DEBUG_OFF&MCLRE_ON &BOREN_ON&FCMEN_ON &WRT_OFF);
    #define RLY_DRV1        RB4
    #define DISPCLK         RB5
    #define DISPDATA        RB6
    #define STROBE          RB7
    #define RLY_DRV2     RC0
    int Bottom_Value = 0;
    unsigned int Top_Display = 0;
    unsigned int Float_Value;
    unsigned int InG_value;
     
    unsigned int On_Time=0;
    unsigned int Off_Time=0;
    unsigned char Cylic_Flag=0;
     
    unsigned char LED_MS_Flag = 0;
    unsigned char LED_SeMi_Flag = 0;
    unsigned char LED_HM_Flag = 0;
    unsigned char ON_Prg_Flag = 0;
    unsigned char Off_Prg_Flag = 0;
    unsigned char RLY_Off_Flag=0;
    unsigned char On_Int_Flag=1;
     
    unsigned int Hour = 0;
    unsigned int Min = 0;
    unsigned int Sec = 0;
    unsigned int MSec = 0;
    const unsigned char DISPTABLE[27] = {0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x11,0x83,0x31,
                                         //0  //1  //2  //3  //4  //5  //6  //7  //8  //9  //A  //U  //P
                                         0xD1,0xE3,0x89,0x63,0xE1,0xF5,0xC1,0x61,0x71,0xF3,0xFD,0xD5,0x85,0xFF
                                        };
     
    unsigned char LEDBuffer_1[8];
     
    void Dissect(unsigned int Value) { // Spliting of process value in digits form
      unsigned char a, Temp;
     
      for (a = 8; a >= 5; a--) {
        Temp = Value % 10;
        Value = Value / 10;
        LEDBuffer_1[a - 1] = DISPTABLE[Temp];
     
      }
     
    }
    void Dissect_2(unsigned int Value) { // Spliting of process value in digits form
      unsigned char a, Temp;
      for (a = 4; a >= 1; a--) {
     
        Temp = Value % 10;
        Value = Value / 10;
        LEDBuffer_1[a - 1] = DISPTABLE[Temp];
     
      }
     
    }
     
    void RLY_CHG_CHK() {
      RLY_Off_Flag = 0;
      if (Sub_Relay == 1) {
        RLY_DRV2 = 1;
        if (Sub_Time == 1) {
          RLY_DRV1 = 0;
        } else if (Sub_Time == 2) {
          RLY_DRV1 = 1;
        } else if (Sub_Time == 3) {
          if (Cylic_Flag == 1) {
            RLY_DRV1 = 0;
          } else if (Cylic_Flag == 0) {
            RLY_DRV1 = 1;
          }
     
        }
     
      } else if (Sub_Relay == 2) { // 2 Change Over relay section
        if (Sub_Time == 1) {
          RLY_DRV2 = 0;
          RLY_DRV1 = 0;
        } else if (Sub_Time == 2) {
          RLY_DRV2 = 1;
          RLY_DRV1 = 1;
        } else if (Sub_Time == 3) {
          if (Cylic_Flag == 1) {
            RLY_DRV1 = 0;
            RLY_DRV2 = 0;
          } else if (Cylic_Flag == 0) {
            RLY_DRV1 = 1;
            RLY_DRV2 = 1;
          }
     
        }
     
      }
     
    }
     
    void Relay_OFF()
     
    {
      RLY_Off_Flag = 1;
      if (Sub_Relay == 1) { 
        RLY_DRV2 = 0;
        if (Sub_Time == 1) {
          RLY_DRV1 = 1;
        } else if (Sub_Time == 2) {
          RLY_DRV1 = 1;
        } else if (Sub_Time == 3) {
          if (Cylic_Flag == 1) {
            RLY_DRV1 = 0;
          } else if (Cylic_Flag == 0) {
            RLY_DRV1 = 1;
          }
     
        }
      } else
     
      if (Sub_Relay == 2) { 
        if (Sub_Time == 1) {
          RLY_DRV2 = 1;
          RLY_DRV1 = 1;
        } else if (Sub_Time == 2) {
          RLY_DRV2 = 0;
          RLY_DRV1 = 0;
        } else if (Sub_Time == 3) {
          if (Cylic_Flag == 1) {
            RLY_DRV1 = 1;
            RLY_DRV2 = 1;
          } else if (Cylic_Flag == 0) {
            RLY_DRV1 = 0;
            RLY_DRV2 = 0;
          }
     
        }
     
      }
     
    }
     
    void Msec_Function() {
      if (LED_MS_Flag == 1) {
     
        if (On_Int_Flag == 0) {
     
          if (Cylic_Flag == 0) {
     
            if (RLY_Off_Flag == 0) {
              //On_Time=2525;
              RLY_CHG_CHK();
            }
     
            LED_HM_Flag = 0; //min_Hour Flag
            LED_SeMi_Flag = 0; // Sec & min FLag
            LED_MS = 1;
            LED_SS = 0;
            LED_HM = 1;
            if (Sub_Time == 3) {
              Dissect_2(On_Time);
              Dissect(Bottom_Value);
            }
          }
          if (Cylic_Flag == 1) {
     
            if (RLY_Off_Flag == 0) {
              //Off_Time=3636;
              RLY_CHG_CHK();
            }
     
            LED_HM_Flag = 0; //min_Hour Flag
            LED_SeMi_Flag = 0; // Sec & min FLag
            LED_MS = 1;
            LED_SS = 0;
            LED_HM = 1;
            if (Sub_Time == 3) {
              Dissect_2(Top_Display);
              Dissect(Off_Time);
            }
            //Cylic_Flag=0;
          }
     
        }
      }
     
    }
     
    void Process_RUN_MODE() {
     
      if (LED_MS_Flag == 1) {
        Msec_Function();
      }
    }
     
    void main() {
      On_Time = 2525;
      Off_Time = 3535;
     
      Sub_Time = 3;
      Sub_Relay = 1;
      LED_MS_Flag = 1;
      On_Int_Flag = 0;
      Cylic_Flag = 0;
      while (1) {
        Process_RUN_MODE();
     
      }
     
    }
     
    void interrupt isr(void) {
     
      asm("clrwdt");
     
      if (TMR1IF) {
        TMR1IF = 0;
        TMR1H = 0xFE;
        TMR1L = 0x0C;
        if (On_Int_Flag == 0) {
     
          if (Cylic_Flag == 0) {
     
            if (On_Time > Bottom_Value) {
              if (LED_MS_Flag == 1) {
                Bottom_Value = (Sec * 1000) + MSec;
              } else if (LED_SeMi_Flag == 1) {
                Bottom_Value = (Min * 100) + Sec;
              } else if (LED_HM_Flag == 1) {
                Bottom_Value = (Hour * 100) + Min;
              }
     
            } else
     
            if (On_Time == Bottom_Value) {
              //Cylic_Flag=1;
              Relay_OFF();
              //Off_Time=3456;
              On_Int_Flag = 0;
              //Bottom_Value=0;
            }
     
          }
     
          if (Cylic_Flag == 1) {
     
            if (Off_Time > Top_Display) {
              if (LED_MS_Flag == 1) {
                Top_Display = (Sec * 1000) + MSec;
              } else if (LED_SeMi_Flag == 1) {
                Top_Display = (Min * 100) + Sec;
              } else if (LED_HM_Flag == 1) {
                Top_Display = (Hour * 100) + Min;
              }
     
            } else
     
            if (Top_Display == Off_Time) {
              //Cylic_Flag=0;
              Relay_OFF();
              On_Int_Flag = 0;
     
            }
     
          }
     
        }
     
        MSec++;
        if (MSec >= 1000) {
          MSec = 0;
          Sec = Sec + 1;
        } else if (Sec >= 59) {
          Sec = 0;
          Min = Min + 1;
        } else if (Min >= 59) {
          Min = 0;
          Hour = Hour + 1;
        } else if (Hour > 99) {
          Hour = 0;
        }
     
        Display();
      }
     
    }
    Last edited by KlausST; 22nd February 2020 at 09:08. Reason: Changed code view

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 82,053, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,652
    Helped
    3775 / 3775
    Points
    82,053
    Level
    69

    Re: Repeat Cycle using timer application

    Hi,

    I don't understsnd your code ... I'm not that experienced with C

    But generally the ISR code should be as short as possible.
    You do a lot of stuff in the ISR.
    Most of it can simply be processed in the main loop.

    Do only the jobs in the ISR that need to be fast or precisely timed.
    I can't see that switching a relay ON/OFF needs that precise timing. ... but maybe it does, it depends on your application..

    Klaus

    Added:
    unsigned int Float_Value;
    does it make sense?
    Your code is undocumented and we don't have useful information about the function.
    What about flow chart, timing diagram, state diagram? You should have this already. If not, then thats most probably the problem

    Display: It seems you update the display every ms. But eyes and brain are not that fast. You should not update a display more often than very 250ms. Just set a flag every 250ms in the ISR, to inform the main loop that new display data should be processed.
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  3. #3
    Member level 5
    Points: 673, Level: 5

    Join Date
    Aug 2018
    Posts
    81
    Helped
    0 / 0
    Points
    673
    Level
    5

    Re: Repeat Cycle using timer application

    Display related activity will be done ISR routine. Else you cant see the changes in dsplay. Currently in my code i am not turning on and off the relay function. I am concentrating on only displaying on time and off time process value.
    It doing its job but only for 1 cycle.
    it will load on time value and change to off time value when it reaches respective values.

    in 3rd cycle, what ever ontime and offtime it will load into display.

    as i understood it is not loading value of ontime and offtime after first cycle.

    Code is running for 1ms so content also get updated for 1sec



    •   AltAdvertisement

        
       

  4. #4
    Super Moderator
    Points: 84,045, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,828
    Helped
    4590 / 4590
    Points
    84,045
    Level
    70

    Re: Repeat Cycle using timer application

    I explained the timing issues to nayakajit87 and how to structure the ISR efficiently in a previous post but it has been ignored.

    As well as that, I can't see where 'LED_MS_Flag" gets reset anywhere or for that matter what it actually does. It seems to be initialized at '1' then checked to see if it is still '1' at various times in the program but never used for anything.

    I'll repeat: Just do the timing inside the ISR then get out of it as quickly as possible. Check the variables you update in the ISR in the main code.

    What does the "Display()" routine do? If it introduces any delays before returning it might also stop the program running.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



  5. #5
    Super Moderator
    Points: 82,053, Level: 69
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    16,652
    Helped
    3775 / 3775
    Points
    82,053
    Level
    69

    Re: Repeat Cycle using timer application

    Hi,

    Display related activity will be done ISR routine. Else you cant see the changes in dsplay
    If this is true, how can some of my applictions's display work?

    And read carefully:
    inform the main loop that new display data should be processed.
    If you use multiplexing techniques (or other requirement) that needs exact timing...you should output the preprocessed data to the display within the ISR. But I recommend that they should be processed in main loop.

    --> Keep most of the processing power in main loop. Keep ISR as short as possible.
    --> keep precision timing in the ISR.

    Klaus
    Please don´t contact me via PM, because there is no time to respond to them. No friend requests. Thank you.



  6. #6
    Member level 5
    Points: 673, Level: 5

    Join Date
    Aug 2018
    Posts
    81
    Helped
    0 / 0
    Points
    673
    Level
    5

    Re: Repeat Cycle using timer application

    Based on suggestion . I have modified code . With below code i have tested. In polling mode the

    Value Bottom display shows setvalue 2525 and stop at 2525
    IT wont continue next cycle.

    The code is for only testing purppose. LED_MS_Flag is not reset since i am testing sec and Msec function. Later It will implemented for other function.
    For testing of code i am passing fake parameters to evaluate the actual operation.those flags can be later used for further calculation.
    The variable declared in main() are onetime executing function. once set always it will be loaded.


    Code:
    unsigned char LED_MS_Flag = 0;
    unsigned char ON_Prg_Flag = 0;
    unsigned char Off_Prg_Flag = 0;
    unsigned char RLY_Off_Flag=0;
    unsigned char On_Int_Flag=1;
    
    
    unsigned int On_Time=0;
    unsigned int Off_Time=0;
    unsigned char Cylic_Flag=0;
    const unsigned char DISPTABLE[27] = {0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09,0x11,0x83,0x31,
                                         //0  //1  //2  //3  //4  //5  //6  //7  //8  //9  //A  //U  //P
                                         0xD1,0xE3,0x89,0x63,0xE1,0xF5,0xC1,0x61,0x71,0xF3,0xFD,0xD5,0x85,0xFF
         unsigned char LEDBuffer_1[8];
    unsigned int Hour = 0;
    unsigned int Min = 0;
    unsigned int Sec = 0;
    unsigned int MSec = 0;
    
    
    
    void interrupt isr(void) {
    
    	asm("clrwdt");
    
    	if (TMR1IF) {
    		TMR1IF = 0;
    		TMR1H = 0xFE;
    		TMR1L = 0x0C; // timer interrupt for 1ms
    		MSec++;
    		if (MSec >= 1000) {
    			MSec = 0;
    			Sec = Sec + 1;
    		} else if (Sec >= 59) {
    			Sec = 0;
    			Min = Min + 1;
    		} else if (Min >= 59) {
    			Min = 0;
    			Hour = Hour + 1;
    		} else if (Hour > 99) {
    			Hour = 0;
    		}
    
    		//Display();
    	}
    
    
    }
    
    void main() {
    	On_Time=2525;
    	Off_Time=3535;
    	Sub_Time=3;
    	Sub_Relay=1;
    	LED_MS_Flag=1;
    	On_Int_Flag=0;
    	Cylic_Flag=0;
    	while(1) {
    		Process_RUN_MODE();
    		Cylic_Fun_CHK();
    
    	}
    
    }
    
    
    
    
    void Cylic_Fun_CHK() {
    
    	//Cyclic function start from Here......................................................................
    	if (On_Int_Flag == 0) {
    
    		if (Cylic_Flag == 0) {
    
    			if (On_Time > Bottom_Value) {
    				if (LED_MS_Flag == 1) {
    					Bottom_Value = (Sec * 1000) + MSec;
    				} else if (LED_SeMi_Flag == 1) {
    					Bottom_Value = (Min * 100) + Sec;
    				} else if (LED_HM_Flag == 1) {
    					Bottom_Value = (Hour * 100) + Min;
    				}
    
    			} else
    
    				if (On_Time == Bottom_Value) {
    					Cylic_Flag = 1;
    					Relay_OFF();
    					On_Int_Flag = 0;
    
    					Bottom_Value = 0;
    				}
    
    		}
    		if (Cylic_Flag == 1) {
    
    			if (Off_Time > Top_Display) {
    				if (LED_MS_Flag == 1) {
    					Top_Display = (Sec * 1000) + MSec;
    				} else if (LED_SeMi_Flag == 1) {
    					Top_Display = (Min * 100) + Sec;
    				} else if (LED_HM_Flag == 1) {
    					Top_Display = (Hour * 100) + Min;
    				}
    
    			} else
    
    				if (Off_Time == Top_Display) {
    					Cylic_Flag = 0;
    					Relay_OFF();
    					On_Int_Flag = 0;
    					Top_Display = 0;
    					//Off_Time =0;
    
    
    				}
    
    
    
    		}
    
    	}
    
    }
    
    
    
    void Process_RUN_MODE() {
    
    	if (LED_MS_Flag == 1) {
    		Msec_Function();
    	}
    }



    •   AltAdvertisement

        
       

  7. #7
    Super Moderator
    Points: 84,045, Level: 70
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    13,828
    Helped
    4590 / 4590
    Points
    84,045
    Level
    70

    Re: Repeat Cycle using timer application

    I'm still confused about what this code is supposed to do and in particular how it produces the display. Am I correct in thinking the lines:
    Code:
    #define DISPCLK         RB5
    #define DISPDATA        RB6
    are digit drive signals to two 7-segment LEDs? If so, how are two different four digit numbers displayed?

    Perhaps a schematic to see how you have it connected would help.

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



--[[ ]]--