PIC18f458 with ultrasonic senzor HC-S04

Status
Not open for further replies.

Vlad.

Full Member level 3
Joined
Jun 4, 2012
Messages
179
Helped
3
Reputation
6
Reaction score
4
Trophy points
1,298
Location
Bucharest/Romania
Visit site
Activity points
2,568
Hello, I design an robot with pic16f458 but i can't make the ultrasonic senzor to work. Can somebody help me?
The problem is here because the sensor work verry good with arduino. Where is the problem? Thanks.






Code:
#define TRIG PORTD.F6
#define ECHO PORTD.F5

 int range;
 int width;
unsigned char ch,uart_rd;
unsigned short current_duty,i;
  void inainte(){
    PORTB.F5=1;
    PORTB.F4=0;
    PORTB.F3=0;
    PORTB.F2=1;
    PORTD.F7=0;
    PORTB.F0=0;
    current_duty = 255;
       PWM1_Set_Duty(current_duty);
  }
  void inapoi(){
    PORTB.F5=0;
    PORTB.F4=1;
    PORTB.F3=1;
    PORTB.F2=0;
    PORTD.F7=1;
    PORTB.F0=1;
    current_duty = 255;
       PWM1_Set_Duty(current_duty);
  }
  void stanga(){
  PORTB.F5=1;
    PORTB.F4=0;
    PORTB.F3=0;
    PORTB.F2=0;
    PORTD.F7=1;
    PORTB.F0=0;
    current_duty = 200;
       PWM1_Set_Duty(current_duty);
  }
  void dreapta(){
    PORTB.F5=0;
    PORTB.F4=0;
    PORTB.F3=0;
    PORTB.F2=1;
    PORTD.F7=0;
    PORTB.F0=1;
    current_duty = 200;
    PWM1_Set_Duty(current_duty);
  }
  void stop(){
    PORTB.F5=0;
    PORTB.F4=0;
    PORTB.F3=0;
    PORTB.F2=0;
    PORTD.F7=0;
    PORTB.F0=0;
  }
  void lumini_on(){
  PORTB.F1=1;
  }
  void lumini_off(){
  PORTB.F1=0;
  }
  void test_mode(){
  PORTB.F1=1;
  inainte();
  Delay_ms(2000);
  stanga();
  Delay_ms(500);
  inainte();
  Delay_ms(1500);
  dreapta();
  Delay_ms(500);
  inainte();
  Delay_ms(2000);
  inapoi();
  Delay_ms(1000);
   PORTB.F1=0;
   PORTB.F1=1;
   PORTB.F1=0;


  }
 void Delay2S(){
  Delay_ms(2000);
}

void main() {
  UART1_Init(9600);                       // Initialize USART module (8 bit, 38400 baud rate, no parity)
  Delay_ms(300);
  TRISB.F5=0;
   TRISB.F4=0;
   TRISB.F3=0;
   TRISB.F2=0;
   TRISB.F1=0;
   TRISB.F0=0;
   TRISD.F7=0;
   TRISD.F6 = 0;
   TRISD.F5 = 1;

  PORTB.F5=0;
  PORTB.F4=0;
  PORTB.F3=0;
  PORTB.F2=0;
  PORTB.F1=0;
  PORTB.F0=0;
  PORTD.F7=0;
  PORTD.F6=0;

  current_duty  = 0;
  PWM1_Start();                        // start PWM1
  PWM1_Set_Duty(current_duty);        // setare   factor de umplere
  range=0;
  width=0;

 while (1) {

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

 while(ECHO==0);

  while(ECHO==1);
   {
 delay_us(10);
 width++;
 }
 range=(width*10)/58; //calculate range

    if (range<100)                     //if range less than 100m
 {
  void lumini_on();
 }

 delay_ms(500);
 }



    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      if (uart_rd==0x41)     // citire A
     inainte();
      if (uart_rd==0x42)     // citire B
     inapoi();
      if (uart_rd==0x43)     // citire C
     stanga();
      if (uart_rd==0x44)     // citire D
     dreapta();
     if (uart_rd==0x45)     // citire E
     stop();
      if (uart_rd==0x46)     // citire F
     lumini_on();
      if (uart_rd==0x47)     // citire G
     lumini_off();
     if (uart_rd==0x48)     // citire H
     test_mode();


  }



      Delay_ms(100);

 }
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…