CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

PIC LOWER FREQ. READING PROBLEM
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Nov 28, 2017 6:12 pm     Reply with quote

You have a mix of FP and INT variables which may account for the 'bad' numbers.

As for the last reading.. test without the function genny inputting, what do you get for the 'reading'? It should be 0.

Also WHAT is different in the old vs. new hardware?
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Tue Nov 28, 2017 8:02 pm     Reply with quote

Yes without any output from function generator in beginning I am getting zero. But once it detects 0.5 MPH it remains same even if I am removing the signal input from function generator.


The old hardware have an 2 digit seven segment led display. Newer hardware have an lcd display. That is the only change.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 28, 2017 8:22 pm     Reply with quote

POPE19 wrote:
Yes without any output from function generator in beginning I am getting zero. But once it detects 0.5 MPH it remains same even if I am removing the signal input from function generator.

See this thread for ways to handle lack of an input signal:
http://www.ccsinfo.com/forum/viewtopic.php?t=56439&start=5
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Wed Nov 29, 2017 8:08 am     Reply with quote

PCM Programmer,

I have tried the recommended code, but the problem still persists. I am sending the updated piece of code. The lcd displays as soon as the input signal is present but does not display 0 when signal is not present.

I don't know where in the loop is it sticking.
Code:

#include <16F914.h>
#device ADC=10
#fuses HS, MCLR, NOWDT, PUT, NOIESO, BROWNOUT, NOFCMEN
#use delay(clock=20000000)
#define INTS_PER_QUARTER_SECOND     19      // (20000000/4*(4*256*256))

int int_count=INTS_PER_QUARTER_SECOND;
int time_delay =0;
int count0 = 0;   //3rd digit
int count1 = 0;   //2nd digit
int count2 = 0;   //1st digit

short Digit2_Dec = true;
float Frequency = 0;
long newspeed = 0;
long speed = 0;
short MPH_KmPH_status;

//========================================================================================//
//                                 LCD Configuration                                      //
//========================================================================================//
// Digit segments  A        B         C          D         E         F         G        DP
//                 b7       b6        b5         b4        b3        b2        b1       b0
#define DIGIT1  COM0+11,  COM1+11,  COM2+11,  COM3+11,  COM2+3,   COM0+3,   COM1+3,   COM3+3
#define DIGIT2  COM0+6,   COM1+6,   COM2+6,   COM3+6,   COM2+16,  COM0+16,  COM1+16,  COM3+16
#define DIGIT3  COM0+17,  COM1+17,  COM2+17,  COM3+17,  COM2+18,  COM0+18,  COM1+18,  COM3+18
#define UNIT1    COM0+19,  COM1+19,  COM2+19,  COM3+19,  COM2+20,  COM0+20,  COM1+20,  COM3+20
//#define DIGIT5  COM0+2,   COM1+2,   COM2+2,   COM3+2,   COM2+3,   COM0+3,   COM1+3,   COM3+3
//    COM0+0,   COM1+0,   COM2+0,   COM3+0,   COM2+1,   COM0+1,   COM1+1,   COM3+1
//       ComX+Y is combination of Backplan0-3 and Segment0-SegmentXX
//========================================================================================//
//           character         0    1    2    3    4    5    6    7    8    9    0   Null
//Decimal
byte const Digit_Map[12]   = {0xFD,0x61,0xDB,0xF3,0x67,0xB7,0xBF,0xE1,0xFF,0xF7,0xFD,0x01};
//No Decimal
byte const Digit_Map_1[12] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,0xFC,0x00};
//========================================================================================//
//           Bars               MPH KMPH MPH&KMPH
byte const Unit_Map[3]      = {0x04,0x02,0x06};
//=======================================================================================//
//                          Segments Initilization                                       //
// Seg23 Seg22  ...  Seg12 Seg11 Seg10 Seg9 Seg8 Seg7 Seg6 Seg5 Seg4 Seg3 Seg2 Seg1 Seg0 //
//  0/1   0/1   0/1   0/1   0/1   0/1  0/1  0/1   0/1  0/1 0/1   0/1 0/1  0/1  0/1   0/1 //
//  if bit_SegX is 1, SegX is set as Segment Output. Otherwise                           //
//=======================================================================================//
#define Segments 0x170848    // Initilize LCD SegmentX

//=================================================================================//
#int_rtcc               //This function is called
void clock_isr()
{                         //every time the RTCC (timer0)
                          //overflows (255->0)
                          //For this program this is apx
                          //31 times per second.

   if(--int_count==0)
   {
      time_delay ++;
      int_count=INTS_PER_QUARTER_SECOND;
   }
}
//================================================================================//

#priority CCP1, TIMER1

#define BytePtr(var, offset) (char *)((char*)&var + offset)

#byte PIR1 = 0xF9E      // to locate PIR1 at address 0xF9E.
#bit  TMR1IF = PIR1.0   // creates TMR1IF Variable & bit 0 of a byte (PIR1 Registor) is stored inside

//========================variables===============//
int8  gc_timer1_extension = 0;   
short capture_flag = false;
int32 g32_ccp_delta;   //Global Variable which stores Timer intervals between two rising edge of input signal
//===============================================//

//=====routine=========
#int_timer1        //everytime Timer-1 Interrupts the below Subroutine is Executed immediately after interrupt.
void timer1_isr(void)
{
  gc_timer1_extension++;    // increment real timer extension
}

#int_ccp1
void ccp1_isr(void)
{
   
   char timer_ext_copy;
   int32 current_ccp = 0;
   static int32 old_ccp = 0;
   
   capture_flag = true;
   MPH_KmPH_status = True;      // MPH mode
 
 
   current_ccp = (int32)CCP_1; // Read the current CCP
   
   timer_ext_copy = gc_timer1_extension; // get a local copy of timer ext

   if(TMR1IF)
   {
      if (make8 (current_ccp, 1) < 2)  // was CCP captured after Timer-1 Wrapped?
      timer_ext_copy++;                // if yes, Increment the copy of timer ext.

      gc_timer1_extension++;           // Increment the real timer extension
      TMR1IF = 0;                      // Then clear the Timer1 interrupt
   }
    // Insert the timer extension into the proper place in the 32-bit
    // CCP value.
   // ie.,  Insert it into location "EE" as follows: 0x00EEnnnn
   // (nnnn = the CCP).
   make8 (current_ccp, 2) = timer_ext_copy;

   g32_ccp_delta = (current_ccp > old_ccp) ? current_ccp - old_ccp : current_ccp + (0x1000000 - old_ccp);

   old_ccp = current_ccp; // Save the current CCP value for next time.
           
}
//=================================================================================//
void Display()
{
   lcd_symbol (Digit_Map_1[count2], DIGIT1);  // from left to right
     
   if (Digit2_Dec == False) 
      lcd_symbol (Digit_Map_1[count1], DIGIT2);  // from left to right
   else                 
      lcd_symbol (Digit_Map[count1], DIGIT2);    // from left to right
     
   lcd_symbol (Digit_Map[count0], DIGIT3);       // from left to right
   
}
//============================================================================//

void Speed_Cal ()
{
   if(capture_flag == true)
   {
      Frequency = (1250000 + (g32_ccp_delta >> 3))*100 / g32_ccp_delta;
                                                   // x100 to get 2 decimal
      capture_flag = false;     
   }
   else if(capture_flag == false) 
   {
      newspeed = 0;
   }
                                                   
   // 1MPH = 74Hz or 0.1MPH = 7.4Hz   application requirement
    // MPH mode
   if (MPH_KmPH_status == True)  // MPH mode
   {
      newspeed = Frequency/740;  // newspeed = 0.1 MPH,
                                // /100 to get back 2 decimal
      if (newspeed < 5)
        newspeed = 0;
   }
   
   
 }
//============================================================================//
void Speed_Mode()
{
   Speed_Cal ();
   
   if (time_delay >= 2)
   {
      speed = newspeed;
      time_delay  = 0;
   }
   
   if (speed >= 100)
   {
      count2 = speed%100/10;
      count1 = speed/100;
      count0 = speed%10;
   }
   else
   {
      Digit2_Dec = true;
      count2 = 11;
      count1 = speed%100/10;
      count0 = speed%10;
   }

   display();
 
}

//============================================================================//
   
   

void main()
{
   setup_lcd(LCD_MUX14|LCD_STOP_ON_SLEEP,2, Segments);
   set_rtcc(0);
   setup_counters (RTCC_INTERNAL, RTCC_DIV_256);

   setup_ccp1 (CCP_CAPTURE_RE);               // Configure CCP1 to capture rise
   setup_ccp2 (CCP_CAPTURE_RE);               // Configure CCP1 to capture rise
   
   set_timer1 (0);
   setup_timer_1 (T1_INTERNAL|T1_DIV_BY_4);   // Start timer 1
   
   clear_interrupt (INT_CCP1);
   enable_interrupts (INT_CCP1);              // Setup interrupt on raising edge
   
   clear_interrupt (INT_CCP2);
   enable_interrupts (INT_CCP2);              // Setup interrupt on raising edge
   
   clear_interrupt (INT_TIMER1);
   enable_interrupts (INT_TIMER1);            // Enable interrupt timer 1
   
   enable_interrupts (INT_RTCC);              // Enable interrupt timer 0
   
   enable_interrupts (GLOBAL);                // Enable interrup Global

      Digit2_Dec = true;
      count2 = 11;
      count1= 8;
      count0 = 8;
      display ();
   
     capture_flag = False;
         
   while(TRUE)      // Modify on 3/09/12
   {
      Speed_Mode();     
   }
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 29, 2017 8:45 am     Reply with quote

Time for serious debugging.

Two possibilities.

Use the spare I/O pins and a 'scope.
At the start of a routine raise a pin high, then low as you leave.
Do this with all the routines (either together or in turn if you run out of pins).
Should enable you to locate the routine where you're stuck.
Then use spare I/O pins to probe deeper.

OR.

Do something similar with printf()'s and a PC terminal.

Mike
alan



Joined: 12 Nov 2012
Posts: 349
Location: South Africa

View user's profile Send private message

PostPosted: Wed Nov 29, 2017 9:04 am     Reply with quote

You never reset theMPH_KmPH_status:
Code:
   if (MPH_KmPH_status == True)  // MPH mode
   {
      newspeed = Frequency/740;  // newspeed = 0.1 MPH,
                                // /100 to get back 2 decimal
      if (newspeed < 5)
        newspeed = 0;
   }

So it will override newspeed with the last Frequency value.

Didn't look further, but this stood out.

Regards
POPE19



Joined: 27 Jun 2017
Posts: 71

View user's profile Send private message

PostPosted: Wed Nov 29, 2017 9:18 am     Reply with quote

I have modified my code and it works. I just made following changes. I think it needs more delay to set the capture_flag = False. As soon as i increased the value of capture_interrupt count to 60, i am getting same behavior as it was with my old hardware.

I think the LCD needs some more time for this process. Maybe i am wrong but the interesting thing is to know why would this affect the output. I will keep updating about this if i find something logical. Meanwhile Experts are welcome for comments.
Code:

if(capture_interrupt >=60)
   {
     Capture_flag = FALSE;
     capture_ready = 0;
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Nov 29, 2017 11:51 am     Reply with quote

If you look at your old code, it has several mSec of delay in the display routine. The new code just writes the LCD, and doesn't allow any time for this to be seen before looping and potentially writing new values. Remember just how quickly the CCP will trigger.....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group