登录站点

用户名

密码

MC9S08AC串口例程

2已有 977 次阅读  2009-10-27 14:22   标签龙丘智能科技中心  例程  串口 
/******************************************************** 
龙丘MC9S08AC单片机开发板 V2.0
编写:龙丘
Designed by Chiu Sir
E-mail:chiusir@163.com
软件版本:V1.0
最后更新:2009年10月26日
相关信息参考下列地址:
网站:  http://www.lqist.cn
淘宝店:http://shop36265907.taobao.com
使用说明:
    串口1演示程序;
    9600,n,8,1
       
 
**********************************************************/              
/*****************头文件****************************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "LQSCI.h"

/*********************变量和函数定义****************************/
#define KEY_OK   PTGD_PTGD0
#define KEY_ESC  PTGD_PTGD1
#define KEY_DOWN PTAD_PTAD0
#define KEY_LEFT PTGD_PTGD2
#define KEY_RIGHT  PTDD_PTDD1
#define KEY_UP   PTAD_PTAD1
#define LED      PTED
#define LED1     PTED_PTED4
#define LED2     PTED_PTED5
#define LED3     PTED_PTED6
#define LED4     PTED_PTED7
#define BEEZ     PTED_PTED2
byte u8_RCV_Ch=0xff;
void SetBusFreq_16M(void);
void DELAY_MS(word x);
void IO_INIT(void);
void Beep(unsigned char cnt)
{  
  word i;   
  for(i=0;i<cnt;i++)
  {
    BEEZ = 1;
    DELAY_MS(20);
    BEEZ = 0;
    DELAY_MS(10);    
  }
}
        
unsigned char uart_getkey(void)

   while(!(SCI1S1&0x80)) ;    //keep waiting when not empty 
   return SCI1D;
}
void uart_init(void) {
  SCI1C2 =0x2c;
  SCI1BDH=0x00;//16MHz,19200bps,SCI0BDL=0x1a
  SCI1BDL=0x68;//12MHz, 9600bps,SCI0BDL=0x4e
}              //16MHz, 9600bps,SCI0BDL=0x68
               //24MHz, 9600bps,SCI0BDL=0x9c

void uart_putchar(unsigned char ch)
{
  if (ch == '\n') 
  {
      while(!(SCI1S1&0x80)) ;    
      SCI1D= 0x0d;            //output'CR'
     return;
   }
   while(!(SCI1S1&0x80)) ;    //keep waiting when not empty 
   SCI1D=ch;
}
void putstr(char ch[])
{
  unsigned char ptr=0;
  while(ch[ptr])
  {
      uart_putchar((unsigned char)ch[ptr++]);
  }
}
/***************************************************
  把0--255的数值转化为3位字符串格式
****************************************************/
void Byte2Str3(char zifu[],byte val,byte StartPtr)
{
  char characters[11]="0123456789";  
  byte tv=0;
 
  tv=val/100;
  zifu[StartPtr++] = characters[tv];
  tv=(val%100)/10;
  zifu[StartPtr++] = characters[tv];
  tv=val%10;
  zifu[StartPtr] = characters[tv];
  //zifu[3] = '\0';
  return;
}
/***************************************************
  把0--65535的数值转化为5位字符串格式
  char zifu[]:  目的字符串数组
  word val:     需要转换的整形数据
  byte StartPtr:数据在字符串中的偏移位置
  例如:输出为Vol:12345V,偏移为4,在其中连续插入12345,其他格式需要自定义.
****************************************************/
void Word2Str5(char zifu[],word val,byte StartPtr)
{
  char characters[11]="0123456789";  
  word tv=0;
 
  tv=val/10000;
  zifu[StartPtr++] = characters[tv];
  tv=(val%10000)/1000;
  zifu[StartPtr++] = characters[tv];
  tv=(val%1000)/100;
  zifu[StartPtr++] = characters[tv];
  tv=(val%100)/10;
  zifu[StartPtr++] = characters[tv];
  tv=val%10;
  zifu[StartPtr] = characters[tv];
  //zifu[5] = '\0';
  return;
}

/********************************键盘中断**************************/

interrupt VectorNumber_Vsci1rx void SCI0_ISR(void)
{    
  SCI1C2_RIE=0;
  //此处为串口中断需要处理的事情
  uart_putchar(u8_RCV_Ch=uart_getkey());
 
  SCI1C2_RIE = 1;   
}
/*********************主函数************************************/
void main(void)
{
    unsigned char LedCnt=0;
    char txtbuf[30]="\n LedCnt Val:             "; 
      
    EnableInterrupts; /* enable interrupts */
 
    SetBusFreq_16M();  
    IO_INIT();
    uart_init();
   putstr("\n http://shop36265907.taobao.com");
   
    for(;;)
    {
      LED4=~LED4;    
      LedCnt=(LedCnt>0XFE?0:++LedCnt);
      DELAY_MS(500);   //修改延时以修改数据发送频率
      putstr("\n http://shop36265907.taobao.com");
      Byte2Str3(txtbuf,LedCnt,14);    
      putstr(txtbuf);            
       
    } /* loop forever */
  /* please make sure that you never leave main */
}
/***************************MCU初始化函数*******************************/
void SetBusFreq_16M(void){
    /*
      Bit 7 HGO 0 Configures oscillator for low power
      Bit 6 RANGE 1 Configures oscillator for high-frequency range; FLL prescale factor is 1
      Bit 5 REFS 1 Requests an oscillator
      Bit 4:3 CLKS 11 FLL engaged, external reference clock mode
      Bit 2 OSCSTEN 0 Disables the oscillator
      Bit 1 LOCD 0 Loss-of-clock detection enabled
      Bit 0 0 Unimplemented or reserved, always reads zero
    */    
    ICGC1 = 0x78;
   
    /* 
      Bit 7 LOLRE 0 Generates an interrupt request on loss of lock
      Bit 6:4 MFD 000 Sets the MFD multiplication factor to 4
      Bit 3 LOCRE 0 Generates an interrupt request on loss of clock
      Bit 2:0 RFD 000 Sets the RFD division factor to ÷1
    */
    ICGC2 = 0x00;
     
    ICGTRM=0xa8;
    SOPT_COPE=0;
    SOPT2=0x00;
}
/***********************I/O初始化********************************/
void IO_INIT(void){
    PTBDD_PTBDD2=0;//PTB2、PTB3设置为
    PTBDD_PTBDD3=0;
    PTGDD_PTGDD0 = 0;
    PTGDD_PTGDD1 = 0;
    PTGDD_PTGDD3 = 0;     
    PTAD_PTAD0 =0;
    PTAD_PTAD1 =0;
   
    PTEDD_PTEDD4=1; 
    PTEDD_PTEDD5=1; 
    PTEDD_PTEDD6=1; 
    PTEDD_PTEDD7=1;
   
    PTEDD_PTEDD2=1;
    PTED_PTED2=0; 
}
/***************************延时函数*******************************/
void DELAY_MS(word x)
{   
    unsigned char y=50;
    while(x--)
    {
      //__RESET_WATCHDOG();
      while(y--);           
    }
}
 
 

上一篇: 龙丘一步一步教你制作飞思卡尔仿真器USBDM 下一篇: 龙丘智能科技贡献:MC9S12DG128 EEPROM完美版读写程序

分享 举报

发表评论 评论 (2 个评论)

涂鸦板