File "serial.c"

Full Path: /home/analogde/www/private/Projet/serial.c
File size: 4.76 KB
MIME-type: text/x-c
Charset: utf-8


   #include "..\ports_d256.h"
   #include "serial.h"

 unsigned char   ioport;         // 0 = use SC0 for standard i/o.  1 = use SC1
//---------------------------------------------------------------------------
// Initialize serial port
void InitSerial(unsigned char baudrate){
    SC0BDL = baudrate;  // set baud rate register
    SC0BDH = 0x00;
    SC0CR1 = 0;     // configure SCI0 control registers
//    7      6      5   4   3   2   1 0
//   LOOPS SCISWAI RSRC M WAKE ILT PE PT
    SC0CR2 = 0x2C;  // enable transmit and receive and receive interrupt
//    7    6   5    4   3 2   1  0
//   TIE TCIE RIE ILIE TE RE RWU SBK
}

/*----------------------------------------------------------------------------
ReadyToSend
-----------
 Wait for the serial port Status register to indicate the transmiter is
 ready to receive another byte.

 INPUT:     ioport      0= SCI0, 1= SCI1
 OUTPUT:    returns 0 when ready.  NO TIMEOUT is currently implemented
----------------------------------------------------------------------------*/
char ReadyToSend(char portnum){
    unsigned char Status;
    do{
        if(portnum == 0)    Status = SC0SR1;
        else                Status = SC1SR1;
    }while((Status & 0x80) == 0);
	return 0;
}

/*----------------------------------------------------------------------------
putchar
-------
 Wait for the serial port transmiter to return ready then write a single byte
 to the transmit register.

 INPUT:     SByte       Value to write
            ioport      0= SCI0, 1= SCI1
 OUTPUT:    returns 0 when finished.  NO TIMEOUT is currently implemented
----------------------------------------------------------------------------*/
char putchar(unsigned char scbyte){
//    ANDA    #$7F    ; mask parity
    if(ReadyToSend(ioport)) return 1;   // wait for ready to send
    if(ioport == 0) SC0DRL = scbyte;
    else            SC1DRL = scbyte;
	return 0;
}


/*----------------------------------------------------------------------------
putch
-----
 Send a single byte out the serial port.
 If this is the '\n' character then 2 bytes are sent which are the carriage
 return followed by line feed characters.

 INPUT:		ch			Value to write
            ioport      0= SCI0, 1= SCI1
----------------------------------------------------------------------------*/
void putch(unsigned char ch){

	if (ch == '\n'){
		putchar(CR);
        putchar(LF);
	}
	else{
		putchar(ch);
	}
}

/*----------------------------------------------------------------------------
puts
----
 Send a string of characters to the serial port.  The string must end in a
 NULL character (0x00).

 INPUT:		sptr		pointer to the string
            ioport      0= SCI0, 1= SCI1
----------------------------------------------------------------------------*/
void puts(char *sptr){
	while(*sptr){
		putch(*sptr);
		++sptr;
	}
}

/*----------------------------------------------------------------------------
strlength
---------
 Returns the number of bytes in a 0 terminated string.
 The 0 is not counted.

 INPUT:		sptr		pointer to the string
 OUTPUT:	returns the length of the string
----------------------------------------------------------------------------*/
unsigned short strlength(char *sptr){
    unsigned short strlencount;
	strlencount=0;
	while(*sptr != 0){
		++strlencount;
		++sptr;
	}
	return(strlencount);
}
/*----------------------------------------------------------------------------
reverse
-------
 Reverse the character order of a 0 terminating string

 INPUT:		rptr		pointer to the string
 OUTPUT:	rptr		new string
----------------------------------------------------------------------------*/
void reverse(char *rptr){
    unsigned short countb, counte, middle;
	char tbyte;

	counte = (strlength(rptr)) - 1;  // get number of digits in the string
	for(countb=0; countb < counte; countb++, counte--){
		tbyte = *(rptr+countb);
		*(rptr+countb) = *(rptr+counte);
		*(rptr+counte) = tbyte;
	}
}

/*----------------------------------------------------------------------------
itoa
----
 Convert a number to a 0 terminating ASCII string representing a Base 10
 Decimal Value

 INPUT:		n		number to convert
 			s		pointer to the array to hold the string
----------------------------------------------------------------------------*/
void itoa(unsigned long n, char *s){
  unsigned long sign;
  char *ptr;

  ptr = s;
//  sign = n;
//  if (sign < 0) n = -n;
  do {
    *ptr++ = n % 10 + '0';
    } while ((n = n / 10) > 0);
//  if (sign < 0) *ptr++ = '-';
  *ptr = '\0';
  reverse(s);
}

/*----------------------------------------------------------------------------
puti
----
 Output an ASCII Decimal string representation of a number to the serial port

 INPUT:		num		number to send
----------------------------------------------------------------------------*/
void puti(unsigned long num){
	char tempbuf[7];

	itoa(num, tempbuf);
	puts(tempbuf);
}