TheLegace

sci0.c

Mar 28th, 2012
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.83 KB | None | 0 0
  1. // filename  ***************  sci0.c ******************************
  2. // Simple I/O routines to 9S12C32 serial port  
  3. // Jonathan W. Valvano 1/29/04
  4.  
  5. //  This example accompanies the books
  6. //   "Embedded Microcomputer Systems: Real Time Interfacing",
  7. //         Brooks-Cole, copyright (c) 2000,
  8. //   "Introduction to Embedded Microcomputer Systems:
  9. //    Motorola 6811 and 6812 Simulation", Brooks-Cole, copyright (c) 2002
  10.  
  11. // Copyright 2004 by Jonathan W. Valvano, [email protected]
  12. //    You may use, edit, run or distribute this file
  13. //    as long as the above copyright notice remains
  14. // Modified by EE345L students Charlie Gough && Matt Hawk
  15. // Modified by EE345M students Agustinus Darmawan + Mingjie Qiu
  16. //
  17. // adapted to the Dragon12 board using SCI0            --  fw-07-04
  18. // allows for 24 MHz bus (PLL) and 4 MHz bus (no PLL)  -- fw-07-04
  19.  
  20. #include <mc9s12dp256.h>        /* derivative information */
  21. #include "sci0.h"
  22. //#include "pll.h"                /* macro _SYSCLOCK */
  23.  
  24.  
  25. #define RDRF 0x20   // Receive Data Register Full Bit
  26. #define TDRE 0x80   // Transmit Data Register Empty Bit
  27.  
  28.  
  29. //-------------------------SCI0_Init------------------------
  30. // Initialize Serial port SCI0
  31. // Input: baudRate is the baud rate in bits/sec
  32. // Output: none
  33. void SCI0_Init(unsigned short baudRate) {
  34.  
  35.   /* check if bus frequency has been boosted to 24 MHz (fw-07-04) */
  36. //  #if _BUSCLOCK == 24
  37.  
  38.   /* 24 MHz bus frequency (PLL is used, SYNR = 2, REFDV = 0 -> factor 6)
  39.      Baud rate generator: SCI0BDL/H = (24e6/16)/baudrate = 1.5e6/baudrate */
  40.   switch(baudRate){
  41.     case BAUD_300:
  42.       SCI0BDH=19;
  43.       SCI0BDL=136;
  44.       break;
  45.     case BAUD_600:
  46.       SCI0BDH=9;
  47.       SCI0BDL=196;
  48.       break;
  49.     case BAUD_1200:
  50.       SCI0BDH=4;
  51.       SCI0BDL=226;
  52.       break;
  53.     case BAUD_2400:
  54.       SCI0BDH=2;
  55.       SCI0BDL=113;
  56.       break;
  57.     case BAUD_4800:
  58.       SCI0BDH=1;
  59.       SCI0BDL=56;
  60.       break;
  61.     case BAUD_9600:
  62.       SCI0BDH=0;
  63.       SCI0BDL=156;
  64.       break;
  65.     case BAUD_19200:
  66.       SCI0BDH=0;
  67.       SCI0BDL=78;
  68.       break;
  69.     case BAUD_38400:
  70.       SCI0BDH=0;
  71.       SCI0BDL=39;
  72.       break;
  73.     case BAUD_57600:
  74.       SCI0BDH=0;
  75.       SCI0BDL=26;
  76.       break;
  77.     case BAUD_115200:
  78.       SCI0BDH=0;
  79.       SCI0BDL=13;
  80.       break;
  81.   }
  82.  
  83. //  #else
  84.  
  85.   /* 4 MHz bus frequency (PLL not used, SYNR = REFDV = 0 -> factor 2)
  86.      Baud rate generator: SCI0BDL/H = (4e6/16)/baudrate = 250000/baudrate */
  87.   switch(baudRate){
  88.     case BAUD_300:
  89.       SCI0BDH=3;
  90.       SCI0BDL=64;
  91.       break;
  92.     case BAUD_600:
  93.       SCI0BDH=1;
  94.       SCI0BDL=160;
  95.       break;
  96.     case BAUD_1200:
  97.       SCI0BDH=0;
  98.       SCI0BDL=208;
  99.       break;
  100.     case BAUD_2400:
  101.       SCI0BDH=0;
  102.       SCI0BDL=104;
  103.       break;
  104.     case BAUD_4800:
  105.       SCI0BDH=0;
  106.       SCI0BDL=52;
  107.       break;
  108.     case BAUD_9600:
  109.       SCI0BDH=0;
  110.       SCI0BDL=26;
  111.       break;
  112.     case BAUD_19200:
  113.       SCI0BDH=0;
  114.       SCI0BDL=13;
  115.       break;
  116.   }
  117.  
  118. //  #endif /* _BUSCLOCK */
  119.  
  120.    
  121.   SCI0CR1 = 0;
  122. /* bit value meaning
  123.     7   0    LOOPS, no looping, normal
  124.     6   0    WOMS, normal high/low outputs
  125.     5   0    RSRC, not appliable with LOOPS=0
  126.     4   0    M, 1 start, 8 data, 1 stop
  127.     3   0    WAKE, wake by idle (not applicable)
  128.     2   0    ILT, short idle time (not applicable)
  129.     1   0    PE, no parity
  130.     0   0    PT, parity type (not applicable with PE=0) */
  131.   SCI0CR2 = 0x0C;
  132. /* bit value meaning
  133.     7   0    TIE, no transmit interrupts on TDRE
  134.     6   0    TCIE, no transmit interrupts on TC
  135.     5   0    RIE, no receive interrupts on RDRF
  136.     4   0    ILIE, no interrupts on idle
  137.     3   1    TE, enable transmitter
  138.     2   1    RE, enable receiver
  139.     1   0    RWU, no receiver wakeup
  140.     0   0    SBK, no send break */
  141.  
  142. }    
  143. //-------------------------SCI0_InChar------------------------
  144. // Wait for new serial port input, busy-waiting synchronization
  145. // Input: none
  146. // Output: ASCII code for key typed
  147. char SCI0_InChar(void) {
  148.  
  149.   while((SCI0SR1 & RDRF) == 0){};
  150.   return(SCI0DRL);
  151.  
  152. }
  153.        
  154. //-------------------------SCI0_OutChar------------------------
  155. // Wait for buffer to be empty, output 8-bit to serial port
  156. // busy-waiting synchronization
  157. // Input: 8-bit data to be transferred
  158. // Output: none
  159. void SCI0_OutChar(char data) {
  160.  
  161.   while((SCI0SR1 & TDRE) == 0){};
  162.   SCI0DRL = data;
  163.  
  164. }
  165.  
  166.    
  167. //-------------------------SCI0_InStatus--------------------------
  168. // Checks if new input is ready, TRUE if new input is ready
  169. // Input: none
  170. // Output: TRUE if a call to InChar will return right away with data
  171. //         FALSE if a call to InChar will wait for input
  172.  
  173. char SCI0_InStatus(void) {
  174.  
  175.   return(SCI0SR1 & RDRF);
  176.  
  177. }
  178.  
  179. //-----------------------SCI0_OutStatus----------------------------
  180. // Checks if output data buffer is empty, TRUE if empty
  181. // Input: none
  182. // Output: TRUE if a call to OutChar will output and return right away
  183. //         FALSE if a call to OutChar will wait for output to be ready
  184. char SCI0_OutStatus(void) {
  185.  
  186.   return(SCI0SR1 & TDRE);
  187.  
  188. }
  189.  
  190.  
  191. //-------------------------SCI0_OutString------------------------
  192. // Output String (NULL termination), busy-waiting synchronization
  193. // Input: pointer to a NULL-terminated string to be transferred
  194. // Output: none
  195. void SCI0_OutString(char *pt) {
  196.  
  197.   while(*pt) {
  198.  
  199.     SCI0_OutChar(*pt);
  200.     pt++;
  201.    
  202.   }
  203.  
  204. }
  205.  
  206. //----------------------SCI0_InUDec-------------------------------
  207. // InUDec accepts ASCII input in unsigned decimal format
  208. //     and converts to a 16 bit unsigned number
  209. //     valid range is 0 to 65535
  210. // Input: none
  211. // Output: 16-bit unsigned number
  212. // If you enter a number above 65535, it will truncate without an error
  213. // Backspace will remove last digit typed
  214. unsigned short SCI0_InUDec(void) { 
  215.  
  216. unsigned short number=0, length=0;
  217. char character;
  218.  
  219.   character = SCI0_InChar();   
  220.  
  221.   while(character!=CR) {
  222.  
  223.     // accepts until carriage return input
  224.     // The next line checks that the input is a digit, 0-9.
  225.     // If the character is not 0-9, it is ignored and not echoed
  226.     if((character>='0') && (character<='9')) {
  227.       number = 10*number+(character-'0');   // this line overflows if above 65535
  228.       length++;
  229.       SCI0_OutChar(character);
  230.     }
  231.    
  232. // If the input is a backspace, then the return number is
  233. // changed and a backspace is outputted to the screen
  234.     else if((character==BS) && length) {
  235.    
  236.       number /= 10;
  237.       length--;
  238.       SCI0_OutChar(character);
  239.      
  240.     }
  241.    
  242.     character = SCI0_InChar(); 
  243.    
  244.   }
  245.  
  246.   return number;
  247.  
  248. }
  249.  
  250.  
  251.  
  252. //-----------------------SCI0_OutUDec-----------------------
  253. // Output a 16-bit number in unsigned decimal format
  254. // Input: 16-bit number to be transferred
  255. // Output: none
  256. // Variable format 1-5 digits with no space before or after
  257. void SCI0_OutUDec(unsigned short n){
  258. // This function uses recursion to convert decimal number
  259. //   of unspecified length as an ASCII string
  260.   if(n >= 10){
  261.     SCI0_OutUDec(n/10);
  262.     n = n%10;
  263.   }
  264.   SCI0_OutChar(n+'0'); /* n is between 0 and 9 */
  265. }
  266.  
  267.  
  268.  
  269. //---------------------SCI0_InUHex----------------------------------------
  270. // Accepts ASCII input in unsigned hexadecimal (base 16) format
  271. // Input: none
  272. // Output: 16-bit unsigned number
  273. // No '$' or '0x' need be entered, just the 1 to 4 hex digits
  274. // It will convert lower case a-f to uppercase A-F
  275. //     and converts to a 16 bit unsigned number
  276. //     value range is 0 to FFFF
  277. // If you enter a number above FFFF, it will truncate without an error
  278. // Backspace will remove last digit typed
  279. unsigned short SCI0_InUHex(void){  
  280. unsigned short number=0, digit, length=0;
  281. char character;
  282.   character = SCI0_InChar();
  283.   while(character!=CR){
  284.     digit = 0x10; // assume bad
  285.     if((character>='0') && (character<='9')){
  286.       digit = character-'0';
  287.     }
  288.     else if((character>='A') && (character<='F')){
  289.       digit = (character-'A')+0xA;
  290.     }
  291.     else if((character>='a') && (character<='f')){
  292.       digit = (character-'a')+0xA;
  293.     }
  294. // If the character is not 0-9 or A-F, it is ignored and not echoed
  295.     if(digit<=0xF ){   
  296.       number = number*0x10+digit;
  297.       length++;
  298.       SCI0_OutChar(character);
  299.     }
  300. // Backspace outputted and return value changed if a backspace is inputted
  301.     else if(character==BS && length){
  302.       number /=0x10;
  303.       length--;
  304.       SCI0_OutChar(character);
  305.     }
  306.     character = SCI0_InChar();
  307.   }
  308.   return number;
  309. }
  310.  
  311. //--------------------------SCI0_OutUHex----------------------------
  312. // Output a 16 bit number in unsigned hexadecimal format
  313. // Input: 16-bit number to be transferred
  314. // Output: none
  315. // Variable format 1 to 4 digits with no space before or after
  316. void SCI0_OutUHex(unsigned short number){
  317. // This function uses recursion to convert the number of
  318. //   unspecified length as an ASCII string
  319.   if(number>=0x10)  {
  320.     SCI0_OutUHex(number/0x10);
  321.     SCI0_OutUHex(number%0x10);
  322.   }
  323.   else if(number<0xA){
  324.     SCI0_OutChar(number+'0');
  325.   }
  326.   else{
  327.     SCI0_OutChar((number-0x0A)+'A');
  328.   }
  329. }
  330.  
  331. //------------------------SCI0_InString------------------------
  332. // This function accepts ASCII characters from the serial port
  333. //    and adds them to a string until a carriage return is inputted
  334. //    or until max length of the string is reached.  
  335. // It echoes each character as it is inputted.  
  336. // If a backspace is inputted, the string is modified
  337. //    and the backspace is echoed
  338. // InString terminates the string with a null character
  339. // -- Modified by Agustinus Darmawan + Mingjie Qiu --
  340. void SCI0_InString(char *string, unsigned short max) { 
  341. int length=0;
  342. char character;
  343.   character = SCI0_InChar();
  344.   while(character!=CR){
  345.     if(character==BS){
  346.       if(length){
  347.         string--;
  348.         length--;
  349.         SCI0_OutChar(BS);
  350.       }
  351.     }
  352.     else if(length<max){
  353.       *string++=character;
  354.       length++;
  355.       SCI0_OutChar(character);
  356.     }
  357.     character = SCI0_InChar();
  358.   }
  359.   *string = 0;
  360. }
Advertisement
Add Comment
Please, Sign In to add comment