Error in programming Project 6 (ultrasonic range finder)

Programmer, In-Circuit Debugger, PIC Start-Up Kit, Memory Interface...

Re: Error in programming Project 6 (ultrasonic range finder)

Postby zhenning » Sat Jul 14, 2012 7:49 am

yeosteven WROTE:I follow the connection base on this one...the connect the sensor with long wire thats y it cannot be seen...the other 2 mode shows 000 in the reading..i guess is not affected rite?


Hi,
Pose the schematics. it is easier to see than the picture here. :roll:
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby zhenning » Sat Jul 14, 2012 7:53 am

yeosteven WROTE:The sensor doesnt have any problem...and the connnection I check alot of time follow the project six 6 guild...all going smoothly...only when I start the programming everything ruined


Hi,

Since, you are saying your sensor is working? May I know how you test them?
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby yeosteven » Sat Jul 14, 2012 9:09 am

haha...I dun have the schmatic...I build it follow that picture...I test using a power supply and multimeter...I connect the ground and 5V to the sensor connection ground and 5V respectively...then I connect the multimeterground to sensor ground and the other one to the AN pin...the volt change when I move the sensor near and far...thats y i said is working...but when I move it to the project 6 circuit...it is not working anynmore..
yeosteven
Novice
 
Posts: 26
Joined: Wed Jul 04, 2012 8:21 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby yonghui » Sat Jul 14, 2012 9:56 am

hi,

try do some debug on lcd. sending so different value to the lcd make the lcd display change according to you values?
then try measure the output of the sensor which is connected to the an pin of the pic. does it change according to the distance?
then if u are using pic16f887. make sure the ansel mux is configured properly for the analog input pin so that the pin is configured as the an input and not digital port. also tris is set as input.
then try to disconnect the sensor from board. take the an pin, connect to 5v/vcc. see the reading on lcd is correct?and connect to gnd the reading on lcd is correct?to confirm , mayb u can connect potentiometer change the voltage,see if lcd value change accordingly.

if first lcd displaying correct values. second analog pin of sensor is changing with distance. then adc of pic is reading analog value correctly and display value correctly to lcd. then there is ghost. if one of the above step failed. means probblem come from there and u need to solve the problem 1st before checking another.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Error in programming Project 6 (ultrasonic range finder)

Postby yeosteven » Sat Jul 14, 2012 10:50 am

PORTA = 0; // Clear Port
PORTB = 0;
PORTC = 0;
PORTD = 0;


TRISA = 0b11111111; // set PORTA as INPUT
TRISB = 0b00000111; // set PORTB<7:3> as OUTPUT , PORTB<2:0> as INPUT
TRISC = 0b10000000;
TRISD = 0b00000000; // set PORTD as output


ANSELH = 0; // SET PORTB as DIGITAL I/O for PIC16F887
WPUB = 1; // PORTB Weak Pull Up enable

this is the code...how I change the ANSELH to input ? as It written output there...
there are 4 TRIS ? how to change it?
yeosteven
Novice
 
Posts: 26
Joined: Wed Jul 04, 2012 8:21 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby zhenning » Sat Jul 14, 2012 1:17 pm

yeosteven WROTE:PORTA = 0; // Clear Port
PORTB = 0;
PORTC = 0;
PORTD = 0;


TRISA = 0b11111111; // set PORTA as INPUT
TRISB = 0b00000111; // set PORTB<7:3> as OUTPUT , PORTB<2:0> as INPUT
TRISC = 0b10000000;
TRISD = 0b00000000; // set PORTD as output


ANSELH = 0; // SET PORTB as DIGITAL I/O for PIC16F887
WPUB = 1; // PORTB Weak Pull Up enable

this is the code...how I change the ANSELH to input ? as It written output there...
there are 4 TRIS ? how to change it?


Change ANSELH to input?? I think you didnt get what is ANSELH? Please read the datasheet.. ANSELH is used to set the PORTB of the PIC to digital. Please have the courtesy to read the comment of the code. It is written there clearly.
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby yeosteven » Sat Jul 14, 2012 1:36 pm

so where is the ansel mux?
yeosteven
Novice
 
Posts: 26
Joined: Wed Jul 04, 2012 8:21 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby zhenning » Sat Jul 14, 2012 1:39 pm

zhenning WROTE:
yeosteven WROTE:I manage to find the problem with the error there as I change the ___ to __
howeever i got another problem now....my code is

CODE: SELECT_ALL_CODE
//==========================================================================
//   Author            : CYTRON TECHNOLOGIES SDN BHD
//   Project            : SK40C sample code for PIC16F887
//   Project description   : Analog Sensor: Range using Ultrasonic Range Finder
//                    3 mode : (1: Analog)
//                           (2: PWM   )
//                           (3: UART  )
//==========================================================================

//   include
//==========================================================================
#include <htc.h>
#include "lcd.h"
#include "adc.h"
#include "system.h"
#include "uart.h"

//   Configuration
//==========================================================================
__CONFIG(MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_INTRCIO);

//   global variable
//===========================================================================
unsigned int To=0,T=0,TH=0;
unsigned char data[6] = {0};

//  Interrrupt Subroutine
//============================================================================
static void interrupt isr(void)
{
   if (T0IF)                        // TMR0 is overflow
   {
      T0IF = 0;                      // clear flag bit
      To +=0x100;                     // count number of TMR0 overflow ( make it to 16bit TMR)
   }
      
   if(RBIF)                        // there is change bit on RB4-RB7
   {
      RBIF = 0;                     //                                           ____
      if (PWM_IN)                     // PWM_IN(RB2) is 1 mean is rising form 0  __|         
      {
         TMR0 = 0;                  // clear all counter involved, start new count for period of RB4 high
         To = 0;
      }
                                 //                                 ___
      else TH = TMR0 + To;            // PWM_IN(RB2) is 0 mean is falling form 1       |_____  // save TH, RB4 high period
   }
}

//   main function
//==========================================================================
void main(void)
{
unsigned char mode = 1;
   PORTA = 0;                     // Clear Port
   PORTB = 0;
   PORTC = 0;
   PORTD = 0;

   
   TRISA = 0b11111111;               // set PORTA as INPUT
   TRISB = 0b00000111;               // set PORTB<7:3> as OUTPUT , PORTB<2:0> as INPUT
   TRISC = 0b10000000;
   TRISD = 0b00000000;               // set PORTD as output
   

   ANSELH = 0;                   // SET PORTB as DIGITAL I/O for PIC16F887
   WPUB = 1;                     // PORTB Weak Pull Up enable

   // Interupt on change
   IOCB0 = 0;            
   IOCB1 = 0;
   IOCB2 = 1;                     // when PORTB.2 detect change will interrupt
   IOCB3 = 0;
   IOCB4 = 0;
   IOCB5 = 0;
   IOCB6 = 0;
   IOCB7 = 0;

   RBIE = 1;                     // PORTB Change Interrupt Enable bit   


   // TMR 0 configuation
   T0CS = 0;                  
   PSA = 0;                  
   PS2 = 1;                     // prescale 1:256
   PS1 = 1;                  
   PS0 = 1;                  
   T0IE = 1;                     // TMR0 Interrupt
   TMR0 = 0;
   
   GIE = 1;                     // Global Intterupt enable
   PEIE = 1;                     // Peripheral Interrupt enable

   lcd_initialize();               // Initialise LCD

   adc_initialize();               // Initialise ADC

   uart_initialize();               // Initialise UART

   TX == 1;                     // ON Transmit pin (refer Maxbotix EZ1 Ultrasonic datasheet)

   lcd_home();
   lcd_putstr("Dist:");
   lcd_goto(0x0B);
   lcd_putstr("inch");

while (1)
   {
   switch(mode)
      {
   case 1:         lcd_2ndline();
               lcd_putstr("Analog");
               while (SW1 == 1)
                  {
                  unsigned int adc_value = 0;
                  unsigned int range_an = 0; 
                  unsigned char j;
                  adc_on();
                  for(j = 0 ; j < 10 ; j++)                  // take analog result for 20 times
                     {
                     adc_value = adc_value + ui_adc_read();      
                     }
   
                  adc_value = adc_value/10;                  // adc_value devide by 10 to get average result
                  range_an = adc_value/2;                     // max vslue adc_value = 2.55/5 *1024 - 1 =  522
                  lcd_goto(0x05);                           // max is 254 inch... 522/254 = ~2
                   lcd_bcd(3,range_an);                     // (1 to 6 inch) is readed as 6 inch (refer to ultrasonic datasheet)
                  }   
               while (SW1 == 0);
               break;

   case 2:         lcd_2ndline();
               lcd_putstr("PWM   ");
               unsigned int range_pwm;
               while(SW1 == 1)
                  {
                  range_pwm = TH;                           // read TH from interrupt subroutine
                  lcd_goto(0x05);                           // each value = 256*4/20mhz = 51.2us, 1 inch = 147us 
                  range_pwm = (range_pwm*100)/288;            // 147us/51.2us = 2.87
                  lcd_bcd(3,range_pwm);   
                  }
               while (SW1 == 0);
               break;

   case 3:          lcd_2ndline();
               lcd_putstr("UART   ");
               unsigned int value,k,l = 0;
               while(SW1 == 1)
                  {
                  
                  for (l=0 ; l<6 ; l++)
                     {
                     uc_uart_receive();                     //take value from UART subroutine
                     if(RCREG == 'R') data[k=0] = RCREG;         // check if start byte 'R' is met 
                     if(data[0] == 'R') data[k++] = RCREG;      // save the data in data array
                     if (k>4) k = 4;                        // if the data array reached max, set the index to 4
                     }
                     lcd_goto(0x05);
                     send_lcd_data(1,data[1]);
                     send_lcd_data(1,data[2]);
                     send_lcd_data(1,data[3]);
                  }
      }

      while (SW1 == 0);
      if (++mode > 3)             // if SW1 is press, increase the mode number until it is max and loop back
         {
         mode = 1;
         }   
      
   }   
}


