Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Help with RC5 decoding clocks in PIC 16F690

Status
Not open for further replies.

Eng_MC

Member level 1
Joined
Nov 16, 2010
Messages
39
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,865
hi i am using a pic16f690 to control a dual stepper motor system using a 20MHz clock to control the system , I want to control the system from remotely using infra red using want Rc5 protocol to contol the system . the codes i have seen all use 4MHz, i think because of the speed of the code to read the input from the RC5 . I want to use the rc5 beacuse of the available number of keys i am at cross road to continue to try to use a disfunctional HT12A and HT12D encoder and decoder. Please any suggestions on how to adress this problem. Thanks
 

hi there ca anyone help me with this code please i have written up the major parts namely thd functions of the code but am having dificulties with the struct and enum setup can some one help me set it up please;;;;;;;;;

//////**********Chamwasu Musa Gali******
//////*********Advanced Stepper Motor Control Codes*******
//////********Final Year Project Codes******************
//////********BEng Electrical and Electronics Engineering******
//////********University of Hertfordshire*******************

//
#include <htc.h>
#include <pic.h>
#define stepcnt 300 //max number of steps
#define _XTAL_FREQ 20000000

void Run_Motor1_cw(void);
void Run_Motor2_cw(void);
void Accel_Motor1_cw(void);
void Deccel_Motor1_cw(void);
void Accel_Motor2_cw(void);
void Deccel_Motor2_cw(void);
void Run_Motor1_ccw(void);
void Run_Motor2_ccw(void);
void Accel_Motor1_ccw(void);
void Deccel_Motor1_ccw(void);
void Accel_Motor2_ccw(void);
void Deccel_Motor2_ccw(void);

enum MotorID {Motor1, Motor2};
enum Bool {False, True};
enum Direction {Clockwise, Anticlockwise};
int iSpeed=0;

struct Motor
{
enum MotorID MotorName;
enum Direction ActiveDirection;
enum Bool Active;
int iMotorSpeed;
int fMotorSpeed;
};


void main(void)
{
struct Motor PrimaryMotor, SecondaryMotor;
PrimaryMotor.MotorName=Motor1;
SecondaryMotor.MotorName=Motor2;
PrimaryMotor.iMotorSpeed=0;
SecondaryMotor.iMotorSpeed=0;
PrimaryMotor.ActiveDirection=Clockwise;
SecondaryMotor.ActiveDirection=Clockwise;
PrimaryMotor.Active=True;
SecondaryMotor.Active=False;

//this is an example to check if Primary motor is active!!

if(PrimaryMotor.Active ==True)
{
//putstatments to drive primary motor
if(PrimaryMotor.ActiveDirection==Clockwise)
Run_Motor1_cw();
else
Run_Motor1_ccw();
}

if(SecondaryMotor.Active ==True)
{
//put statments to drive secondary motor
if(SecondaryMotor.ActiveDirection==Clockwise)
Run_Motor2_cw();
else
Run_Motor2_ccw();
}

if(PrimaryMotor.iMotorSpeed==1)//if final motor speed is select accelerate motor to speed up
{
if(PrimaryMotor.ActiveDirection==Clockwise)
Accel_Motor1_cw();
else
Accel_Motor1_ccw();
}

if(SecondaryMotor.iMotorSpeed==1)
{
if(SecondaryMotor.ActiveDirection==Clockwise)
Accel_Motor2_cw();
else
Accel_Motor2_ccw();
}

if(PrimaryMotor.fMotorSpeed==1)//if iniital motor speed is select decelerate motor to slow down
{
if(PrimaryMotor.ActiveDirection==Clockwise)
Deccel_Motor1_cw();
else
Deccel_Motor1_ccw();
}

//if(SecondaryMotor.fMotorSpeed==1)
{
/*if(SecondaryMotor.ActiveDirection==Clockwise)
Deccel_Motor2_cw();
else
Deccel_Motor2_ccw();*/
}

/*if(PrimaryMotor.iMotorSpeed == 1)
{
//statements to speed up motor

if(PrimaryMotor.ActiveDirection==Clockwise) //&& PrimaryMotor.ActiveDirection=Clockwise)
Accel_Motor1_cw();
else
Accel_Motor1_ccw();
}

if(PrimaryMotor.iMotorSpeed == 0)
{
if(PrimaryMotor.ActiveDirection==Clockwise)
Deccel_Motor1_cw();
else
Deccel_Motor1_ccw();
}

if(SecondaryMotor.iMotorSpeed == 1)
{
if(SecondaryMotor.Active==True)
if(SecondaryMotor.ActiveDirection==Clockwise)
Accel_Motor2_cw();
else
Accel_Motor2_ccw();
}

if(SecondaryMotor.iMotorSpeed==0)
{
if(SecondaryMotor.ActiveDirection==Clockwise)
Deccel_Motor2_cw();
else
Deccel_Motor2_ccw();
}

*/



//--------------------------------------------------------------------------------------------

//You will need to write a function for the speed control of the motors.

//You will also need to write statments to assign the properities of the Motor structure....
//You will need to connect the IR reciever to a port of the PIC and read the data that come in.
//for example if Motor selection is pressed and you need to know which pin in the port is activated.. read it and toggle the motors states.... ie

//if you want to activate Primary motor
// PrimaryMotor.Activate=True;
// SecondaryMotor.Activate=False;



TRISA = 0xFF; //SET PORT A AS INPUT
PORTA = 0; //INITIALISE PORTA AS 0

TRISB = 0xFF; //SET PORT B AS INPUT PORT
PORTB = 0; //INITIALISE PORT B AS 0

TRISC = 0x00; //SET PORT C AS AN OUTPUT PORT

INTE = 1;
INTF = 0;
INTEDG = 1;
GIE = 1;

while(1)
{

Run_Motor1_cw();
__delay_ms(3000);
Run_Motor2_cw();
__delay_ms(3000);

}

}

void Run_Motor1_cw(void) //function to run motor 1 at constant speed
{
int b;
PORTC = 0x32; //reset motor phases to initial position 0101 and light up led to indicate motor 1 is active and wait for the step signal
__delay_ms(50);

for(b=0;b<=stepcnt;b++) //loop to step 100 times running motor1
{
PORTC = 0xB2;
__delay_ms(250);
PORTC= 0xB3;
__delay_ms(250);
}
}

void Run_Motor2_cw(void) //function to run motor 2 at constant speed
{
int d;
PORTC = 0x52; //turn on led to indicate motor 2 is about to start running and wait for the step signal
__delay_ms(50);

for(d=0;d<=stepcnt;d++) //loop to run motor 2 for 100 steps
{
PORTC = 0xD2;
__delay_ms(250);
PORTC = 0xD6;
__delay_ms(250);
}
}

void Accel_Motor1_cw(void) //function to accelerate motor 1
{
int x;
PORTC = 0xB2;//delay tables to accelerate motor 1
__delay_ms(250);
PORTC = 0xB3;
__delay_ms(250);
PORTC = 0xB2;
__delay_ms(250);
PORTC = 0xB3;
__delay_ms(250);
PORTC = 0xB2;
__delay_ms(220);
PORTC = 0xB3;
__delay_ms(220);
PORTC = 0xB2;
__delay_ms(220);
PORTC = 0xB3;
__delay_ms(220);
PORTC = 0xB3;
__delay_ms(200);
PORTC = 0xB2;
__delay_ms(200);
PORTC = 0xB3;
__delay_ms(200);
PORTC = 0xB2;
__delay_ms(200);
PORTC = 0xB3;
__delay_ms(170);
PORTC = 0xB2;
__delay_ms(170);
PORTC = 0xB3;
__delay_ms(170);
PORTC = 0xB2;
__delay_ms(170);
PORTC = 0xB3;
__delay_ms(140);
PORTC = 0xB2;
__delay_ms(140);
PORTC = 0xB3;
__delay_ms(140);
PORTC= 0xB2;
__delay_ms(140);
PORTC = 0xB3;
__delay_ms(100);
PORTC= 0xB2;
__delay_ms(100);
PORTC= 0xB3;
__delay_ms(100);
PORTC= 0xB2;
__delay_ms(100);
PORTC = 0xB3;
__delay_ms(60);
PORTC =0xB2;
__delay_ms(60);
PORTC = 0xB3;
__delay_ms(60);
PORTC = 0xB2;
__delay_ms(60);
PORTC = 0xB3;
__delay_ms(30);
PORTC = 0xB2;
__delay_ms(30);
PORTC = 0xB3;
__delay_ms(30);
PORTC = 0xB2;
__delay_ms(30);
for(x=0;x<=stepcnt;x++) //loop to maintain final speeed after delay table implementation for 100 steps
{
PORTC = 0xB2;
__delay_ms(15);
PORTC = 0xB3;
__delay_ms(15);
}
}

void Deccel_Motor1_cw(void) //function to decelerate motor 1
{
int y;
PORTC = 0xB2; //delay tables to decelerate motor 1
__delay_ms(15);
PORTC = 0xB3;
__delay_ms(15);
PORTC = 0xB2;
__delay_ms(25);
PORTC = 0xB3;
__delay_ms(25);
PORTC= 0xB2;
__delay_ms(25);
PORTC = 0xB3;
__delay_ms(25);
PORTC= 0xB2;
__delay_ms(60);
PORTC= 0xB3;
__delay_ms(60);
PORTC= 0xB2;
__delay_ms(60);
PORTC = 0xB3;
__delay_ms(60);
PORTC = 0xB2;
__delay_ms(80);
PORTC = 0xB3;
__delay_ms(80);
PORTC = 0xB2;
__delay_ms(80);
PORTC = 0xB3;
__delay_ms(80);
PORTC = 0xB2;
__delay_ms(120);
PORTC = 0xB3;
__delay_ms(120);
PORTC = 0xB2;
__delay_ms(120);
PORTC = 0xB3;
__delay_ms(120);
PORTC= 0xB2;
__delay_ms(140);
PORTC = 0xB3;
__delay_ms(140);
PORTC = 0xB2;
__delay_ms(140);
PORTC = 0xB3;
__delay_ms(140);
PORTC = 0xB2;
__delay_ms(140);
PORTC = 0xB3;
__delay_ms(160);
PORTC = 0xB2;
__delay_ms(160);
PORTC = 0xB3;
__delay_ms(160);
PORTC = 0xB2;
__delay_ms(180);
PORTC = 0xB3;
__delay_ms(180);
PORTC = 0xB2;
__delay_ms(180);
PORTC = 0xB3;
__delay_ms(180);
PORTC= 0xB2;
__delay_ms(180);
PORTC = 0xB3;
__delay_ms(200);
PORTC= 0xB2;
__delay_ms(200);
PORTC= 0xB3;
__delay_ms(220);

for(y=0;y<=stepcnt;y++) //loop to maintain final speed after deceleration table implentation
{
PORTC = 0xB2;
__delay_ms(250);
PORTC = 0xB3;
__delay_ms(250);
}
}

void Accel_Motor2_cw(void) //function to accelerate motor 2
{
int m;
PORTC= 0xD2; //delay tables to accelerate motor two
__delay_ms(250);
PORTC = 0xD6;
__delay_ms(250);
PORTC = 0xD2;
__delay_ms(250);
PORTC = 0xD6;
__delay_ms(250);
PORTC = 0xD2;
__delay_ms(220);
PORTC = 0xD6;
__delay_ms(220);
PORTC = 0xD2;
__delay_ms(220);
PORTC = 0xD6;
__delay_ms(220);
PORTC = 0xD2;
__delay_ms(200);
PORTC = 0xD6;
__delay_ms(200);
PORTC = 0xD2;
__delay_ms(200);
PORTC = 0xD6;
__delay_ms(200);
PORTC = 0xD2;
__delay_ms(170);
PORTC = 0xD6;
__delay_ms(170);
PORTC = 0xD2;
__delay_ms(170);
PORTC = 0xD6;
__delay_ms(170);
PORTC = 0xD2;
__delay_ms(140);
PORTC = 0xD6;
__delay_ms(140);
PORTC = 0xD2;
__delay_ms(140);
PORTC = 0xD6;
__delay_ms(140);
PORTC = 0xD2;
__delay_ms(100);
PORTC= 0xD6;
__delay_ms(100);
PORTC= 0xD2;
__delay_ms(100);
PORTC= 0xD6;
__delay_ms(100);
PORTC = 0xD2;
__delay_ms(60);
PORTC =0xD6;
__delay_ms(60);
PORTC = 0xD2;
__delay_ms(60);
PORTC = 0xD6;
__delay_ms(60);
PORTC = 0xD2;
__delay_ms(30);
PORTC = 0xD6;
__delay_ms(30);
PORTC = 0xD2;
__delay_ms(30);
PORTC = 0xD6;
__delay_ms(30);

for(m=0;m<=stepcnt;m++) //loop to maintain final speeed after delay table implementation for 100 steps
{
PORTC = 0xD2;
__delay_ms(15);
PORTC = 0xD6;
__delay_ms(15);
}
}

