///////////////////////////main.c
//包括所需头文件
#include
#include
#include “disp.h”
#define uint unsigned int
#define uchar unsigned char
#define IR_read (PIND&BIT(2))
const uchar shu[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,
0x82,0xF8,0x80,0x90};
uchar yd,xh,yhm;
uchar buf[4]={0,0,0,0};
void delay_100us( uchar shi )
{
uchar i,j;
for(i=0;i for(j=0;j<105;j++);
}
void delay()
{
uint i,j;
for(i=700;i>0;i–)
for(j=700;j>0;j–);
}
//端口初始化
void port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF;
PORTD = 0x00;
DDRD = 0x00;
}
//外中止初始化
void int_init(void)
{
MCUCR |= 0x02;
MCUCSR|= 0x00;
GICR |= 0x40;
}
//外中止0服务程序
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
CLI();
for(yd=0;yd<10;yd++) //判别9ms引导码
{
delay_100us(8); //延时860us
if(IR_read!=0)
{
SEI();
return ;
}
}
delay_100us(44); //延时4.76ms避开4.5毫秒的成果码
for(xh=0;xh<4;xh++)
{
for(yd=0;yd<8;yd++)
{
while(!IR_read);
delay_100us(8); //延时860us
if(IR_read)
{
yhm|=0x80;
delay_100us(10); //980us
}
yhm>>=1;
}
buf[xh]=yhm;
}
delay(); //削减搅扰
SEI();
}
void init_devices(void)
{
CLI(); //制止一切中止
MCUCR = 0x00;
MCUCSR = 0x80;//制止JTAG
GICR = 0x00;
port_init();
int_init();
SEI();//开大局中止
}
main()
{
uchar ii;
buf[3]=0xff;
init_devices();
while(1)
{
ii=buf[3];
display(2,shu[ii/100]);
display(1,shu[ii%100/10]);
display(0,shu[ii%10]);
}
}
/////disp.h
#define uchar unsigned char
#define uint unsigned int
#define SHCP_0 PORTA&=~BIT(1)
#define SHCP_1 PORTA|=BIT(1)
#define DS_0 PORTA&=~BIT(3)
#define DS_1 PORTA|=BIT(3)
#define STCP_0 PORTA&=~BIT(2)
#define STCP_1 PORTA|=BIT(2)
void CKin()
{
SHCP_0;
NOP();
SHCP_1;
}
void Dataout() //并行输出
{
STCP_0;
NOP();
STCP_1;
}
void Datein( uchar date ) //数据串行输入
{
uchar i,mod;
DDRA=0xff;
for(i=0;i<8;i++)
{
mod=date&0x80;
if(mod==0x80)
{DS_1;}
else
{DS_0;}
CKin();
date<<=1;
}
Dataout();//并行输出
}
void weihao(uchar add)
{
DDRA=0xff;
switch(add)
{
case 0:PORTA=0x1f;break;
case 1:PORTA=0x1f|0x80;break;
case 2:PORTA=0x1f|0x40;break;
case 3:PORTA=0x1f|0xc0;break;
case 4:PORTA=0x1f|0x20;break;
case 5:PORTA=0x1f|0xA0;break;
case 6:PORTA=0x1f|0x60;break;
case 7:PORTA=0x1f|0xE0;break;
default:break;
}
}
void DELAY(uint tt)
{
uint mm;
while(tt–)
{
for(mm=30;mm>0;mm–);
}
}
void display(uchar wei,uchar data)
{
weihao(wei);
Datein(data);
DELAY(20);
weihao(wei);
Datein(0xff);
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/261153.html