and i got this error...

Clean: Deleting intermediary and output files.
Clean: Deleted file "C:\Users\Yeo\Desktop\ETP\Project_6\adc.p1".
Clean Warning: File "C:\Users\Yeo\Desktop\ETP\Project_6\Project_6.p1" doesn't exist.
Clean: Deleted file "C:\Users\Yeo\Desktop\ETP\Project_6\lcd.p1".
Clean: Deleted file "C:\Users\Yeo\Desktop\ETP\Project_6\uart.p1".
Clean: Done.
Build C:\Users\Yeo\Desktop\ETP\Project_6\Project_6 for device 16F887
Using driver C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe

Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Yeo\Desktop\ETP\Project_6\adc.c -q --chip=16F887 -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Yeo\Desktop\ETP\Project_6\Project_6.c -q --chip=16F887 -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Yeo\Desktop\ETP\Project_6\lcd.c -q --chip=16F887 -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" --pass1 C:\Users\Yeo\Desktop\ETP\Project_6\uart.c -q --chip=16F887 -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "C:\Program Files\HI-TECH Software\PICC\9.83\bin\picc.exe" -oProject_6.cof -mProject_6.map --summary=default,-psect,-class,+mem,-hex --output=default,-inhx032 adc.p1 Project_6.p1 lcd.p1 uart.p1 --chip=16F887 -P --runtime=default,+clear,+init,-keep,+osccal,-download,-resetbits,-stackcall,+clib --opt=default,+asm,-debug,-speed,+space,9 --warn=0 -D__DEBUG=1 --double=24 --float=24 --addrqual=ignore -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
HI-TECH C Compiler for PIC10/12/16 MCUs (Lite Mode) V9.83
Copyright (C) 2011 Microchip Technology Inc.
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error [800] C:\Users\Yeo\AppData\Local\Temp\s4s4.; 45. undefined symbol "FOSC_INTRCIO"

how should I define this?? anyone help??


Hi,
I think i know whats your problem. The original source code has HS as Fosc which means it is using 20MHz crystal. That is from
http://tutorial.cytron.com.my/2011/08/0 ... ge-finder/

SPBRG = 129;
SPBRG is in decimal value which is use to select the baud rate of 9600 with condition of SYNC = 0, BRGH = 1, BRG16 = 0, Fosc = 20MHz. Please refer PIC16F887 datasheet for detail.

And based on your code you are trying to use FOSC_INTRCIO which is 4MHz internal oscillator with oscillator pin as I/O. Furthermore, you leave the TMR setting unchanged. Screwing up the TMR1 timing.
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby zhenning » Sat Jul 14, 2012 1:54 pm

yeosteven WROTE:so where is the ansel mux?


ANSEL is the ansel mux for ANS<7:0> which is meant for PORTA and ANSELH is the ansel mux for PORTB.

.how I change the ANSELH to input ?


These mux doesnt set it to input. It only set the port whether it is analog or digital. In default, usually, PORTA comes with analog input.

TRISx is the one that sets it to input or output.
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: Error in programming Project 6 (ultrasonic range finder)

Postby yeosteven » Sat Jul 14, 2012 10:56 pm

I had overcome the problem already...thnks everyone!!
yeosteven
Novice
 
Posts: 26
Joined: Wed Jul 04, 2012 8:21 am

PreviousNext

Return to PIC Development Tool

Who is online

Users browsing this forum: No registered users and 5 guests

cron