//ICC-AVR application builder : 2011/10/14 14:12:17
// Target : M32
// Crystal: 8.0000Mhz
#include <iom32v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#define CLR 0
#define SET 1
/*存储寄存器*/
#define stcp PC6
#define SET_stcp (PORTC) |=(1<<stcp)
#define CLR_stcp (PORTC) &=~(1<<stcp)
#define stcp_IN (PINC) & (1<<stcp)
#define SET_OUT_stcp (DDRC) |=(1<<stcp)
#define SET_IN_stcp (DDRC) &=~(1<<stcp)
/*移位寄存器*/
#define shcp PC7
#define SET_shcp (PORTC) |=(1<<shcp)
#define CLR_shcp (PORTC) &=~(1<<shcp)
#define shcp_IN (PINC) & (1<<shcp)
#define SET_OUT_shcp (DDRC) |=(1<<shcp)
#define SET_IN_shcp (DDRC) &=~(1<<shcp)
/*数据串入*/
#define ds PC5
#define SET_ds (PORTC) |=(1<<ds)
#define CLR_ds (PORTC) &=~(1<<ds)
#define ds_IN (PINC) & (1<<ds)
#define SET_OUT_ds (DDRC) |=(1<<ds)
#define SET_IN_ds (DDRC) &=~(1<<ds)
#define IR_read (PIND&BIT(2))
uchar num;
uchar KeyValue;
uchar table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};
uchar buf[4]={0,0,0,0};
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
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;
MCUCSR=0x80;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
}
/*8MHz下的毫秒级延时*/
void Delay_ms(unsigned int time)
{
unsigned char n;
while(time>0)
{
for(n=1;n<1140;n++)
{
NOP();
NOP();
}
time--;
}
}
void Delay_100us(uchar shi)
{
uchar i,j;
for(i=0;i<shi;i++)
for(j=0;j<105;j++);
}
void IR_SystemInit(void)
{
DDRD=0X00;
PORTD=0x00;
MCUCR|=0X02; //下降沿触发
MCUCSR|=0X00;
GICR|=0X40; //INT0中断使能
SREG=0X80; //使能总中断
}
/*外部中断0处理函数*/
#pragma interrupt_handler int0_isr:iv_INT0
void int0_isr(void)
{
unsigned char n;
uchar i,j,temp=0;
CLI();
for(n=0;n<10;n++) //检测9ms开始码 ,9ms的引导码
{
Delay_100us(8);
if(IR_read!=0)
{
SEI();
return;
}
}
Delay_100us(44);
for(i=0;i<4;i++)
{
for(j=0;j<8;j++)
{
while(!IR_read);
Delay_100us(8);
if(IR_read)
{
temp|=0x80;
Delay_100us(10);
}
temp>>=1;
}
buf
=temp;
}
buf[3]=0xa1;
KeyValue=buf[3];
Delay_ms(1000);
SEI();
}
unsigned char IR_getKey()
{
if(KeyValue == 0xf3) //KeyValue 键值码
return 1;
if(KeyValue == 0xe7)
return 2;
if(KeyValue == 0xa1)
return 3;
if(KeyValue == 0xf7)
return 4;
if(KeyValue == 0xe3)
return 5;
if(KeyValue == 0xa5)
return 6;
if(KeyValue == 0xbd)
return 7;
if(KeyValue == 0xad)
return 8;
if(KeyValue == 0xb5)
return 9;
if(KeyValue == 0xbb) //前后
return 11;
if(KeyValue == 0xbf)
return 12;
if(KeyValue == 0xbc) //退出
return 13;
}
//给数码管传递要显示的数据
void SendData(uchar send_data)
{
uchar i;
SET_OUT_stcp;
CLR_stcp;
for(i=0 ; i < 8 ; i++)
{
SET_OUT_shcp; //引脚设置为输入
CLR_shcp;
if( (send_data & 0x80) == 0)
{
SET_OUT_ds;
CLR_ds;
}
else
{
SET_OUT_ds;
SET_ds;
}
send_data<<=1;
SET_shcp;
}
SET_stcp;
}
void LED()
{
int a,temp=1;
int i;
DDRC=0Xff; //引脚设置为输入
PORTC=0XFF; //灯全灭
for(a=1;a<=4;a++)
{
PORTC=~temp;
Delay_ms(500);
temp<<=1;
}
}
void main()
{
int i;
init_devices();
IR_SystemInit();
DDRA=0X00; //数码管第一位选通
while(1)
{
/*DDRD=0X00;
PORTD=0X00;
MCUCR|=0X02; //下降沿触发
MCUCSR|=0X00;
GICR|=0X40; //INT0中断使能
SREG=0X80; //使能总中断
*/
num=IR_getKey();
SendData(table[num]);
Delay_ms(1000);
}
}
此帖出自小平头技术问答
一周热门 更多>