Jump to content
Sign in to follow this  

Recommended Posts

/*
* 18f2525
* 8MHz internal oscillator
* UART Communcations with wTerm on PC
* MAX232N
*/

#include <system.h>
#include <stdlib.h>
#include <string.h>
#include <lcd_driver.h>
#include <stdio.h>

#pragma CLOCK_FREQ 8000000

#define BAUDRATE 9600
#define _XTAL_FREQ 8000000	//	Hz

//subroutine declarations
void setup(void);
void InitUART(void);
void UART_SendByte(unsigned char);
void UART_SendString(const unsigned char*);
unsigned char UART_ReceiveByte(void);

unsigned char flags, rx_dat;

void main()
{	
	char str[4], a = 0;
	
	osccon = 0x72;	//	8MHz internal osc
	while(!test_bit(osccon,2)){}	//	wait till osc stable
	
	setup();

	lcd_setup();
	lcd_clear();
	lprintf("");
	
	InitUART();
	
	UART_SendString("Hello?");
	UART_SendByte(0x0d);	//	carriage return
	UART_SendByte(0x0a);	//	line feed
	
	while(1)
	{		
		if(flags.0)
		{
			flags.0 = 0;
			pir1.RCIF = 0;
			lcd_write(rcreg);
		}
	}
}



void setup()
{
	intcon = 0xc0;	//	interrupts off
	intcon2 = 0x80;	//	pullups disabled
	rcon = 0x80;	//	interrupt priority enabled
	pie1.RCIE = 1;	//	rx interrupt enabled
	ipr1.RCIP = 0;	//	rx int low priority
	
	ccp1con = 0x00;	//	cap/comp disabled
	ccp2con = 0x00;
	
	sspcon1 = 0x00; //  spi disabled
	
	adcon0 = 0x00;	//	adc disabled
	adcon1 = 0x0f;	//	all digital i/o
		
	cmcon = 0x07;	//	comparaor disabled
	cvrcon = 0x00;	//	comparator volt ref disabled
	
	hlvdcon = 0x00;	//	hi/lo volt det disabled
	
	trisa = 0x00;
	lata = 0x00;
	
	trisb = 0x00;
	latb = 0x00;
	
	trisc = 0x00;
	latc = 0x00;
	//flags = 0;
}


void InitUART()
{
	trisc.6 = 1;	//	TX pin
	trisc.7 = 1;	//	RX pin
	
	spbrg = ((_XTAL_FREQ/16)/BAUDRATE) - 1;
	txsta.BRGH = 1;	//	fast baudrate
	txsta.SYNC = 0;	//	asynchronous
	rcsta.SPEN = 1;	//	enable serial port pins
	rcsta.CREN = 1;	//	enable reception
	rcsta.SREN = 0;	//	single receive disabled
	pie1.TXIE = 0;	//	disable tx interrupt
	pie1.RCIE = 1;	//	enable rx interrupt
	txsta.TX9 = 0;	//	8-bit transmission
	rcsta.RX9 = 0;	//	8-bit reception
	txsta.TXEN = 0;	//	reset transmitter
	txsta.TXEN = 1;	//	enable transmitter	
}


void UART_SendByte(unsigned char dat)
{
	while(!pir1.TXIF);	//	wait till last transmission finishes
	pir1.TXIF = 0;
	txreg = dat;
}



void UART_SendString(const unsigned char* str)
{
	while(*str) UART_SendByte(*str++);
}



unsigned char UART_ReceiveByte(void)   // Reads a character from the serial port
{
	if(rcsta.OERR) // If over run error, then reset the receiver
	{
		rcsta.CREN = 0;
		rcsta.CREN = 1;
	}
	return RCREG;
}




//------------------------------------------------------------------		interrupt routines
void interrupt_low()	//low priority ISR
{
	if(pir1.RCIF)  // If UART Rx Interrupt
	{
		pir1.RCIF = 0;
		flags.0 = 1;
		if(rcsta.OERR) // If over run error, then reset the receiver
		{
			rcsta.CREN = 0;
			rcsta.CREN = 1;
		}
		rx_dat = rcreg;
	}
}



This is the first time I have tried to send data to a pc using the UART. I have done this between PICs and use it (sort of) to save data to an SD card, but have never sent data to or received data from the PC. That being said, I have managed to get it working pretty well. Very few GOOD working examples on the net. Anyway, I seem to have a small issue that I can't resolve. The pic sends 2 strings at the beginning of operation so I know it's alive. Then it just sits and waits to receive data that it will echo bach to the PC. No biggie. Problem: every time the pic starts (and only when it starts) it send a null character to the pc. I am using Realterm as my pc side terminal. Code is below (or above -- not sure why it won't put the code where it makes sense).

 

Anyway, does anyone have an idea why I keep getting the null at the start?

 

Share this post


Link to post
Share on other sites

trisc = 0x00;

latc = 0x00;

 

these commands in setup() will look like a serial start to the receiver, don't set the tx line low or to an output

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...