標(biāo)題: 剛學(xué)PIC單片機(jī),分享第一個(gè)電子時(shí)鐘,定時(shí)器計(jì)時(shí) [打印本頁(yè)]

作者: bbxyliyang    時(shí)間: 2019-10-12 12:15
標(biāo)題: 剛學(xué)PIC單片機(jī),分享第一個(gè)電子時(shí)鐘,定時(shí)器計(jì)時(shí)


    #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í)函數(shù)部分******************
    大約延時(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);//延時(shí)
        EN=1;//使能
        delayms(5);//延時(shí)
        EN=0;//關(guān)閉
    }
    /*******************1602顯示主函數(shù)******************
    //指定位置顯示字符
    //輸入:列地址X(0~15),行地址y(0~1)
    // 字符串指針*p,要顯示的字符個(gè)數(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);//設(shè)置8位總線,雙行顯示
         LCD_WRITE(0x01,0);//清屏
         LCD_WRITE(0x0c,0);//開顯示,關(guān)光標(biāo)
         LCD_WRITE(0x06,0);//光標(biāo)右移
         LCD_WRITE(0x80,0);//第一行顯示位置
    }
    /*******************主程序函數(shù)******************
************************************************/
void main(void)
{
LCD_init();//端口初始化
    T1CKPS0=1;         
    T1CKPS1=1;                //前置分頻器8分頻
    TMR1CS=0;                 //TMR1工作于定時(shí)器方式
    TMR1L=(65536-12500)%256;  //定時(shí)12500個(gè)時(shí)鐘
    TMR1H=(65536-12500)/256;
    GIE=1;     //總中斷允許
    PEIE=1;    //外圍功能模塊中斷允許
    TMR1IE=1;  //TMR1中斷允許
    TMR1ON=1;  //啟動(dòng)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ù)      
}
}
//*********************中斷服務(wù)程序******************
void interrupt isr(void)
{
  if(TMR1IE&&TMR1IF)      //判斷是否為TMR1中斷
  {
    TMR1L=(65536-12500)%256;
    TMR1H=(65536-12500)/256;
   
    TMR1IF=0;             //清TMR1中斷標(biāo)志位(必須用軟件清零)  
    count++;
    if(count==10)
    {
        count=0;
        miao++;
        if(miao==60)
        {
            miao=0;
            fen++;
            if(fen==60)
            {
                fen=0;
                shi++;
                if(shi==24)
                    shi=0;
            }
        }
    }
  }
}



作者: bbxyliyang    時(shí)間: 2020-2-7 10:15
希望幫助需要的人
作者: 3310mad2    時(shí)間: 2020-2-7 11:33
謝謝了,初學(xué)者,學(xué)習(xí)一下
作者: 小菜雞.....    時(shí)間: 2020-12-28 22:27
謝謝樓主,小萌新慢慢看
作者: 1275601423    時(shí)間: 2020-12-29 11:49
謝謝樓主無(wú)私的分享,小萌新好好看好好學(xué)
作者: bbxyliyang    時(shí)間: 2021-8-22 20:06
小菜雞..... 發(fā)表于 2020-12-28 22:27
謝謝樓主,小萌新慢慢看

客氣了,技術(shù)在于交流
作者: bbxyliyang    時(shí)間: 2021-8-22 20:07
3310mad2 發(fā)表于 2020-2-7 11:33
謝謝了,初學(xué)者,學(xué)習(xí)一下

希望幫助需要的人




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1