找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 4147|回復(fù): 1
打印 上一主題 下一主題
收起左側(cè)

pic16f1939單片機(jī)pwm XC編譯器例子

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:869731 發(fā)表于 2020-12-28 19:34 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
LED5運(yùn)行閃亮,LED0 漸亮漸暗

#include "xc.h"                     // 調(diào)用頭文件

// CONFIG1
#pragma config FOSC = HS            // Oscillator Selection (HS Oscillator, High-speed crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF           // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF          // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON           // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF             // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF            // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF          // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF       // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON            // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON           // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF            // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF         // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
#pragma config PLLEN = OFF          // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON          // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO            // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF            // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

//-------------------------------------------------------------------------------

#define _XTAL_FREQ      8000000L    //外部 8MHz

#define LED0  LATCbits.LATC2        //LED宏定義
#define LED1  LATCbits.LATC1
#define LED2  LATCbits.LATC0
#define LED3  LATAbits.LATA5
#define LED4  LATAbits.LATA3
#define LED5  LATAbits.LATA2

#define SCLK  LATBbits.LATB0        //74HC595時(shí)鐘
#define SDAT  LATBbits.LATB1        //74HC595數(shù)據(jù)
#define SRCK  LATCbits.LATC5        //74HC595鎖存

#define KEY0  PORTEbits.RE3         //KEY宏定義
#define KEY1  PORTBbits.RB5
#define KEY2  PORTBbits.RB4

//-------------------------------------------------------------------------------

#define TMR1HPRELOAD    0xF0        //定時(shí)器初值
#define TMR1LPRELOAD    0x9B

unsigned char TickClock  = 0;      //2.5mS 時(shí)基
unsigned char Tmr1conunt = 0;      //定時(shí)計(jì)數(shù)

//-------------------------------------------------------------------------------
//        TMR1定時(shí)器初始化
//-------------------------------------------------------------------------------
void TMR1_Init(void)
{           
        T1CONbits.TMR1CS = 0;   //TMR1時(shí)鐘 FOSC/4         
        T1CONbits.T1CKPS = 1;   //輸入分頻比1:2
        TMR1H  = 0xF0;          //8MHz  4mS
        TMR1L  = 0x5F;
        TMR1IF = 0;             //清中斷標(biāo)志
        TMR1IE = 1;             //中斷允許
}

//-------------------------------------------------------------------------------
//        中斷程序
//-------------------------------------------------------------------------------
void interrupt SystemISR(void)
{
        if(TMR1IE & TMR1IF)              // TMR1中斷
        {
                TMR1IF= 0;
                TMR1H = 0xF0;
                TMR1L = 0x5F;
                TickClock = 1;                 // 標(biāo)志位
                Tmr1conunt ++;           // 計(jì)數(shù)
        }               
}

//-------------------------------------------------------------------------------
//        CCP1初始化
//-------------------------------------------------------------------------------
void CCP1_Init(void)
{
        CCP1CON = 0b00001100;         //CCP1 為PWM模式
        CCPTMRS0= 0b00000000;         //CCP1~4 時(shí)鐘來(lái)源TMR2
        CCPTMRS1= 0b00000000;         //CCP5 以Timer2 作為定時(shí)器
        CCP1IE = 0;
        CCP1IF = 0;
        
        T2CON = 0b01001100;           // Postcaler 1:10, T2ON
        PR2 = 249;                    // PWM周期 = (PR2+1)*4*TOSC
                                      // TOSC = 1/FOSC
        CCPR1L = 0x00;                // 占空比 0
        
        TMR2IF = 0;
        TMR2IE = 0;
}        

//-------------------------------------------------------------------------------
//        CCP占空比程序
//-------------------------------------------------------------------------------
unsigned int  PWM_Duty = 0;      //PWM 占空比
unsigned char PWM_FLAG  = 0;     //漸變標(biāo)志位
void Conver_CCPR1L(unsigned int Duty)         //PWM 占空比轉(zhuǎn)換
{
        CCPR1L = (unsigned char)(Duty>>2);
        CCP1CON |= (unsigned char)((Duty&0x0003)<<4);
}

//-------------------------------------------------------------------------------
//        PWM控制LED部分
//-------------------------------------------------------------------------------
void PWM_LED(void)
{   
        if (TickClock == 1)             //2.5mS
        {
                TickClock = 0;
                if(PWM_FLAG == 1)       //PWM LED  漸暗   
                {
                        if(PWM_Duty != 0)  
                                PWM_Duty --;
                        else               
                                PWM_FLAG = 0;
                }
                else                    //PWM LED  漸亮
                {
                        if(PWM_Duty <1023)
                                PWM_Duty++;
                        else               
                                PWM_FLAG = 1;               
                }
                Conver_CCPR1L(PWM_Duty);                  
        }
}

//-------------------------------------------------------------------------------
//        系統(tǒng)初始化
//-------------------------------------------------------------------------------
void System_Init(void)
{
        ADCON1 = 0b10010011;            //Fosc/8 Vref = FVR
        ADCON0 = 0b00000001;            //ADON = 1 As AN0
        
        FVRCON = 0b10000011;            //Vref+ = 4.096V

        LATA  = 0b00000000;             //端口電平初始化
        LATB  = 0b00000000;
        LATC  = 0b00000000;
        LATD  = 0b00000000;
        LATE  = 0b00000000;
        
        TRISA  = 0b11000001;            //輸入
        ANSELA = 0b00000001;            //RA0模擬 LED   
                  
        TRISB  = 0b00110000;            //RB4 RB5 mTouch
        ANSELB = 0b00110000;            //74HC164 CLK DAT

        TRISC  = 0b10011000;            //UART I2C
        
        TRISD  = 0b00000000;            //輸出
        ANSELD = 0b00000000;            //數(shù)字I/O

        TRISE  = 0b00000000;            //輸出
        ANSELE = 0b00000000;            //數(shù)字I/O
}

//-------------------------------------------------------------------------------
//        LED燈開(kāi)機(jī)顯示
//-------------------------------------------------------------------------------
void StartViewLED(void)
{
        LED0 = 1;                //LED0-LED5 亮
        LED1 = 1;
        LED2 = 1;
        LED3 = 1;
        LED4 = 1;
        LED5 = 1;
        __delay_ms(800);         //延時(shí)
        LED0 = 0;                //LED0-LED5 滅
        LED1 = 0;
        LED2 = 0;
        LED3 = 0;
        LED4 = 0;
        LED5 = 0;
        __delay_ms(50);          //延時(shí)
}
               
//-------------------------------------------------------------------------------
//        主程序部分
//-------------------------------------------------------------------------------
void main(void)
{
        System_Init();               //系統(tǒng)初始化
        StartViewLED();              //開(kāi)機(jī)LED顯示
        
        TMR1_Init();                 //TMR1初始化
        CCP1_Init();                 //CCP1初始化
        
        INTCONbits.PEIE = 1;         //外設(shè)中斷
        INTCONbits.GIE  = 1;         //系統(tǒng)中斷
        T1CONbits.TMR1ON= 1;         //TMR1使能
        while(1)
        {
                PWM_LED();                   //漸變
                if(Tmr1conunt > 199)         //500mS
                {
                        Tmr1conunt = 0;
                        LED5 = !LED5;        //運(yùn)行燈
                }        
        }
}


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1019848 發(fā)表于 2024-8-21 11:35 | 只看該作者
中斷函數(shù)要改成:void __interrupt() isr(void)
不然會(huì)報(bào)錯(cuò)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表