⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm.c

📁 本人s12的一部分机器人巡线程序
💻 C
字号:
/*********************************************************************/
/* PWM_Init.c   ------------ Hardware related functions                   

		PWM_Init();
		PWM Initialization rounte

**********************************************************************/    

void PWM_Init(void) {
    PWME=0xFA;        // PWM Enable Register each bit figure the each channels to enable PWM
/**The function of PWME is enable that eight channel of PWM,if the relative bit is seted,the relative channel will enable to PWM function**/    
/**PWME功能为使能8个PWM通道,PWME的每个位对应一个通道,相对位置一的时候就使能相对通道**/
    PWMPOL=0x00;      // PWM Polarity Register polarity will start with high level in a cycle when the relevant bit set
/**The function of PWMPOL is to select the polarity with relative PWM channel,if the relative bit is seted,the relative channel will be output PWM shart with High level, else output with Low level in shart   **/    
/**PWMPOL功能为选择PWM输出的极性,如果相对位置的位置1则相对通道的PWM先输出高电平,否则先输出低电平**/
    PWMCLK=0xff;      // PWM Clock Select Register X channel select clock SB(or SA) with PWMCLK.X equal one,else select clock B(or A) 
/**The function of PWMCLK is to select a CLOCK(SB,B,SA or A) source work for it ,Channel 0,1,4,5 will select CLOCK SA if the reletive bit set 1,else select CLOCK A;similar,Channel 2,3,6,7 will select CLOCK SB or CLOCK B **/                                                                                                                           
/**PWMCLK功能为选择通道的使用时钟源,通道0,1,4,5在相对位置1的时候选择时钟SA,否则使用时钟A作为时钟源;同样的,通道2,3,6,7将选择时钟SB或者时钟B**/
    PWMCAE=0xff;      // PWM Center Align Enable Register PWMCAE.X set will case the channel X run for center align output mode,reset case right align output mode.
/**The function of PWMCAE is to determine the Center Align or the Right Align output mode will be use,if reletive bit set 1,that channel's waveform be Center Align ,else output right align waveform**/
/**PWMCAE功能为选择输出波形中间对齐还是右对齐排列形式,如果相对的位置1,则选择中间对齐方式,否则选择右对齐方式**/
    PWMCTL=0x30;   
    PWMSCLA=0x06;     // PWM Scale A Register   Clock SA = Clock A / (2 * PWMSCLA) ,when PWMSCLA=0x00,PWMSCLA value is consider a full scale value of 256
/**The function of PWMSCLA is to setting the clock frequency of CLOCK SA according as CLOCK A frequency :Clock SA = Clock A / (2 * PWMSCLA)
if the PWMSCLA equal zero ,PWMSCLA value is consider a full value of 256,in this time ,Clock SA = Clock A/512,so when you setting PWMSCLA you must carefull enough**/
/** PWMSCLA功能为设置时钟SA 的工作频率,这是基于时钟A的,公式如:Clock SA = Clock A / (2 * PWMSCLA) ,当PWMSCLA=0X00 的时候,PWMSCLA的值会
被认为256,那么Clock SA=Clock A/512,所以需要注意**/
    PWMSCLB=0x06;     // PWM Scale B Register   similar as PWMSCLA
/**The function of PWMSCLB is similar as PWMSCLA ,the different is that setting Clock SB according as Clock A ,the detail see the previous note about PWMSCLA**/
/**PWMSCLB功能和PWMSCLA的功能使一致的,只是控制的基于时钟B确定时钟SB频率,详细见上一注释**/
//    PWMPER0=0xFF;     //PWM Channel Period Registers PWMx Period = Channel Clock Period * (2 * PWMPERx)
/**The function of PWMPERx is setting the x channel PWM Period according following equation :PWMx Period = Channel Clock Period * (2 * PWMPERx).**/
/**NOTE :The x figure one of the PWM channel 0 to 7  **/
/**PWMPERx功能为设定x通道的PWM波周期。公式为:PWMx Period = Channel Clock Period * (2 * PWMPERx)**/
/**注意:这里的x表示0到7PWM通道之一**/
//    PWMDTY0=0x80;     //PWM Channel Duty Registers ,Polarity = 0 (PPOLx=0) :Duty Cycle = [(PWMPERx-PWMDTYx)/PWMPERx] * 100% ; Polarity = 1 (PPOLx=1) : Duty Cycle = [PWMDTYx / PWMPERx] * 100%  .This Register value need to sure in the future
/**The function of PWMDTYx is ensuring the duty of PWM waveform ,if Polarity equal zero(PPOLx equal zero) ,the duty equation as following:
Duty Cycle = [(PWMPERx-PWMDTYx)/PWMPERx] * 100% ;if Polarity equal one(PPOLx euqal one),the duty equation as following: Duty Cycle = [PWMDTYx / PWMPERx] * 100% ;
This register will be change to agreement your actual requirement.**/
/**PWMDTYx功能为确定PWM波形占空比的比较值,如果采用极性控制位等于0的方式则占空比的表达式为:Duty Cycle = [(PWMPERx-PWMDTYx)/PWMPERx] * 100% 
否则公式为Duty Cycle = [PWMDTYx / PWMPERx] * 100%,这个寄存器是根据你的实际需要用户在程序运行中自己进行改变的**/    
//    PWMPER1=0xFF;
//    PWMPER2=0xFF;
//    PWMPER3=0xFF;
    PWMPER01=0x682E; //50Hz
    PWMPER23=0x682E;
    PWMPER4=0xFF;    //4.7KHz
    PWMPER5=0xFF;
    PWMPER6=0xFF;
    PWMPER7=0xFF;
//    PWMDTY1=0x30;
//    PWMDTY2=0x50;
//    PWMDTY3=0x70;
    PWMDTY01=0x05D4;
    PWMDTY23=0x05D4;
    PWMDTY4=0x00;
    PWMDTY5=0x00;
    PWMDTY6=0x00;
    PWMDTY7=0x00;        
    PWMSDN=0x00;      //PWM Shutdown Register The register above shutdown PWM and restart PWM and PWM interrupt enable and so on.In general,PWMSDN equal zero.
/**The function of PWMSDN is to shutdown and restart the PWM waveform output or enable the PWM interrupt and so on,In general,the PWMSDN equal zero **/
/**PWMSDN的功能为关闭和重启PWM波输出或者使能PWM中断等,一般的时候PWMSDN应该保持为0**/    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -