本帖最后由 2287779862 于 2012-12-22 15:51 编辑
// 中断代码
#pragma code ToIntFunc = 0x008
void ToIntFunc( void )
{
_asm
goto IntFunc
_endasm
}
#pragma code
#pragma interrupt IntFunc
void IntFunc( void )
{
if ( INTCONbits.GIE == 1 )
{
if ( INTCONbits.TMR0IE == 1 )
{
if ( INTCONbits.TMR0IF == 1 )
{
INTCONbits.TMR0IF = 0;
sec++;
if ( sec > 60 )
{
sec = 0;
min++;
}
if ( min > 60 )
{
hour++;
min = 0;
}
if ( hour > 24 )
{
hour = 0;
}
}
}
if ( PIE1bits.TMR2IE == 1 )
{
if ( PIR1bits.TMR2IF == 1 )
{
PIR1bits.TMR2IF = 0;
sec++;
if ( sec > 60 )
{
sec = 0;
min++;
}
if ( min > 60 )
{
hour++;
min = 0;
}
if ( hour > 24 )
{
hour = 0;
}
}
}
}
}
// 测试代码
void InitTmr2( void )
{
PIR1bits.TMR2IF = 0;
PIE1bits.TMR2IE = 1;
OpenTimer2( T2_POST_1_9&T2_PS_1_4&0b01111011 );
}
void main(void)
{
unsigned char key, _key;
hour = min = sec = 0;
init( ); // 初始化其他引脚
init_lcd( ); // lcd初始化
InitTmr2( ); // TMR2中断引脚初始化
T2CONbits.TMR2ON = 1; // 开启计时器TMR2
Delay1KTCYx( 10 );
send_lcd( 0, 1 ); // lcd清屏
Delay1KTCYx( 4 );
WriteTimer0( 0 ); //
_key = 0;
while ( 1 )
{
key = scan_key( ); // 按键搜索,没有按键反应返回0,取值返回[0,6]
//display(129,"00:00:00");
ShowInfo( );/*
if ( PIR1bits.TMR2IF )
{
PIR1bits.TMR2IF = 0;
sec++;
if ( sec > 60 )
{
sec = 0;
min++;
}
if ( min > 60 )
{
hour++;
min = 0;
}
if ( hour > 24 )
{
hour = 0;
}
}*/
if ( key == _key )
continue;
if ( key == 2 )
{
hour = 0;
min = 0;
sec = 0;
}
else if ( key == 3 )
{
hour = 0;
}
else if ( key == 4 )
{
min = 0;
}
else if ( key == 5 )
{
sec = 0;
}
else if ( key == 6 )
{
T0CONbits.TMR0ON = ~T0CONbits.TMR0ON;
//if ( T0CONbits.TMR0ON )
//{
// WriteTimer0( 0 );
//}
}
_key = key;
}
}
// 代码很简单,时:分:秒,显示这个玩意 |