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.

MIKRO C code is not working ?

Status
Not open for further replies.

synchrone

Newbie level 4
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:

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;

 
what is the difference i put the trig in ra0 and the echo in ra1
please help me
 

i'm sorry my friend i tried but it still don't working
 

I see that the if / else is out of the loop too!!!

Sorry, I dont have the modules for test the code!!!
 
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     ;
}
 

Code still wrong!!! Is out of loop (while(1){})!!!

Now run once and sleep!!!
 

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
 

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.
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top