Lately I've been trying to interface HC-SR04 with PIC16F877A using capture mode and interrupt but am getting "Warnings" and I've pasted the code below:
Compile output:
"C:\Program Files (x86)\Microchip\xc8\v1.10\bin\xc8.exe" --pass1 --chip=16F877A -Q -G --asmlist --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,-speed,+space,-debug,9 --addrqual=ignore -P -N31 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,-config,+clib,-plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -obuild/default/production/Ultrasonic.p1 Ultrasonic.c
Ultrasonic.c:23: warning: unknown pragma "code"
Ultrasonic.c:29: warning: unknown pragma "interrupt"
Ultrasonic.c:36: warning: unknown pragma "code"
Ultrasonic.c:64: warning: illegal conversion between pointer types
pointer to const unsigned char -> pointer to unsigned char
Ultrasonic.c:66: warning: illegal conversion between pointer types
pointer to const unsigned char -> pointer to unsigned char
Ultrasonic.c:83: warning: implicit conversion of float to integer
Ultrasonic.c:88: warning: illegal conversion between pointer types
pointer to const unsigned char -> pointer to unsigned char
Ultrasonic.c:92: warning: illegal conversion between pointer types
pointer to const unsigned char -> pointer to unsigned char
"C:\Program Files (x86)\Microchip\xc8\v1.10\bin\xc8.exe" --chip=16F877A -G --asmlist -mdist/default/production/Ultrasonic.X.production.map --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,-speed,+space,-debug,9 --addrqual=ignore -P -N31 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --runtime=default,+clear,+init,-keep,-no_startup,+osccal,-resetbits,-download,-stackcall,-config,+clib,-plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s" -odist/default/production/Ultrasonic.X.production.cof build/default/production/Ultrasonic.p1
Microchip MPLAB XC8 C Compiler (PRO Mode) V1.10
Copyright (C) 2012 Microchip Technology Inc.
Ultrasonic.c:21: warning: variable "_rf" is not used
Memory Summary:
Program space used 3F1h ( 1009) of 2000h words ( 12.3%)
Data space used 3Ch ( 60) of 170h bytes ( 16.3%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
Configuration bits used 0h ( 0) of 1h word ( 0.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
make[2]: Leaving directory `E:/MPLAB Projects/Ultrasonic/Ultrasonic.X'
make[1]: Leaving directory `E:/MPLAB Projects/Ultrasonic/Ultrasonic.X'
BUILD SUCCESSFUL (total time: 6s)
Loading code from E:/MPLAB Projects/Ultrasonic/Ultrasonic.X/dist/default/production/Ultrasonic.X.production.hex...
Loading symbols from E:/MPLAB Projects/Ultrasonic/Ultrasonic.X/dist/default/production/Ultrasonic.X.production.cof...
Loading completed
I've read your previous posts but still couldn't resolve the problem and
I've never used #pragma directive before please help me to resolve this problem. I've pasted the code and any help will be appreciated a lot.
Code
#include <pic.h>
#include <htc.h>
#define TRIG RB7
#define PD PORTD
#define RS RE0
#define RW RE1
#define E RE2
void cmdwrite(unsigned char);
void datawrite(unsigned char);
void strdis(unsigned char *str);
void MSDelay(unsigned int itime);
void ms_delay();
/*void msd();*/
void dis4digit(unsigned int var);
void CCP1_ISR(void);
void rising(void);
void falling(void);
void chk_isr();
void My_HiPrio_Int();
unsigned char disp=0;
unsigned char rf=0;
#pragma code My_HiPrio_Int=0x0008
void My_HiPrio_Int(void)
{
chk_isr();
}
#pragma interrupt chk_isr
void chk_isr(void)
{
if(CCP1IF==1)
CCP1_ISR();
}
#pragma code
void main()
{
unsigned long int distance_in_cm;
unsigned long int distance_in_inches;
unsigned long int F_byte;
unsigned char H_byte,L_byte;
ADCON1=0x0F;
GIE=1;
PEIE=1;
CCP1IE=1;
TRISD=0;
TRISE=0;
TRISB7=0;
TRISC2=1;
CCP1CON=0x05;
T1CON=0x00;
CCPR1L=0x00;
CCPR1H=0x00;
TRIG=0;
E=0;
cmdwrite(0x01);
cmdwrite(0x38);
cmdwrite(0x38);
cmdwrite(0x38);
cmdwrite(0x0C);
cmdwrite(0x83);
strdis("Ultrasonic");
cmdwrite(0xC0);
strdis("Distance Sensor");
MSDelay(2000);
while(1)
{
H_byte=0; L_byte=0; F_byte=0;
TRIG=1;
TRIG=0;
TMR1H=0x00;
TMR1L=0x00;
while(disp==0);
disp=0;
H_byte=CCPR1H;
L_byte=CCPR1L;
CCPR1L=0x00; CCPR1H=0x00;
F_byte=(H_byte * 256);
F_byte=F_byte + L_byte;
F_byte=(F_byte * 0.5);
distance_in_cm=F_byte/58;
distance_in_inches=F_byte/148;
cmdwrite(0x01);
cmdwrite(0x80);
strdis("Dist.cm:");
cmdwrite(0x88);
dis4digit(distance_in_cm);
cmdwrite(0xC0);
strdis("Dist.in:");
cmdwrite(0xC8);
dis4digit(distance_in_inches);
MSDelay(6000);
cmdwrite(0x01);
}
}
void cmdwrite(unsigned char var1)
{
PD=var1;
RS=0;
RW=0;
E=1;
ms_delay();
E=0;
}
void datawrite(unsigned char var2)
{
PD=var2;
RS=1;
RW=0;
E=1;
ms_delay();
E=0;
}
void strdis(unsigned char *str)
{
while(*str!='\0')
{
datawrite(*str);
str++;
}
}
void dis4digit(unsigned int var)
{
unsigned char thp,hp,up,tp,w,x,y;
w=var/10;
x=w/10;
y=x/10;
thp=y;
hp=x%10;
tp=w%10;
up=var%10;
datawrite(thp+48);
datawrite(hp+48);
datawrite(tp+48);
datawrite(up+48);
}
void ms_delay()
{
T1CON = 0x30;
TMR1H=0xC2;
TMR1L=0xF7;
TMR1ON=1;
while(TMR1IF==0);
TMR1ON=0;
TMR1IF=0;
}
/*void msd()
{
T1CON = 0x30;
TMR1H=0xFF;
TMR1L=0xFE;
T1CON.TMR1ON=1;
while(PIR1.TMR1IF==0);
T1CON.TMR1ON=0;
PIR1.TMR1IF=0;
} */
void MSDelay(unsigned int itime)
{
unsigned int i;
unsigned int j;
for(i=0;i<itime;i++)
for(j=0;j<165;j++);
}
void CCP1_ISR()
{
if(rf==0) rising();
else falling();
}
void rising()
{
TMR1ON=1;
rf=1;
CCP1M0=0;
CCP1IF=0;
}
void falling()
{
TMR1ON=0;
disp=1;
rf=0;
CCP1CON=0x00;
CCP1M0=1;
CCP1IF=0;
}
Best Answer
Even if you use MPLab IDE, the code you are writing is still C.
xC8, xc16 and xc32 The XC8, XC16 and XC32 are compilers for the PIC processors by Microchip. XC8 are used for 8bit-MCUs, XC16 for 16bit-MCUs and XC32 for 32bit-MCUs. Here you will get a clear overview about the compilers
The C18 was an old compiler for 8bit MCUs. If you just start programming PICs use the XC-equivalent :-)
Pragma warnings
I already showed you the Migration guide. For getting the warning on line 23 away write
instead of
Have a clear look at page 34 for this!
The
pragma
statement on line 36 is not neccessary anymore, when compiling with XC8 instead of C18.To avoid the warning in line 30 you can write
instead of
Page 32 will help you with this ;-)
illegal pointer conversion
You try to send a
const
-String with thestrdis()
-function. Writeto show the compiler that the pointer values won't be overwritten in the function (hope this works, i don't have XC8 compiler with we, sorry). Otherwise you can make a type conversion when calling
strdis()
. For example:But i won't recommend this! Even if you know what you do!
Last in line 83 you write
Instead write the following
When calculating with float numbers the compiler will do a type conversion by himself. So he converts
F_byte
to a float and then multiplies with0.5
.float
-calculation is much slower thaninteger
-calculation, so avoid this whereever possible.If you find something like
F_byte=(F_byte >> 1);
anywhere in the www, you can be sure that every compiler, which is nearly up-to-date will do this code optimisation while compiling.Complete Code
With XC8 v1.33 this code compiles without any error or warning:
Here's my build log:
The errors and warnings you get are not MPLab specific... Consider reading a GOOD book about C programming (for example K&R) and google the errors at first.