<acronym id="xonnx"></acronym>
      <td id="xonnx"></td>
    1. <pre id="xonnx"></pre>

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

        單片機紅外遙控+步進電機+1602液晶顯示c語言源程序

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

        這是我做的紅外遙控控制的1602顯示轉速和轉向的步進電機c語言原程序,已經調試成功!有些模塊是以前做其他實驗時做的
        ,現在是直接調用的,包括1602液晶顯示技術,TC9012紅外解碼,然后就是四相步進電機的控制程序了,我是把這些東西結合起來。對這個東西有興趣的或者要做畢業設計的可以看看啊,不懂的請上單片機論壇http://www.thefapper.com/bbs/ 公開討論,我會給大家做出詳細的解答。

        #include <AT89X51.h>

        #include<stdio.h>
        #include<intrins.h>
        #define uchar unsigned char
        #define uint unsigned int
        static unsigned int count; //計數
        unsigned int hour,minute,second,count;

        sbit RS = P2^3;//Pin4
        sbit RW = P2^2; //Pin5
        sbit E = P2^4;//Pin6

        #define Data P0 //數據端口
        static int step_index; //步進索引數,值為0-7
        bit k=0; //紅外解碼判斷標志位,為0則為有效信號,為1則為無效
        uchar n=0; //用來控制外部中斷
        static bit turn; //步進電機轉動方向
        static bit stop_flag; //步進電機停止標志
        static int speedlevel; //步進電機轉速參數,數值越大速度越慢,最小值為1,速度最快
        static int spcount; //步進電機轉速參數計數
        void delay(unsigned int endcount); //延時函數,延時為endcount*0.5毫秒
        void gorun(); //步進電機控制步進函數
        uchar data date[4]; //date數組為存放地址原碼,反碼,數據原碼,反碼
        char data Test1[]=" ";
        #define IR_RE P3_2
        void DelayUs(unsigned char us)//delay us
        {
        unsigned char uscnt;
        uscnt=us>>1;/* Crystal frequency in 12MHz*/
        while(--uscnt);
        }
        /******************************************************************/
        void DelayMs(unsigned char ms)//delay Ms
        {
        while(--ms)
        {
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
        DelayUs(250);
        }
        }
        void WriteCommand(unsigned char c)
        {
        DelayMs(5);//short delay before operation
        E=0;
        RS=0;
        RW=0;
        _nop_();
        E=1;
        Data=c;
        E=0;
        }
        /****************************************************************/
        void WriteData(unsigned char c)
        {
        DelayMs(5); //short delay before operation
        E=0;
        RS=1;
        RW=0;
        _nop_();
        E=1;
        Data=c;
        E=0;
        RS=0;
        }
        /***********http://www.thefapper.com單片機原創作品******************/
        void ShowChar(unsigned char pos,unsigned char c)
        {
        unsigned char p;
        if (pos>=0x10)
        p=pos+0xb0; //是第二行則命令代碼高4位為0xc
        else
        p=pos+0x80; //是第二行則命令代碼高4位為0x8
        WriteCommand (p);//write command
        WriteData (c); //write data
        }
        /*************************************************************************/
        void ShowString (unsigned char line,char *ptr)
        {
        unsigned char l,i;
        l=line<<4;
        for (i=0;i<16;i++)
        ShowChar (l++,*(ptr+i));//循環顯示16個字符
        }
        /*********************************************************************/
        void InitLcd()
        {
        DelayMs(15);
        WriteCommand(0x38); //display mode
        WriteCommand(0x38); //display mode
        WriteCommand(0x38); //display mode
        WriteCommand(0x06); //顯示光標移動位置
        WriteCommand(0x0c); //顯示開及光標設置
        WriteCommand(0x01); //顯示清屏

        }

        /*--------------------------延時1ms程子程序-----------------------*/
        delay1000()
        {
        uint i,j;
        for(i=0;i<1;i++)
        for(j=0;j<124;j++)
        ;
        }

        /*---------------------------延時882us子程序-----------------------*/

        delay882()
        {
        uint i,j;
        for(i=0;i<1;i++)
        for(j=0;j<109;j++)
        ;
        }

        /*--------------------------延時2400ms程子程序-----------------------*/

        delay2400()
        {
        uint i,j;
        for(i=0;i<3;i++)
        for(j=0;j<99;j++)
        ;
        }
        void IR_decode()
        {
        uchar i,j;
        while(IR_RE==0);
        delay2400();
        if(IR_RE==1) //延時2.4ms后如果是高電平則是新碼
        {
        delay2400(); //延時4.8ms避開4.5ms的高電平
        for(i=0;i<4;i++)
        {
        for(j=0;j<8;j++)
        {
        while(IR_RE==0); //等待地址碼第1位高電平到來
        delay882(); //延時882ms判斷此時引腳電平
        ///CY=IR_RE;
        if(IR_RE==0)
        {
        date[i]>>=1;
        date[i]=date[i]&0x7f;
        }
        else if(IR_RE==1)
        {
        delay1000();
        date[i]>>=1;
        date[i]=date[i]|0x80;
        }
        } //1位數據接收結束
        } //32位二進制碼接收結束

        }
        if (date[2]==0x16)
        TR0 = 0;
        else if(date[2]==0x14)
        {TR0 = 1;
        }
        else if(date[2]==0x10)
        {if (speedlevel>1)
        speedlevel--;
        else
        speedlevel=1;
        }
        else if(date[2]==0x1A)
        {if(speedlevel<5)
        speedlevel++;
        else
        speedlevel=5;
        }
        else if(date[2]==0x11)
        turn=~turn;

         

        }
        void int0() interrupt 0 using 1
        {
        uint i;
        for(i=0;i<4;i++)
        {
        delay1000();
        if(IR_RE==1){k=~k;} //剛開始為4.5ms的引導碼,如果4ms內出現高電平則退出解碼程序
        }

        if(k==0)
        {
        EX0=0; //檢測到有效信號關中斷,防止干擾
        IR_decode(); //如果接收到的是有效信號,則調用解碼程序
        //解碼成功,調用顯示程序,顯示該鍵值
        }
        EX0=1; //開外部中斷,允許新的遙控按鍵
        }
        void main(void)
        {
        InitLcd();//
        DelayMs(15);

        sprintf(Test1,"STEP MOTOR SPEED");//the first line
        ShowString(0,Test1);
        ShowChar(0x19,'r');
        ShowChar(0x1a,'p');
        ShowChar(0x1b,'m');
        count = 0;
        step_index = 0;
        spcount = 0;
        stop_flag = 0;

        P1_0 = 0;
        P1_1 = 0;
        P1_2 = 0;
        P1_3 = 0;
        SP=0x60;
        EA = 1; //允許CPU中斷
        TMOD = 0x11; //設定時器0和1為16位模式1
        ET0 = 1; //定時器0中斷允許
        EX0=1;
        TH0 = 0xFE;
        TL0 = 0x0C; //設定時每隔0.5ms中斷一次
        TR0 = 1; //開始計數
        turn=0;

        speedlevel=1;

        while(1)
        {
        if(turn==0)
        ShowChar(0x14,'+');
        else
        ShowChar(0x14,'-');
        if(TR0 ==0)
        {
        ShowChar(0x15,'0');
        ShowChar(0x16,'0');
        ShowChar(0x17,'0');
        ShowChar(0x18,'0');
        }
        else
        {
        if(speedlevel==1)
        {
        ShowChar(0x15,'3');
        ShowChar(0x16,'0');
        ShowChar(0x17,'0');
        ShowChar(0x18,'0');
        }
        else if(speedlevel==2)
        {
        ShowChar(0x15,'1');
        ShowChar(0x16,'5');
        ShowChar(0x17,'0');
        ShowChar(0x18,'0');
        }
        else if(speedlevel==3)
        {
        ShowChar(0x15,'1');
        ShowChar(0x16,'0');
        ShowChar(0x17,'0');
        ShowChar(0x18,'0');
        }
        else if(speedlevel==4)
        {
        ShowChar(0x15,'0');
        ShowChar(0x16,'7');
        ShowChar(0x17,'5');
        ShowChar(0x18,'0');
        }
        else if(speedlevel==5)
        {
        ShowChar(0x15,'0');
        ShowChar(0x16,'6');
        ShowChar(0x17,'0');
        ShowChar(0x18,'0');
        }
        }
        }
        }
        //定時器0中斷處理
        void timeint(void) interrupt 1 using 0
        {
        TH0=0xFE;
        TL0=0x0C; //設定時每隔0.5ms中斷一次

        count++;

        spcount--;
        if(spcount<=0)
        {
        spcount = speedlevel;
        gorun();
        }

        }

        void delay(unsigned int endcount)
        {
        count=0;
        do{}while(count<endcount);
        }

        void gorun()
        {
        if (stop_flag==1)
        {
        P1_0 = 0;
        P1_1 = 0;
        P1_2 = 0;
        P1_3 = 0;
        return;
        }

        switch(step_index)
        {
        case 0: //0
        P1_0 = 1;
        P1_1 = 0;
        P1_2 = 0;
        P1_3 = 0;
        break;
        case 1: //0、1
        P1_0 = 1;
        P1_1 = 1;
        P1_2 = 0;
        P1_3 = 0;
        break;
        case 2: //1
        P1_0 = 0;
        P1_1 = 1;
        P1_2 = 0;
        P1_3 = 0;
        break;
        case 3: //1、2
        P1_0 = 0;
        P1_1 = 1;
        P1_2 = 1;
        P1_3 = 0;
        break;
        case 4: //2
        P1_0 = 0;
        P1_1 = 0;
        P1_2 = 1;
        P1_3 = 0;
        break;
        case 5: //2、3
        P1_0 = 0;
        P1_1 = 0;
        P1_2 = 1;
        P1_3 = 1;
        break;
        case 6: //3
        P1_0 = 0;
        P1_1 = 0;
        P1_2 = 0;
        P1_3 = 1;
        break;
        case 7: //3、0
        P1_0 = 1;
        P1_1 = 0;
        P1_2 = 0;
        P1_3 = 1;
        }

        if (turn==0)
        {
        step_index++;
        if (step_index>7)
        step_index=0;
        }
        else
        {
        step_index--;
        if (step_index<0)
        step_index=7;
        }

        }


         

         

        關閉窗口
        欧美性色欧美精品视频,99热这里只有精品mp4,日韩高清亚洲日韩精品一区二区,2020国自产拍精品高潮