Hello all, I am hoping someone has some incite as to what I am doing wrong here, I figure
it is a small problem but I am out of ideas. This is just a test to figure out how to pass
char * through methods. The code should print Hello World to the serial port which it
does, then print tes repeatedly, which it does not, rather the output is as follows:
Hello World"""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Here is the code, also below is the sci.c and sci.h that do the actually serial
communication.
Thank You in advanced !
Zach Long
MAIN:
#include "sci.h"
int main(void);
void encode(char *out);
int main(void)
{
char* head = "Hello World"; // declare first print statement
char* outData =(char*) malloc (sizeof (char*)* 5); // make room for 5 char
SCI_Init(9600,0); //Startup serial port 0(under LCD) at 9600 BAUD
SCI_OutStatus(1); // Is it clear to send (buffer empty)
SCI_OutString(head,0); // print "Hello World" to serial port (WORKS !!)
while(1)
{
if(SCI_OutStatus(0))// Is it clear to send (buffer empty)
{
encode(outData);//,out.throt,out.heading); // build output
SCI_OutString(outData,0); // print out put to serial port
}
}
return 0;
}
void encode(char *out) // take in pointer and fill with chars
{
out[0] = 't';
out[1] = 'e';
out[2] = 's';
out[3] = 0;
}
SCI include:
// filename ******************* SCI.H **************************
// Jonathan W. Valvano 1/29/04
// This example accompanies the books
// "Embedded Microcomputer Systems: Real Time Interfacing",
// Brooks-Cole, copyright (c) 2000,
// "Introduction to Embedded Microcomputer Systems:
// Motorola 6811 and 6812 Simulation", Brooks-Cole, copyright (c) 2002
// Copyright 2004 by Jonathan W. Valvano, v...@mail.utexas.edu
// You may use, edit, run or distribute this file
// as long as the above copyright notice remains
// Modified by EE345L students Charlie Gough && Matt Hawk
// Modified by EE345M students Agustinus Darmawan + Mingjie Qiu
// Modified by Zachary Long Oct 16, 2009 (setup for Dragon Board 12 9600 BAUD, use of both
serial ports enabled)
//How To
/*
Each method has an unsigned int port parameter, this specifices the serial port
the method will act on. Dragon Board 12: Serial port under LCD (top) = 0, Serial port
near power plug (bottom) = 1
*/
// standard ASCII symbols
#define CR 0x0D
#define LF 0x0A
#define BS 0x08
#define ESC 0x1B
#define SP 0x20
#define DEL 0x7F
//-------------------------SCI_Init------------------------
//-------------------------SCI_Init------------------------
// Initialize Serial port SCI
// Input: baudRate is tha baud rate in bits/sec
// Output: none
// SCIBDL = 24000000/(16 x BR)
// baudRate = 2400 bits/sec SCIBDL=625
// baudRate = 4800 bits/sec SCIBDL=313
// baudRate = 9600 bits/sec SCIBDL=156
// baudRate = 19200 bits/sec SCIBDL=78
// assumes a module clock frequency of 4 MHz
// sets baudRate to 9600
void SCI_Init(unsigned short baudRate,unsigned int port);
//-------------------------SCI_InStatus--------------------------
// Checks if new input is ready, TRUE if new input is ready
// Input: unsigned int port
// Output: TRUE if a call to InChar will return right away with data
// FALSE if a call to InChar will wait for input
char SCI_InStatus(unsigned int port);
//-------------------------SCI_InChar------------------------
// Wait for new serial port input, busy-waiting synchronization
// Input: unsigned int port
// Output: ASCII code for key typed
char SCI_InChar(unsigned int port);
void SCI_InString(char* string,unsigned short max,unsigned int port); // Reads in a String
of max length
//----------------------SCI_InUDec-------------------------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 16 bit unsigned number
// valid range is 0 to 65535
// Input: unsigned int port
// Output: 16-bit unsigned number
// If you enter a number above 65535, it will truncate without an error
// Backspace will remove last digit typed
unsigned short SCI_InUDec(unsigned int port);
//---------------------SCI_InUHex----------------------------------------
// Accepts ASCII input in unsigned hexadecimal (base 16) format
// Input: unsigned int port
// Output: 16-bit unsigned number
// No '$' or '0x' need be entered, just the 1 to 4 hex digits
// It will convert lower case a-f to uppercase A-F
// and converts to a 16 bit unsigned number
// value range is 0 to FFFF
// If you enter a number above FFFF, it will truncate without an error
// Backspace will remove last digit typed
unsigned short SCI_InUHex(unsigned int port);
//-----------------------SCI_OutStatus----------------------------
// Checks if output data buffer is empty, TRUE if empty
// Input: unsigned int port
// Output: TRUE if a call to OutChar will output and return right away
// FALSE if a call to OutChar will wait for output to be ready
char SCI_OutStatus(unsigned int port);
//-------------------------SCI_OutChar------------------------
// Wait for buffer to be empty, output 8-bit to serial port
// busy-waiting synchronization
// Input: 8-bit data to be transferred, unsigned int port
// Output: none
void SCI_OutChar(char data,unsigned int port);
//-----------------------SCI_OutUDec-----------------------
// Output a 16-bit number in unsigned decimal format
// Input: 16-bit number to be transferred, unsigned int port
// Output: none
// Variable format 1-5 digits with no space before or after
void SCI_OutUDec(unsigned short,unsigned int port);
//-------------------------SCI_OutString------------------------
// Output String (NULL termination), busy-waiting synchronization
// Input: pointer to a NULL-terminated string to be transferred, unsigned int port
// Output: none
void SCI_OutString(char *pt,unsigned int port);
//--------------------------SCI_OutUHex----------------------------
// Output a 16 bit number in unsigned hexadecimal format
// Input: 16-bit number to be transferred, unsigned int port
// Output: none
// Variable format 1 to 4 digits with no space before or after
void SCI_OutUHex(unsigned short,unsigned int port);
// filename *************** SCI.C ******************************
// Simple I/O routines to 9S12C32 serial port
// Jonathan W. Valvano 1/29/04
// This example accompanies the books
// "Embedded Microcomputer Systems: Real Time Interfacing",
// Brooks-Cole, copyright (c) 2000,
// "Introduction to Embedded Microcomputer Systems:
// Motorola 6811 and 6812 Simulation", Brooks-Cole, copyright (c) 2002
// Copyright 2004 by Jonathan W. Valvano, v...@mail.utexas.edu
// You may use, edit, run or distribute this file
// as long as the above copyright notice remains
// Modified by EE345L students Charlie Gough && Matt Hawk
// Modified by EE345M students Agustinus Darmawan + Mingjie Qiu
// Modified by Steven Lamb April 28, 2004 (minor changes for g++)
// Modified by Zachary Long Oct 16, 2009 (serial init (9600)updated, use of both serial
ports enabled)
//How To
/*
*/
#include "hcs12.h" // io register map
#include "SCI.h"
#define RDRF 0x20 // Receive Data Register Full Bit
#define TDRE 0x80 // Transmit Data Register Empty Bit
//-------------------------SCI_Init------------------------
// Initialize Serial port SCI
// Input: baudRate is tha baud rate in bits/sec
// Output: none
// SCIBDL = 24000000/(16 x BR)
// baudRate = 2400 bits/sec SCIBDL=625
// baudRate = 4800 bits/sec SCIBDL=313
// baudRate = 9600 bits/sec SCIBDL=156
// baudRate = 19200 bits/sec SCIBDL=78
// assumes a module clock frequency of 4 MHz
// sets baudRate to 9600
void SCI_Init(unsigned short baudRate, unsigned int port)
{
switch(port)
{
case 0:
{
SCI0BDH = 0;
switch(baudRate)
{
case 2400: SCI0BDL=625; break;
case 4800: SCI0BDL=313; break;
case 9600: SCI0BDL=156; break;
case 19200: SCI0BDL=78; break;
default: SCI0BDL = 156 ; // 9600
}
break;
SCI0CR1 = 0;
/* bit value meaning
7 0 LOOPS, no looping, normal
6 0 WOMS, normal high/low outputs
5 0 RSRC, not appliable with LOOPS=0
4 0 M, 1 start, 8 data, 1 stop
3 0 WAKE, wake by idle (not applicable)
2 0 ILT, short idle time (not applicable)
1 0 PE, no parity
0 0 PT, parity type (not applicable with PE=0)
*/
SCI0CR2 = 0x0C;
/* bit value meaning
7 0 TIE, no transmit interrupts on TDRE
6 0 TCIE, no transmit interrupts on TC
5 0 RIE, no receive interrupts on RDRF
4 0 ILIE, no interrupts on idle
3 1 TE, enable transmitter
2 1 RE, enable receiver
1 0 RWU, no receiver wakeup
0 0 SBK, no send break
*/
break;
}
case 1:
{
SCI1BDH = 0;
switch(baudRate)
{
case 2400: SCI1BDL=625; break;
case 4800: SCI1BDL=313; break;
case 9600: SCI1BDL=156; break;
case 19200: SCI1BDL=78; break;
default: SCI1BDL = 156 ; // 9600
}
break;
SCI1CR1 = 0;
/* bit value meaning
7 0 LOOPS, no looping, normal
6 0 WOMS, normal high/low outputs
5 0 RSRC, not appliable with LOOPS=0
4 0 M, 1 start, 8 data, 1 stop
3 0 WAKE, wake by idle (not applicable)
2 0 ILT, short idle time (not applicable)
1 0 PE, no parity
0 0 PT, parity type (not applicable with PE=0)
*/
SCI1CR2 = 0x0C;
/* bit value meaning
7 0 TIE, no transmit interrupts on TDRE
6 0 TCIE, no transmit interrupts on TC
5 0 RIE, no receive interrupts on RDRF
4 0 ILIE, no interrupts on idle
3 1 TE, enable transmitter
2 1 RE, enable receiver
1 0 RWU, no receiver wakeup
0 0 SBK, no send break
*/
break;
}
}
}
//-------------------------SCI_InChar------------------------
// Wait for new serial port input, busy-waiting synchronization
// Input: int port
// Output: ASCII code for key typed
char SCI_InChar(unsigned int port)
{
switch(port)
{
case 0:
while((SCI0SR1 & RDRF) == 0){};
return(SCI0DRL);
break;
case 1:
while((SCI1SR1 & RDRF) == 0){};
return(SCI1DRL);
break;
}
}
//-------------------------SCI_OutChar------------------------
// Wait for buffer to be empty, output 8-bit to serial port
// busy-waiting synchronization
// Input: 8-bit data to be transferred
// Output: none
void SCI_OutChar(char data,unsigned int port)
{
switch(port)
{
case 0:
while((SCI0SR1 & TDRE) == 0){};
SCI0DRL = data;
break;
case 1:
while((SCI1SR1 & TDRE) == 0){};
SCI1DRL = data;
break;
}
}
//-------------------------SCI_InStatus--------------------------
// Checks if new input is ready, TRUE if new input is ready
// Input: unsigned int port
// Output: TRUE if a call to InChar will return right away with data
// FALSE if a call to InChar will wait for input
char SCI_InStatus(unsigned int port)
{
switch(port)
{
case 0:
return(SCI0SR1 & RDRF);break;
case 1:
return(SCI1SR1 & RDRF);break;
}
}
//-----------------------SCI_OutStatus----------------------------
// Checks if output data buffer is empty, TRUE if empty
// Input: unsigned int port
// Output: TRUE if a call to OutChar will output and return right away
// FALSE if a call to OutChar will wait for output to be ready
char SCI_OutStatus(unsigned int port)
{
switch(port)
{
case 0:
return(SCI0SR1 & TDRE);
break;
case 1:
return(SCI1SR1 & TDRE);
break;
}
}
//-------------------------SCI_OutString------------------------
// Output String (NULL termination), busy-waiting synchronization
// Input: pointer to a NULL-terminated string to be transferred, unsigned int port
// Output: none
void SCI_OutString(char *pt,unsigned int port)
{
while(*pt){
SCI_OutChar(*pt,port);
pt++;
}
}
//----------------------SCI_InUDec-------------------------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 16 bit unsigned number
// valid range is 0 to 65535
// Input: unsigned int port
// Output: 16-bit unsigned number
// If you enter a number above 65535, it will truncate without an error
// Backspace will remove last digit typed
unsigned short SCI_InUDec(unsigned int port)
{
unsigned short number=0, length=0;
char character;
character = SCI_InChar(port);
while(character!=CR) // accepts until carriage return input
{
// The next line checks that the input is a digit, 0-9.
// If the character is not 0-9, it is ignored and not echoed
if((character>='0') && (character<='9'))
{
number = 10*number+(character-'0'); // this line overflows if above 65535
length++;
SCI_OutChar(character,port);
}
// If the input is a backspace, then the return number is
// changed and a backspace is outputted to the screen
else if((character==BS) && length)
{
number /= 10;
length--;
SCI_OutChar(character,port);
}
character = SCI_InChar(port);
}
return number;
}
//-----------------------SCI_OutUDec-----------------------
// Output a 16-bit number in unsigned decimal format
// Input: 16-bit number to be transferred, unsigned int port
// Output: none
// Variable format 1-5 digits with no space before or after
void SCI_OutUDec(unsigned short n,unsigned int port)
{
// This function uses recursion to convert decimal number
// of unspecified length as an ASCII string
if(n >= 10){
SCI_OutUDec(n/10,port);
n = n%10;
}
SCI_OutChar(n+'0',port); /* n is between 0 and 9 */
}
//---------------------SCI_InUHex----------------------------------------
// Accepts ASCII input in unsigned hexadecimal (base 16) format
// Input: unsigned int port
// Output: 16-bit unsigned number
// No '$' or '0x' need be entered, just the 1 to 4 hex digits
// It will convert lower case a-f to uppercase A-F
// and converts to a 16 bit unsigned number
// value range is 0 to FFFF
// If you enter a number above FFFF, it will truncate without an error
// Backspace will remove last digit typed
unsigned short SCI_InUHex(unsigned int port){
unsigned short number=0, digit, length=0;
char character;
character = SCI_InChar(port);
while(character!=CR){
digit = 0x10; // assume bad
if((character>='0') && (character<='9')){
digit = character-'0';
}
else if((character>='A') && (character<='F')){
digit = (character-'A')+0xA;
}
else if((character>='a') && (character<='f')){
digit = (character-'a')+0xA;
}
// If the character is not 0-9 or A-F, it is ignored and not echoed
if(digit<=0xF ){
number = number*0x10+digit;
length++;
SCI_OutChar(character,port);
}
// Backspace outputted and return value changed if a backspace is inputted
else if(character==BS && length){
number /=0x10;
length--;
SCI_OutChar(character,port);
}
character = SCI_InChar(port);
}
return number;
}
//--------------------------SCI_OutUHex----------------------------
// Output a 16 bit number in unsigned hexadecimal format
// Input: 16-bit number to be transferred, unsigned int port
// Output: none
// Variable format 1 to 4 digits with no space before or after
void SCI_OutUHex(unsigned short number,unsigned int port){
// This function uses recursion to convert the number of
// unspecified length as an ASCII string
if(number>=0x10) {
SCI_OutUHex(number/0x10,port);
SCI_OutUHex(number%0x10,port);
}
else if(number<0xA){
SCI_OutChar(number+'0',port);
}
else{
SCI_OutChar((number-0x0A)+'A',port);
}
}
//------------------------SCI_InString------------------------
// This function accepts ASCII characters from the serial port
// and adds them to a string until a carriage return is inputted
// or until max length of the string is reached.
// It echoes each character as it is inputted.
// If a backspace is inputted, the string is modified
// and the backspace is echoed
// InString terminates the string with a null character
// -- Modified by Agustinus Darmawan + Mingjie Qiu --
void SCI_InString(char *string,unsigned short max,unsigned int port)
{
int length=0;
char character;
character = SCI_InChar(port);
while(character!=CR)
{
if(character==BS)
{
if(length)
{
string--;
length--;
SCI_OutChar(BS,port);
}
}
else if(length
{
*string++=character;
length++;
SCI_OutChar(character,port);
}
character = SCI_InChar(port);
}
*string = 0;
}
------------------------------------

(You need to be a member of 68hc12 -- send a blank email to 68hc12-subscribe@yahoogroups.com )