• AVR Freaks

Helpful ReplyHot!Parallax Ping

Author
Givi
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2014/02/18 15:26:47
  • Location: 0
  • Status: offline
2019/07/15 15:04:16 (permalink)
0

Parallax Ping

Hi,
 
I have had no luck with Parallax Ping and I to see if anyone can help or has had any experience with this Ultrasonic Distance Sensor.  I would like to share my code with the forum and see if there is any inputs that might help me.
 
 
Regards,
 
Givi
#1
katela
Super Member
  • Total Posts : 1457
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Parallax Ping 2019/07/15 15:19:19 (permalink)
4 (1)
You can start by sharing your code with other details such as your PIC, versions of your software etc. Explain what is not working and what you are expecting.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#2
Givi
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2014/02/18 15:26:47
  • Location: 0
  • Status: offline
Re: Parallax Ping 2019/07/15 16:18:16 (permalink)
0
Hi,
Please see attached code I am working on.  I am using MikroC Pro Ver. 7.5.0 and PIC18F2525.  I do see my message-1 on the 8Bit LCD, but I am not getting any number or Distance showing up on the first line that I left for it.  I am not sure its the Interrupt issue or presentation or conversion.  
Thank you for your input.
 
 
Regards 
#3
Givi
New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2014/02/18 15:26:47
  • Location: 0
  • Status: offline
Re: Parallax Ping 2019/07/15 16:19:19 (permalink)
0
#include "lcdbit.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned int combined1=0 , combined2=0 , HiR=0 , LoR=0 , HiF=0 , LoF ,i=0;
 float  Distance ;
unsigned char out[20]= {'\0'}, *str;
//******************************
                     // LCD module connections    buffer[20],
                     sbit LCD_RS at RA0_bit;
                     sbit LCD_RW at RA1_bit;
                     sbit LCD_EN at RA2_bit;

                     sbit LCD_RS_Direction at TRISA.b0;
                     sbit LCD_RW_Direction at TRISA.b1;
                     sbit LCD_EN_Direction at TRISA.b2;

                     // LCD module connections
                     sbit LCD_D0 at RB0_bit;
                     sbit LCD_D1 at RB1_bit;
                     sbit LCD_D2 at RB2_bit;
                     sbit LCD_D3 at RB3_bit;
                     sbit LCD_D4 at RB4_bit;
                     sbit LCD_D5 at RB5_bit;
                     sbit LCD_D6 at RB6_bit;
                     sbit LCD_D7 at RB7_bit;

                     sbit LCD_D0_Direction at TRISB.b0;
                     sbit LCD_D1_Direction at TRISB.b1;
                     sbit LCD_D2_Direction at TRISB.b2;
                     sbit LCD_D3_Direction at TRISB.b3;
                     sbit LCD_D4_Direction at TRISB.b4;
                     sbit LCD_D5_Direction at TRISB.b5;
                     sbit LCD_D6_Direction at TRISB.b6;
                     sbit LCD_D7_Direction at TRISB.b7;

//====================================================

