另外定时器计数器1是16位的 建议换成0或者2足矣 另外 最好别用PC端口 PC是烧flash的 用它易出问题
我给你写的,主循环中没用循环语句,因为只显示2位没必要。
//省略宏
uint pp = 0
int time_1s = 0, time_8ms, time
ISR(TIMER2_OVF_vect) //Time2 溢出中断
{
TCNT2 = 0x83
time_8ms = 1
if(time_8ms == 1)
{
timexs_8ms = 0
if(++time >= 125)
{
time_1s = 0
++pp
}
}
}
void Init_T2()
{
TCCR2 = 0x04 //1M晶振 64分频 普通模式 1M/64=15.625K
TCNT2 = 0x83 // (256-130-1)/15.625K= 8ms
TIMSK |= 0x40
}
int main()
{
uchar num,num_1,num_10
DDRA=0xFF
DDRB=0x02
PORTA = 0xff
PORTB = 0xff
Init_T2()
asm("sei")
while(1)
{
num = pp
num_1 = num % 10
num_10 = num / 10
if(num >60)num = 0
else {
PORTA = table[num_1]
PORTB = 0xfe
PORTA = table[num_10]
PORTB = 0xfd
_delay_ms(5)
}
}
//ICC-AVR application builder : 2016-1-4 上午 11:32:30// Target : M16
// Crystal: 12.000Mhz
#include <iom16v.h>
#include <macros.h>
//利用T2实现2ms定时,而后计时500次就是1S
unsigned int dly_ms,timer_1s
void port_init(void)
{
PORTA = 0x00
DDRA = 0x00
PORTB = 0x00
DDRB = 0x00
PORTC = 0x00//m103 output only
DDRC = 0x00
PORTD = 0x00
DDRD = 0x00
}
//TIMER2 initialize - prescale:128
// WGM: Normal
// desired value: 2mSec
// actual value: 1.995mSec (0.3%)
void timer2_init(void)
{
TCCR2 = 0x00//stop
ASSR = 0x00//set async mode
TCNT2 = 0x45//setup
OCR2 = 0xBB
TCCR2 = 0x05//start
}
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
TCNT2 = 0x45//reload counter value
dly_ms++//2MS定时
if(dly_ms>=500) //定时2ms*500=1000ms=1S
{
dly_ms=0//500次复位后继续冲0-500
timer_1s++//1S往上加吧!!!!
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI()//disable all interrupts
port_init()
timer2_init()
MCUCR = 0x00
GICR = 0x00
TIMSK = 0x40//timer interrupt sources
SEI()//re-enable interrupts
//all peripherals are now initialized
}
main()
{
init_devices()//设备初始化
while(1)//作死的循环吧
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)