秒表

动图:

这段代码是用C语言编写的,用于在基于德州仪器MSP430微控制器的平台上实现一个简易的电子秒表功能。
cpp展开代码#include <msp430.h>
#include "LCD.h"
unsigned int    second      = 0;
unsigned int    millisecond100  = 0;
char        Stopwatch_open  = 0;
int main( void )
{
    unsigned char display_str[15], str_count;
    WDTCTL = WDTPW | WDTHOLD;       /* stop watchdog timer */
    if ( CALBC1_1MHZ == 0xFF )      /* If calibration constant erased */
    {
        while ( 1 )
            ;               /* do not load, trap CPU!! */
    }
    DCOCTL  = 0;                    /* Select lowest DCOx and MODx settings */
    BCSCTL1 = CALBC1_1MHZ;          /* Set range */
    DCOCTL  = CALDCO_1MHZ;          /* Set DCO step + modulation * / */
    Port_init();                    /* lcd */
    LCD_init();                     /* lcd */
    LCD_write_str( 0, 0, "Stopwatch" );
    _EINT();
    while ( 1 )
    {
        /*按键输入 */
        P1DIR   &= ~BIT3;
        P1SEL   &= ~BIT3;
        P1REN   |= BIT3;
        P1OUT   |= BIT3;
        if ( !(P1IN & BIT3) )
        {
            while ( !(P1IN & BIT3) )
                ;
            Stopwatch_open = !Stopwatch_open;      /* 开始或者暂停 */
        }
        delay_ms( 10 );
        str_count           = 0;
        display_str[str_count++]    = (second / 60) % 100 / 10 + '0';
        display_str[str_count++]    = (second / 60) % 10 + '0';
        display_str[str_count++]    = ':';
        display_str[str_count++]    = (second % 60) % 100 / 10 + '0';
        display_str[str_count++]    = (second % 60) % 10 + '0';
        display_str[str_count++]    = '.';
        display_str[str_count++]    = (millisecond100) % 10 + '0';
        display_str[str_count++]    = 0;
        Port_init(); /* lcd */
        LCD_write_str( 0, 1, display_str );
    }
}
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A( void )
{
    static char num2 = 0;
    if ( Stopwatch_open )
    {
        num2 = (num2 + 1) % 10;
        if ( num2 == 9 )
        {
            millisecond100++;
            if ( millisecond100 == 10 )
            {
                millisecond100 = 0;
                second++;
            }
        }
    }
}

cpp展开代码https://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2


本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!