Thread: Help with printf/puts conversion

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    12

    Help with printf/puts conversion

    Hello I am interfacing my PIC16F874a microchip to a SP03 text to speech module (http://www.robotstorehk.com/SP03Tech.pdf), and I wrote my code using printf to output the commands to the chip. I have two problems: 1) it is not working, and 2) I would like to re-write it using "puts" to save code space on my PIC. I beleive I am calling my USART function correctly and my rs232 circuit it correct, so I beleive it is an error in my code. I also do not know how to use any type of "put" function ( I am just starting with C), so I was wondering if I can get help and suggestions on how to do this. I basically just have to output starting commands, then the ASCII I need to send, followed by 2 more commands. Thank you.

    Code:
    #include <pic.h>
    #include <stdio.h>
    #include "delay.h"
    #include "usart.h"
    
    void speech(char c)
    {
    	int cnum;
    	cnum=c;
    	printf("%c", cnum);
    	DelayMs(1);
    }
    
    void Writespeech(const char* s)
    {
    	while(*s)
    	speech(*s++);
    
    }
    
    main()
    {
    	int i;
    	
    
    			INTCON=0;	// purpose of disabling the interrupts.
    	
    			init_comms();	// set up the USART - settings defined in usart.h
    			
    			printf("%c",0x80);
    			DelayMs(1);
    			printf("%c",0x00);
    			DelayMs(1);
    			printf("%c",0x04);
    			DelayMs(1);
    			printf("%c",0x02);
    			DelayMs(1);
    			
    			Writespeech("How are You");
    			DelayMs(1);
    
    	                                printf("%c",0x1A);
    			DelayMs(1);
    			printf("%c",0x00);
    			DelayMs(1);
    			for(i=0;i<=35;i++)
    				{
    				DelayMs(200);
    				}
    For(;;);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Where exactly did you get the idea that puts, which you don't know how to use, is going to save space?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    The development system on which you are working is most likely the most reliable source of information on how to use it.

    This question is not necessarily a C question, per se, but rather one of the system in which it is implemented.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    12
    Because printf on a microchip eats up more program memory I was told. I figured this was more of a C syntax question than anything else. I will turn to other forums if no one can help. Thanks anyway!

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by baseball07 View Post
    Because printf on a microchip eats up more program memory I was told.
    And so would sprintf (followed by puts).
    Quote Originally Posted by baseball07 View Post
    I figured this was more of a C syntax question than anything else.
    Try puts with a basic test, such as puts("hello world"); -- if it has the same issues, it will save you little to micromanage it in this fashion. That may mean writing your own replacements, but that is a different sort of question.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    printf() is indeed rather inefficient if you're just printing a single character or a string. You could use putchar() or puts() if you can. Instead of "&#37;c", you could use putchar():
    Code:
    printf("%c",0x80);
    ->
    Code:
    putchar(0x80);
    And instead of
    Code:
    printf("%s\n", str);
    (note the \n!) you could use
    Code:
    puts(str);
    BTW, C is case-sensitive.
    Code:
    For(;;);
    [edit] Eight whole minutes too slow . . . who ever invented tabbed browsing anyway? [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 06:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 09:44 PM