找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 6171|回復: 6
打印 上一主題 下一主題
收起左側

剛學PIC單片機,分享第一個電子時鐘,定時器計時

[復制鏈接]
跳轉到指定樓層
樓主
ID:190577 發(fā)表于 2019-10-12 12:15 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式


    #define _XTAL_FREQ 4000000 //晶振定義
    #include<pic.h> //頭文件包含
//__CONFIG(0x3b31);//芯片配置字定義
// CONFIG1
#pragma config FOSC = XT        // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)
    //宏定義
    #define uchar unsigned char
    #define uint unsigned int
    #define RS RC0 //命令數(shù)據(jù)選擇
    #define RW RC1 //讀寫選擇
    #define EN RC2 //使能
    uint count=0;
    uchar miao,fen,shi;
    /*******************延時函數(shù)部分******************
    大約延時1ms
    ************************************************/
    void delayms(uint ms)
    {
        uchar i,j;
        for(j=ms;j>0;j--)
        for(i=110;i>0;i--);
    }
   
    /*******************1602發(fā)送主函數(shù)******************
    ************************************************/
    void LCD_WRITE(uchar data,uchar com)
    {
        RS=com;//1表示數(shù)據(jù);0表示命令
        PORTD=data;//將數(shù)據(jù)寫入
        delayms(5);//延時
        EN=1;//使能
        delayms(5);//延時
        EN=0;//關閉
    }
    /*******************1602顯示主函數(shù)******************
    //指定位置顯示字符
    //輸入:列地址X(0~15),行地址y(0~1)
    // 字符串指針*p,要顯示的字符個數(shù)num
    ************************************************/
    void disp_1602(uchar x,uchar y,uchar *p,uchar num)
    {
        uchar i;
        for(i=0;i<num;i++)
        {
        if(0==y)
        x|=0x80;//第一行地址為0x80
        else
        x|=0xc0;//第二行地址為0xC0
        LCD_WRITE(x,0); //寫地址命令
        LCD_WRITE(*p,1);//寫數(shù)據(jù)
        x++;//地址加
        p++;//顯示字符地址加
    }
    }
    /*******************初始化函數(shù)部分******************
    ************************************************/
    void LCD_init(void)
    {
        // ADCON1=0x07;//定義RE為數(shù)據(jù)口
         TRISC0=0;
         TRISC1=0;
         TRISC2=0;//RE口為輸出
         RC0=0;
         RC1=0;
         RC2=0;//初始化輸出低電平
         TRISD=0;//D口為輸出
         PORTD=0;//輸出0
         LCD_WRITE(0x38,0);//設置8位總線,雙行顯示
         LCD_WRITE(0x01,0);//清屏
         LCD_WRITE(0x0c,0);//開顯示,關光標
         LCD_WRITE(0x06,0);//光標右移
         LCD_WRITE(0x80,0);//第一行顯示位置
    }
    /*******************主程序函數(shù)******************
************************************************/
void main(void)
{
LCD_init();//端口初始化
    T1CKPS0=1;         
    T1CKPS1=1;                //前置分頻器8分頻
    TMR1CS=0;                 //TMR1工作于定時器方式
    TMR1L=(65536-12500)%256;  //定時12500個時鐘
    TMR1H=(65536-12500)/256;
    GIE=1;     //總中斷允許
    PEIE=1;    //外圍功能模塊中斷允許
    TMR1IE=1;  //TMR1中斷允許
    TMR1ON=1;  //啟動TMR1
while(1)
{
            disp_1602(0,0,"Time:",5);//第一行顯示temp:
            delayms(20);
            LCD_WRITE(0x80+5,0); //寫地址命令
            LCD_WRITE(shi/10+0x30,1);//寫數(shù)據(jù)
            LCD_WRITE(shi%10+0x30,1);//寫數(shù)據(jù)
            LCD_WRITE(':',1);//寫數(shù)據(jù)
            LCD_WRITE(fen/10+0x30,1);//寫數(shù)據(jù)
            LCD_WRITE(fen%10+0x30,1);//寫數(shù)據(jù)
            LCD_WRITE(':',1);//寫數(shù)據(jù)
            LCD_WRITE(miao/10+0x30,1);//寫數(shù)據(jù)
            LCD_WRITE(miao%10+0x30,1);//寫數(shù)據(jù)      
}
}
//*********************中斷服務程序******************
void interrupt isr(void)
{
  if(TMR1IE&&TMR1IF)      //判斷是否為TMR1中斷
  {
    TMR1L=(65536-12500)%256;
    TMR1H=(65536-12500)/256;
   
    TMR1IF=0;             //清TMR1中斷標志位(必須用軟件清零)  
    count++;
    if(count==10)
    {
        count=0;
        miao++;
        if(miao==60)
        {
            miao=0;
            fen++;
            if(fen==60)
            {
                fen=0;
                shi++;
                if(shi==24)
                    shi=0;
            }
        }
    }
  }
}


評分

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

查看全部評分

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

使用道具 舉報

沙發(fā)
ID:190577 發(fā)表于 2020-2-7 10:15 | 只看該作者
希望幫助需要的人
回復

使用道具 舉報

板凳
ID:106272 發(fā)表于 2020-2-7 11:33 | 只看該作者
謝謝了,初學者,學習一下
回復

使用道具 舉報

地板
ID:869854 發(fā)表于 2020-12-28 22:27 | 只看該作者
謝謝樓主,小萌新慢慢看
回復

使用道具 舉報

5#
ID:870114 發(fā)表于 2020-12-29 11:49 | 只看該作者
謝謝樓主無私的分享,小萌新好好看好好學
回復

使用道具 舉報

6#
ID:190577 發(fā)表于 2021-8-22 20:06 | 只看該作者
小菜雞..... 發(fā)表于 2020-12-28 22:27
謝謝樓主,小萌新慢慢看

客氣了,技術在于交流
回復

使用道具 舉報

7#
ID:190577 發(fā)表于 2021-8-22 20:07 | 只看該作者
3310mad2 發(fā)表于 2020-2-7 11:33
謝謝了,初學者,學習一下

希望幫助需要的人
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表