void Deccel_Motor2_cw(void)
{
int n;
PORTC = 0xD2; //delay tables to decelerate motor 2
__delay_ms(15);
PORTC = 0xD6;
__delay_ms(15);
PORTC = 0xD2;
__delay_ms(25);
PORTC= 0xD6;
__delay_ms(25);
PORTC = 0xD2;
__delay_ms(25);
PORTC= 0xD6;
__delay_ms(60);
PORTC= 0xD2;
__delay_ms(60);
PORTC= 0xD6;
__delay_ms(60);
PORTC = 0xD2;
__delay_ms(60);
PORTC = 0xD6;
__delay_ms(80);
PORTC = 0xD2;
__delay_ms(80);
PORTC = 0xD6;
__delay_ms(80);
PORTC = 0xD2;
__delay_ms(80);
PORTC = 0xD6;
__delay_ms(120);
PORTC = 0xD2;
__delay_ms(120);
PORTC = 0xD6;
__delay_ms(120);
PORTC = 0xD2;
__delay_ms(120);
PORTC= 0xD6;
__delay_ms(140);
PORTC = 0xD2;
__delay_ms(140);
PORTC = 0xD6;
__delay_ms(140);
PORTC = 0xD2;
__delay_ms(140);
PORTC = 0xD6;
__delay_ms(140);
PORTC = 0xD2;
__delay_ms(160);
PORTC = 0xD6;
__delay_ms(160);
PORTC = 0xD2;
__delay_ms(160);
PORTC = 0xD6;
__delay_ms(180);
PORTC = 0xD2;
__delay_ms(180);
PORTC = 0xD6;
__delay_ms(180);
PORTC = 0xD2;
__delay_ms(180);
PORTC= 0xD6;
__delay_ms(180);
PORTC = 0xD2;
__delay_ms(200);
PORTC= 0xD6;
__delay_ms(200);
PORTC= 0xD2;
__delay_ms(220);
for(n=0;n<=stepcnt;n++) //loop to maintain final speed after deceleration table implentation
{
PORTC = 0xD2;
__delay_ms(15);
PORTC = 0xD6;
__delay_ms(15);
}
}

void Run_Motor1_ccw(void) //function to run motor 1 counter clockwise
{
int n;
PORTC = 0x30; //initialise motor 1 and turn on led for motor 1
__delay_ms(10);
for(n=0;n<=stepcnt;n++) // loop to run motor one at contstan speed counter clockwise
{
PORTC = 0xB0;
__delay_ms(250);
PORTC = 0xB1;
__delay_ms(250);
}
}

void Run_Motor2_ccw(void)
{
int n;
PORTC = 0x50;
__delay_ms(10);
for(n=0;n<=stepcnt;n++)
{
PORTC = 0xD0;
__delay_ms(250);
PORTC = 0xD4;
__delay_ms(250);
}
}

void Accel_Motor1_ccw(void)//FUNCTION FOR CCW ACCELERATION OF M1
{
int x;
PORTC= 0xB0; //delay tables to accelerate motor 1
__delay_ms(250);
PORTC = 0xB1;
__delay_ms(250);
PORTC = 0xB0;
__delay_ms(250);
PORTC = 0xB1;
__delay_ms(220);
PORTC = 0xB0;
__delay_ms(220);
PORTC = 0xB1;
__delay_ms(220);
PORTC = 0xB0;
__delay_ms(220);
PORTC = 0xB1;
__delay_ms(200);
PORTC = 0xB0;
__delay_ms(200);
PORTC = 0xB1;
__delay_ms(200);
PORTC = 0xB0;
__delay_ms(200);
PORTC = 0xB1;
__delay_ms(170);
PORTC = 0xB0;
__delay_ms(170);
PORTC = 0xB1;
__delay_ms(170);
PORTC = 0xB0;
__delay_ms(170);
PORTC = 0xB1;
__delay_ms(140);
PORTC = 0xB0;
__delay_ms(140);
PORTC = 0xB1;
__delay_ms(140);
PORTC = 0xB0;
__delay_ms(140);
PORTC = 0xB1;
__delay_ms(100);
PORTC= 0xB0;
__delay_ms(100);
PORTC= 0xB1;
__delay_ms(100);
PORTC= 0xB0;
__delay_ms(100);
PORTC = 0xB1;
__delay_ms(60);
PORTC =0xB0;
__delay_ms(60);
PORTC = 0xB1;
__delay_ms(60);
PORTC = 0xB0;
__delay_ms(60);
PORTC = 0xB1;
__delay_ms(30);
PORTC = 0xB0;
__delay_ms(30);
PORTC = 0xB1;
__delay_ms(30);
PORTC = 0xB0;
__delay_ms(30);

for(x=0;x<=stepcnt;x++) //loop to maintain final speeed after delay table implementation for 100 steps
{
PORTC = 0xB0;
__delay_ms(15);
PORTC = 0xB1;
__delay_ms(15);

}
}
void Deccel_Motor1_ccw(void)
{
int a;
PORTC = 0xB0;
__delay_ms(15);
PORTC = 0xB1;
__delay_ms(15);
PORTC = 0xB0;
__delay_ms(25);
PORTC = 0xB1;
__delay_ms(25);
PORTC= 0xB0;
__delay_ms(25);
PORTC = 0xB1;
__delay_ms(25);
PORTC= 0xB0;
__delay_ms(60);
PORTC= 0xB1;
__delay_ms(60);
PORTC= 0xB0;
__delay_ms(60);
PORTC = 0xB1;
__delay_ms(60);
PORTC = 0xB0;
__delay_ms(80);
PORTC = 0xB1;
__delay_ms(80);
PORTC = 0xB0;
__delay_ms(80);
PORTC = 0xB1;
__delay_ms(80);
PORTC = 0xB0;
__delay_ms(120);
PORTC = 0xB1;
__delay_ms(120);
PORTC = 0xB0;
__delay_ms(120);
PORTC = 0xB1;
__delay_ms(120);
PORTC= 0xB0;
__delay_ms(140);
PORTC = 0xB1;
__delay_ms(140);
PORTC = 0xB0;
__delay_ms(140);
PORTC = 0xB1;
__delay_ms(140);
PORTC = 0xB0;
__delay_ms(140);
PORTC = 0xB1;
__delay_ms(160);
PORTC = 0xB0;
__delay_ms(160);
PORTC = 0xB1;
__delay_ms(160);
PORTC = 0xB0;
__delay_ms(180);
PORTC = 0xB1;
__delay_ms(180);
PORTC = 0xB0;
__delay_ms(180);
PORTC = 0xB1;
__delay_ms(180);
PORTC= 0xB0;
__delay_ms(180);
PORTC = 0xB1;
__delay_ms(200);
PORTC= 0xB0;
__delay_ms(200);
PORTC= 0xB1;
__delay_ms(220);

for(a=0;a<=stepcnt;a++)
{
PORTC = 0xB1;
__delay_ms(250);
PORTC = 0xB0;
__delay_ms(250);
}

}

void Accel_Motor2_ccw(void)
{
int z;
PORTC = 0xD0;
__delay_ms(250);
PORTC = 0xD4;
__delay_ms(250);
PORTC = 0xD0;
__delay_ms(250);
PORTC = 0xD4;
__delay_ms(220);
PORTC = 0xD0;
__delay_ms(220);
PORTC = 0xD4;
__delay_ms(220);
PORTC = 0xD0;
__delay_ms(220);
PORTC = 0xD4;
__delay_ms(200);
PORTC = 0xD0;
__delay_ms(200);
PORTC = 0xD4;
__delay_ms(200);
PORTC = 0xD0;
__delay_ms(200);
PORTC = 0xD4;
__delay_ms(170);
PORTC = 0xD0;
__delay_ms(170);
PORTC = 0xD4;
__delay_ms(170);
PORTC = 0xD0;
__delay_ms(170);
PORTC = 0xD4;
__delay_ms(140);
PORTC = 0xD0;
__delay_ms(140);
PORTC = 0xD4;
__delay_ms(140);
PORTC = 0xD0;
__delay_ms(140);
PORTC = 0xD4;
__delay_ms(100);
PORTC= 0xD0;
__delay_ms(100);
PORTC= 0xD4;
__delay_ms(100);
PORTC= 0xD0;
__delay_ms(100);
PORTC = 0xD4;
__delay_ms(60);
PORTC =0xD0;
__delay_ms(60);
PORTC = 0xD4;
__delay_ms(60);
PORTC = 0xD0;
__delay_ms(60);
PORTC = 0xD4;
__delay_ms(30);
PORTC = 0xD0;
__delay_ms(30);
PORTC = 0xD4;
__delay_ms(30);
PORTC = 0xD0;
__delay_ms(30);
PORTC = 0xD4;
__delay_ms(30);

for(z=0;z<=stepcnt;z++)
{
PORTC = 0xD0;
__delay_ms(15);
PORTC = 0xD4;
__delay_ms(15);
}
}

/*void Deccel_Motor2_ccw(void)
// {
// int n;
/* PORTC = 0xD0; //delay tables to decelerate motor 2 counter clockwise
__delay_ms(15);
PORTC = 0xD4;
__delay_ms(15);
PORTC = 0xD0;
__delay_ms(25);
PORTC = 0xD4;
__delay_ms(25);
PORTC= 0xD0;
__delay_ms(25);
PORTC = 0xD4;
__delay_ms(25);
PORTC= 0xD0;
__delay_ms(60);
PORTC= 0xD4;
__delay_ms(60);
PORTC= 0xD0;
__delay_ms(60);
PORTC = 0xD4;
__delay_ms(60);
PORTC = 0xD0;
__delay_ms(80);
PORTC = 0xD4;
__delay_ms(80);
PORTC = 0xD0;
__delay_ms(80);
PORTC = 0xD4;
__delay_ms(80);
PORTC = 0xD0;
__delay_ms(120);
PORTC = 0xD4;
__delay_ms(120);
PORTC = 0xD0;
__delay_ms(120);
PORTC = 0xD4;
__delay_ms(120);
PORTC= 0xD0;
__delay_ms(140);
PORTC = 0xD4;
__delay_ms(140);
PORTC = 0xD0;
__delay_ms(140);
PORTC = 0xD4;
__delay_ms(140);
PORTC = 0xD0;
__delay_ms(140);
PORTC = 0xD4;
__delay_ms(160);
PORTC = 0xD0;
__delay_ms(160);
PORTC = 0xD4;
__delay_ms(160);
PORTC = 0xD0;
__delay_ms(180);
PORTC = 0xD4;
__delay_ms(180);
PORTC = 0xD0;
__delay_ms(180);
PORTC = 0xD4;
__delay_ms(180);
PORTC= 0xD0;
__delay_ms(180);
PORTC = 0xD4;
__delay_ms(200);
PORTC= 0xD0;
__delay_ms(200);
PORTC= 0xD4;
__delay_ms(220);


for(n=0;n<=stepcnt;n++) //loop to maintain final speed after deceleration table implentation
{
PORTC = 0xD0;
__delay_ms(250);
PORTC = 0xD4;
__delay_ms(250);
}
}*/

/*void interrupt ISR(void)

{

if (INTEDG == 1 && INTF == 1) //IF INTERRUPT FLAG IS SET AND INTERRUPT IS AT RISING EDGE
{

Run_Motor1_cw(); //call motor 2 to start
}

INTEDG = ~INTEDG; //toggle intedg to allow for the two types of input from the transmitter

{
Run_Motor2_ccw(); //call motor 1 to start
}

INTF = 0; //CLEAR INTERRUPT FLAG BITl

}*/
 

Do you have use of a logic analyzer?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top