Having trouble with recieving data with PIC18F452 HW USART

Started by Travis L. F Bailey November 29, 2003
I am trying to get the serial to work on my pic18f452 chip. I can print
data to hyperterm from pic with no problem. But, I can not figure out
how to get data from hypterterm to the pic.

I am able to print, here is a sample of my output:
First 2 RCSTA 144 TXSTA 160 First 3 RCSTA 144 TXSTA 160 First 4 RCSTA
144 TXSTA
160 First 5 RCSTA 144 TXSTA 160 First 6 RCSTA 144 TXSTA 160 First 7
RCSTA 144 T
XSTA 160 First 8 RCSTA 144 TXSTA 160 First 9 RCSTA 144 TXSTA 160 First
10 RCSTA
144 TXSTA 160

I think everything is set properly, but I am not sure.

Does anyone see any obvoius errors in my code?

Thanks,

Travis MPLAB C18 Code I am using: #include <p18cxxx.h> /* for TRISB and PORTB declarations */
#include <delays.h>
#include <usart.h>
#include <stdlib.h>

int shorttime;
int longtime;
int counter;
char data;
char counter_data[20];
char rcsta_data[20];
char txsta_data[20];

void main (void)
{
TRISB = 0; /* configure PORTB for output */
LATB = 0;
shorttime = 5;
longtime = 100;
counter = 1; OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_CONT_RX & USART_BRGH_LOW, 25);

while (1)
{
PORTB = 3; /* display value of 'counter' on the LEDs */
Delay10KTCYx(longtime); /* delay 1000 */
while (counter < 10)
{
counter++;
itoa(counter,&counter_data);
putrsUSART (" First " );
putsUSART ( &counter_data );

itoa(RCSTA,&rcsta_data);
putrsUSART (" RCSTA " );
putsUSART ( &rcsta_data );

itoa(TXSTA,&txsta_data);
putrsUSART (" TXSTA " );
putsUSART ( &txsta_data );

PORTB = 7;
Delay10KTCYx(longtime);
PORTB = 5;
Delay10KTCYx(longtime);
}

PORTB = 1;
while (1) {
/* Problem is here somewhere.... */
while(!DataRdyUSART())
{
/*
Blink LED's until input is recieved
Stays in here forever......
*/
PORTB = 128;
Delay10KTCYx(longtime);
PORTB = 0;
Delay10KTCYx(longtime);
}

data = ReadUSART();
putrsUSART ( " Input: " );
WriteUSART ( data );
if (data=='d')
{
PORTB = 5;
Delay10KTCYx(longtime);
}
if (data=='s')
{
PORTB = 31;
Delay10KTCYx(longtime);
}

if (data=='q')
break;
}

}
}



here my init code in hi-tech c
if you want tx interrupt de-comment le line
change the baud rate and and crystal to match your setup
in hytertem uncheck hardware flow control, xon-xoff

regards
S.

#include <pic18.h>
#include <stdio.h #define BAUD 9600
#define FOSC 2000000L
#define NINE 0 /* Use 9bit communication? FALSEit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

/* Serial initialization */
void init_comms(void)
{
SPBRG 9; //DIVIDER;
//TXIE=1; //enable tx/rx interrupt
RCIE=1;
TXSTA = (SPEED|NINE_BITS|0x20);
RCSTA = (NINE_BITS|0x90);
}

void putch(unsigned char byte)
{
/* output one byte */
while(!TXIF) /* set when register is empty */
continue;
TXREG = byte;
}

unsigned char getch()
{
/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}

unsigned char getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}

void print(const uchar *string)
{
while(*string!='\0')
{
putch(*string);
string++;
}
putch(0x0d);// cr
}
--- In , "Travis L. F Bailey" <tbailey@l...>
wrote:
> I am trying to get the serial to work on my pic18f452 chip. I can
print
> data to hyperterm from pic with no problem. But, I can not figure
out
> how to get data from hypterterm to the pic.
>
> I am able to print, here is a sample of my output:
> First 2 RCSTA 144 TXSTA 160 First 3 RCSTA 144 TXSTA 160 First 4
RCSTA
> 144 TXSTA
> 160 First 5 RCSTA 144 TXSTA 160 First 6 RCSTA 144 TXSTA 160 First
7
> RCSTA 144 T
> XSTA 160 First 8 RCSTA 144 TXSTA 160 First 9 RCSTA 144 TXSTA 160
First
> 10 RCSTA
> 144 TXSTA 160
>
> I think everything is set properly, but I am not sure.
>
> Does anyone see any obvoius errors in my code?
>
> Thanks,
>
> Travis > MPLAB C18 Code I am using: > #include <p18cxxx.h> /* for TRISB and PORTB declarations */
> #include <delays.h>
> #include <usart.h>
> #include <stdlib.h>
>
> int shorttime;
> int longtime;
> int counter;
> char data;
> char counter_data[20];
> char rcsta_data[20];
> char txsta_data[20];
>
> void main (void)
> {
> TRISB = 0; /* configure PORTB for output */
> LATB = 0;
> shorttime = 5;
> longtime = 100;
> counter = 1; > OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
> USART_ASYNCH_MODE & USART_EIGHT_BIT &
> USART_CONT_RX & USART_BRGH_LOW, 25);
>
> while (1)
> {
> PORTB = 3; /* display value of 'counter' on the LEDs */
> Delay10KTCYx(longtime); /* delay 1000 */
> while (counter < 10)
> {
> counter++;
> itoa(counter,&counter_data);
> putrsUSART (" First " );
> putsUSART ( &counter_data );
>
> itoa(RCSTA,&rcsta_data);
> putrsUSART (" RCSTA " );
> putsUSART ( &rcsta_data );
>
> itoa(TXSTA,&txsta_data);
> putrsUSART (" TXSTA " );
> putsUSART ( &txsta_data );
>
> PORTB = 7;
> Delay10KTCYx(longtime);
> PORTB = 5;
> Delay10KTCYx(longtime);
> }
>
> PORTB = 1;
> while (1) {
> /* Problem is here somewhere.... */
> while(!DataRdyUSART())
> {
> /*
> Blink LED's until input is recieved
> Stays in here forever......
> */
> PORTB = 128;
> Delay10KTCYx(longtime);
> PORTB = 0;
> Delay10KTCYx(longtime);
> }
>
> data = ReadUSART();
> putrsUSART ( " Input: " );
> WriteUSART ( data );
> if (data=='d')
> {
> PORTB = 5;
> Delay10KTCYx(longtime);
> }
> if (data=='s')
> {
> PORTB = 31;
> Delay10KTCYx(longtime);
> }
>
> if (data=='q')
> break;
> }
>
> }
> }




