Send string of characters/UART - P9S08SG8

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Send string of characters/UART - P9S08SG8

5,566 Views
slosjo
Contributor I
Hello all-
I am using a P9S08SG8 microcontroller.
I am in need of some help.  First of all, I am new to this forum and new to Freescale hardware and software, but have some experience in C, C++, etc.  I have been searching for answers in the forum, online, and the datasheet, but haven't found too much. 
 
I need to send a string of characters over a UART line.  I need to send these characters when there are different states on input pins...for example, if pin 1 is high, I need to send "do this action\n" on the UART line.  If someone has some advice for me, or could direct me to some literature to read, that would be great. 
 
Thanks
 
slosjo
 
 
Added p/n to subject.


Message Edited by NLFSJ on 2008-01-14 04:05 PM
Labels (1)
0 Kudos
6 Replies

1,292 Views
allawtterb
Contributor IV
How far have you gotten? Do you understand how to set the baud rate for the SCI that you are using?  Do you know how to set inputs and outputs for the pins that you will be reading?  How are you debugging the code that you have written?  To know how we can help you best we need to know where you are starting from.
0 Kudos

1,292 Views
slosjo
Contributor I
Hello-
 
I am starting from scratch, and I'm using codewarrior.  I am trying to get all the pieces straight in my head before I start writing code ( i.e. how to check pin status, set baud rate, how to output on the uart by sending 1 character at a time using the transmit buffer...these are things that I am unfarmiliar with and haven't quite got my head wrapped around yet)  From what I gather from the datasheet for my uC(P9S08SG8), setting the direction for a pin is as follows:  PTADD1 = 0 would mean that port A #1 is set to an input, and PTADD1 = 1 would mean that port A #1 is set to an output.  Ok, so then assuming that is correct, I have a switch on port 1, and I am in a loop checking it.  I am unsure of the syntax used, but I need to do something similar to this:
 
               for(int i=1, i>0, i++){      
                   If PTA1 = 1
                        output string on UART
                   else
                        //nothing 
                
}
 
The program will be more complex when all is said and done, probably including switch debouncing and many more inputs and outputs, but if I can understand the syntax needed to make this simple program happen, It will help me get started. 
 
Thanks
 
slosjo
0 Kudos

1,292 Views
ballen
Contributor I
slosjo,
Here's a little example code for you. I try to hide the lowest level code, so the actual transmit stuff could be put into one function. In my app., I have to be able to switch back and forth between SCI1 and SCI2, hence the Enable and Disable functions, but you probably will only need to enable the SCI once using the CW gui (it ends up in the MCUinit function if you enable C initialization in the New Project Wizard).
 
It might also help to look at how you can use the #defines in the header file for your processor. These #defines let you refer directly to registers and bits in those registers without having to create them yourself. It takes a little study to figure it out, but is really a time-saver once you do. The #defines are usually named the same as they are in the data sheet, which makes it pretty easy.
 
//-----------------------------------------------------------------------
// SCI1_Enable() - function to enable SCI channel 1
//
// arguments: none
// returns: nothing
//-----------------------------------------------------------------------
void SCI1_Enable( void )
{
    SCI1C2 = 0x2C;      // enable Rx irq's, enable Tx, enable Rx
}
 
//-----------------------------------------------------------------------
// SCI1_Disable() - function to disable SCI channel 1
//
// arguments: none
// returns: nothing
//-----------------------------------------------------------------------
void SCI1_Disable(void)
{
    SCI1C2 = 0;    // disable everything
}
 
//-----------------------------------------------------------------------
// SCI1_SendChar() - function to write a char to SCI channel 1
//
// arguments: value - char to send
// returns: nothing
//-----------------------------------------------------------------------
void SCI1_SendChar( char value )
{
    while( SCI1S1_TDRE == 0 );      // wait for xmit register to be MT
   
    SCI1D = value;
}
 
/*--------------- 3/2/07 -----------------------------
 * Function to output a NULL terminated C string to SCI1.
 -------------------------------------------------------
 */
void TxPutStr(char *st)
{
    while(*st) SCI1_SendChar(*st++);
}
 
Hope this helps,
bill
0 Kudos

1,292 Views
peg
Senior Contributor IV
Hello and welcome to the forums, slosjo

First lets just assume you want to a "Hello World" when a button is pressed.

Here is what you need to do in principle:

setup a main loop that polls the pushbutton and also checks a "something to send" flag
when a press is detected, set "something to send" on and point to the string "Hello World"
when "something to send" is detected check the SCI for transmit buffer empty
If the tx buffer is MT then  send the first character of the string.
Then the loop will continue looping through until the transmit buffer is MT again and you will put the next character in the transmit buffer.
and so on until the string terminator is detected,
When you hit the string terminator just clear "something to send" and your string index.

This does not handle the case where the button is repressed while the string is still being sent.
A simple fix here for now would be to stop responding to button presses while "something to send" is true.
This will provide a debounce for the pushbutton as well.
Good Luck!

0 Kudos

1,292 Views
slosjo
Contributor I
Thank you for the reply, but could you please explain this in a slightly lower level?  It would be very helpful to have some examples of syntax.  I understand loops and strings and whatnot, but I'm not sure about the syntax of how to poll a specific pin, or to check for an empty TX buffer.  A small piece of example code would be more helpful than anything else. 
 
Thanks for your help
 
slosjo
0 Kudos

1,292 Views
Witztronics
Contributor IV
The demo software from our Viper products may be helpful.

http://witztronics.com/product_info.php?products_id=44

It contains the routines used for reading/writing data and strings to the UART.

0 Kudos