跨届大侠
北京龙邱智能科技有限公司
- 积分
- 10332
- 威望
- 2905
- 贡献
- 6151
- 兑换币
- 4515
- 注册时间
- 2008-6-7
- 在线时间
- 638 小时
|
MC9S12DG128系列例程--SCI
/****************************************************
龙丘MC9S12(DG128)多功能开发板
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.1
最后更新:2008年11月20日
------------------------------------
Code Warrior 4.7
Target : MC9S12DG128
Crystal: 16.000Mhz
busclock: 8.000MHz
pllclock:16.000MHz
本程序主要包括以下功能:
1.设置锁相环和总线频率;
2.IO口使用;
3.SCI口使用:提供接收/发送字符、字符串、格式化字符串。
LED计数,根据灯亮可以读取系统循环了多少次
*****************************************************/
#i nclude <hidef.h>
#i nclude <mc9s12dg128.h>
#i nclude "LQprintp.h"
#i nclude <ctype.h>
#i nclude <string.h>
#i nclude <stdarg.h>
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define Baud_9600 55
#define Baud_19200 26
#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<num1; 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;
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);
}
//-----------------------------------------------------
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)
{
SCI0CR2=0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
SCI0BDH=0x00; //出口波特率为9600
SCI0BDL=0x68; //SCI0BDL=busclk/(16*SCI0BDL)
//busclk 8MHz, 9600bps,SCI0BDL=0x34
//busclk 16MHz, 9600bps,SCI0BDL=0x68
//busclk 24MHz, 9600bps,SCI0BDL=0x9C
} //busclk 32MHz, 9600bps,SCI0BDL=0xD0
//-----------------------------------------------------
void Dly_ms(int ms)
{
int ii,jj;
if (ms<1) ms=1;
for(ii=0;ii<ms;ii++)
for(jj=0;jj<2670;jj++); //busclk:16MHz--1ms
}
void main(void){
unsigned char LedCnt=0;
setbusclock();
SCI_Init();
DDRB=0xFF;
PUCR_PUPBE=1;
EnableInterrupts;
for(;;)
{
LedCnt=(LedCnt>0XFE?0:++LedCnt);
Dly_ms(1000); //修改延时以修改数据发送频率
//低电平灯亮用这句,注释掉下面那句
PORTB=~LedCnt;
//高电平灯亮用这句,注释掉上面那句
//PORTB=LedCnt;
putstr("\n http:/ /shop36265907.taobao.com");
printp("\n Minute elapsed: %03ds",LedCnt);
}
}
下面是完整工程,需要的请点击下载:demoSCI.rar
看帖不回是不道德的! |
|