Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // filename *************** sci0.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, [email protected]
- // 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
- //
- // adapted to the Dragon12 board using SCI0 -- fw-07-04
- // allows for 24 MHz bus (PLL) and 4 MHz bus (no PLL) -- fw-07-04
- #include <mc9s12dp256.h> /* derivative information */
- #include "sci0.h"
- //#include "pll.h" /* macro _SYSCLOCK */
- #define RDRF 0x20 // Receive Data Register Full Bit
- #define TDRE 0x80 // Transmit Data Register Empty Bit
- //-------------------------SCI0_Init------------------------
- // Initialize Serial port SCI0
- // Input: baudRate is the baud rate in bits/sec
- // Output: none
- void SCI0_Init(unsigned short baudRate) {
- /* check if bus frequency has been boosted to 24 MHz (fw-07-04) */
- // #if _BUSCLOCK == 24
- /* 24 MHz bus frequency (PLL is used, SYNR = 2, REFDV = 0 -> factor 6)
- Baud rate generator: SCI0BDL/H = (24e6/16)/baudrate = 1.5e6/baudrate */
- switch(baudRate){
- case BAUD_300:
- SCI0BDH=19;
- SCI0BDL=136;
- break;
- case BAUD_600:
- SCI0BDH=9;
- SCI0BDL=196;
- break;
- case BAUD_1200:
- SCI0BDH=4;
- SCI0BDL=226;
- break;
- case BAUD_2400:
- SCI0BDH=2;
- SCI0BDL=113;
- break;
- case BAUD_4800:
- SCI0BDH=1;
- SCI0BDL=56;
- break;
- case BAUD_9600:
- SCI0BDH=0;
- SCI0BDL=156;
- break;
- case BAUD_19200:
- SCI0BDH=0;
- SCI0BDL=78;
- break;
- case BAUD_38400:
- SCI0BDH=0;
- SCI0BDL=39;
- break;
- case BAUD_57600:
- SCI0BDH=0;
- SCI0BDL=26;
- break;
- case BAUD_115200:
- SCI0BDH=0;
- SCI0BDL=13;
- break;
- }
- // #else
- /* 4 MHz bus frequency (PLL not used, SYNR = REFDV = 0 -> factor 2)
- Baud rate generator: SCI0BDL/H = (4e6/16)/baudrate = 250000/baudrate */
- switch(baudRate){
- case BAUD_300:
- SCI0BDH=3;
- SCI0BDL=64;
- break;
- case BAUD_600:
- SCI0BDH=1;
- SCI0BDL=160;
- break;
- case BAUD_1200:
- SCI0BDH=0;
- SCI0BDL=208;
- break;
- case BAUD_2400:
- SCI0BDH=0;
- SCI0BDL=104;
- break;
- case BAUD_4800:
- SCI0BDH=0;
- SCI0BDL=52;
- break;
- case BAUD_9600:
- SCI0BDH=0;
- SCI0BDL=26;
- break;
- case BAUD_19200:
- SCI0BDH=0;
- SCI0BDL=13;
- break;
- }
- // #endif /* _BUSCLOCK */
- 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 */
- }
- //-------------------------SCI0_InChar------------------------
- // Wait for new serial port input, busy-waiting synchronization
- // Input: none
- // Output: ASCII code for key typed
- char SCI0_InChar(void) {
- while((SCI0SR1 & RDRF) == 0){};
- return(SCI0DRL);
- }
- //-------------------------SCI0_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 SCI0_OutChar(char data) {
- while((SCI0SR1 & TDRE) == 0){};
- SCI0DRL = data;
- }
- //-------------------------SCI0_InStatus--------------------------
- // Checks if new input is ready, TRUE if new input is ready
- // Input: none
- // Output: TRUE if a call to InChar will return right away with data
- // FALSE if a call to InChar will wait for input
- char SCI0_InStatus(void) {
- return(SCI0SR1 & RDRF);
- }
- //-----------------------SCI0_OutStatus----------------------------
- // Checks if output data buffer is empty, TRUE if empty
- // Input: none
- // 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 SCI0_OutStatus(void) {
- return(SCI0SR1 & TDRE);
- }
- //-------------------------SCI0_OutString------------------------
- // Output String (NULL termination), busy-waiting synchronization
- // Input: pointer to a NULL-terminated string to be transferred
- // Output: none
- void SCI0_OutString(char *pt) {
- while(*pt) {
- SCI0_OutChar(*pt);
- pt++;
- }
- }
- //----------------------SCI0_InUDec-------------------------------
- // InUDec accepts ASCII input in unsigned decimal format
- // and converts to a 16 bit unsigned number
- // valid range is 0 to 65535
- // Input: none
- // 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 SCI0_InUDec(void) {
- unsigned short number=0, length=0;
- char character;
- character = SCI0_InChar();
- 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++;
- SCI0_OutChar(character);
- }
- // 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--;
- SCI0_OutChar(character);
- }
- character = SCI0_InChar();
- }
- return number;
- }
- //-----------------------SCI0_OutUDec-----------------------
- // Output a 16-bit number in unsigned decimal format
- // Input: 16-bit number to be transferred
- // Output: none
- // Variable format 1-5 digits with no space before or after
- void SCI0_OutUDec(unsigned short n){
- // This function uses recursion to convert decimal number
- // of unspecified length as an ASCII string
- if(n >= 10){
- SCI0_OutUDec(n/10);
- n = n%10;
- }
- SCI0_OutChar(n+'0'); /* n is between 0 and 9 */
- }
- //---------------------SCI0_InUHex----------------------------------------
- // Accepts ASCII input in unsigned hexadecimal (base 16) format
- // Input: none
- // 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 SCI0_InUHex(void){
- unsigned short number=0, digit, length=0;
- char character;
- character = SCI0_InChar();
- 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++;
- SCI0_OutChar(character);
- }
- // Backspace outputted and return value changed if a backspace is inputted
- else if(character==BS && length){
- number /=0x10;
- length--;
- SCI0_OutChar(character);
- }
- character = SCI0_InChar();
- }
- return number;
- }
- //--------------------------SCI0_OutUHex----------------------------
- // Output a 16 bit number in unsigned hexadecimal format
- // Input: 16-bit number to be transferred
- // Output: none
- // Variable format 1 to 4 digits with no space before or after
- void SCI0_OutUHex(unsigned short number){
- // This function uses recursion to convert the number of
- // unspecified length as an ASCII string
- if(number>=0x10) {
- SCI0_OutUHex(number/0x10);
- SCI0_OutUHex(number%0x10);
- }
- else if(number<0xA){
- SCI0_OutChar(number+'0');
- }
- else{
- SCI0_OutChar((number-0x0A)+'A');
- }
- }
- //------------------------SCI0_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 SCI0_InString(char *string, unsigned short max) {
- int length=0;
- char character;
- character = SCI0_InChar();
- while(character!=CR){
- if(character==BS){
- if(length){
- string--;
- length--;
- SCI0_OutChar(BS);
- }
- }
- else if(length<max){
- *string++=character;
- length++;
- SCI0_OutChar(character);
- }
- character = SCI0_InChar();
- }
- *string = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment