MC9S12XS128 串口操作例程
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
MC9S12XS128 串口操作例程
Code Warrior 4.7
Target : MC9S12XS128
Crystal: 16.000Mhz
busclock: 8.000MHz
pllclock:16.000MHz
本程序主要包括以下功能:
1.设置锁相环和总线频率;
2.IO口使用;
3.共四路ATD使用及显示方法。
LED计数,根据灯亮可以读取系统循环了多少次
************************************************************** ***************************/
#include
#include
#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 "\n\r",but can get a higher speed
static 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; //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 void padding( const int l_flag)
{
int i;
if (do_padding && l_flag && (len < num1))
for (i=len; i uart_putchar( pad_character); } 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); } 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); } static int getnum( char** linep) { int n; char* cp;