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 CCS Technical Support

PIC18F4431 & QuadratureEncoderInterface Module on PCWH 4
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Jan 30, 2009 8:58 am     Reply with quote

Hi Ttelmah,

I have tried this but the LCD is now blank.

Code:

#include <18F2331.h>
#include <flex_lcd.c>
#use delay(clock=20000000)
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,PWMPIN
//configuracoes QEI
// Definitions
#byte QEICON      = 0xFB6  // Quadrature Encoder Interface control register
#byte DFLTCON     = 0xF60  // Digital filter control register
#byte POSCNTH     = 0xF67  // Position register high byte
#byte POSCNTL     = 0xF66  // Position register low byte
int PPR;
union
{
   int8 bytes[2];
   int16 word;
} position_union;
//
void main()
{
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();   
   QEICON = 0xB4;       
   DFLTCON = 0x7B;
   enable_interrupts(GLOBAL);
  while(1)
   {
      position_union.bytes[0] = POSCNTL;
      position_union.bytes[1] = POSCNTH;
      // Workaround for hardware bug caused by lack of buffering
      if ((position_union.bytes[0] == 0) || (position_union.bytes[0] == 0xFF))
      {
         position_union.bytes[0] = POSCNTL;
         position_union.bytes[1] = POSCNTH;
      } 
  if (PPR==1){
      position_union.word = ((position_union.word/4)+1);
      lcd_gotoxy(1,1);
      printf(lcd_putc,"\fpos = %5lu\n\r",position_union.word);
      delay_ms(50);
     PPR=0;         
   }
}
}

#int_IC2QEI
void ic2qei_interrupt (void) {
PPR=1;
}




Is this the right technique??

Carl
Ttelmah
Guest







PostPosted: Fri Jan 30, 2009 9:57 am     Reply with quote

A number of comments.
Look at using a int1, rather than an int. The compiler can do boolean operations on these much faster.
Second, record the value in the interrupt. By the time you arrive at the print, the value may have changed.
How is the code ever goint to get to the interrupt handler?. You have not enabled the interrupt. How is the code ever going to get to the print?. I is after your while loop....
Code:

int1 PPR=false;
int16 storedval;
int16 position;
#byte position=0xF66;

//
void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();   
   QEICON = 0xB4;       
   DFLTCON = 0x7B;
   enable_interrupts(GLOBAL);
   enable_interrupts (INT_IC2QEI);
   while(1) {
      if (PPR){
         PPR=false;
         lcd_gotoxy(1,1);
         printf(lcd_putc,"\fpos = %5lu\n\r",storedval);
      }
   }
}

#int_IC2QEI
void ic2qei_interrupt (void) {
    PPR=true;
    storedval=position;
}



Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Jan 30, 2009 10:41 am     Reply with quote

Just tried it Ttelmah,

It just displays pos = 0.

This doesn't surprise me because the registor is changed to 0 on index match. I need to find a way of getting the value as it rolls over from 1023 to 0. It seems that when i go into the interrupt there is no point getting the value then because it is 0.

Any ideas?

Best Regards
carl
Ttelmah
Guest







PostPosted: Fri Jan 30, 2009 3:10 pm     Reply with quote

Just turn off the option to reset the counter on the index. Doesn't make much sense to read it here otherwise.....

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Jan 30, 2009 3:49 pm     Reply with quote

Hi Ttelmah,

Sorry i dont undersatnd. The whole idea is that the index provides an interrupt for the line count (x4 in A & B Quadrature).

My task is to make a PPR checker, so that whatever the pulse count of the encoder (from 1 to 5000) it will be displayed onto the LCD. all I want is the PPR value at roll over (Index) to be displayed.

If I turn off this function it will never interrupt at the required time and the pulse count (PPR) will not be detected.

I am not interested in actual position just the PPR of the encoder to be displayed.

Best regards
Carl
Ttelmah
Guest







PostPosted: Fri Jan 30, 2009 4:16 pm     Reply with quote

At the index, the count will always be the same. It'l be zero, if you have asked the unit to reset, or a multiple of 4096, if not. The number of lines on the encoder is fixed, so this number will never change. You can specify X4 mode, with the counter set to reset on MAXCNT, and then you will get an interrupt on index, but the counter won't reset. However, reading the signal here, seems 'pointless'.....
If you want to display a value 'on the move', then far better to update the display at a time interval like half a second, and the count will then reflect how the unit is moving.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Fri Jan 30, 2009 4:32 pm     Reply with quote

Hi Ttelmah,

Thanks for all your assistance. The whole idea is to only display the PPR of a given encoder (this will range between 1 and 5000).

I work for Hohner Automation and am making a PPR checker in the QC department. Various encoders with different pulse counts go through the QC department every day and I want a way of confirming the PPR matches the stated encoder very quickly.

I could do it with up counters and use the marker as a trigger - then display the count that was generated - but I want to use the PIC.

I do not want to display the encoder on the move - only the final PPR.

I thought that using this module would be easier and more accurate than using a standard L_H interrupt (For Channel A), then count the interrupts (lines on the disc), then when the index is detected (this will then be the PPR value), the ppr value is displayed, and the counter is reset.

so for a 1024ppr encoder - ONLY 1024 is ever displayed on the LCD.
or for a 250ppr encoder - ONLY 250 is ever displayed on the LCD.

Best Regards
Carl
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

PostPosted: Fri Jan 30, 2009 8:02 pm     Reply with quote

Your purpose is now clear although there is a potential (or likely) problem with the quality of the signals arriving at the pins of the PIC. There are lots of output options and this has to be taken into consideration.

For example, if the option has differential outputs and a long cable attached, then a differential receiver chip is needed to convert the signals back to single-ended TTL to get good signals.

Just about any PIC could be used to count the pulses from one phase between successive index pulses. The advantage though I guess with this PIC is that all signals can be checked for correct operation at the same time (only if the x4 mode is used, in x2 mode only the phase A edges are counted). The lack of a strobed latch on the counter is disappointing.

I think that the following should work.
Setup the QEI module not to clear the count with the index, and x4 mode. Then rotating the shaft clockwise measure the difference in counts between successive index interrupts, and divide the result by 4. Clearing the counter is not necessary.


Last edited by Kenny on Sat Jan 31, 2009 3:29 pm; edited 1 time in total
Ttelmah
Guest







PostPosted: Sat Jan 31, 2009 3:39 am     Reply with quote

Some further comments apply:
1) If you only want to count the pulses per rev, why bother with x4 mode?. You are increasing the speed at which events occur, for no reason.
2) If you are just doing this as a way of testing the PPR of the encoder, why bother with quadrature at all. Just count the pulses on the A channel, or the B channel.
3) If this is all it has to do, then don't fiddle around with interrupts at all. Just have the code sitting in a tight loop, and when it sees the index signal change, record the count. When it changes again, record the new count.
So, something like:
Code:

int16 storedval;
int16 currentval;
itn16 temp;
int16 position;
#byte position=0xF66;
#define INDEX_PIN (PIN_XX) //define your pin here
int16 maxcnt;
#byte maxcnt=0xF64

//
void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();   
   QEICON = 0xA8;       
   DFLTCON = 0x7B;
   maxcnt=0;
   while(input(INDEX_PIN==0)) ; //wait for pin to go high
   storedval=position;
   while(1) {
         while(input(INDEX_PIN==1)); //make sure pin has dropped
         while(input(INDEX_PIN==0)); //wait to go high again
         //This is recorded on the rising edge of the index pulse
         currentval=position;
         temp=currentval-storedval;
         //arithmetic will wrap if the counter has gone past zero, and
         //still give the correct 'difference' value.
         storedval=currentval
         lcd_gotoxy(1,1);
         printf(lcd_putc,"\fpos = %5lu\n\r",temp/2);
      }
   }
}


Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Jan 31, 2009 4:40 am     Reply with quote

The PPR counter is my primary task, although I would also like to display the RPM on the second line - this is another reason why I am using this module.

Regardless though I have tried your code below and it still just shows pos=0.

Code:

#include <18F2331.h>
#include <flex_lcd.c>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,PWMPIN
#use delay(clock=20000000)
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#byte QEICON      = 0xFB6  // Quadrature Encoder Interface control register
#byte DFLTCON     = 0xF60  // Digital filter control register

int16 storedval;
int16 currentval;
int16 temp;
int16 position;
#byte position    = 0xF66
#define INDEX_PIN (PIN_A2)
int16 maxcnt;
#byte maxcnt      = 0xF64



void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();   
   QEICON = 0xA8;       
   DFLTCON = 0x7B;
   maxcnt=0;
   while(input(INDEX_PIN==0)) ; //wait for pin to go high
   storedval=position;
   while(1) {
         while(input(INDEX_PIN==1)); //make sure pin has dropped
         while(input(INDEX_PIN==0)); //wait to go high again
         //This is recorded on the rising edge of the index pulse
         currentval=position;
         temp=currentval-storedval;
         //arithmetic will wrap if the counter has gone past zero, and
         //still give the correct 'difference' value.
         storedval=currentval;
         lcd_gotoxy(1,1);
         printf(lcd_putc,"\fpos = %5lu\n\r",temp/2);
         delay_ms(50);
      }
   }


I think i would like to get your idea for the PPR above working first.

After that would it be possible to scrap the index idea in the QEI as Kenny has said but keep the counter running and set up another external interrupt (INT1 on RC4 - this will be the new index interrupt) by setting the INTCON2 and INTCON3 registers.

Carl
Ttelmah
Guest







PostPosted: Sat Jan 31, 2009 5:45 am     Reply with quote

Bracketting wrong in the input lines. You need:
while(input(INDEX_PIN)==1)

etc..

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Jan 31, 2009 5:58 am     Reply with quote

Still the same.

Is it because the count gets reset to 0 whatever the conditions - wether it be index or max count.

Carl

Code:
#include <18F2331.h>
#include <flex_lcd.c>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,PWMPIN
#use delay(clock=20000000)
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#byte QEICON      = 0xFB6  // Quadrature Encoder Interface control register
#byte DFLTCON     = 0xF60  // Digital filter control register 
int16 storedval;
int16 currentval;
int16 temp;
int16 position;
#byte position    = 0xF66
#define INDEX_PIN (PIN_A2)
int16 maxcnt;
#byte maxcnt      = 0xF64



void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();
   QEICON = 0xA8;       
   DFLTCON = 0x7B;
   maxcnt=0;
   while(input(INDEX_PIN)==0);  //wait for pin to go high
   storedval=position;    
   while(1) {
         while(input(INDEX_PIN)==1); //make sure pin has dropped
         while(input(INDEX_PIN)==0); //wait to go high again
         //This is recorded on the rising edge of the index pulse
           currentval=position;
           temp=currentval-storedval;
         //arithmetic will wrap if the counter has gone past zero, and
         //still give the correct 'difference' value.
         storedval=currentval;
         lcd_gotoxy(1,1);
         printf(lcd_putc,"\fpos = %5lu\n\r",temp/2);
         delay_ms(50);
      }
   }
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Jan 31, 2009 6:05 am     Reply with quote

I tried this as well which should incremnet the value - just to see if it was working - but the display is still reading pos =0.

Code:

#include <18F2331.h>
#include <flex_lcd.c>
#fuses HS,NOWDT,PUT,BROWNOUT,NOLVP,PWMPIN
#use delay(clock=20000000)
#use fast_io(a)     
#use fast_io(b)
#use fast_io(c)     
#byte QEICON      = 0xFB6  // Quadrature Encoder Interface control register
#byte DFLTCON     = 0xF60  // Digital filter control register 
int16 storedval;
int16 currentval;
int16 temp;
int16 position;
#byte position    = 0xF66
#define INDEX_PIN (PIN_A2)
int16 maxcnt;
#byte maxcnt      = 0xF64



void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();
   QEICON = 0xA8;       
   DFLTCON = 0x7B;
   maxcnt=0;
   while(input(INDEX_PIN)==0);  //wait for pin to go high
   storedval=position;    
   lcd_gotoxy(1,1);
   printf(lcd_putc,"\fpos = %5lu\n\r",position/2);
   delay_ms(50);
}



I then went back and tried an older code and it did increment - so it is not the hardware.

Carl
Ttelmah
Guest







PostPosted: Sat Jan 31, 2009 6:17 am     Reply with quote

Is the display actually updating?. Try with maxcnt=0xFFFF.
I don't remember whether the value clears when it reaches maxcnt, or the cycle afterwards. I was assuming it did it when it it reached, but if it does it the cycle after, it'll never be able to count past 0. I'd need to check the data sheet, for where in the cycle it does the test.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Sat Jan 31, 2009 6:28 am     Reply with quote

Hi Ttelmah,
I changed it so max = 0xFFFF.

The code below works and displays pos=1024

Code:

void main(void) {
   setup_timer_5(T5_INTERNAL|T5_DIV_BY_1);
   lcd_init();
   QEICON = 0xA8;       
   DFLTCON = 0x7B;
   maxcnt=0xFFFF ;
   while(input(INDEX_PIN)==0);  //wait for pin to go high
   storedval=position;    
   while(1) {
         while(input(INDEX_PIN)==1); //make sure pin has dropped
         while(input(INDEX_PIN)==0); //wait to go high again
         //This is recorded on the rising edge of the index pulse
           currentval=position;
           temp=currentval-storedval;
         //arithmetic will wrap if the counter has gone past zero, and
         //still give the correct 'difference' value.
         storedval=currentval;
         lcd_gotoxy(1,1);
         printf(lcd_putc,"\fpos = %5lu\n\r",temp/2);
         delay_ms(50);
      }
   }


So it looks good. I am going to try it now with different encoders (with different PPR's) to see if the results match. I will let you know in the next 1/2 hour.

Thanks for your help Ttelmah.

Carl
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, 3  Next
Page 2 of 3

 
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