⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 t2_timer.c

📁 intel 196nt 例程
💻 C
字号:
/*
** t2_timer.c
**      Connect timer1 and timer2 together to make a
**      32 bit UP counter, and display some bits of this
**      counter on the LEDs.
*/

#include <stdio.h>

/* IMPORT functions */

/* LOCAL functions */
static	void		enable_led( void );
static	void		set_led( unsigned char );
static	unsigned char	get_t1control( void );
static	void		set_t1control( unsigned char );
static	unsigned int	get_timer1( void );
static	unsigned char	get_t2control( void );
static	void		set_t2control( unsigned char );
static	unsigned int	get_timer2( void );

void main(void)
{
    unsigned int	i;
    int			j;

    enable_led();
    /*
    ** Timer control bits:
    **  timer1:
    **   11000010
    **   ||||||||
    **   |||||+++-- 010= divide by 4
    **   |||||
    **   ||+++----- 000= take XTAL/4 clock,
    **   ||                use UD-bit for Up or Down
    **   |+-------- 1  = UD-bit: Up counting
    **   +--------- 1  = enable
    **  timer2:
    **   11110000
    **   ||||||||
    **   |||||+++-- 000= divide by 1
    **   |||||
    **   ||+++----- 110= take timer 1 overflow as clock,
    **   ||                Up or Down same as timer 1
    **   |+-------- 1  = UD-bit: Up counting (ignored in this case)
    **   +--------- 1  = enable
    */
    set_t2control( 0xF0 );
    set_t1control( 0xC2 );

    /* Wait for timer 1 ms-bit set */
    while ( ((i = get_timer1()) & 0x8000) )
	;

    /* Wait for timer 1 ms-bit clear */
    while ( !((i = get_timer1()) & 0x8000) )
	;

    /* Wait for timer 1 ms-bit set */
    while ( ((i = get_timer1()) & 0x8000) )
	;

    /* Show some bits of timer2 */
    while ( !((i = get_timer2()) & 0x8000) )
	set_led( i>>3 );
}

#define	P1_MODEnw	0x1FD0
#define	P1_DIRnw	0x1FD2
#define	P1_REGnw	0x1FD4
#define	P1_PINnw	0x1FD6
static void enable_led( void )
{
    asm stb	0,P1_DIRnw[0];
}

static void set_led( register unsigned char b )
{
    asm stb	b,P1_REGnw[0];
}

#define	T1CONTROLnw	0x1F98
#define	TIMER1nw	0x1F9A
#define	T2CONTROLnw	0x1F9C
#define	TIMER2nw	0x1F9E
static unsigned char get_t1control( void )
{
    register unsigned char	b;

    asm ldb	b,T1CONTROLnw[0];
    return b;
}

static void set_t1control( register unsigned char b )
{
    asm stb	b,T1CONTROLnw[0];
}

static unsigned int get_timer1( void )
{
    register unsigned int	w;

    asm ld	w,TIMER1nw[0];
    return w;
}

static unsigned char get_t2control( void )
{
    register unsigned char	b;

    asm ldb	b,T2CONTROLnw[0];
    return b;
}

static void set_t2control( register unsigned char b )
{
    asm stb	b,T2CONTROLnw[0];
}

static unsigned int get_timer2( void )
{
    register unsigned int	w;

    asm ld	w,TIMER2nw[0];
    return w;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -