📄 +Ҥ
字号:
/********************************************************龙丘MC9S12X多功能开发板V1.0 Designed by 龙丘E-mail:chiusir@163.com 软件版本:V1.1最后更新:2009年2月21日 相关信息参考下列地址:博客: http://longqiu.21ic.org淘宝店:http://shop36265907.taobao.com------------------------------------Code Warrior 4.7Target : MC9S12XS128Crystal: 16.000Mhzbusclock:16.000MHzpllclock:32.000MHz *********************************************************/#include <hidef.h> /* common defines and macros */#include "derivative.h" /* derivative-specific definitions */#include <ctype.h>#include <string.h>#include <stdarg.h>#ifndef TRUE#define TRUE 1#endif#ifndef FALSE#define FALSE 0#endif #define CR_as_CRLF TRUE // if true , you can use "\n" to act as CR/LF, // if false , you have to use "\n\r",but can get a higher speedunsigned char uart_getkey(void) //接收串口数据函数{ while(!(SCI0SR1&0x80)) ; //keep waiting when not empty return SCI0DRL;}/*void uart_init(void) { SCI0CR2=0x0c; SCI0BDH=0x00;//16MHz,19200bps,SCI0BDL=0x1a SCI0BDL=0x34;//16MHz,9600bps,SCI0BDL=0x34 } */void uart_init(void) //串口初始化{ //SCI0改为SCI1即为第二个串口 SCI0CR1 = 0x00; //设置SCI0为正常模式,八位数据位,无奇偶校验 SCI0CR2_TE = 1; //允许发送 SCI0CR2_RE =1; //允许接受//SCI0CR2_TIE=1; //允许发送中断SCI0CR2_RIE=1; //允许接受中断/**************************** SCI0CR2寄存器说明 7 6 5 4 3 2 1 0 TIE TCIE RIE ILIE TE RE RWU SBK Reset 0 0 0 0 0 0 0 0*/ SCI0BDH=0x00; SCI0BDL=0x45; // SCI0BD=BUS_CLOCK/(BAUD*16) 波特率公式 // 64000000/(57600*16) }//*********************//发送一个字符函数//*********************void uart_putchar(unsigned char ch){ if (ch == '\n') { while(!(SCI0SR1&0x80)) ; SCI0DRL= 0x0d; //output'CR' return; } while(!(SCI0SR1&0x80)) ; //keep waiting when not empty SCI0DRL=ch;}//*********************//发送一个字符串函数//*********************void putstr(char ch[]){ unsigned char ptr=0; while(ch[ptr]) { uart_putchar((unsigned char)ch[ptr++]); } }static int do_padding;static int left_flag;static int len;static int num1;static int num2;static char pad_character;/*---------------------------------------------------*//* *//* This routine puts pad characters into the output *//* buffer. *//* */static void padding( const int l_flag){ int i; if (do_padding && l_flag && (len < num1)) for (i=len; i<num1; i++) uart_putchar( pad_character); }/*---------------------------------------------------*//* *//* This routine moves a string to the output buffer *//* as directed by the padding and positioning flags. *//* */static void outs( char* lp){ /* pad on left if needed */ len = strlen( lp); padding( !left_flag); /* Move string to the buffer */ while (*lp && num2--) uart_putchar( *lp++); /* Pad on right if needed */ len = strlen( lp); padding( left_flag); }/*---------------------------------------------------*//* *//* This routine moves a number to the output buffer *//* as directed by the padding and positioning flags. *//* */ static void reoutnum(unsigned long num, unsigned int negative, const long base ) { char* cp; char outbuf[32]; const char digits[] = "0123456789ABCDEF"; /* Build number (backwards) in outbuf */ cp = outbuf; do { *cp++ = digits[(int)(num % base)]; } while ((num /= base) > 0); if (negative) *cp++ = '-'; *cp-- = 0; /* Move the converted number to the buffer and */ /* add in the padding where needed. */ len = strlen(outbuf); padding( !left_flag); while (cp >= outbuf) uart_putchar( *cp--); padding( left_flag);}static void outnum(long num, const long base ,unsigned char sign)//1, signed 0 unsigned{ unsigned int negative; if ( (num < 0L) && sign ) { negative=1; num = -num; } else negative=0; reoutnum(num,negative,base); } /*---------------------------------------------------*//* *//* This routine gets a number from the format *//* string. *//* */static int getnum( char** linep){ int n; char* cp; n = 0; cp = *linep; while (isdigit(*cp)) n = n*10 + ((*cp++) - '0'); *linep = cp; return(n);}/*---------------------------------------------------*//* *//* This routine operates just like a printf/sprintf *//* routine. It outputs a set of data under the *//* control of a formatting string. Not all of the *//* standard C format control are supported. The ones *//* provided are primarily those needed for embedded *//* systems work. Primarily the floaing point *//* routines are omitted. Other formats could be *//* added easily by following the examples shown for *//* the supported formats. *///和电脑上的printf函数很类似/* 使用例子如 printp("\n voltage: %d.%d",u16_zheng,u16_xiao); */void printp( char* ctrl, ...){ int long_flag; int dot_flag; char ch; va_list argp; va_start( argp, ctrl); for ( ; *ctrl; ctrl++) { /* move format string chars to buffer until a */ /* format control is found. */ if (*ctrl != '%') { uart_putchar(*ctrl);#if CR_as_CRLF==TRUE if(*ctrl=='\n') uart_putchar('\r');#endif continue; } /* initialize all the flags for this format. */ dot_flag = long_flag = left_flag = do_padding = 0; pad_character = ' '; num2=32767;try_next: ch = *(++ctrl); if (isdigit(ch)) { if (dot_flag) num2 = getnum(&ctrl); else { if (ch == '0') pad_character = '0'; num1 = getnum(&ctrl); do_padding = 1; } ctrl--; goto try_next; } switch (tolower(ch)) { case '%': uart_putchar( '%'); continue; case '-': left_flag = 1; break; case '.': dot_flag = 1; break; case 'l': long_flag = 1; break; case 'd': if (long_flag ==1 ) { if(ch == 'D') {outnum( va_arg(argp, unsigned long), 10L , 0);continue;} else /* ch == 'd' */ {outnum( va_arg(argp, long), 10L,1);continue;} } else { if(ch == 'D') {outnum( va_arg(argp, unsigned int),10L,0);continue;} else /* ch == 'd' */ {outnum( va_arg(argp, int), 10L,1);continue;} } case 'x': // X 无符号 , x 有符号 if (long_flag ==1 ) { if(ch == 'X') {outnum( va_arg(argp, unsigned long), 16L,0);continue;} else /* ch == 'x' */ {outnum( va_arg(argp, long), 16L,1);continue;} } else { if(ch == 'X') {outnum( va_arg(argp, unsigned int), 16L,0);continue;} else /* ch == 'x' */ {outnum( va_arg(argp, int), 16L,1);continue;} } //如果按照16进制打印,将全部按照无符号数进行 continue; case 's': outs( va_arg( argp, char*)); continue; case 'c': uart_putchar( va_arg( argp, int)); continue; default: continue; } goto try_next; } va_end( argp); } //******************************************* //串口接收中断服务函数如果需要则将下面一段复制到 *.prm文件的最后 //VECTOR ADDRESS 0xFFD6 SCI0_ISR //串口接收中断入口 //然后设置串口初始化中的 接收允许和接收中断允许 #pragma CODE_SEG __NEAR_SEG NON_BANKED void interrupt SCI0_ISR(void){ SCI0CR2_RIE=0; //此处为串口中断需要处理的事情 uart_putchar(uart_getkey()); putstr("sksdjd;ssjs\n") ; SCI0CR2_RIE = 1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -