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.
![Smiley :)](https://www.eevblog.com/forum/Smileys/default/xsmiley.gif.pagespeed.ic.R8GFI-pF6f.png)
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 ==
![Cool 8)](https://www.eevblog.com/forum/Smileys/default/xcool.gif.pagespeed.ic.cOxV6sPNRd.png)
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;
}
}