Thanks!!
   I will review and give it a shot.

Travis atomic_ant wrote:
here my init code in hi-tech c
if you want tx interrupt de-comment le line change the baud rate and and crystal
to match your setup
in hytertem uncheck hardware flow control, xon-xoff
regards
S.
#include <pic18.h>
#include <stdio.h>
#define BAUD 9600 #define FOSC 2000000L
#define NINE 0 /* Use 9bit communication? FALSEit */
#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1
#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif
#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif
/* Serial initialization */
void init_comms(void)
{
SPBRG 9; //DIVIDER; //TXIE=1;	//enable tx/rx interrupt
RCIE=1; TXSTA = (SPEED|NINE_BITS|0x20);
RCSTA = (NINE_BITS|0x90);
}
void putch(unsigned char byte) {
/* output one byte */
while(!TXIF)	/* set when register is empty */
continue;
TXREG = byte;
}
unsigned char getch()
{
/* retrieve one byte */
while(!RCIF)	/* set when register is not empty */
continue;
return RCREG;	}
unsigned char getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}
void print(const uchar *string)
{
while(*string!='\0')
{
putch(*string);
string++;
}
putch(0x0d);// cr
}
--- In p...@yahoogroups.com, "Travis L. F Bailey" <tbailey@l...> wrote:
I am trying to get the serial to work on my pic18f452 chip. I can
print 
data to hyperterm from pic with no problem. But, I can not figure
out 
how to get data from hypterterm to the pic.
I am able to print, here is a sample of my output:
First 2 RCSTA 144 TXSTA 160 First 3 RCSTA 144 TXSTA 160 First 4 
RCSTA 
144 TXSTA
160 First 5 RCSTA 144 TXSTA 160 First 6 RCSTA 144 TXSTA 160 First 
7 
RCSTA 144 T
XSTA 160 First 8 RCSTA 144 TXSTA 160 First 9 RCSTA 144 TXSTA 160 
First 
10 RCSTA
144 TXSTA 160
I think everything is set properly, but I am not sure.
Does anyone see any obvoius errors in my code?
Thanks,
Travis
MPLAB C18 Code I am using:
#include <p18cxxx.h> /* for TRISB and PORTB declarations */
#include <delays.h>
#include <usart.h>
#include <stdlib.h>
int shorttime;
int longtime;
int counter;
char data;
char counter_data[20];
char rcsta_data[20];
char txsta_data[20];
void main (void)
{
TRISB = 0; /* configure PORTB for output */
LATB = 0;
shorttime = 5;
longtime = 100;
counter = 1;
OpenUSART (USART_TX_INT_OFF & USART_RX_INT_OFF &
USART_ASYNCH_MODE & USART_EIGHT_BIT &
USART_CONT_RX & USART_BRGH_LOW, 25);
while (1)
{
PORTB = 3; /* display value of 'counter' on the LEDs */
Delay10KTCYx(longtime); /* delay 1000 */
while (counter < 10)
{
counter++;
itoa(counter,&counter_data);
putrsUSART (" First " );
putsUSART ( &counter_data );
itoa(RCSTA,&rcsta_data);
putrsUSART (" RCSTA " );
putsUSART ( &rcsta_data );
itoa(TXSTA,&txsta_data);
putrsUSART (" TXSTA " );
putsUSART ( &txsta_data );
PORTB = 7;
Delay10KTCYx(longtime);
PORTB = 5;
Delay10KTCYx(longtime);
}
PORTB = 1;
while (1) {
/* Problem is here somewhere.... */
while(!DataRdyUSART())
{
/*
Blink LED's until input is recieved
Stays in here forever......
*/
PORTB = 128;
Delay10KTCYx(longtime);
PORTB = 0;
Delay10KTCYx(longtime);
}
data = ReadUSART();
putrsUSART ( " Input: " );
WriteUSART ( data );
if (data=='d')
{
PORTB = 5;
Delay10KTCYx(longtime);
}
if (data=='s')
{
PORTB = 31;
Delay10KTCYx(longtime);
}
if (data=='q')
break;
}
}
}
------------------------ Yahoo! Groups Sponsor ---------------------~-->
Buy Ink Cartridges or Refill Kits for your HP, Epson, Canon or Lexmark
Printer at MyInks.com. Free s/h on orders $50 or more to the US & Canada.
http://www.c1tracking.com/l.asp?cidU11
http://us.click.yahoo.com/mOAaAA/3exGAA/qnsNAA/dN_tlB/TM
---------------------------------~->
to unsubscribe, go to http://www.yahoogroups.com and follow the instructions
">http://docs.yahoo.com/info/terms/