///////////////////////////main.c//包含所需頭文件
#include
#include
#include "disp.h"
本文引用地址:http://m.butianyuan.cn/article/201611/320297.htm#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服務(wù)程序
#pragma interrupt_handler int0_isr:2
void int0_isr(void)
{
CLI();
for(yd=0;yd<10;yd++) //判斷9ms引導(dǎo)碼
{
delay_100us(8); //延時860us
if(IR_read!=0)
{
SEI();
return ;
}
}
delay_100us(44); //延時4.76ms避開4.5毫秒的結(jié)果碼
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 ) //數(shù)據(jù)串行輸入
{
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);
}
評論