Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] PICF4580 CCP1 programming error

Status
Not open for further replies.

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 everyone, im totally new to this pic microcontroller programming, I have tried to do a project
to obtain distance from HC-SR04 ultrasonic sensor. I Have start with the basic to display the timer value for the certain distance first. But i'm stucked here not getting the out put displayed in hypeterminal, Please some one help ..

im getting out like this
ULTRA SONIC SENSOR = 0XXXXXXULTRA SONIC SENSOR = 0ULTRA SONIC SENSOR = 0XXXXXXULTRA SONIC SENSOR = 0ULTRA SONIC SENSOR = 0XXXXXXULTRA SONIC SENSOR = 0ULTRA SONIC SENSOR = 0XXXXXXULTRA SONIC SENSOR = 0ULTRA SONIC SENSOR = 0XXXXXXULTRA SONIC SENSOR = 0

im using microchip pic18f4580 with sk40c cytron board and HC-SR04 ultrasonic sensor
software:mplab ide c18 compiler and pickit2



Code:
#include <p18f4580.h>
#include <usart.h>
#include <delays.h>
#include <stdio.h>
#include <stdlib.h>

#pragma config OSC= HS
#pragma config WDT=OFF
#pragma config LVP=OFF

void T0Delay10us(void);
void T0Delay5ms(void);
void uart(void);
void Call_Init_PIC (void);
void Call_Init_USART (void);
void uart(void);

#define Trig PORTCbits.RC0
//ECHO is TRISCbits.TRISC2=1;  //CCP1 input pin
void main(void)
{

unsigned char a,b;
CCP1CON=0x05;
T3CON=0x0;
T1CON=0x0;
TRISCbits.TRISC2=1;  //CCP1 input pin
CCPR1L=0;
CCPR1H=0;
while(1)
{
uart();
Call_Init_PIC ();

TMR1H=0;
TMR1L=0;
Trig=1;
T0Delay10us();
Trig=0;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF=0);
T1CONbits.TMR1ON=1;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF=0);
T1CONbits.TMR1ON=0;
a=CCPR1L;
b=CCPR1H;


while (1)
{ 
printf("ULTRA SONIC SENSOR = %5d",a);
printf("XXXXXX");
printf("ULTRA SONIC SENSOR = %5d",b);
Delay10KTCYx(255); 
}
}

//PIR1bits.TMR1IF=0;
T0Delay5ms();
}

void Call_Init_PIC (void)
{

TRISCbits.TRISC2=0;
TRISCbits.TRISC0=0;

}

void uart(void)
{
TXSTA=0B00100000;
SPBRG=32;
TRISCbits.RC6=0;
TXSTAbits.TXEN=1;
RCSTAbits.SPEN=1;
}


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;
	}
 

Mention what clock frequency you are using for PIC. Zip and post complete MPLAB project.
 

Mention what clock frequency you are using for PIC. Zip and post complete MPLAB project.
Hello sir, this is my attachment, my crystal is 20Mhz
 

Attachments

  • hc-sr04.zip
    44.8 KB · Views: 34

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top