void init()
{
   //OSCCON =  0xFE;       //bit 6-4 IRCF2:IRCF0: Internal Oscillator Frequency Select bits   11111110
  OSCCON =  0xFE;         //111 = 0xFE 8 MHz, EE=4 MHZ(INTOSC drives clock directly)       11011111
  OSCTUNE = 0xDF;
  //OSCTUNE = 0XDF;       //OSCILATION AT 8MHZ     11011111
  ADCON1 = 0xF;           //DIGITAL I/O
  //====================
  //INTCON= 0xA0;  //0xA4;
  TRISA = 0x0;                  // Set PORTA as output
  TRISB = 0x0;                  // Set PORTB as output
  TRISC = 0x0;                 // Set PORTC as output
  LATA = 0x00;                  // Clear PORTA
  LATB = 0x00;                  // Clear PORTB
  LATC = 0x00;                  // Clear PORTC
  //******************************
  RCON.IPEN = 0x1;     // interrupt priority Dis/enabled
  INTCON = 0x80; //  0xC0 Enables all high-priority interrupts ; Enables all low-priority peripheral interrupts When IPEN = 1:
  INTCON2 = 0x0 ;  //0- PORTB pull-ups + Interrupt on rising edge,By programming the Configuration bit PBADEN,
  INTCON3 = 0x0 ;   // External Interrupt Priority bit
  IPR1= 0x5;  //bit 2 CCP1IP: CCP1 Interrupt Flag bit
  IPR2 = 0x3; //bit 1 TMR3IP: TMR3 Overflow Interrupt Priority bit
  PIE1 = 0x5;  //bit 2 CCP1IE: CCP1 Interrupt Enable bit
  PIE2 = 0x3; //bit 1 TMR3IE: TMR3 Overflow Interrupt Enable bit bit 0 CCP2IE: CCP2 Interrupt Enable bit
  PIR1 =0x00; //bit 0 TMR1IF: TMR1 Overflow Interrupt Flag bit bit 2 CCP1IF: CCP1 Interrupt Flag bit
  PIR2 =0x0;   //bit 0 CCP2IF: CCP2 Interrupt Flag bit bit 1 TMR3IF: TMR3 Overflow Interrupt Flag bit
  //T0CON = 0x0;
  T1CON = 0x1; //  Timer1 is the capture/compare clock source for CCP1
  //T2CON = 0x0;
  T3CON = 0x9;//bit 6,3 : 01 = Timer3 is the capture/compare clock source for CCP2;
  //INTCON|=(1<<GIE);  // enable INTCON.GIE
  TMR1H = 0x0;
  TMR1L = 0x0;
  TMR3H = 0x0;
  TMR3L = 0x0;
 //******************************
  TRISC.b2 = 0x1;   //In Capture mode, CCP1 pin should be ++input++
  TRISC.b1 = 0x1;   //When the Generate Software Interrupt mode is chosen
                    //(CCPxM3:CCPxM0 = 1010), the corresponding CCP1
                    //and the CCP1IE bit is set.
                    //(CCPxM3:CCPxM0 = 1011)The Special Event Trigger is enabled by selecting
                    //allows the CCPR1 registers to serve as a programmable Period register for either timer.
                     //CCP2M3:CCP2M0 = 1011), this signal will reset Timer3.
  CCP2CON = 0x04; // Switch edge to falling ; 4 = falling edge
  CCP1CON = 0x05; //5 = every rising edge ; Capture rising edge
                  //if 1011, this signal will reset Timer3.
  CCPR1L= 0x0;    //An event is defined as one of the following:
  CCPR1H= 0x0;     //every falling edge
                    // The event is selected by the mode select bits,
                    //CCPxM3:CCPxM0 (CCPxCON<3:0>). When a capture
              //is made, the interrupt request flag bit, CCPxIF, is set; it
             //must be cleared in software. If another capture occurs
  CCPR2L= 0x0;    //An event is defined as one of the following:
  CCPR2H= 0x0;     //every falling edge
 TMR1L=TMR1H=0;
 }
 //******************************
//******************************
void message1(void)
{
    //LCD_cmd(LCD_LINE1);
    //LCD_string("              ");
    LCD_cmd(LCD_LINE2);
    LCD_string("This is a test");
    LCD_cmd(LCD_LINE3);
    LCD_string("This is a test");
    LCD_cmd(LCD_LINE4);
    LCD_string("This is a test");
    Delay_ms(2);       // delay
}
//******************************
void LCD_blink(void)
{
    LCD_cmd(DISPLAY_OFF);
    Delay_ms(10);
    Delay_ms(10);       // give a delay close to 1 sec.
    LCD_cmd(DISPLAY_ON);
    Delay_ms(10);
    Delay_ms(10);       // give a delay close to 1 sec.
    LCD_cmd(DISPLAY_OFF);
    Delay_ms(10);
    Delay_ms(10);       // give a delay close to 1 sec.
    LCD_cmd(DISPLAY_ON);
    Delay_ms(10);
    Delay_ms(10);       // give a delay close to 1 sec.
}
//******************************
void LCD_initit(void){
    LCD_cmd(MODE_8BIT);
    LCD_cmd(CURSOR_ON);
    LCD_cmd(DISPLAY_ON);
    LCD_cmd(CLRSCR);                      // clear the screen
    LCD_cmd(CURSOR_INC);                  // Cursor Incremetns on each write
    //LCD_cmd(DISPLAY_ON | CURSOR_OFF);     // Display on and Cursor Off
    return;
}
//******************************
void LCD_data(unsigned char txt){
    //LCD_isbusy();
    RS = HIGH;
    RW = LOW;
    EN = HIGH;
    DATAPORT = txt;
    Delay_ms(2);
    EN = LOW;
}
//******************************
void LCD_cmd(unsigned char cmd){
    //LCD_isbusy();
    RS = LOW;
    RW = LOW;
    EN = HIGH;
    DATAPORT = cmd;
    Delay_ms(1);
    EN = LOW;
}
//******************************
void LCD_string( char *buffer) { //Changed
while(*buffer)              // Write data to LCD up to null
    {
        //LCD_isbusy();           // Wait while LCD is busy
        LCD_data(*buffer);      // Write character to LCD
        buffer++;               // Increment buffer
    Delay_ms(1);
    }
}

