📄 t0.c
字号:
//
// File Name: t0.c
//
// Title: Internal timer 0 implementation
//
// Description:
//
// Creation Date: 2/6/00 5:31:06 PM
//
// By: A.C. Verbeck
//
// This file is subject to the terms and conditions of the GNU General Public
// License. See the file COPYING in the main directory of this archive
// for more details.
//
#include <io8515v.h>
#include <stdio.h>
#include <macros.h>
#include "Basetype.h"
#include "t0.h"
#pragma interrupt_handler t0_isr:8;
//
// Local defines
//
#define MAX_CT 0xFF // Max count that can be assigned
#define _1MS 0x3f // 1.008mS delay time
#define _2MS 0x7D // 2mS delay time (exact)
#define _4MS 0xFA // 4mS delay time (exact)
//
// Local data
//
UInt16 timer_ct = 0; // Current timer count
UInt16 timer_set = 0; // Timer time out value
//
// Local functions
//
static void t0_isr(void);
//
// Function Name:
// T0_init
//
// Description:
// Initialize internal timer 0.
// - set divider to ck/64
// - set count to 63
// - this gives a 1.008mS interrupt interval (992Hz)
//
// Parameters:
// (none)
//
// Returns:
// (nothing)
//
void T0_init(void)
{
CLI(); // Interrupts OFF
TCCR0 = 0x03; // Set divider to ck/64
TCNT0 = (MAX_CT-_1MS); // Set the time out
TIMSK |= 0x02; // Enable the interrupt
timer_ct = 0; // Init the soft timer
SEI(); // Interrupts ON
}
void T0_clear(void)
{
timer_ct = 0;
}
//
// Function Name:
// T0_set
//
// Description:
// Set the next timeout for the internal timer 0.
// Note that this code works even if the timer
// "rolls over".
//
// Parameters:
// UInt16 time_out - the wakeup time
//
// Returns:
// (nothing)
//
void T0_set(UInt16 time_out)
{
CLI(); // Interrupts OFF
timer_ct = 0; // Clear the timer count to 0
timer_set = time_out; // Set the new timeout
SEI(); // Interrupts ON
}
UInt16 T0_get(void)
{
return timer_ct;
}
void T0_enable(void)
{
TIMSK |= 0x02; // Enable the interrupt
}
void T0_disable(void)
{
TIMSK &= ~0x02; // Disable the interrupt
}
void T0_sleep(UInt16 time_out)
{
CLI(); // Interrupts OFF
timer_ct = 0; // Clear the timer count to 0
timer_set = time_out; // Set the new timeout
SEI(); // Interrupts ON
while (timer_ct != timer_set); // Just wait here
}
static void t0_isr(void)
{
++timer_ct; // Boost the software timer
TCNT0 = (MAX_CT-_1MS); // Set the time out
}
//
// End of: t0.c
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -