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.

Interfacing Stepper Motor , HC-SR04(Ultrasonic Sensor) to PIC18F4550

Status
Not open for further replies.

bala kumaran

Newbie level 4
Joined
Feb 4, 2013
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,382
I've written a code in MPlab, the compiler im making use is C18 and i want it to do as follows..
Once we started the process, TIMER in PIC 18F4550 and also Ultrasonic sensor(HC-SR04) must be triggered. If obstacle is detected by the sensor,the present value of the timer have to be taken for calculating distance between obstacle and the model by formula mentioned in coding. and if calculated distance is equal or below threshold level (say 20 cm) the running of motor should stop and the sensor should sense right side for obstacle and if there is nothing the movement has to be started and also the timer,HC-SR 04 must be triggered...the following is my code and i need someone to fix the problem associated with it...

#include <p18F4550.h>
#define trig PORTCbits.RC1
#define echo PORTCbits.RC2
// #include<p18f4550.h>
//#include<delay.h>
#define s1 PORTDbits.RD0
#define s2 PORTDbits.RD1
#define s3 PORTDbits.RD2
#define s4 PORTDbits.RD3
#define s5 PORTDbits.RD4
void TMR0_init();
int time , value;
float distance; // Defining variables
unsigned char ECHO,input;
void delay(unsigned int itime)
{
unsigned int i;
unsigned char j;
for(i=0;i<itime;i++)
for(j=0;j<128;j++);
}

void TMR0_init()
{
TMR0H=0xD2;
TMR0L=0x39;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
// T0CONbits.TMR0IF=0;
}
void TMR_CNT(unsigned float value)
{
T0CON=0b10000000;
if (ECHO!=1)
T0CONbits.TMR0ON=0b10000000;
else
value=T0CON;
}
void TIMER_ON()
{
TRISC=0;
TRISCbits.TRISC1=0;
T0CONbits.T0PS0 = 1;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS2 = 1;
T0CONbits.PSA = 0;
T0CONbits.T0SE = 0;
T0CONbits.T0CS = 0;
T0CONbits.T08BIT = 0;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 0;
T0CONbits.TMR0ON = 1;
}
void CW_ROTATION()
{

LATB=0b1001; LATA=0b1110;
delay(300);

LATB=0b1100; LATA=0b1101;
delay(300);

LATB=0b0110; LATA=0b1011;
delay(300);

LATB=0b0011; LATA=0b0111;
delay(300);
}
void CCW_ROTATION()
{
LATB=0b1110; LATA=0b1001;
delay(300);

LATB=0b1101; LATA=0b1100;
delay(300);

LATB=0b1011; LATA=0b0110;
delay(300);

LATB=0b0111; LATA=0b0011;
delay(300);

}
void RIGHT_TURN()
{
LATB=0b0000;
delay(300);

LATA=0b1110;
delay(300);

LATA=0b1101;
delay(300);

LATA=0b1011;
delay(300);

LATA=0b0111;
delay(300);
}
void LEFT_TURN()
{
LATA=0b0000;
delay(300);

LATB=0b1001;
delay(300);

LATB=0b1100;
delay(300);

LATB=0b0110;
delay(300);

LATB=0b0011;
delay(300);
}
void SENSOR_RIGHT_TURN()
{

LATE=0b1001;
delay(300);

LATE=0b1100;
delay(300);

LATE=0b0110;
delay(300);

LATE=0b0011;
delay(300);
}
void SENSOR_LEFT_TURN()
{
LATA=0b0000;
delay(300);

LATB=0b1001;
delay(300);

LATB=0b1100;
delay(300);

LATB=0b0110;
delay(300);

LATB=0b0011;
delay(300);
}
void WORST_CASE()
{
LATA=0; LATB=0;
SENSOR_RIGHT_TURN();
{
if(TRISCbits.TRISC2!=1)
{
LATB=0b0000;
delay(300);

LATA=0b1110;
delay(300);

LATA=0b1101;
delay(300);

LATA=0b1011;
delay(300);
CW_ROTATION();
TIMER_ON();
TRISCbits.TRISC1 = 1;
delay(300);
}
if(TRISCbits.TRISC2 = 1)
{ LATA=0; LATB=0;
SENSOR_LEFT_TURN();
TRISCbits.TRISC1 = 1;
delay(300);
}
if(TRISCbits.TRISC2!=1)
{
LATA=0b0000;
delay(300);

LATB=0b1001;
delay(300);

LATB=0b1100;
delay(300);

LATB=0b0110;
delay(300);
CW_ROTATION();
TIMER_ON();
TRISCbits.TRISC1 = 1;
delay(300);
}
}
}
int alreadySet = 0;
unsigned int timerValue;
unsigned char* ptr;
void main()
{
unsigned int i=0;
TRISB=0;
TRISA=0;
TRISE=0;
while(1)
if(s5==1)
{
if(s1==1)
{
// WHEEL MOVEMENT
LATB=0b1001; LATA=0b1110;
delay(300);

LATB=0b1100; LATA=0b1101;
delay(300);

LATB=0b0110; LATA=0b1011;
delay(300);

LATB=0b0011; LATA=0b0111;
delay(300);

// TIMER ON

TRISC=0;
TRISCbits.TRISC1=0;
T0CONbits.T0PS0 = 1;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS2 = 1;
T0CONbits.PSA = 0;
T0CONbits.T0SE = 0;
T0CONbits.T0CS = 0;
T0CONbits.T08BIT = 0;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 0;
T0CONbits.TMR0ON = 1;
TRISCbits.TRISC1 = 1;
delay(300);
}
if(TRISCbits.TRISC2!=1)
{
LATB=0b1001; LATA=0b1110;
delay(300);

LATB=0b1100; LATA=0b1101;
delay(300);

LATB=0b0110; LATA=0b1011;
delay(300);

LATB=0b0011; LATA=0b0111;
delay(300);
}
if(TRISCbits.TRISC2==1)
{
T0CONbits.T0PS0 = 1;
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS2 = 1;
T0CONbits.PSA = 0;
T0CONbits.T0SE = 0;
T0CONbits.T0CS = 0;
T0CONbits.T08BIT = 0;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 0;
T0CONbits.TMR0ON = 0;
return (value);
distance = (value * 343.2) / 2 ;
{
if (distance>20)
{
CW_ROTATION();
TIMER_ON();
}
else
{
LATA=0; LATB=0;
SENSOR_RIGHT_TURN();
TRISCbits.TRISC1 = 1;
delay(300);
{
if(TRISCbits.TRISC2!=1)
{
LATB=0b0000;
delay(300);

LATA=0b1110;
delay(300);

LATA=0b1101;
delay(300);

LATA=0b1011;
delay(300);
CW_ROTATION();
TIMER_ON();
TRISCbits.TRISC1 = 1;
delay(300);
}
if(TRISCbits.TRISC2 = 1)
{
LATA=0; LATB=0;
SENSOR_LEFT_TURN();
TRISCbits.TRISC1 = 1;
delay(300);
{
if(TRISCbits.TRISC2!=1)
{
LATA=0b0000;
delay(300);

LATB=0b1001;
delay(300);

LATB=0b1100;
delay(300);

LATB=0b0110;
delay(300);
CW_ROTATION();
TIMER_ON();
TRISCbits.TRISC1 = 1;
delay(300);
}
else if(TRISCbits.TRISC2==1)
{
CCW_ROTATION();
delay(1500);
WORST_CASE();
}
}
}
}
}
}
}
}
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top