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.

[PIC] Programing using pic18f4580

Status
Not open for further replies.

LiyanaHmzh

Newbie level 4
Joined
Sep 15, 2017
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
61
Hi,
i got a problem with my project..
i must control the detection of distance by using ultrasonic sensor .
If my sensor detect , the motor will turn on ,i'm using 4 sensor , and 2 motor . If sensor 1 at the in front and sensor 2 at the behind detect hazard , motor 1 will turn on .. And if sensor 3 at right side and sensor 4 at left side detect hazard , motor 2 will turn on .
i must use PIC18F4580..my problem is i didn't get correct program cod..
can i get help to get the program use PIC18F4580
i need fast respond because i need to submit it in this week ..
 

What sensors are you using? Do you know how to program even a simple code? Which compiler are you using?
 
I'm using ultrrasonic sensor (HC-SR04) and mplab 18 compiler
 

Hello!

i got a problem with my project..
[...]
i need fast respond because i need to submit it in this week ..

If you want a fast and efficient response, please post your code and tell us what doesn't work
as expected... But I have the impression you have no program yet, so you might consider fo
first do your homework and then ask questions.

Dora.
 

there is my code :

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <p18f4580.h>
#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config MCLRE = ON
 
//XTAL
#define _XTAL_FREQ 20000000UL
 
//HC-SR04 S1
#define Trig PORTBbits.RB0
#define Echo PORTBbits.RB1
 
//HC-SR04 S2
#define Trig PORTBbits.RB2
#define Echo PORTBbits.RB3
 
//HC-SR04 S3
#define Trig PORTBbits.RB4
#define Echo PORTBbits.RB5
 
//HC-SR04 S4
#define Trig PORTBbits.RB6
#define Echo PORTBbits.RB7
 
void chk_isr(void);
void INTO_ISR(void);
void msdelay (unsigned int z);
 
#pragma interrupt chk_isr
    void chk_isr(void){
      if(INCONBITS.INTOIF==1)
      INTO_isr();
    }
#pragma HiPrio_int=0x008)
    void hiPrio_int(void){
     _asm
     GOTO chk_isr
     _endsm
 }
#pragma code
    void main(void){
      ADCON1=0X08;
      INTCONbits.GIE=1;
      INTCONbits.IPEN=1;
      INTCONbits.INTOIF=0;
      INTCONbits.INTOIE=1;
      INTCONbits.INTEDGO=1;
      TRISBbits.RB0=1;
      TRISBbits.RB1=1;
      TRISBbits.RB2=1;
      TRISBbits.RB3=1;
      TRISBbits.RB4=1;
      TRISBbits.RB5=1;
      TRISBbits.RB6=1;
      TRISBbits.RB7=1;
      TRISDbits.RD2=0;
      TRISDbits.RD3=0;
 
    while(1){
      LATDbits.LATD2=0;//turn off motor 1
      LATDbits.LATD3=0;//turn off motor 2
    if(HC-SR04 S1==1){
      LATDbits.LATD2=1;//motor 1 reverse
      LATDbits.LATD3=0;//turn off motor 2
      msdelay(500);
    }Else(HC-SR04 S1==0){
      LATDbits.LATD2=0;//turn off motor 1 
      LATDbits.LATD3=0;//turn off motor 2
    }
    if(HC-SR04 S4==1){
      LATDbits.LATD2=1;//motor 1 forward
      LATDbits.LATD3=0;//turn off motor 2
      msdelay(500);
    }Else(HC-SR04 S4==0){
      LATDbits.LATD2=0;//turn off motor 1 
      LATDbits.LATD3=0;//turn off motor 2
    }
    
    if(HC-SR04 S2==1){
      LATDbits.LATD2=0;//turn off motor 1
      LATDbits.LATD3=1;//motor 2 reverse
      msdelay(500);
    }Else(HC-SR04 S2==0){
      LATDbits.LATD2=0;//turn off motor 1 
      LATDbits.LATD3=0;//turn off motor 2
    }
    
    if(HC-SR04 S3==1){
      LATDbits.LATD2=0;//turn off motor 1
      LATDbits.LATD3=1;//motor 2 forward
      msdelay(500);
    }Else(HC-SR04 S3==0){
      LATDbits.LATD2=0;//turn off motor 1 
      LATDbits.LATD3=0;//turn off motor 2
    }
}



can't you correct me if i'm wrong ? i'm just trying with this code but does'nt work .
 
Last edited by a moderator:

That code should not compile
- "Else" should not have a capital 'E' and should not have a conditional expression after it
- the problem that EasyRider pointed out
- you define 'trig' and 'Echo' 3 times (each) [this will not stop the compile but it will stop your program doing what you intend]
If your assignment is to be marked by the logic you have used then you should take another look at your main loop. You turn off the motors and do a sequence of tests. After each test you turn on one of the motors (and you seem to use the same command to turn the motor on for both forward AND reverse!) and also turn off the already off motor (a waste). If the test fails then (if it could be executed) the 'else' clause would turn off both (already off) motors.
Worse than that, if the NEXT text returns false, then the 'else' clause of that would turn off the motor you just turned on (delays not withstanding)!!!!!
I suggest that you stop coding and work out your logic on paper to make sure it doing what you actually intend.
Susan
 

i'm already get the code. but my problem now, i'm just not sure how to set the distance that will sensor detect. example, i want to set 30cm before the hazards that sensor will detect .
 

Start with the formula distance = speed * time.
You know the distance (30cm - possibly double that if the 30cm represents the distance and not the round-trip) and you can find the speed of sound in air (generally at NTP - OK for practical purposes unless you are n a very unusual environment).
That will let you work out the 'time' it will take for the sound pulse to go from the transmitter, be reflected and get back to the receiver.
You then need to work out how to measure the time between the Tx and Rx pulse and compare that to the time you worked out above.
Susan
 

so , i have to make another code for the measuring distance detection ?
 

What does your code get from the sensor? You need to read the sensor's data sheet. It shows you basically the same formula I gave on the first page, along with the speed of sound you can use, and the 2nd page (under the 'Timing Diagram' heading) shows the signal you can get form the sensor.
As it explains, the width of the pulse you get is proportional to the range and you need to measure the width (which will probably be in uSec) and convert that to your range.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top