//----------------60秒定时器实验之 C 程序--------------
//------------------------------------------------------------------------
//发行公司: 北京竟择信息技术有限公司
//作者: 机器人创作室
//Email: robot@jingze.cn
//软件版本: ICCAVR ver 7.05
// Target : M48
// <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Crystal: 8.0000Mhz <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
#include <iom48v.h>
#include <macros.h>
#pragma interrupt_handler timer0_handler: iv_TIMER0_OVF
#pragma interrupt_handler timer1_handler: iv_TIMER1_OVF
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0x03;
//Timer0
TCCR0B = 0x00; //stop
TCNT0 = 0x06; //set count
TCCR0A = 0x00;
TCCR0B = 0x04; //start timer
//Timer1
TCCR1B = 0x00; //stop
TCNT1H = 0x85; //setup
TCNT1L = 0xEE;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
SEI(); //re-enable interrupts
}
//定义字型数组
#pragma data:code
const unsigned char tabs[]=
{
0xC0,//"0"
0xF9,//"1"
0xA4,//"2"
0xB0,//"3"
0x99,//"4"
0x92,//"5"
0x82,//"6"
0xF8,//"7"
0x80,//"8"
0x98,//"9"
};
#pragma data:data
static unsigned char ledbuf[2];
unsigned char _led;
unsigned char _change;
unsigned char _second;
void main(void)
{
unsigned char digits = 0;
init_devices();
_second = 0;
while (1)
{
if (_change > 0)
{
_change = 0; //更新显示
digits = _second / 10; //取十位数
ledbuf[0] = tabs[digits];
digits = _second % 10; //取个位数
ledbuf[1] = tabs[digits];
}
}
}
void timer0_handler()
{
TCNT0 = 0x06; //set count
PORTB = ledbuf[_led]; //显示字型送PortB
if (_led == 0)
{
PORTC = 1; //PortC控制显示
_led = 1;
}
else
{
PORTC = 2; //PortC控制显示
_led = 0;
}
}
void timer1_handler()
{
TCNT1H = 0x85; //reload counter high value
TCNT1L = 0xEE; //reload counter low value
_change = 1; //定时变化标记
if (_second < 59)
_second += 1;
else
_second = 0;
文章评论(0条评论)
登录后参与讨论