新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > AT89C2051利用中斷產生PWM

        AT89C2051利用中斷產生PWM

        作者: 時間:2016-11-25 來源:網絡 收藏
        原理圖


        本文引用地址:http://www.104case.com/article/201611/321335.htm

        #include "REG2051.H"
        #include "intrins.h"
        #define uint unsigned int
        #define uchar unsigned char


        const TH_600=0xfd;
        const TL_600=0xd4;
        const TH_1200=0xfb;
        const TL_1200=0x7c;
        const TH_2400=0xf6;
        const TL_2400=0xa0;

        sbit PWM=P3^7;
        bit ld=0; //上一次發送的數據
        uchar _pool[10]; //數據頭
        uchar _length; //當前數組長度
        uchar _index; //當前索引
        uchar _bit_index; //位索引


        void onKeyDown(uchar,uchar);
        void push(uchar);
        void init();

        void main(){
        unsigned int r,c,prsd;
        init();
        while(1){
        P1=0x0f;
        if(P1!=0x0f){
        r=~P1-0xf0;
        P1=0xf0;
        c=~(P1>>4)-0xf0;
        if(!prsd){
        onKeyDown(r,c);
        prsd=1;
        }
        }else{
        prsd=0;
        }
        }
        }

        void init(){
        _length=0;

        PWM=0;//啟動時低電平
        TMOD=0x01;
        TH0=0x9E;
        TL0=0x57;
        ET0=1;
        TF0=1;
        TR0=1; //打開計時器
        }

        void push(uchar xcode){
        _pool[_length]=xcode;
        _length++;
        }

        void reset(){
        _index=0;
        _length=0;
        _bit_index=0;
        }

        void onKeyDown(uchar row,uchar column){
        unsigned char xcode;
        reset();
        push(0xaa); //壓入地址碼
        xcode=row<<4+column;
        push(xcode); //壓入鍵盤數據
        EA=1;
        while(EA);
        }

        void timer_pwm() interrupt 1{
        uchar current,tmp;
        if(PWM){
        PWM=~PWM;
        TH0=TH_600;
        TL0=TL_600;
        }else{
        if(_index==_length){
        EA=0;
        }else{
        PWM=~PWM;
        _bit_index++;
        current=_pool[_index];
        tmp=current<<_bit_index;
        ld=CY;
        if(ld){
        TH0=TH_1200;
        TL0=TL_1200;
        }else{
        TH0=TH_600;
        TL0=TL_600;
        }
        }
        if(_bit_index==8){
        _bit_index=0;
        _index++;
        }
        }
        }


        上一頁 1 2 下一頁

        關鍵詞: AT89C2051中斷PW

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 喜德县| 宜君县| 北宁市| 镇康县| 山丹县| 高雄县| 许昌市| 三河市| 都昌县| 玉树县| 娱乐| 宣城市| 东莞市| 绵竹市| 黑水县| 镇康县| 盐亭县| 南岸区| 伽师县| 马龙县| 日土县| 醴陵市| 萝北县| 霞浦县| 读书| 乐业县| 杭州市| 高台县| 郎溪县| 大理市| 长葛市| 金昌市| 昌邑市| 同德县| 满洲里市| 德江县| 香河县| 舒兰市| 南通市| 定远县| 浦江县|