您当前的位置:五五电子网电子知识单片机-工控设备PIC单片机用PIC16F877的c语言写的一个时闹钟程序(转贴) 正文
用PIC16F877的c语言写的一个时闹钟程序(转贴)

用PIC16F877的c语言写的一个时闹钟程序(转贴)

点击数:7643 次   录入时间:03-04 11:39:06   整理:http://www.55dianzi.com   PIC单片机
        }
    }
    else if (adjust_item == 6)    //    set on/off
    {
        if ((KeyNew == key_up) | (KeyNew == key_down))
        {
            if (Alarm2Enable)
            {
                Alarm2Enable =false;    //    disable alarm2
                lcd_write_data('f');
                lcd_write_data('f');
            }
            else
            {
                Alarm2Enable =true;        //    enable alarm2
                lcd_write_data('n');
                lcd_write_data(' ');
            }
            //lcd_write_inst(time_secondl_addr+3);
            adjust_item = 0;
            lcd_write_inst(time_hourh_addr);
            Alarm2Cnt = 0;
        }
        else if(KeyNew == key_left)
        {
            adjust_item --;
            lcd_write_inst(time_secondl_addr);
        }
        else if(KeyNew == key_right)
        {
            adjust_item = 0;
            lcd_write_inst(time_hourh_addr);
        }
    }

}

//    -------------------------------------------------------
void main(void)
{
    unsigned char cnt;
    TRISC = 0x03;    //    PORTC.3 drive led,low active
    PORTC_MAP = 0x00;
    led = 0;
    BackLightEn = 0;
    BackLightTimer = 0;
    PORTC = PORTC_MAP;
    TRISA = 0xff;    //    low half byte as keyscan in
    TRISE = 0x00;
    ADCON0 = 0x00;
    ADCON1 = 0x06;    //    all digital I/Os
    lcd_init();
    INTCON = 0x00;
    lcd_write_inst(0x80);    //    set lcd ddram address
    for (pStr = StrPower1;*pStr!=0;pStr++)
    {
        lcd_write_data(*pStr);
    }
    lcd_write_inst(0x0c);        //    LCD cursor off
    PIR1 = PIR2 = 0x00;
    T1CON = 0x0f;    //    T1CON:    -- T1CKPS1 T1CPS0 T1OSCEN /T1SYNC TMR1CS TMR1ON
    TMR1H = 0x80;
    TMR1L = 0x00;
    TMR1IF = 0;
    CCPR2H = 0x7f;
    CCPR2L = 0xff;
    CCP2CON = 0x0b;    //    compare mode,set ccp2if,reset tmr1
    CCP2IF = 0;
    for (cnt=5;cnt>0;cnt--)    //    test for 5sec.
    {
        lcd_write_inst(0xc0+8);    //    set LCD adress
        lcd_write_data(cnt + '0');
        while (CCP2IF == 0)    //    wait for timer1 overflow
        {
            restart_wdt();
        }
        //TMR1H = 0x80;
        CCP2IF = 0;
    }
    //    write current date to lcd line 2
    lcd_write_inst(0x01);        //    clear LCD display
    lcd_write_inst(0xc0+3);        //    set LCD line2
    lcd_write_data(CurrentDate.year1 + '0');
    lcd_write_data(CurrentDate.year2 + '0');
    lcd_write_data(CurrentDate.year3 + '0');
    lcd_write_data(CurrentDate.year4 + '0');
    lcd_write_data('/');
    lcd_write_data(CurrentDate.monthh + '0');
    lcd_write_data(CurrentDate.monthl + '0');
    lcd_write_data('/');
    lcd_write_data(CurrentDate.dayh + '0');
    lcd_write_data(CurrentDate.dayl + '0');
    Alarm1Enable = true;    //false;
    Alarm2Enable = true;    //false;
    INTCON = 0xc0;    //    set GIE & PEIE
    //TMR1IE = 1;        //    enable timer1 interrupt
    CCP2IE = 1;            //    enable ccp2 interrupt
    CurrentMode = mode_time;
    Alarm1Cnt = Alarm2Cnt = 0;
    T2CON = 0x07;    //    T2CON: - TOUTPS3 TOUTPS2 TOUTPS1 TOUTPS0 TMR2ON T2CKPS1 T2CKPS0
                    //    timer2 enable,prescaler = 16
    PR2 = PWM_period;    //    set pwm period
    CCPR1L = PWM_DC;    //    set pwm duty cycle
    CCP1CON = PWM_off;
    while(1)
    {
        restart_wdt();
        KeyOld = get_key();
        delay_ms(20);
        KeyNew = get_key();
        if (((KeyNew & 0x0f) != 0x00) & (KeyNew == KeyOld))
        {                            //    some key pressed
            if (BackLightEn == 1)    //    back light not on
            {
                BackLightEn = 0;        //    enable back-light
                BackLightTimer = 0;        //    start delay counter
            }
            else
            {
                if (KeyNew == key_0)
                    KeyNew = 0;
                else if(KeyNew == key_1)

上一页  [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]  下一页


本文关键字:程序  C语言  PIC单片机单片机-工控设备 - PIC单片机