1: //Project : SCISubroutines.c

     2: //Version : 1.01

     3: //Date    : 12/24/2002, modified 5/23/2005

     4: //Author  : JianJian Song w/ modification by Curtis Rhodes

     5: //Company : rose-hulman institute of technolog

     6: //Purpose : RS232 serial communicaiton routines

     7: //Input:    serial port from PC

     8: //Outputs:  serial output to PC

     9: // Serial port

    10: //          RC6 (pin 25) == TX pin to send data to PC

    11: //          RC7 (pin 26) == RX pin to receive data from PC

    12: //

    13: //Chip type   : PIC16F877A

    14: //Clock frequency     : 20 MHz

    15: #include <pic.h>

    16: #include "memoryProgrammer.h"

    17: //===================================================

    18: // display_string()

    19: //===================================================

    20: void display_string(const char *string, char length)

    21: // send a string to serial port. length is string length

    22: {

    23: char i;

    24:         for(i=0;i<length;i++)

    25:                 send_byte_to_PC(string[i]);

    26:         send_byte_to_PC(0x0A);  // send line feed

    27:         send_byte_to_PC(0x0D);  // send carriage return

    28:         return;

    29: }

    30: // end of display_string()

    31: 

    32: //===================================================

    33: // rs232_initliazation(baud, crystal)

    34: //===================================================

    35: // baud -- baud rate in # of characters per second 

    36: // crystal -- crystal speed in MHz

    37: void rs232_initliazation(unsigned int baud, unsigned int crystal)

    38: {

    39: unsigned int x; // temp variable

    40: // transmit configuration: 8 bits, transmit enable, asynchronous mode, high speeed

    41: //Register TXSTA = 0B10100100;  // 0B is binary data format

    42:         TX9     = 0;            // 8-BIT MODE

    43:         TXEN    = 1;            // transmit enabled

    44:         SYNC    = 0;            // asynchronous mode

    45:         BRGH    = 0;            // low speed

    46: // receive configuration: serial port enable, 8 bits, continuous receive

    47: // Register TXSTA = 0B10010000;

    48:         SPEN    = 1;            // serial port enabled

    49:         RX9     = 0;            // 8-bit mode

    50:         CREN    = 1;            // continuous receive enabled

    51: 

    52: // baud rate configuration: Baud Rate = Fosc/(16(X+1)) when BRGH=1 for high speed

    53: // where X is the value in SPBRG

    54:         x = 100000/baud;

    55:         x = x*crystal;          // crystal speed in MHz

    56:         x = (x - 64)/64;

    57:         SPBRG = x;              // initialize baud rate generator register

    58:         return;

    59: }

    60: // end of rs232_initliazation()

    61: 

    62: //===================================================

    63: // send_byte_to_PC()

    64: //===================================================

    65: // send one character in char to PC through serial port

    66: void send_byte_to_PC(char letter)

    67: {

    68: // check transmit register status bit TRMT in register TXSTA

    69:         while(TRMT==0) {};      // wait for previous transfer to complete

    70:         TXREG   = letter;       // send letter to PC

    71:         return;

    72: } //end send_byte_to_PC()

    73: //===================================================

    74: // receive_byte_from_PC()

    75: //===================================================

    76: // receive one character from PC through serial port

    77: void receive_byte_from_PC(char *word)

    78: {

    79: // check RCIF in Register PIR1 to see if one character is received

    80:         while(RCIF==0) {};      // wait for receive completion

    81:         *word   = RCREG;        // get character from Register RCREG

    82:         RCIF    = 0;            // clear receive flag

    83: // echo receive character

    84:         send_byte_to_PC(*word);

    85: // check for receive error

    86:         return;

    87: } // end send_byte_to_PC()