Problems with Timer Start/Stop (newbe)

General discussion on mikroC.
Post Reply
Author
Message
C.Schlehaus
Posts: 3
Joined: 18 Apr 2011 05:57

Problems with Timer Start/Stop (newbe)

#1 Post by C.Schlehaus » 18 Apr 2011 07:50

Hi,
I just started working with PIC and mikroC and now I'm facing my first problem which drives me crazy..
The application drives an external kinematics until an external switch on GPIO.GP0 is deactivated (Pos1), invertes operation, should start counting on reactivation of switch (hysteresis) (Pos2) drives until the switch opens again (Pos3) and should as the last step drive half the time back (to get the kinematics more or less in half between both switching positions).
To achieve this, I'd like to use the Timer1 in Timer mode, activated on Pos(2) and deactivated on Pos(3). My code seems to operate, as the system drives to Pos(1), Pos(2), Pos(3) but then stops as if the timer1 would be still Zero, thus the time to drive to final position equals zero...
As the operation of the timer cannot be checked in debugger mode in the watch window, I do not know whether the timer does not operate or if there is any other bug...
Thanks a lot for every support

Carlhermann

Code:

Code: Select all

void initialize() {
INTCON = 0b00000000;                    // configure the interrupt sources
IOC = 0b00000000;                       // Interrupt on change control
PIE1 = 0b00000000;                      // Peripheral interrupt enable control
PIR1 = 0b00000000;                      // Peripheral interrupt flag register
PCON = 0b00000011;                      // Power ON Control POR and Brown_Out
TRISIO = 0b00110001;                    // configure the direction on Pins (I/O)
WPU = 0b11111111;                       // enable the internal pulls if Input
CMCON = 0b00000111;                     // disable the Comparator function
OPTION_REG = 0b00001111;                // Prescaler WDT set to 1:128
GPIO = 0b00000000;                      // Clear all outputs
VRCON = 0b00000000;                     // Voltage Reference Control Register
T1CON = 0b00110100;                     // Timer 1 Control register
TMR1L = 0x00;                           // clear the lower Byte of Timer 1
TMR1H = 0x00;                           // clear the upper Byte of Timer 1
return;
..
void Referencing() {
int Drv_L = 0;
int Drv_R = 0;
int completed = 0;
unsigned Drive_Time = 0;
int Pwr_On = 1;                         // this shall be the flag to indicate Reed-Contact open on Pwr-ON
int left_CC = 0;
int right_CC = 0;
T1CON.TMR1ON = 0;                       // Stop the timer
TMR1L = 0x00;                           // clear the lower Byte of Timer 1
TMR1H = 0x00;                           // clear the upper Byte of Timer 1
PIR1.TMR1IF = 0;                        // clear the interrupt flag (indication for overflow
...
else if (button(&GPIO,GP0,10,0)&&(Drv_L == 0))      // The reed contact is closed and initialization 1st drive to be performed
  {
  GPIO.GP1 = 1;                         // drive left (until Reed opens) activated
  GPIO.GP2 = 0;                         // drive right deactivated
  Pwr_ON = 0;                           // indicate no longer PWR_ON Condition
  }
else if (button(&GPIO,GP0,10,1)&&(Drv_R == 0)) // now drive right (1st step until reed is closed again)
  {
  GPIO.GP1 = 0;                         // drive left deactivated
  GPIO.GP2 = 1;                         // drive right
  Drv_L = 1;                            // indicate left operation has been performed and completed
  Pwr_ON = 0;                           // indicate no longer PWR_ON Condition
  }
else if (button(&GPIO,GP0,10,0))                 // The reed contact is closed until second open position reached
  {
  GPIO.GP1 = 0;                         // drive left deactivated
  GPIO.GP2 = 1;                         // drive right
  Drv_R = 1;
  Pwr_ON = 0;                           // indicate no longer PWR_ON Condition
  T1CON.TMR1ON = 1;                     // start the Timer to measure the time to nest stop
  }
else                                    // driven to the second position deactivate
  {
  T1CON.TMR1ON = 0;                     // Stop the timer
  Drive_Time = (TMR1H*512+TMR1L)/2;
  TMR1H = 0;
  TMR1L = 0;
  T1CON.TMR1ON = 1;                     // retart the timer
  do {
     GPIO.GP1 = 1;
     GPIO.GP2 = 0;
     } while ((TMR1H*512+TMR1L)<Drive_Time);
  GPIO.GP1 = 0;
  GPIO.GP2 = 0;
  T1CON.TMR1ON = 0;                     // Stop the timer
  completed = 1;
  Pwr_ON = 0;                           // indicate no longer PWR_ON Condition
  };
} while (completed == 0);
return;

Post Reply

Return to “mikroC General”