go_away

Author Topic: Capture Pulse Per Second From Quadrature Encoder.  (Read 1098 times)

0 Members and 1 Guest are viewing this topic.

Offline twinsratioTopic starter

  • Jr. Member
  • **
  • Posts: 16
  • Helpful? 0
Capture Pulse Per Second From Quadrature Encoder.
« on: March 01, 2011, 10:32:33 PM »
Hi there,

My code below is actually to capture pulse per second that generated from the motor's encoder and display the pulse per second and rpm on LCD module. However I couldn't hold the value and display on the LCD. In my code I created a 1sec interrupt using timer0 with 4Mhz frequency. I have no idea how to modify my code so it just interrupt for 1 sec and hold the value. I also found a problem here which is when I only display 1 line on the LCD the value for the pulse is getting around 33-39. However when I display 2 lines on LCD the value for the pulse is half of (33-39). How can I solve it? Could it better if I use 8Mhz?? When I on the motor the programming actually does the while loop, but i could not hold and clear the value, let say the value is 18 or 19, and it keeps update every 1 sec, when I stop the motor the value stop and without return or clear to 0. How can I clear it?



#include <htc.h>
#include <stdlib.h>
#include <string.h>

__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & BORDIS & LVPDIS);

#define RS RE0
#define RW RE1
#define EN RE2
unsigned int reload=15;
unsigned int rpm;   

unsigned int  s, k, w, a, b, c, y;


//LCD instruction function   
   void instruction(char C)      
   {

      PORTE = 0x04; //RS =1 , W/R = 0 , E = 0
      PORTD = C;
      PORTE = 0x00;
      for (s=0;s<70;s++){}
   }


// LCD display function
   void lcd (char C)            
   {

      PORTE = 0x05;   //RS =1 , W/R = 0 , E = 1
      PORTD = C;
      PORTE = 0x01;   //RS =0 , W/R = 0 , E = 1

      PORTE = 0x01;   //RS =0 , W/R = 0 , E = 1
      PORTD = C;
      PORTE = 0x04;   //RS =1 , W/R = 0 , E = 0
   
       for (s=0;s<1;s++){}
   }


/* write a string of chars to the LCD */
// LCD lcdout function for character input   
   void lcdout (const char *C)
   {
   PORTE = 0x04;
   while (*C)
      {
      lcd(*C++);
            }
   }

void out ()
{
   
      unsigned long counter = y;
      char output[12];
        ltoa(output, counter, 10);
      instruction(0x80);
      lcdout("Pulse/sec:");
      lcdout(output);      //output
      


      }

void out2 ()

{   

      unsigned long speed = rpm;
      char r [12];
      ltoa (r,speed,10);
      instruction (0xC0);
      lcdout("RPM:");
      lcdout(r);
      
}


// Main Function

void main(void)
{
//OSCCON= 0x70;
TRISA = 0xFF;         //set PORTA as Input
TRISD = 0x00;         //Set PORTD as Output (Data)
TRISE = 0x00;         //Set PORTE as Output (EN, R/W, RS)
TRISC = 0x00;         //Set PORT C as Output
ANSEL = 0x00;
ANSELH = 0x00;
ADCON0 =0x00;          //’disable ADC
CM1CON0 =0x00;          //’disable COMPARATOR 1
CM2CON0 =0x00;          //’disable COMPARATOR 2
T1CON =0x00;          //’disable TIMER1
OPTION = 0x07;
T0IE = 1;             // Enable Timer
GIE = 1;             // Enable Global Interrupt
y=0;               //Initial counter value=0

//LCD Function
instruction(0x0C);     //Display on/off & cursor HEX 08-0F (1st)
instruction(0x38);     //Function Set HEX 20-3F
instruction(0x01);     //Clear Display HEX 01 (last)


  while(1)   
   {

   if (RA0==0){

   if (RA1==1){

         y++;
         if (reload==0){
         b=y;
         a=0;
         
         y=0;
         for(s=0;s<10000;s++){}
            
         }
         }

      }

      rpm=((y*60)/(3*20)); //formula for rpm
      out();
      out2();
}
      
}
static void interrupt
isr(void)
{
if(reload == 0)
{
reload = 16;
}
reload--;
T0IF = 0; // Clear Timer Interrupt Flag
}

« Last Edit: March 01, 2011, 10:39:18 PM by twinsratio »

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Capture Pulse Per Second From Quadrature Encoder.
« Reply #1 on: March 02, 2011, 05:01:45 AM »
Hi,

Look the datasheet for the controller in question - the section on setting up the SFR's etc. and take a good hard stare at your routine starting with "while(1)" as well.

Please post in "Software", it has nothing to do in "Electronics".
Please use the "code" tag.
Please add plenty of comments, as it is impossible to know what you are planning it to be doing without them.
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,941
  • Helpful? 98
Re: Capture Pulse Per Second From Quadrature Encoder.
« Reply #2 on: March 02, 2011, 08:31:26 AM »
In addition to Soeren's suggestions:
Run your code in the MPLAB Simulator to check timing, code flow and all register values. I debug code to better than 95% using the simulator.

Since you are use the variable "reload" in two functions it needs to be declared as 'volatile'. See the HiTech manual.

A better way than test the variable 'reload' in two places is to test 'reload' in the ISR and if zero set a flag (a single bit in a variable) then is tested and clear in main().


 


Get Your Ad Here

data_list