📄 timer0.c
字号:
//ICC-AVR application builder : 2007-12-14 下午 03:35:52
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
#include<slavr.h>
unsigned char hour=0;
unsigned char minute=0;
unsigned char second=0;
unsigned char counter=0;
//*******************发送字节*******************
void send_byte( unsigned char out_byte)
{
unsigned char i;
for (i=0;i<8;i++)
{
if (out_byte&0x80)
{
PORTB|=BIT(PB1); //置1data
}
else
{
PORTB&=~BIT(PB1);//清零
}
PORTB|=BIT(PB2);//置1
out_byte<<=1;
delay_us(30);//20//32//30
PORTB&=~BIT(PB2); //清零
delay_us(30);//32//30
}
PORTB&=~BIT(PB1);//清零
//delay_us(50);
//PORTB|=BIT(PB3);//清零cs1
}
//*******************写入7279*******************
//**********************************************
void write7279(unsigned char cmd, char dta ) //写入7279
{
CLI();
delay_us(62);//50//55//62
send_byte (cmd);
delay_us(33);//20//28//33
send_byte (dta);
SEI();
}
void display(void)
{
write7279(0x83,second%10);//
write7279(0x82,second/10);
write7279(0x81,minute%10);
write7279(0x80,minute/10);
write7279(0x85,hour%10);
write7279(0x84,hour/10);
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x06;
DDRB = 0x0E;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.984mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xB2; //set count
OCR0 = 0x4E; //set compare
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0xB2; //reload counter value
if (counter<10) counter++;
else {counter=0 ;
if (second<60) second++;
else {second=0 ;
if (minute<60) minute++;
else {minute=0;
if (hour<24) hour++;
else hour=0;
}
}
}
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main(void)
{
init_devices();
for(;;)
display();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -