新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > PIC16F917 電壓表

        PIC16F917 電壓表

        作者: 時間:2016-11-18 來源:網絡 收藏
        下午調試好NOKIA5110液晶程序后就再練習下PIC16F917內部AD程序,把AD采集的數據通過LCD進行顯示。制作一個簡單的電壓表

        /*************PIC16F917單片機程序******************************/
        /*********************************************************************/
        /*****File Function : AD采集,LCD顯示程序*****/
        /*****Program Author : ZhengWen(ClimberWin)*****/
        /*****MCU : PIC16F917 內部晶振*****/
        /*****Compile Date : 2010/08/18 *****/
        /*****Edition Info : V1.0 *****/
        /*************************************************************/
        //測量AD,并且通過LCD來進行顯示
        //引腳定義:PORTD=8LED,KEY=RB0(INT) RA5(AN4)作為AD輸入
        /*修改日期: */

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

        /************************************/
        #include
        #include "english_6x8_pixel.h"
        #define uchar unsigned char
        #define uint unsigned int
        void Init(void); //初始化子程序
        void LCD_init(void); //LCD初始化程序
        void LCD_clear(void);
        void LCD_write_char(unsigned char c);
        void LCD_set_XY(unsigned char X, unsigned char Y);
        void LCD_write_english_string(unsigned char X,unsigned char Y,const char *s);
        void LCD_write_byte(unsigned char data, unsigned char command);
        void delayms(unsigned int count);
        void interrupt ADint(void);

        #define KEY RB0
        #define SPI_CLK RD0
        #define SPI_MOSI RD1
        #define LCD_DC RD2
        #define LCD_CE RD3
        #define LCD_RST RD4
        /***********************************************/

        const unsigned char mask_table[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
        float ad_data;//AD數據存儲地址
        uchar temp1,temp2;
        uint temp3;
        /*********************************************/
        void delayms(unsigned int count)
        {
        uint i,j;
        for(i=0;ifor(j=0;j<120;j++);
        }
        /*********************************************/
        void Init(void)
        {
        PORTA = 0B00000000;
        PORTB = 0B00000000;
        PORTD = 0B00000000;

        TRISA = 0B00100010;//設置RA5(AN4) RA1作為輸入
        TRISB = 0B00100001;//設置RB0為輸入,作為按鍵口
        TRISD = 0B00000000;//設置PORTD口為輸出,作為LCD/LED顯示口
        RD5=1;//關閉LED
        RD6=1;
        RD7=1;

        ///////////AD配置///////////////////////////////////////
        ANSEL= 0B00010000; //選擇AN4作為AD輸入 (PDF 148)
        ADCON0=0B10010001;//AD結果格式Right justified,選擇參考電壓VDD-VSS,AN4輸入,關閉AD轉換
        ADCON1=0B01010000; //AD轉換時鐘選擇


        ADIE=1;//AD中斷使能
        PEIE=1;
        ADIF=0;//清除中斷標志位
        GIE=1; //開總中斷
        /////////////////////////////////////
        LCD_init(); //初始化液晶
        }

        void interrupt ADint(void)
        {

        temp1=ADRESL;
        temp2=ADRESH;
        temp3=temp2*256+temp1; //Dx值
        ad_data=(temp3*33000)/1024; //電壓值
        ADIF=0;//清除中斷標志位

        }


        void LCD_init(void)
        {
        LCD_RST=0; //LCD復位
        NOP();
        LCD_RST=1;

        LCD_CE=0 ;// 關閉LCD
        NOP();
        LCD_CE=1;// 使能LCD
        NOP();

        LCD_write_byte(0x21, 0);// 使用擴展命令設置LCD模式
        LCD_write_byte(0xc8, 0);// 設置偏置電壓
        LCD_write_byte(0x06, 0);// 溫度校正
        LCD_write_byte(0x13, 0);// 1:48
        LCD_write_byte(0x20, 0);// 使用基本命令
        LCD_clear(); // 清屏
        LCD_write_byte(0x0c, 0);// 設定顯示模式,正常顯示

        LCD_CE=0 ; // 關閉LCD
        }

        /////////LCD清屏程序/////////////
        void LCD_clear(void)
        {
        uint i;
        LCD_write_byte(0x0c, 0);
        LCD_write_byte(0x80, 0);
        for (i=0; i<504; i++)
        LCD_write_byte(0x00, 1);//清零
        }

        ///////////設置LCD坐標///////////////////
        void LCD_set_XY(unsigned char X, unsigned char Y)
        {
        LCD_write_byte(0x40 | Y, 0);
        LCD_write_byte(0x80 | X, 0);
        }

        ////////////////字符顯示程序/////////////////////
        void LCD_write_char(unsigned char c)
        {
        uint line;
        c=c-32;
        for (line=0; line<6; line++)
        LCD_write_byte( font6x8[c][line], 1);
        }


        /////////////////打印字符串/////////////////////////
        void LCD_write_english_string(unsigned char X,unsigned char Y, const unsigned char *s)
        {
        uchar i = 0;
        LCD_set_XY(X,Y);
        while(*s) {LCD_write_char(*s++);}
        }


        ////////////寫數據到LCD//////////////////////
        void LCD_write_byte(unsigned char data, unsigned char command)
        {
        uchar i;
        LCD_CE=0 ; // 使能LCD

        if (command == 0)
        {LCD_DC=0 ;} // 傳送命令
        else
        {LCD_DC=1 ;} // 傳送數據

        for(i=0;i<8;i++)
        {
        if(data&mask_table[i])
        {SPI_MOSI=1;}
        else
        {SPI_MOSI=0;}
        SPI_CLK=0;
        NOP();
        SPI_CLK=1;
        }

        LCD_CE=1 ;// 關閉LCD
        }


        ////////////主程序/////////////////////////
        void main (void)
        {

        Init();//初始化程序
        LCD_clear(); //LCD清屏
        delayms(1000);
        LCD_write_english_string(0,0," AD Test " );
        LCD_write_english_string(0,1,"AD4 Vref=3.3V" );

        ADCON0=ADCON0|0B00000010;//開始AD轉換
        LCD_set_XY(0,3);
        LCD_write_char(D);
        LCD_write_char(x);
        LCD_write_char( );
        LCD_write_char(=);


        LCD_set_XY(0,5);
        LCD_write_char(V);
        LCD_write_char(i);
        LCD_write_char(n);
        LCD_write_char(=);
        LCD_set_XY(72,5);
        LCD_write_char(V);
        while(1)
        {
        LCD_set_XY(30,3);
        LCD_write_char((temp3/1000)+16+32);
        LCD_write_char( (temp3%1000)/100+16+32);
        LCD_write_char( ((temp3%1000)%100)/10+16+32);
        LCD_write_char( ((temp3%1000)%100)%10+16+32);

        //ad_data=ad_data*10000;
        temp3=(uint)ad_data;
        LCD_set_XY(30,5);
        LCD_write_char((temp3/10000)+16+32);
        LCD_write_char(.);
        LCD_write_char( (temp3%10000)/1000+16+32);
        LCD_write_char( ((temp3%10000)%1000)/100+16+32);
        LCD_write_char( (((temp3%10000)%1000)%100)/10+16+32);
        LCD_write_char( (((temp3%10000)%1000)%100)%10+16+32);

        delayms(100);
        ADCON0=ADCON0|0B00000010;//開始AD轉換

        }

        }



        關鍵詞: PIC16F917電壓

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 晋城| 定襄县| 河南省| 调兵山市| 华亭县| 柳州市| 巴林左旗| 新龙县| 余姚市| 仁布县| 桂林市| 彩票| 友谊县| 阿坝县| 尉氏县| 石屏县| 鲜城| 绍兴县| 阳信县| 南澳县| 罗田县| 岱山县| 阿巴嘎旗| 皋兰县| 乌拉特后旗| 汽车| 城口县| 远安县| 长治市| 甘泉县| 汝城县| 甘肃省| 大丰市| 青神县| 宜阳县| 莱芜市| 祥云县| 竹北市| 慈溪市| 永吉县| 安泽县|