📄 数字钟._c
字号:
//ICC-AVR application builder : 2009-3-11 12:47:48
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/************************************
用 途:微秒级延时程序
Taget :mega8
crystal :8M
介 绍:在8M的晶振上进行us级的延时
入口参数:
*************************************/
void delay_us(int time)
{
do
{
time--;
}
while (time > 1);
}
/************************************
用 途:六位数码管的显示程序
Taget :mega8
crystal :8M
介 绍:数码管共阳
a-PB0
b-PB1
...
h-PB6
DP-PB7
-------
1-PC5
2-PC4
3-PC3
4-PC2
5-PC1
6-PC0
使用show_6_digit(p)
unsigned char数组
B用作数据口
C用作片选
入口参数:显示数组
*************************************/
const unsigned char num[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show_6_digit(unsigned char *p)
{
unsigned char i,j;
DDRB=0xff;
DDRC=0xff;
for (j=0;j<200;j++)
{
for (i=0;i<6;i++)
{
PORTC=0;//先关显示
PORTB=~num[*(p+5-i)];//送数据
PORTC=(0x1<<i);//开显示
delay_us(200);//小延时
}
}
}
/************************************
用 途:用TC0做一个时钟
Taget :mega8
crystal :8M
介 绍:8M晶振
入口参数:
出口参数:
*************************************/
unsigned char CountNum;//用来记录中断,第一次中断是1/500S
unsigned char Count;//用来做1/100S
unsigned char Time[3]={12,0,0};//表示时间,时分秒
//初始化TC0
void init_timer0()
{
TCCR0=0x00;//TCCR0工作于定时方式,先停止TC0
TCNT0=0x06;//计数器初始值
TIMSK|=(1<<TOIE0);//开放TOIE0中断
TCCR0=(1<<CS01)|(1<<CS00);//打开TC0用64分频
}
//TC0中断函数
#pragma interrupt_handler timer0_fun:iv_TIMER0_OVF
void timer0_fun()
{
TCNT0=0x06;//重装初值
if (++CountNum==5)
{
CountNum=0;
Count++;
}
if (Count==100)//Count=100就是一秒到时了
{
Count=0;
Time[2]++;
}
if (Time[2]==60)
{
Time[2]=0;
Time[1]++;
}
if (Time[1]==60)
{
Time[1]=0;
Time[0]++;
}
if (Time[0]==24)
{
Time[0]=0;
}
}
//************************************************
unsigned char time6[6];
/************************************
用 途:时间的转换
Taget :mega8
crystal :8M
介 绍:
入口参数:
出口参数:
*************************************/
void time_convert()
{
time6[0]=Time[0]/10;
time6[1]=Time[0]%10;
time6[2]=Time[1]/10;
time6[3]=Time[1]%10;
time6[4]=Time[2]/10;
time6[5]=Time[2]%10;
}
void main()
{
port_init();
init_devices();
init_timer0();
while(1)
{
time_convert();
show_6_digit(time6);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -