• 專注電子技術學習與研究
    當前位置:單片機教程網 >> MCU設計實例 >> 瀏覽文章

    PIC單片機16F716產生SPWM程序和仿真

    作者:佚名   來源:本站原創   點擊數:  更新時間:2014年08月09日   【字體:

    是pic單片機做的完整代碼和仿真文件下載地址: http://www.thefapper.com/bbs/dpj-26880-1.html   可以產生spwm波形
    #include "main.h"

    #include "pic16f716.h"
     
    #define _BV(N) (1<<N)
     
    void Port_Init(void)
    {
    set_tris_b(0x00);
    }
     
    void T2PWM_OutPut(unsigned int16 t2_pwm)
    {
    unsigned int8 value = 0;
    T2CON &= ~_BV(2);
    value = CCP1CON;
    value |= (unsigned int8)((t2_pwm&0x03)<<4);
    CCP1CON = value;
    CCPR1L = (unsigned int8)(t2_pwm>>2);
    T2CON |= _BV(2);
    }
     
    void PWM_Init(void)
    {
    PWM1CON = 0b01100100;
    #if 0
    setup_ccp1(CCP_PWM);
    setup_timer_2(T2_DIV_BY_4,127,1);
    set_pwm1_duty(408);
    #else
    TRISB = 0x00;
    PR2 = 249;
    CCP1CON = 0b10001110;
    //CCPR1L = 0x00;
    T2PWM_OutPut(512);
    TMR2 = 0x00;
    TMR2IF = 0;
    T2CON = 0b00000101;
    while(TMR2IF==0);
    TRISB = 0x00;
    /*
    TRISB = 0xFF;
    PR2 = 249;
    CCP1CON = 0b00001100;
    //CCPR1L = 0x00;
    T2PWM_OutPut(512);
    TMR2 = 0x00;
    TMR2IF = 0;
    T2CON = 0b00000101;
    while(TMR2IF==0);
    TRISB = 0x00;
    */
    #endif
    }
     
    void main(void)
    {
    Port_Init();
    PWM_Init();
    //for(;;)
    while (TRUE)
    {
    //output_b(0x55);
    delay_ms(200);
    //output_b(0xaa);
    delay_ms(200);
    }
    }
     
    關閉窗口

    相關文章

    亚洲一区二区制服在线|在绩专区欧美自拍日韩|青春娱乐网97超碰人人射|在线观看国产网址你懂的