Author Topic: rotary encoder pic-program. anyone see the problem?  (Read 2228 times)

0 Members and 1 Guest are viewing this topic.

Offline matiseTopic starter

  • Contributor
  • Posts: 28
rotary encoder pic-program. anyone see the problem?
« on: January 27, 2014, 03:03:51 pm »
Dosent work.
I have the basefrequency coming up on oscilloscope but encoder gives nothing.
its my first try to programming an encoder and i thought i found a simple way.  :)
the dds is a AD9850-module.

#include <p18f1220.h>
#pragma config WDT=OFF , OSC=INTIO2 , PWRT=ON, LVP=OFF, MCLRE=OFF
#include <delays.h>

// data out pin 10
// clk out pin 11
// ud   out pin 12

unsigned long freq;
int count;
int pin1,pin2;
    int ny1;
    int ny2;
    int sum1;
    int sum2;

void main(void)
{
   //SET UP
   //OSCCON defaults to 31kHz. So no need to alter it.
   ADCON1 = 0x7F;        //All io is digital or 0b01111111 in binary
   TRISA = 0b11111111;   //sets PORTA as all inputs
   PORTA = 0b00000000;   //turns off PORTA outputs
   TRISB = 0b00000000;   //sets PORTB as all outputs
   PORTB = 0b00000000;   //sets off PORTB outputs, good start position
   
   freq =125413045;
   
   while(1);   
{
   
   for(count=0;count<32;count++) //Data count
   {
   if(freq=1)                 //if data is 1
   {
   PORTBbits.RB4 = 1;    //Data
   PORTAbits.RA0 = 1;    //rotary encoder in
        PORTAbits.RA1 = 1;    //rotary encoder in
    pin1 = PORTAbits.RA0;
    pin2 = PORTAbits.RA1;
   
    if(pin1==1)         
    sum1 = 0x08;            //if encoder/RA0 is 1 then bit 3 is 1
   
    if(pin2==1)
    sum1 |= (1<<2);         //mask RA1 to bit 2

    ny1 = PORTAbits.RA0;    //Next encoder rotation
    ny2 = PORTAbits.RA1;
   
    if(ny1 != pin1 || ny2 != pin2)    //if new numbers
    {
    if(ny1==1)
    sum1 |= (1<<1);         //if RA0 is 1 then bit 1 is 1
   
    if(ny2==1)
    sum1 |= (1<<0);         //if RA1 is 1 then bit 0 is 1
   
    if(sum1 == 1 || sum1 == 7 || sum1 == 14 || sum1 == 8) freq =+1024;  //clockwise movement
    if(sum1 == 11 || sum1 == 13 || sum1 == 4 || sum1 == 2) freq=-1024;  //counter-clockwise movement
   
}
   }
   else
   PORTBbits.RB4 = 0;
   
   PORTBbits.RB5 = 1;    //Clock
   PORTBbits.RB5 = 0;
   }
   
   PORTBbits.RB4 = 0;
   for(count=0;count<8;count++)
   {
   PORTBbits.RB5 = 1;
   PORTBbits.RB5 = 0;
   }
   
   PORTBbits.RB6 = 1;    //Ud
   PORTBbits.RB6 = 0;
   
   }
}
 

Offline angst7

  • Contributor
  • Posts: 16
  • Country: us
Re: rotary encoder pic-program. anyone see the problem?
« Reply #1 on: January 27, 2014, 03:30:36 pm »
You initialize freq, but then you set it to 1 each time you hit the if statement.

 if(freq=1)                 //if data is 1
   
should probably be:

 if (freq==1)


  Otherwise freq is never modified.   Should you increment or decrement it somewhere in the else block?
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: rotary encoder pic-program. anyone see the problem?
« Reply #2 on: January 27, 2014, 04:19:45 pm »
Your code will never get beyond this line:

Code: [Select]
while(1);
Try removing that semicolon for starters.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf