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.

MIKRO C code is not working ?

Status
Not open for further replies.

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
Hello
i have a mikrocontroller pic 16f628a and a ultrasonic sonsor HC-SR04
I'm working with the intern pulse 4MHz
pleas tell me why this code not working



Code:
//Crystal 4MHz
//speed of sound 340m/s
//1 TMR0 cycle = (340/1000000)*256*0.5*100 = 4.352 in cm
#define TRIG PORTA.F0
#define ECHO PORTA.F1

double distance;
void main() {

TRISa.F0 = 1;
TRISa.F1 = 0;
TRISb=0;

OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;

  while(1){

  TRIG = 0;
  delay_us(10);
  TRIG = 1;
  delay_us(10);
  TRIG = 0;

  while(ECHO==0);
  TMR0=0;
  while(ECHO==1);
  distance = (double)TMR0 * 4.352;
  Delay_ms(100);
  }
  if(distance < 6) portb=1; // if distance < 6 cm
  else portb=0;

}
 
Last edited by a moderator:

Joined
Dec 4, 2012
Messages
4,294
Helped
822
Reputation
1,654
Reaction score
793
Trophy points
1,393
Location
Bangalore, India
Activity points
0
I don't know whether your code works but the mistake is this...


Code C - [expand]
1
2
TRISa.F0 = 1;
TRISa.F1 = 0;




Code C - [expand]
1
2
TRISa.F0 = 0;
TRISa.F1 = 1;

 

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
what is the difference i put the trig in ra0 and the echo in ra1
please help me
 

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
i'm sorry my friend i tried but it still don't working
 

nagkiller

Full Member level 4
Joined
Jul 9, 2009
Messages
231
Helped
37
Reputation
74
Reaction score
37
Trophy points
1,308
Location
Brazil
Activity points
2,507
I see that the if / else is out of the loop too!!!

Sorry, I dont have the modules for test the code!!!
 

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
thank you all
i changed the program to this
but still don't work
Code:
//Crystal 4MHz
//speed of sound 340m/s
//1 TMR0 cycle = (340/1000000)*256*0.5*100 = 4.352 in cm
#define TRIG PORTA.F0
#define ECHO PORTA.F1

double distance;
void main() {
CMCON = 0x07;

TRISa.F0 = 0;
TRISa.F1 = 1;
TRISb=0x00;

OPTION_REG.T0CS = 0;
OPTION_REG.PSA = 0;
OPTION_REG.PS0 = 1;
OPTION_REG.PS1 = 1;
OPTION_REG.PS2 = 1;

  while(distance<100){
      portb=0xff;
TRIG= 0;
  delay_us(10);
  TRIG = 1;
  delay_us(10);

 TRIG= 0;

  while(ECHO==0);
  TMR0=0;
  while(ECHO==1);
  distance = (double)TMR0 * 4.352;


  }

    portb=0x00     ;
}
 

nagkiller

Full Member level 4
Joined
Jul 9, 2009
Messages
231
Helped
37
Reputation
74
Reaction score
37
Trophy points
1,308
Location
Brazil
Activity points
2,507
Code still wrong!!! Is out of loop (while(1){})!!!

Now run once and sleep!!!
 

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
Hi every one
I try every program but it's not working please if there is any one tray successfully to program this ultrasonic sensor please help me
I'm going crazy
 

123jack

Advanced Member level 2
Joined
Apr 21, 2010
Messages
547
Helped
91
Reputation
182
Reaction score
77
Trophy points
1,308
Location
UK
Activity points
5,357
I'd suggest you single step your code in the mikroe IDE
Take a look around "while(ECHO==1);" etc to start with
and ask what happens if you dont ever get an echo.

A flow chart may help you but I doubt this will do what you want anyway.
Have a read of the stuff jayanth has suggested especially the parts about interrupts.
 

synchrone

Newbie level 4
Joined
Nov 20, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
37
forget about my program i just need when an object detected of 4 cm or less the portb=1
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top