📄 四位数码管显示.c
字号:
//ICC-AVR application builder : 2009-3-10 14:16:46
// 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-PC3
2-PC2
3-PC1
4-PC0
使用show_4_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_4_digit(unsigned char *p)
{
unsigned char i,j;
DDRB=0xff;
DDRC=0xff;
for (j=0;j<200;j++)
{
for (i=0;i<4;i++)
{
PORTC=0;//先关显示
PORTB=~num[*(p+3-i)];//送数据
PORTC=(0x1<<i);//开显示
delay_us(200);//小延时
}
}
}
void main()
{
unsigned char p[4]={1,2,3,4};
port_init();
init_devices();
while(1)
{
show_4_digit(p);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -