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.

Receive string on PIC18F452 UART

Status
Not open for further replies.

Swys

Junior Member level 1
Joined
Jul 22, 2008
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,412
pic18f452 uart

Hi all,

I was wondering if anyone could help me on this. I am working on MPLAB with a PIC18F452 and the HI-TECH compiler. I want to write a function which must be able to receive a string of characters from the UART. I enter this string on my terminal program.

I am able to receive a single character, but i want to receive a whole string of characters.

Code:
//Receive one character
unsigned char getch()
{
	while (!RCIF)
		continue;
	
	return RCREG;
}

Code:
//Input a series of characters into the specified buffer
//NOT WORKING!!!
void gets(char *s)
{
	char c = 0;
	
	while (c != '\0')
	{
		c = getch();
		*s = c;
		*s++;
	}	
}

I've been struggeling with this for a while now. If anyone can help I would greatly appreciate it!
 

pic18f452 usart

I am sending you exact files

usart.c and usart.h use in your program

it have a function receive string

it receives string terminated by NULL

I hope this will help you.
you can change return type from void to 'const char *'
 

Re: pic18f452 usart

I am sending you exact files

usart.c and usart.h use in your program

it have a function receive string

it receives string terminated by NULL

I hope this will help you.
you can change return type from void to 'const char *'


Dear
I am doing Final year project about GSM and i have much problem to reading and displaying my data on LCD
basically i have problem with UART
how i can read my complete data from UART and display on LCD
and i am using MikroC v6 compiler
Plz guide me
my code is:

#include <built_in.h>
unsigned char i;
unsigned char Byte[30];
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;

sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
void main()
{
TRISB=0X00;
PORTB=0X00;

UART1_init(9600);

Lcd_Init();
while(1)
{
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"PROJECT OF");
Lcd_Out(2,1,"MSc ELT");
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"MTU");
Delay_ms(500);
TXSTA = 0X20;
UART1_write_Text("AT\r");
Delay_ms(200);
UART1_write_Text("AT+CMGF=1\r");
Delay_ms(200);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"RAEDING RTU DATA");
Lcd_Out(2,1,"WAITING....");
UART1_write_Text("AT+CMGR=1\r");
CREN = 0X10;
while (UART1_Data_Ready() == 1) //problem is how UART will read the data transmited by GSM
{
Byte = UART1_Read();
Lcd_Out(2,1,Byte);
i++;
}
Delay_ms(500);
//UART1_write_Text("AT+CMGD=1\r");

Delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"RTU DATA READ");
Delay_ms(10000);
}
}
 

try to put some delay after UART1_init(9600) function and remove
Code:
while (UART1_Data_Ready() == 1) //problem is how UART will read the data transmited by GSM
this to like
if((UART1_Data_Ready())

and also reply .
 

Re: pic18f452 uart

Code:
void gets(char *s)
{
	char c = 0;
	
	while (c != '\0')
	{
		c = getch();
		*s = c;
		*s++;              //<---- WRONG
	}	
}

it should be

Code:
s++;

since you want the pointer to point to the next character position in the string, and not increment the value.

Review your C pointer operations in the meantime.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top