I need to set up RF communication from a pic 18F4520 to another pic 18f4520.
I have developed transmitter and receiver code in mplab using hitech C compiler.
Now I need to debug the code. The transmitter is working fine and I am getting the USART output from USART I/O tab.
But while I am debugging receiver code, RCIF tag is not getting set even I am supplying input file through USART.
Transmitter
Receiver
usart.c
I have developed transmitter and receiver code in mplab using hitech C compiler.
Now I need to debug the code. The transmitter is working fine and I am getting the USART output from USART I/O tab.
But while I am debugging receiver code, RCIF tag is not getting set even I am supplying input file through USART.
Transmitter
Rich (BB 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;
//Initialize the USART
USARTInit();
//Now Read some input
while(1)
{
data=0x01;
/*
Now send a Packet
Packet Format is AA<data><data inverse>Z
total Packet size if 5 bytes.
*/
//Stabilize the Tx Module By Sending JUNK data
//J for Junk
USARTWriteByte('J');
_delay(100);
//Send 'A'
USARTWriteByte('A');
_delay(100);
//Send Another 'A'
USARTWriteByte('A');
_delay(100);
//Send the data;
USARTWriteInt(data,4);
_delay(100);
//Send inverse of data for error detection purpose
USARTWriteInt(~data,4);
_delay(100);
//End the packet by writing 'Z'
USARTWriteByte('Z');
_delay(100);
}
}
Rich (BB 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 packet[2],data;
TRISC=0x00;
//Initialize the USART
USARTInit();
//Get data from Tx
while(1)
{
//Wait for a packet
if(USARTReadByte()=='A')
{
if(USARTReadByte()=='A')
{
packet[0]=USARTReadByte();
packet[1]=USARTReadByte();
if((packet[0]!=~(packet[1]))&&(USARTReadByte()!='Z'))
continue;
else
{
data=packet[0];
PORTC=data;
}
}
}
}
}
Rich (BB 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 "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;
PEIE=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=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);
}
}
unsigned char USARTReadByte()
{
while(!RCIF); //Wait for a byte
return RCREG;
}