//Project : SCISubroutines.c //Version : 1.01 //Date : 12/24/2002, modified 5/23/2005 //Author : JianJian Song w/ modification by Curtis Rhodes //Company : rose-hulman institute of technolog //Purpose : RS232 serial communicaiton routines //Input: serial port from PC //Outputs: serial output to PC // Serial port // RC6 (pin 25) == TX pin to send data to PC // RC7 (pin 26) == RX pin to receive data from PC // //Chip type : PIC16F877A //Clock frequency : 20 MHz #include <pic.h> #include "memoryProgrammer.h" //=================================================== // display_string() //=================================================== void display_string(const char *string, char length) // send a string to serial port. length is string length { char i; for(i=0;i<length;i++) send_byte_to_PC(string[i]); send_byte_to_PC(0x0A); // send line feed send_byte_to_PC(0x0D); // send carriage return return; } // end of display_string() //=================================================== // rs232_initliazation(baud, crystal) //=================================================== // baud -- baud rate in # of characters per second // crystal -- crystal speed in MHz void rs232_initliazation(unsigned int baud, unsigned int crystal) { unsigned int x; // temp variable // transmit configuration: 8 bits, transmit enable, asynchronous mode, high speeed //Register TXSTA = 0B10100100; // 0B is binary data format TX9 = 0; // 8-BIT MODE TXEN = 1; // transmit enabled SYNC = 0; // asynchronous mode BRGH = 0; // low speed // receive configuration: serial port enable, 8 bits, continuous receive // Register TXSTA = 0B10010000; SPEN = 1; // serial port enabled RX9 = 0; // 8-bit mode CREN = 1; // continuous receive enabled // baud rate configuration: Baud Rate = Fosc/(16(X+1)) when BRGH=1 for high speed // where X is the value in SPBRG x = 100000/baud; x = x*crystal; // crystal speed in MHz x = (x - 64)/64; SPBRG = x; // initialize baud rate generator register return; } // end of rs232_initliazation() //=================================================== // send_byte_to_PC() //=================================================== // send one character in char to PC through serial port void send_byte_to_PC(char letter) { // check transmit register status bit TRMT in register TXSTA while(TRMT==0) {}; // wait for previous transfer to complete TXREG = letter; // send letter to PC return; } //end send_byte_to_PC() //=================================================== // receive_byte_from_PC() //=================================================== // receive one character from PC through serial port void receive_byte_from_PC(char *word) { // check RCIF in Register PIR1 to see if one character is received while(RCIF==0) {}; // wait for receive completion *word = RCREG; // get character from Register RCREG RCIF = 0; // clear receive flag // echo receive character send_byte_to_PC(*word); // check for receive error return; } // end send_byte_to_PC()