/* Lab 06 code skeleton file: lab_6_skeleton.c author: Justin Ray Revision History: 9-21-2009 - Updated to include modclock.c/h and lab renumbering. This file contains a starting point for lab 6. make the following connections on the board for this lab: connect SW1_1 to PAD04 connect SW1_2 to PAD05 connect SW1_3 to PAD06 connect SW1_4 to PAD07 connect PB1 to PAD00 The PAD4:7 bits represent the index for the baud rate lookup The PAD0 bit represent the push button (for activating communication). */ #include /* common defines and macros */ #include /* derivative information */ #pragma LINK_INFO DERIVATIVE "mc9s12c128" //user includes //TODO include lcd_lib //TODO include modclock //defines #define RESPONSE_MAX_LEN 16 #define QUERY_MAX_LEN 16 #define POLY 0xEA //function prototypes unsigned char CRC(char * string, unsigned char length, unsigned char polynomial) ; void doSerialComm( void ); void serialSetup(char baudIndex); //globals //TODO modify groupString with your group number (e.g. "1A"). char groupString[] = "EX"; char msgBuf[17]; void main(void) { //char oldPTAD; EnableInterrupts; //set up I/O //TODO: set up port A as output //TODO: set up port AD is input //call clock setup to set module bus clock to 8 MHz clockSetup(); //call serial setup to configure control registers serialSetup(/* TODO pass the port index*/); //call to set up lcd for output lcdSetup(); //call to initiate serial communciation doSerialComm(); for(;;) { } /* wait forever */ /* please make sure that you never leave this function */ } /* This function initilizes the SCI control registers. Baud rate is determined by looking up the baudIndex parameter in a switch statement. */ void serialSetup(char baudIndex) { //set up serial comm //TODO: Use a switch statement to select the baud rate based on the baudIndex. // The case values should correspond to the table below: //case 0 - 300 baud //case 1 - 600 baud //case 2 - 1200 baud //case 3 - 2400 baud //case 4 - 4800 baud //case 5 - 9600 baud //case 6 - 14400 baud //case 7 - 19200 baud //case 8 - 38400 baud //case 9 - 56000 baud //case 10 - 115200 baud //default - 2400 buad //TODO configure SCICR1 and SCICR2 for 8 bit, one stop bit, no parity } void doSerialComm( void ) { //TODO: Declare variables as needed char query[QUERY_MAX_LEN+1]; //string to hold query value; char response[RESPONSE_MAX_LEN+1]; //string to hold response value char i; //counter char crcRx; //received CRC value char crcComp; //computed CRC value int errFlag; //to hold return values /***********TRANSMISSION PREP PORTION**************/ //compute CRC of groupString crcComp = CRC(groupString,2,POLY); //TODO: display group string, the CRC, and the current baud rate on the LCD: //hint, use sprintf with "%s 0x%02X BR=%d" as your control string //wait for button press //TODO wait for the user to press PB1 to initiate transmission /****************TRANSMISSION PORTION *************/ //TODO: Construct the query by concatentating groupString:crcComp:NULL // Note: NULL denoted a null byte, not the string "NULL" //TODO: Transmit each byte of the query, including the NULL terminator // hint: After sending each byte, you should monitor the status // register to see when the transmitter is ready for the next // value. /*************RECEPTION PORTION*****************/ //TODO: Receive bytes and copy them into 'response' string until // the null character is received or the RESPONSE_MAX_LEN // is reached. //Hint: You must monitor the status register to see when a value // has been received into the data register. //TODO: CRC check // Extract the CRC from the received data and replace the CRC with a NULL byte. // Compute the CRC of the received message. // Compare the received and computed CRC values and display an error if they don't match //TODO Format the response and the crc and display the values on the LCD //Hint: Use sprintf with the control string "%s 0x%x". } /* CRC - this function computes the CRC of the first bytes at the location using for a polynomial. The return value is the computed CRC value (FCS). */ unsigned char CRC(char * string, unsigned char length, unsigned char polynomial) { unsigned char crc; unsigned char i,j; crc = 0; for (i=0;i