usart_str_write newline not working properly

General discussion on mikroC.
Post Reply
Author
Message
stanigator
Posts: 47
Joined: 01 Nov 2007 19:32

usart_str_write newline not working properly

#1 Post by stanigator » 16 Nov 2007 07:09

I have a problem in my function responsible for writing a string via USART in terms of how the newline operator should work. Below is a summary of my code (also welcome to download the zip file in )

Code: Select all

#include <built_in.h>
#include "typedefs.h"
#include "usart.h"

#define BUFFER_SIZE 5

void usart_str_write(char* string)
{
     while ((*string) != '\0'){
     //while (strlen(string)){
       Usart_Write((char) (*string));
       ++string;
     }
     Usart_Write('\n');               // add a new line to every string
}

void delay(uint number)
{
      while(--number > 0);
}

void main() {
  uint temp_res, last_temp_res;
  int n;

  OSCCON = 0x73;        /* internal oscillator at 8 Mhz, system clock = internal oscillator*/
 	//OSCTUNE = 0X0f;        /* oscillator tuning register at max frequency */
 	CMCON = 7;              // turn off comparators
 	
  ADCON1 = 0x0d;          // Configure Vref and AN0 and AN1
  TRISA  = 0x3f;          // PORTA is input except RA6 and RA7
  TRISB  = 0x00;          // all PORTB pins are output
  TRISC  = 0x00;             // PORTC is output
  
  Usart_Init(9600);        // initialize USART on entered baud rate

  while(1)
  {
    usart_str_write("what's up?");
  }
}
It seems that I can't display my terminal output properly like:

Received: what's up?
Received: what's up?

rather than what is seen in http://imagebin.ca/view/6Gu8MK.html. I have tried both while loop condition methods using strlen and using the null character, but both have shown the same output on the imagebin link. Is it possible to do what I described I want to display, and how hard is it? Any insights would be very helpful.

stanigator
Posts: 47
Joined: 01 Nov 2007 19:32

#2 Post by stanigator » 17 Nov 2007 23:39

After talking to some people on programming forums, I have been suggested to use '\r' as newline operator rather than '\n' as modified in usart_str_write() function shown below. However, I am still getting the same problem with how mikroC terminal shows the data as shown in my last message. Any insights on solving this problem would be great as I'm pretty stuck on it right now.

Code: Select all

void usart_str_write(char* string)
{
     //while ((*string) != '\0'){
     while (strlen(string)){
       Usart_Write((char) (*string));
       ++string;
     }
     Usart_Write('\n');               // add a new line to every string
     Usart_Write('\r');               // add a carriage return to terminal
}

stanigator
Posts: 47
Joined: 01 Nov 2007 19:32

#3 Post by stanigator » 19 Nov 2007 03:09

After looking at the decimal/hex dump mode on the terminal, I have found that the newline operator character has been transmitted properly. It would be much appreciated if I hear from someone soon as I expect the problem to be coming from terminal itself.

florin_niku
Posts: 2
Joined: 12 Mar 2007 16:32
Location: Romania

#4 Post by florin_niku » 20 Nov 2007 00:17

Hi !
I have tested the code you provided and got the same problem with mikroC terminal. Use Windows Hyper Terminal and you will get better results.

stanigator
Posts: 47
Joined: 01 Nov 2007 19:32

#5 Post by stanigator » 20 Nov 2007 05:22

Hi,

Thanks for the heads-up. It appears to be the case with Windows Terminal. I got some weird characters with Windows Terminal a while ago b/c my baud rate settings weren't correct.

XorXaX
Posts: 181
Joined: 14 Nov 2006 17:09
Location: Ludvika, Sweden
Contact:

#6 Post by XorXaX » 20 Nov 2007 09:12

Arent you supposed to send a carriage return before line feed?

Like this:

Code: Select all

Usart_Write('\r');               // add a carriage return to terminal
Usart_Write('\n');               // add a new line to every string
That's what I've learned. Always works for me.
Jonas Andersson, Sweden
EasyPIC3, EasyPIC5, various dev boards and fully licensed mikroC
"It's nice to be important, but it's more important to be nice"

stanigator
Posts: 47
Joined: 01 Nov 2007 19:32

#7 Post by stanigator » 20 Nov 2007 17:54

I ended up using Usart_Write('\r'); only instead of using both the carriage return and newline (although I may have reversed the order the first time). Seems to be working well enough after some tweaks.

Post Reply

Return to “mikroC General”