ruben91
Junior Member level 3
- Joined
- Nov 17, 2014
- Messages
- 29
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 209
hello, i have convert the code to c18 mplab but it don't seem to produce the result, can u help me to solve this? i'm using pic18f4580 with xtal 20Mhz
Code:
#include <p18f4580.h>
#include <usart.h>
#include <delays.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#pragma config OSC= HS
#pragma config WDT=OFF
#pragma config LVP=OFF
#define Trig PORTBbits.RB3
#define Echo PORTBbits.RB2
void T0Delay10us(void);
void T0Delay5ms(void);
void uart(void);
void Call_Init_PIC (void);
void Call_Init_USART (void);
void sendPulse(void);
unsigned int distance;
void main(void)
{
Call_Init_PIC ();
uart();
while(1)
{
sendPulse();
while(Echo == 1);
TMR1H= 0;
TMR1L= 0;
T1CON = 0x01;
while(Echo == 0);
distance = (TMR1H<<8) + TMR1L;
T1CON = 0x00;
distance = (distance/58)*20;
//distance = (distance/58);
printf("Distance = %3d\v\n",distance);
Delay10KTCYx(255);
PIR1bits.TMR1IF=0;
T0Delay5ms();
}
}
void Call_Init_PIC (void)
{
TRISBbits.TRISB3=0;
TRISBbits.TRISB2=1;
}
void uart(void)
{
TXSTA=0B00100000;
SPBRG=32;
TRISCbits.RC6=0;
RCSTAbits.SPEN=1;
}
void sendPulse(void)
{
Trig=1;
T0Delay10us();
Trig=0;
}
void T0Delay10us()
{
T0CON=0x08;
TMR0H=0XFF;
TMR0L=0XCE;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}
void T0Delay5ms()
{
T0CON=0x08;
TMR0H=0X9E;
TMR0L=0X58;
T0CONbits.TMR0ON=1;
while(INTCONbits.TMR0IF==0);
T0CONbits.TMR0ON=0;
INTCONbits.TMR0IF=0;
}