Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Pic Volatile memory reseting in mikroC pro

Status
Not open for further replies.

mrarslanahmed

Full Member level 2
Joined
Nov 15, 2011
Messages
143
Helped
6
Reputation
12
Reaction score
6
Trophy points
1,298
Location
gujranwala pakistan
Activity points
2,205
Hi,
I am working on the controller and declared a global volatile variable, it is changing its value in the interrupt service routine , but when the controller come back from the routine it reset to the default value i declared at the start, i want it to hold that value, is it okay to reset or not OK? please help me in this issue.

regards
 

Hi,

is it okay to reset or not OK?
It seems it is not OK for you...

If you need help you should show us your code....

Klaus
 
wdt is reseting, but dont know why?

- - - Updated - - -

Code:
#define internal_oscilator_frequency    0x71;               // 0b01110010;  8 MHZ internal oscillator enabled
#define clear_bytes                     00;
#define as_output                       00;
#define as_input                        255;
#define all_high                        255;
#define all_low                         00;
#define off                             00;
#define on                              255;

bit timer_0_ready;


const int motor_wait_check  =           2000;

 /*// volatile variable may change its value during runtime independently from the program*/
// volatile int MAXIMUM VALUE OF 281
volatile int motor_forward_count =      0;                                      // used when motor is operating without left right being pressed
volatile int motor_forward_left_count = 0;                                      // used when motor is operating with left being pressed
volatile int motor_forward_right_count= 0;                                      // used when motor is operating with right being pressed

volatile int motor_back_count =      0;                                      // used when motor is operating without left right being pressed
volatile int motor_back_left_count = 0;                                      // used when motor is operating with left being pressed
volatile int motor_back_right_count= 0;                                      // used when motor is operating with right being pressed

volatile int timer_0_jumps_check_1;
volatile int timer_0_jumps_check_2;
volatile int motor_initiated;

void initialise_global();
void all_motors_off();
void update_counters();
void clear_ports();
void IO_configuration();
void TImer1_Gate_Configuration();
void timer0_configuration();
void all_motors_off();
void motor_forward();
void motor_forward_left();
void motor_forward_right();
void motor_back();
void motor_back_left();
void motor_back_right();
void start_robot_to_work();


/////////////////////////////////////////////
/////////////////////////////////////////////

sbit L293_REVERSE_IP_DIR                    at TRISD.B0;
sbit L293_foward_IP_DIR                     at TRISD.B1;
sbit L293_LEFT_IP_DIR                       at TRISD.B2;
sbit L293_RIGHT_IP_DIR                      at TRISD.B3;
sbit Remote_control_forward_signal_DIR      at TRISD.B4;
sbit Remote_control_back_signal_DIR         at TRISD.B5;
sbit Remote_control_left_signal_DIR         at TRISD.B6;
sbit Remote_control_right_signal_DIR        at TRISD.B7;

sbit L293_REVERSE_IP                        at PORTD.B0;         //
sbit L293_FORWARD_IP                        at PORTD.B1;         // COMMAND FROM THE REMOTE LOCATION TO THE CONTROLER
sbit L293_LEFT_IP                           at PORTD.B2;         //
sbit L293_RIGHT_IP                          at PORTD.B3;         //
sbit Remote_control_forward_signal          at PORTD.B4;         // COMMAND FROM THE CONTROLLER TO THE MOTORS VOA L293
sbit Remote_control_back_signal             at PORTD.B5;
sbit Remote_control_left_signal             at PORTD.B6;
sbit Remote_control_right_signal            at PORTD.B7;

///



/////////////////////////////////////////////
/*we will make an array whcih can be used to store data of 10
different inputs, the recording will start as soon as any one key
forward or reverse is pressed and after that if key is not pressed
for specied time say 5 seconds then robot will continue the path it 
stored
*/
/////////////////////////////////////////////
sbit T1__G                                  at PORTB.b5;
sbit T1__G_dir                              at TRISB.b5;

void interrupt() 
{

        INTCON.GIE=0;
        INTCON.PEIE=0;
        PIR1.TMR1IF=0;                                // clear the timer 1 interupt flag
        if(T0IF_bit && !T1__G && motor_initiated==1)
         {
            portc=0xff;
            timer_0_jumps_check_1++;
            if(timer_0_jumps_check_1==150)                                      // approximatly 5 seconds gap
            {
              timer_0_jumps_check_2++;
              timer_0_jumps_check_1=0;
              if(timer_0_jumps_check_2==1)                                      // set this to have time as multiple 5
              {
                start_robot_to_work();
                timer_0_jumps_check_2=0;
                portc=0x00;
              }
            }
         }       
        if(T1__G)
        {
          update_counters();
        }


        tmr1h=0;
        tmr1l=0;                                 // these values are set temporarily will be chaged as per interupt routine time
        INTCON.TMR0IF=0;

        INTCON.GIE=1;
        INTCON.PEIE=1;
}
void main()
{
      osccon=internal_oscilator_frequency;
      initialise_global();
      clear_ports();
      IO_configuration();
      TImer1_Gate_Configuration();

      timer0_configuration();
      all_motors_off();
      T1Con=0xB4;
    timer_0_ready=0;
    timer_0_ready=1;
      while(1)
     {
      if(T1__g)
      {
        motor_initiated=1;
         T1Con=0xB5;
        while (T1__G)
        {
              if(Remote_control_forward_signal)
              {
                if(Remote_control_left_signal)
                    {
                      motor_forward_left();
                    }
                else if(Remote_control_right_signal)
                    {
                        motor_forward_right();
                    }
                else
                motor_forward();
             }
        else if(Remote_control_back_signal)
           {

                  if(Remote_control_left_signal)
                   {
                      motor_back_left();
                   }
                  else  if(Remote_control_right_signal)
                   {
                      motor_back_right();
                   }
                   else
                   motor_back();
           }
                                            // just done not necessary

       }    // end of while timer gate 1
       
       }
       else
       {
              T1Con.TMR1ON=84;
              all_motors_off();
        }
  }
}
void initialise_global()
{
     timer_0_jumps_check_1=0;
     timer_0_jumps_check_2=0;
     motor_initiated=0;
}



 void update_counters()
{
        if(Remote_control_forward_signal)
              {
                if(Remote_control_left_signal)
                    {Motor_forward_left_count=Motor_forward_left_count+1;}
                else if(Remote_control_right_signal)
                    {Motor_forward_right_count=Motor_forward_right_count+1;}
                else Motor_forward_count=Motor_forward_count+1;
             }
        else if(Remote_control_back_signal)
           {
                if(Remote_control_left_signal)
                   {Motor_back_left_count=Motor_back_left_count+1;}
                else if(Remote_control_right_signal)
                   {Motor_back_right_count=Motor_back_right_count+1;}
                else Motor_back_count=Motor_back_count+1;
           }
}


void clear_ports()
{

// port are cleared
      trisa=as_output;
      trisB=as_output;
      trisc=as_output;
      trisd=as_output;
      trise=as_output;

      porta=clear_bytes;
      portb=clear_bytes;
      portc=clear_bytes;
      portd=clear_bytes;
      porte=clear_bytes;
}



void test_check()
{
      porta=all_high;
      portb=all_high;
      portc=all_high;
      portd=all_high;
      porte=all_high;

      Delay_ms(50);

      porta=all_low;
      portb=all_low;
      portc=all_low;
      portd=all_low;
      porte=all_low;

      Delay_ms(50);
}

void motor_check()
{
          Remote_control_forward_signal=all_high;
          Delay_ms(motor_wait_check);
          Remote_control_back_signal=all_high;
          Delay_ms(motor_wait_check);
          Remote_control_left_signal=all_high;
          Delay_ms(motor_wait_check);
          Remote_control_right_signal=all_high;
          Delay_ms(motor_wait_check);

}

void IO_configuration()
{

          L293_foward_IP_DIR =                 as_output;
          L293_REVERSE_IP_DIR=                 as_output;
          L293_RIGHT_IP_DIR=                   as_output;
          L293_LEFT_IP_DIR=                    as_output;
          T1__G_dir=                           as_input;

          Remote_control_forward_signal_DIR=   as_input;
          Remote_control_left_signal_DIR=      as_input;
          Remote_control_right_signal_DIR=     as_input;
          Remote_control_back_signal_DIR      =as_input;



          ANSEL  = 0;                        // Configure AN pins as digital I/O
          ANSELH = 0;
          C1ON_bit = 0;                      // Disable comparators
          C2ON_bit = 0;
          
}
void all_motors_off()
{
            L293_REVERSE_IP=off;
            L293_FORWARD_IP=off;
            L293_LEFT_IP=off;
            L293_RIGHT_IP=off;
}

void motor_forward()
{
            L293_REVERSE_IP=off;
            L293_FORWARD_IP=on;
            L293_LEFT_IP=off;
            L293_RIGHT_IP=off;
}
void motor_forward_left()
{
            L293_REVERSE_IP=off;
            L293_FORWARD_IP=on;
            L293_LEFT_IP=on;
            L293_RIGHT_IP=off;
}
void motor_forward_right()
{
            L293_REVERSE_IP=off;
            L293_FORWARD_IP=on;
            L293_LEFT_IP=off;
            L293_RIGHT_IP=on;
}

void motor_back()
{
            L293_REVERSE_IP=on;
            L293_FORWARD_IP=off;
            L293_LEFT_IP=off;
            L293_RIGHT_IP=off;
}

void motor_back_left()
{
            L293_REVERSE_IP=on;
            L293_FORWARD_IP=off;
            L293_LEFT_IP=on;
            L293_RIGHT_IP=off;
}
void motor_back_right()
{
            L293_REVERSE_IP=on;
            L293_FORWARD_IP=off;
            L293_LEFT_IP=off;
            L293_RIGHT_IP=on;
}

void timer0_configuration()
{
            trisa=0;

            intcon=0xa0;
            option_reg=0x07;
            ccp1con=0;
            trisd=0;
            portd=0;

}

void TImer1_Gate_Configuration()
{
          CM2CON1.T1GSS=1;                                                      // to take pulse from gate
          T1CON.T1GINV=0;                                                       // measures only when gate is high
          TMR1GE_Bit=1;                                                         // enable t1gate
          T1CON.TMR1CS=0;                                                       // Fosc/4 if it was 1 then it would be waiting for the pulses from pin 15
          T1CON.T1CKPS0=1;
          T1CON.T1CKPS1=1;                                                      // prescalor set 8 to measure maximum time =65536*8
          T1CON.T1SYNC=1;                                                       // run synchronously

          T1CON=0X74;
          PIE1.TMR1IE=1;
          INTCON.GIE=1;
          INTCON.PEIE=1;

          TMR1H=0;
          TMR1L=0;
}
void start_robot_to_work()
{
// in this function robot will start repeating its function
portc=00;
}
 

Hi,

a lot of variables are declared as volatile, which variable are you talking about?

wdt is resetting
wdt is the watchdog timer.
I don´t find anything about it in your code. If you dan´t want to use the watchdog, then maybe you need to disable it.
How to use/enable/disable/reset the watchdog is written in the datasheet.

Klaus
 

A globally declared variable with the volatile attribute is expected to hold the last assigned value, either in interrupt or main function. The volatile attribute instructs the compiler to always reload the actual variable content when processing it.

If this doesn't work, it's either a coding fault or compiler bug. There may be under circumstances a problem of non-atomic variable operations disturbed by the interrupt. E.g. an assignment interrupted in the middle.

volatile_value = volatile_value + something;

It must be protected by a temporary interrupt disable.
 

I suspect the watchdog reset occurs because you never reset it anywhere. Try adding a reset in the main loop. NEVER reset it in an ISR!
Also beware of "while(..)" statements inside an interrupt or called from it, they can delay the interrupt routine finishing and cause later interrupts to be missed.

Brian.
 

all volatile variable are doing this behavior , i am working on proteus 8.1 and loading .cof file , can there be a bug at that side i think code is quite simple . there should be any thing like this,
 

Attachments

  • asdd.JPG
    asdd.JPG
    101.6 KB · Views: 118
Last edited:

I think you will find that 'volatile' only tells the compiler to always read the current value of the variable and not rely on any 'alternative' copy it may have made (i.e. in a register for faster access). In this way an ISR can update a variable and the non-interrupt code can detect the change.
It does NOT make any promises about the value of the variable after a reset. Most compilers will place the global variables in a set region of RAM and will initialise them on startup (which also implies on reset in most cases) if the code tells them to. Otherwise they can take on random initial values just like any other non-initialised global variable..
Being declared 'volatile' ONLY refers to the ability to update the variable in the ISR and have this seem by the non-interrupt code.
Susan
 

As I already reported in post #6, the watchdog is reseting the processor and reinitializing your variables. Look at the image you uploaded, it shows it reseting over and over again approximately every 10 seconds.

Brian.
 

Hi i am disabling it by clearing wdtcon register but its not helping is there i am missing, any ways i worked with timer 2 for the same work and its helping me, but still i didnt figured out my setting why wdt resseting my controller when i am not using or resettting it

Regards
 

Hi,

If the watchdog is not disabled..
then you need to reset the wdt within a given interval
Else it will time out and thus cause a RESET.

Klaus
 

I don't think you have told us the MCU you are using but from the fuzzy (on my screen anyway) image in Post #7 I think it might be a PIC16F887.
Have a look at the "Special Features of the CPU" section (section 14) of the data sheet and check out the WDTE bit of the CONFIG1 register. If you don't disable it there, then you can't turn it off in the WDTCON register. (I'm not sure how MikroC controls the config bits but the user guide should tell you.)
Susan
 

OK - it was just that I cannot see your config settings in any of the code you have posted.
Susan
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top