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.

Incorrect output for RF Transmitter code for pic

Status
Not open for further replies.

areeckal

Member level 2
Joined
Jan 22, 2011
Messages
42
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,567
Hi,
I am posting my RF transmitter code:
Code:
#include <htc.h>
#include "usart.h"

//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);

void main()
{
	unsigned int data,addr,synchro;
	//Initialize the USART
	USARTInit();
	//TRISC=0xC0;
	//Now Read some input
	while(1)
	{
		data=0b00000001;
		addr=0b00000000;
		synchro=0b10101010;
		/* 
      	Now send a Packet
      	Packet Format is AA<data><data inverse>Z
		total Packet size if 5 bytes.
      	*/
		
		//SYNCHRO byte
		USARTWriteInt(synchro,8);
		_delay(100);

		//Address
      	USARTWriteInt(addr,8);
	    _delay(100);
		//Data
    	USARTWriteInt(data,8);
	   	_delay(100);
		//Checksum
      	USARTWriteInt(data+addr,8);
		_delay(100);
		//Stop bit
		USARTWriteInt(0,8);
		TXEN=0;
	}
}
Code:
/*****************************************************************

Most Basic USART (RS232 Serial) Communication Support File.
Simple reading and writing of data without using
Interrupts.

BAUD RATE:57600 Bits per Second
CRYSTAL Frequency: 20MHz

Target Chip: PIC18F4520
Target Compiler: HI-TECH C For PIC18 (http://www.htsoft.com/)
Project: MPLAP Project File

*****************************************************************/

#include <htc.h>
#include<stdio.h>

#include "usart.h"

void USARTInit()
{
	//Baud Rate = 57600 Bits per Second
	//*** Note: Valid On 20MHz Crystal ONLY ***
	//For other crystal freq calculate new values for SPBRG
	SPBRG=86;

	//TXSTA REG
	TXEN=1;
	BRGH=1;
	
	//RCSTA
	SPEN=1;
	CREN=1;	//Enable Receiver (RX)
	
	//BAUDCON
	BRG16=1;

}

void USARTWriteByte(char ch)
{
	//Wait for TXREG Buffer to become available
	while(!TXIF);

	//Write data
	TXREG=ch;
}

void USARTWriteString(const char *str)
{
	while((*str)!='\0')
	{
		//Wait for TXREG Buffer to become available
		while(!TXIF);

		//Write data
		TXREG=(*str);

		//Next goto char
		str++;
	}
}

/*

Writes a line of text to USART and goes to new line
The new line is Windows style CR/LF pair.

This will work on Hyper Terminal Only NOT on Linux

*/

void USARTWriteLine(const char *ln)
{
	USARTWriteString(ln);
	USARTWriteString("\r\n");
}

void USARTWriteInt(int val,unsigned char field_length)
{
	if(val<0) 
	{
		USARTWriteByte('-');	//Write '-' sign for negative numbers.
		val=(val*(-1));				//Make it positive.
	}

	//Convert Number To String and pump over Tx Channel.
	char str[5]={0,0,0,0,0};
	int i=4,j=0;
	while(val)
	{
		str[i]=val%10;
		val=val/10;
		i--;
	}
	if(field_length>5)
		while(str[j]==0) j++;
	else
		j=5-field_length;
	
	for(i=j;i<5;i++)
	{
		USARTWriteByte('0'+str[i]);
	}
}


unsigned char USARTReadByte()
{
	while(!RCIF);	//Wait for a byte
	
	return RCREG;
}

While I debugged it in mplab , i got the following output from uart-i/o tab.
1701.
Whats the bug?
 

Attachments

  • RF_PIC18F_TX.rar
    77.8 KB · Views: 81

Hi,

I think you should try first at low speeds snd then you can go for high speeds of USART. Please check the PC baud rate if you are trying to communicate with PC.

Thankyou
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top