//******************************
void InitTimers(){
  T3CON = 0x9;  
  CCP1IE_bit = 1;
  CCP2IE_bit = 1;
  TMR3IE_bit = 1;
  TMR1IE_bit = 1;
  CCPR1L = 0x0;
  CCPR1H = 0x0;
  CCPR2L = 0x0;
  CCPR2H = 0x0;
  TMR3IF_bit = 0x0;
  TMR1IF_bit = 0x0;
  INTCON = 0x80;      // 0xC0
}   
//******************************
//******************************
void PingIt(void){
                  TRISC.B7=0x0;//RC7 set to OUT signal
                  LATC.B7=0;
                  LATC.B7=1; //RC7 goes up for 10us
                  delay_us(10);//10us wait TO TURN On PARALLAX SONAR
                  //RC2 set to INPUT CCP1 To LISTEN for ECHO BACK
                  LATC.B7=0;
                  T3CON = 0x9;
               }
//******************************
void ECHORise(void){
          LATC.B2=0;
          TRISC.b2 = 0x1;   //In Capture mode, CCP1 pin should be ++input++
          CCP1CON = 0x5; //CCP1 5 = every rising edge
          //TMR3IF_bit = 0x00;
          TMR1IF_bit = 0x00;
                }
//******************************
void ECHOFall(void){
          LATC.B1=0;
          TRISC.b1 = 0x1;   //In Capture mode, CCP1 pin should be ++input++
          CCP2CON = 0x4; //CCP2 4 = every Faling edge
          TMR3IF_bit = 0x00;
          //TMR1IF_bit = 0x00;
                }
//******************************
//******************************
  void high_isr() iv 0x0008 ics ICS_AUTO {
     if ((TMR3IF_bit)&& (TMR1IF_bit)){
      TMR1IF_bit = 0x00;
      TMR3IF_bit = 0x00;
      CCP1IF_bit = 0x00;
      CCP2IF_bit = 0x00;
                  PingIt();
                  ECHORise() ;
             while ((CCP1IF_bit == 0) && (TMR1IF_bit == 0))      { //while
          PingIt();
          ECHORise() ;
          ECHOFall() ;
                                          }//while
          if ((CCP2IF_bit) && (TMR3IF_bit)){//IF Falling edge
                        
      LoR = CCPR1L;
      HiR = CCPR1H;
      combined1 = HiR;
      combined1 = (HiR << 8 );
                           combined1 =(HiR|LoR);
                          
                         LoF = CCPR2L;
                         HiF = CCPR2H;
                         combined2 = HiF;
                         combined2 = (HiF << 8 );
                         combined2 =(HiF|LoF);
                         Distance =((combined2 + combined1)/(58.47));
                                                    }//if
                         TMR1H = 0 ;
                         TMR1L = 0 ;
                         TMR3H = 0 ;
                         TMR3L = 0 ;
                          }//Heard the signal END
                         CCP1IF_bit = 0x0;
                         CCP2IF_bit = 0x0;
                               }//Interrupt
//******************************
//******************************
void main() {
     init();
     LCD_initit();
   InitTimers() ;
//******************************
        
         while(1)
         {      
    //Distance =((combined2 + combined1)/(58.47));
    FloatToStr(Distance, out[20]);
    LCD_cmd(LCD_LINE1);
    for(i==1;i<=20;i++){
      LCD_data(out);
      Delay_ms(300);
                      }
           message1();
          Delay_ms(800);
          TMR1IF_bit = 0x1;
          TMR3IF_bit = 0x1;
         }
}
#4
katela
Super Member
  • Total Posts : 1457
  • Reward points : 0
  • Joined: 2013/06/11 05:25:18
  • Location: South Africa
  • Status: online
Re: Parallax Ping 2019/07/16 16:07:03 (permalink) ☄ Helpfulby Givi 2019/07/17 13:12:33
2.5 (2)
It would be better if you post your question in MikroC compiler forum, there are many library functions and other syntax used by just this compiler. You could get better answers there than here.

Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
#5
Jump to:
© 2019 APG vNext Commercial Version 4.5