MC9S12XS128 串口操作例程.doc_第1页
MC9S12XS128 串口操作例程.doc_第2页
MC9S12XS128 串口操作例程.doc_第3页
MC9S12XS128 串口操作例程.doc_第4页
MC9S12XS128 串口操作例程.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

MC9S12XS128 串口操作例程Code Warrior 4.7Target : MC9S12XS128Crystal: 16.000Mhzbusclock: 8.000MHzpllclock:16.000MHz本程序主要包括以下功能:1.设置锁相环和总线频率;2.IO口使用;3.共四路ATD使用及显示方法。LED计数,根据灯亮可以读取系统循环了多少次*/#include /* common defines and macros */#include /* derivative information */#include #include #include #pragma LINK_INFO DERIVATIVE mc9s12xs128#pragma CODE_SEG DEFAULT#define CR_as_CRLF TRUE / if true , you can use n to act as CR/LF, / if false, you have to use nr,but can get a higher speedstatic int do_padding;static int left_flag;static int len;static int num1;static int num2;static char pad_character;unsigned 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_putchar(unsigned char ch) if (ch = n) while(!(SCI0SR1&0x80) ; SCI0DRL= 0x0d; /outputCR return; while(!(SCI0SR1&0x80) ; /keep waiting when not empty SCI0DRL=ch;void putstr(char ch) unsigned char ptr=0; while(chptr) uart_putchar(unsigned char)chptr+); static void padding( const int l_flag) int i; if (do_padding & l_flag & (len num1) for (i=len; i 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);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);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); #pragma CODE_SEG _NEAR_SEG NON_BANKEDvoid interrupt 20 SCI0_ISR(void) SCI0CR2_RIE=0; /此处为串口中断需要处理的事情 uart_putchar(uart_getkey(); PORTA_PA0=PORTA_PA0; SCI0CR2_RIE = 1;#pragma CODE_SEG DEFAULT/-void setbusclock(void) CLKSEL=0X00;/disengage PLL to system PLLCTL_PLLON=1;/turn on PLL SYNR=1; REFDV=1; /pllclock=2*osc*(1+SYNR)/(1+REFDV)=32MHz; _asm(nop); /BUS CLOCK=16M _asm(nop); while(!(CRGFLG_LOCK=1); /when pll is steady ,then use it; CLKSEL_PLLSEL =1; /engage PLL to system;/-static void SCI_Init(void) SCI0CR1 =0x00; SCI0CR2 =0x2c; /enable Receive Full Interrupt,RX enable,Tx enable SCI0BD =0x68; /SCI0BDL=busclk/(16*SCI0BDL) /busclk 8MHz, 9600bps,SCI0BD=0x34 /busclk 16MHz, 9600bps,SCI0BD=0x68 /busclk 24MHz, 9600bps,SCI0BD=0x9C /busclk 32MHz, 9600bps,SCI0BD=0xD0 /busclk 40MHz, 9600bps,SCI0BD=0x106/-void Dly_ms(int ms) int ii,jj; if (ms1) ms=1; for(ii=0;iims;ii

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论