Últimamente he intentado conectar la interfaz HC-SR04 con PIC16F877A utilizando el modo de captura e interrupción, pero recibo "Advertencias" y pegué el código a continuación:
Salida de compilación:
"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
He leído tus publicaciones anteriores pero aún no he podido resolver el problema y Nunca he usado la directiva #pragma antes, ayúdame a resolver este problema. He pegado el código y cualquier ayuda será apreciada mucho.
Código
#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!='"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
')
{
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;
}