智能车制作

标题: MC9S12XS128系列例程--PWM [打印本页]

作者: chiusir    时间: 2009-3-3 16:53
标题: MC9S12XS128系列例程--PWM
/**************************************************************************************
龙丘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.7
Target : MC9S12XS128
Crystal: 16.000Mhz
busclock:16.000MHz
pllclock:32.000MHz  
============================================
演示程序使用说明:
1.串口9600bps,中断显示
2.PWM波形测试,测试方法:
  按u:增加频率
  按d:降低频率
  按l:减小占空比
  按r:增加占空比
  按o:开启声音
  按c:关闭声音  
*****************************************************************************************/
#include <hidef.h>           /* common defines and macros */
#include <MC9S12XS128.h>
#include <stdio.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
byte u8_RCV_Ch=0xff;
//====================中断函数==================================

#pragma CODE_SEG DEFAULT
void Dly_ms(int ms)
{
  int ii,jj;
   if (ms<1) ms=1;
   for(ii=0;ii<ms;ii++)
     for(jj=0;jj<2770;jj++);    //32MHz--1ms  
}
//-----------------------------------------------------
static void SCI_Init(void)
{
    SCI0CR2=0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
    SCI0BDH=0x00; //busclk  8MHz,19200bps,SCI0BDL=0x1a
    SCI0BDL=0x68; //SCI0BDL=busclk/(16*SCI0BDL)                  
                  //busclk 16MHz, 9600bps,SCI0BDL=0x68                  
}            
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++]);
  }
}
unsigned char uart_getkey(void)
{  
   byte res=0;
   while(!(SCI0SR1&0x80)) ;    //keep waiting when not empty  
   return (SCI0DRL);
}
//-----------------------------------------------------
static void Port_Init(void)
{   
    DDRB = 0xff;  //LED  PTB0--7,
    PORTB= 0xff;  //LEDs on  
}
// PLL初始化子程序   BUS Clock=16M
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 PWM_Init(void)
{     
    //SB,B for ch2367
    //SA,A for ch0145  
    PWMPRCLK = 0X55;    //clockA,CLK B 32分频:500khz   
    PWMSCLA = 0x02;     //对clock SA 进行2*PWMSCLA=4分频;pwm clock=clockA/4=125KHz;     
    PWMSCLB = 0X02;     //clk SB=clk B/(2*pwmsclb)=125KHZ
  //pwm1  
    PWMCNT1 = 0;
    PWMCAE_CAE1=0;      
    PWMPOL_PPOL1=0;                     
    PWMPER1 =125;        
    PWMDTY1 =100;        
    PWMCLK_PCLK1 = 1;                 
    PWME_PWME1 = 0;
}
//-----------------------------------------------------
void Init_Dev(void)
{
    setbusclock();
    Port_Init();
    SCI_Init();
    PWM_Init();   
}
//-----------------------------------------------------
void Beep(void)
{   
  word tmper=125,ty=100;
  tmper=PWMPER1;
  ty=PWMDTY1;
  PWME_PWME1 = 1;
  PWMPER1 =125;         
  PWMDTY1 =100;   
  Dly_ms(40);
  PWMPER1 =tmper;         
  PWMDTY1 =ty;
  PWME_PWME1 = 0;  
}

#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 20 SCI0_ISR(void)
{     
  SCI0CR2_RIE=0;
  //此处为串口中断需要处理的事情
  uart_putchar(u8_RCV_Ch=uart_getkey());
  //PORTA_PA0=~PORTA_PA0;
  PORTB_PB7=~PORTB_PB7;
  switch(u8_RCV_Ch)
    {
      case 'u':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;           //开启PWM0波形输出   
        if(PWMPER1>15)            //增大频率
            PWMPER1-=5;
      break;
      case 'd':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMPER1<255)           //减小频率
            PWMPER1++;
        if(PWMDTY1>PWMPER1+10)    //减小占空比
              PWMDTY1=PWMPER1*4/5;  //20%
      break;
      case 'l':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMDTY1<PWMPER1)       //增大占空比
            PWMDTY1++;
      break;
      case 'r':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMDTY1<PWMPER1)       //减小占空比
              PWMDTY1--;
      break;      
      default:            
      break;      
    }//switch(u8_RCV_Ch)      
   
  //Beep();
  SCI0CR2_RIE = 1;   
}
//====================main()==================================
#pragma CODE_SEG DEFAULT     
void main(void)   
{   
  char  txtbuf[16]="";
   
  Init_Dev();  
  PORTB=0x00;
  putstr("\nLongQiu s.&t. Co. Ltd.");   
  putstr("\nhttp://shop36265907.taobao.com");
  Dly_ms(1000);   
  PORTB=0xFF;
  EnableInterrupts;   
  for(;;)
  {     
    Dly_ms(500);
//----------PWM0----------------------------------
    sprintf(txtbuf,"\nFrequence: %d0Hz",PWMPER1);  
    putstr(txtbuf);
    sprintf(txtbuf,"\n duty circle: %d%%",(PWMPER1-PWMDTY1)*100/PWMDTY1);
    putstr(txtbuf);        
    PORTB_PB0=~PORTB_PB0;        
  }//for(;;)
} //main
作者: vallis    时间: 2009-3-3 17:12
大恩不言谢
作者: lynd323    时间: 2009-3-4 14:34
顶哦!!
作者: cys179    时间: 2009-3-4 14:54


补充内容 (2015-4-23 09:36):
真人游戏|足球篮球|时时彩| 六合投注| 网络赚钱去SO娱乐城:顶级信用,提现百分百即时到账SO.CC



作者: liuxing4585    时间: 2009-3-14 09:14
xiexie
作者: ap0604331    时间: 2009-3-14 10:48
谢啦
作者: dalefang    时间: 2009-3-17 07:32
很感谢~~~定鼎力顶!
作者: zhangtielin    时间: 2009-3-18 15:47
试一试吧
作者: 刘晏清    时间: 2009-3-22 11:22
我页试试
作者: gaowei3569    时间: 2009-3-22 16:31
好东西,要好好利用
作者: chuyanliang    时间: 2009-3-28 23:11
谢谢!!!!!!!!!
作者: duanjuan    时间: 2009-3-31 22:47
10# gaowei3569

作者: wangkang2011    时间: 2009-4-3 09:56
谢谢楼主~~~很厉害啊~~
作者: bengzhu1hao    时间: 2009-4-3 22:03
顶哦!!
作者: xiaopang1989    时间: 2009-4-7 17:17
的确很好!谢谢!
作者: xiaopang1989    时间: 2009-4-7 17:17
的确很好!谢谢!
作者: gangwa1987    时间: 2009-4-9 19:42
请问:我在运行此程序时,终端能够显示字符,但无法从键盘读入字符并显示在终端,好像不能进入中断服务程序。当连续按键的时候终端会卡。问题出在哪儿啦?
作者: gangwa1987    时间: 2009-4-9 20:05
请问 :我在用上面的程序调试时,终端能够显示字符,但不能从键盘读入字符并显示在终端,不能进入中断程序。
问题何在?   谢谢
作者: yangyong1011    时间: 2009-4-17 17:51
非常感谢楼主。
作者: 13673123373    时间: 2009-4-18 13:08
不下对不起楼主
作者: 伴月迎风    时间: 2009-4-22 12:42
谢谢
作者: hyx_wolf    时间: 2009-4-22 15:41
很感激很感激
作者: hyx_wolf    时间: 2009-4-22 15:41
很感激很感激
作者: 林金龙    时间: 2009-5-26 17:24
好东西,谢谢楼主了
作者: 青龙    时间: 2009-6-25 09:52
楼主辛苦了
作者: changshasun    时间: 2009-7-2 20:59
飘过!
作者: sxz_101    时间: 2009-8-2 10:32
谢谢了
作者: chihong    时间: 2009-8-28 14:09
daenbuyanxie
作者: huangying223    时间: 2009-9-9 16:39
,谢谢!
作者: wokao    时间: 2009-9-19 19:45
有没简单点的?
作者: dkmy    时间: 2009-10-8 12:09
感谢楼主分享!~~~
作者: starjiajia    时间: 2009-10-17 21:14
谢谢
作者: wangguoxiao    时间: 2009-11-22 10:54
大恩难言谢
作者: chiusir    时间: 2009-12-3 23:49
举手之劳,不言谢!
作者: 凌风    时间: 2010-1-1 20:23

作者: 山孩子    时间: 2010-1-12 13:22
谢啦
作者: Tom_xuan    时间: 2010-1-16 11:52
Think you
作者: yu19880915    时间: 2010-1-16 16:14
不错,学习学习
作者: yu19880915    时间: 2010-1-16 16:14
不错,学习学习
作者: hcx8911    时间: 2010-1-17 15:35
以后会看懂的
作者: 极意旋风    时间: 2010-1-23 18:37
大公无私!!!狂顶
作者: donghong000    时间: 2010-2-17 20:41
谢谢
作者: donghong000    时间: 2010-2-17 20:42
顶了
作者: 凌风    时间: 2010-2-25 18:11
自己写的么 很厉害 我还没有拿到开发板呢
作者: huangkaizhi    时间: 2010-3-6 15:42
xie
作者: 极意旋风    时间: 2010-3-7 10:32
感谢分享交流!!!
作者: qinjiawei    时间: 2010-3-11 22:17
make good use of it  and thank you
作者: zhangjinboxp    时间: 2010-3-13 13:39
hao dong xi a
作者: BPKN    时间: 2010-3-16 16:37
谢啦
作者: 仁者    时间: 2010-4-13 21:17

作者: 灵水    时间: 2010-4-21 22:47
顶了
作者: huajiangxuetu    时间: 2010-5-11 17:49
感谢
作者: aimei126    时间: 2010-5-12 14:18

作者: liaofuyan    时间: 2010-6-11 10:19
(⊙o⊙)哦。。。O(∩_∩)O谢谢
作者: zhangluhuixing    时间: 2010-6-23 17:23
[单片机] MC9S12XS128系列例程--PWM
龙丘, MC9S12X, 多功能开发板
/**************************************************************************************
龙丘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.7
Target : MC9S12XS128
Crystal: 16.000Mhz
busclock:16.000MHz
pllclock:32.000MHz  
============================================
演示程序使用说明:
1.串口9600bps,中断显示
2.PWM波形测试,测试方法:
  按u:增加频率
  按d:降低频率
  按l:减小占空比
  按r:增加占空比
  按o:开启声音
  按c:关闭声音  
*****************************************************************************************/
#include <hidef.h>           /* common defines and macros */
#include <MC9S12XS128.h>
#include <stdio.h>
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"
byte u8_RCV_Ch=0xff;
//====================中断函数==================================

#pragma CODE_SEG DEFAULT
void Dly_ms(int ms)
{
  int ii,jj;
   if (ms<1) ms=1;
   for(ii=0;ii<ms;ii++)
     for(jj=0;jj<2770;jj++);    //32MHz--1ms  
}
//-----------------------------------------------------
static void SCI_Init(void)
{
    SCI0CR2=0x2c; //enable Receive Full Interrupt,RX enable,Tx enable
    SCI0BDH=0x00; //busclk  8MHz,19200bps,SCI0BDL=0x1a
    SCI0BDL=0x68; //SCI0BDL=busclk/(16*SCI0BDL)                  
                  //busclk 16MHz, 9600bps,SCI0BDL=0x68                  
}            
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++]);
  }
}
unsigned char uart_getkey(void)
{  
   byte res=0;
   while(!(SCI0SR1&0x80)) ;    //keep waiting when not empty  
   return (SCI0DRL);
}
//-----------------------------------------------------
static void Port_Init(void)
{   
    DDRB = 0xff;  //LED  PTB0--7,
    PORTB= 0xff;  //LEDs on  
}
// PLL初始化子程序   BUS Clock=16M
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 PWM_Init(void)
{     
    //SB,B for ch2367
    //SA,A for ch0145  
    PWMPRCLK = 0X55;    //clockA,CLK B 32分频:500khz   
    PWMSCLA = 0x02;     //对clock SA 进行2*PWMSCLA=4分频;pwm clock=clockA/4=125KHz;     
    PWMSCLB = 0X02;     //clk SB=clk B/(2*pwmsclb)=125KHZ
  //pwm1  
    PWMCNT1 = 0;
    PWMCAE_CAE1=0;      
    PWMPOL_PPOL1=0;                     
    PWMPER1 =125;        
    PWMDTY1 =100;        
    PWMCLK_PCLK1 = 1;                 
    PWME_PWME1 = 0;
}
//-----------------------------------------------------
void Init_Dev(void)
{
    setbusclock();
    Port_Init();
    SCI_Init();
    PWM_Init();   
}
//-----------------------------------------------------
void Beep(void)
{   
  word tmper=125,ty=100;
  tmper=PWMPER1;
  ty=PWMDTY1;
  PWME_PWME1 = 1;
  PWMPER1 =125;         
  PWMDTY1 =100;   
  Dly_ms(40);
  PWMPER1 =tmper;         
  PWMDTY1 =ty;
  PWME_PWME1 = 0;  
}

#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 20 SCI0_ISR(void)
{     
  SCI0CR2_RIE=0;
  //此处为串口中断需要处理的事情
  uart_putchar(u8_RCV_Ch=uart_getkey());
  //PORTA_PA0=~PORTA_PA0;
  PORTB_PB7=~PORTB_PB7;
  switch(u8_RCV_Ch)
    {
      case 'u':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;           //开启PWM0波形输出   
        if(PWMPER1>15)            //增大频率
            PWMPER1-=5;
      break;
      case 'd':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMPER1<255)           //减小频率
            PWMPER1++;
        if(PWMDTY1>PWMPER1+10)    //减小占空比
              PWMDTY1=PWMPER1*4/5;  //20%
      break;
      case 'l':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMDTY1<PWMPER1)       //增大占空比
            PWMDTY1++;
      break;
      case 'r':
        u8_RCV_Ch=0xff;
        PWME_PWME1 = 1;
        if(PWMDTY1<PWMPER1)       //减小占空比
              PWMDTY1--;
      break;      
      default:            
      break;      
    }//switch(u8_RCV_Ch)      
   
  //Beep();
  SCI0CR2_RIE = 1;   
}
//====================main()==================================
#pragma CODE_SEG DEFAULT     
void main(void)   
{   
  char  txtbuf[16]="";
   
  Init_Dev();  
  PORTB=0x00;
  putstr("\nLongQiu s.&t. Co. Ltd.");   
  putstr("\nhttp://shop36265907.taobao.com");
  Dly_ms(1000);   
  PORTB=0xFF;
  EnableInterrupts;   
  for(;;)
  {     
    Dly_ms(500);
//----------PWM0----------------------------------
    sprintf(txtbuf,"\nFrequence: %d0Hz",PWMPER1);  
    putstr(txtbuf);
    sprintf(txtbuf,"\n duty circle: %d%%",(PWMPER1-PWMDTY1)*100/PWMDTY1);
    putstr(txt
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
v很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:23
很好
作者: zhangluhuixing    时间: 2010-6-23 17:24
很好
作者: zhouyong    时间: 2010-7-3 09:46

作者: zhouyong    时间: 2010-7-3 09:46

作者: jiang1101    时间: 2010-7-17 21:58
感谢!
作者: 梧桐1209    时间: 2010-10-9 18:46
ding  ba
作者: SDM    时间: 2010-10-12 23:09
必须顶
作者: ture_2010    时间: 2010-10-14 19:32
初学,疯狂看代码中,谢谢分享
作者: john    时间: 2010-10-27 20:31
恩,不错!谢啦!
作者: TNT2    时间: 2010-11-4 12:21
谢谢  呵呵 顶起
作者: chiusir    时间: 2011-5-17 10:13
自己写的么 很厉害 我还没有拿到开发板呢
凌风 发表于 2010-2-25 18:11



    原创,呵呵
作者: 滴答1滴答2    时间: 2011-5-28 17:50
不错啊
作者: clong_2010    时间: 2011-6-10 13:44
谢谢!
作者: 放开那女孩    时间: 2011-6-15 12:31
不错
作者: 云端暮雪    时间: 2011-11-29 22:33
好啊。。。
作者: lwbwd    时间: 2012-3-18 15:25
不懂哦




欢迎光临 智能车制作 (http://111.231.132.190/) Powered by Discuz! X3.2