|
3楼
楼主 |
发表于 2004-12-2 17:27:51
|
只看该作者
我说的是不使用额外的芯片,直接用MAGE8模拟
我已经完成了。用试波器检测无误。
/******************************************************************
红外通讯函数(发送端)4M晶震
*******************************************************************/
# include <iom8.h>
# include <macros.h>
#pragma interrupt_handler timer1_vof:9 //定时器1中断
#pragma interrupt_handler timer0_ovf_isr:10 //定时器0中断
/**************全局变量定义****************/
unsigned int time_counter=0; //定时器
unsigned int time_counter_end=0; //终止时间
char press_keys=0; //按键代码
unsigned char temp_signals=0; //要发送的数据
unsigned char signals=16;
/**************全局变量定义****************/
/****************函数声明******************/
void timer1_int(void);
void timer0_init(void);
void port_int(void);
void delay(unsigned int times);
void timer1_vof(void);
void timer0_ovf_isr(void);
void send_signal(void);
void scan_key_press(void);
/****************函数声明******************/
/***************************************
定时器
****************************************/
void timer1_int(void)
{
TCCR1B = 0x00; //stop
//TCNT1H = 0xd4; //setup
//TCNT1L = 0xce;
TCNT1H=0xf0;
TCNT1L=0x60;
TCCR1A = 0x00;
TCCR1B = 0x01; //start Timer
TIMSK = 0x04;
}
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xcb; //set count 38k*2
}
/****************************************
端口初始化
****************************************/
void port_int(void)
{
//发送端程序
PORTD=0x00;
DDRD=0xff;//
DDRB=0xff;
PORTB=0xff;
DDRB=0x00; //使用PB1-5作为按键输入
DDRC=0xff;
PORTC=0xff;
DDRC=0X00; //使用PC1-3作为按键输入
}
/****************************************
延时程序
****************************************/
void delay(unsigned int times)
//times 单位 ms
{
time_counter=1; //打开计数器
time_counter_end=times; //设置延时
res:
if (time_counter!=0)
{
goto res;
}
}
/****************************************
定时器中断处理
****************************************/
void timer1_vof(void)
{
unsigned char a=0,b=0;
//逻辑自锁计数器
if (time_counter>0)
{
time_counter++;
if (time_counter>time_counter_end)
{
time_counter=0;
}
}
if (temp_signals!=0) //发送数据
{
PORTD|=(1<<7);
for (a=1;a<=10;a++)
{
for(b=1;b<=8;b++)
{
TCCR0|=((temp_signals)&(1<<(b-1)))>>(b-1); //发送38K载波
}
}
}
else
{
PORTD&=(~(1<<7));
}
}
void timer0_ovf_isr(void)
{
TCNT0 = 0xcb; //REset count 38k*2
signals=~signals;
if (signals==16)
{
PORTD|=signals;
}
else
{
PORTD&=signals;
}
}
/****************************************
按键查询
****************************************/
void scan_key_press(void)
{//返回按键值 press_keys
char a=0;
//测试B口
for(a=1;a<=5;a++)
{
if (((PINB&(1<<a))!=(1<<a)))
{
delay(10); //去陡
if (((PINB&(1<<a))!=(1<<a)))
{
press_keys=a;
goto end_sub;
}
}
}
//测试C口
for (a=6;a<9;a++)
{
if (((PINC&(1<<(a-5)))!=(1<<(a-5))))
{
delay(10);
if (((PINC&(1<<(a-5)))!=(1<<(a-5))))
{
press_keys=a;
goto end_sub;
}
}
}
press_keys=0;
temp_signals=0; //不发送数据
TCCR0=0x00;
end_sub:
;
}
/****************************************
信号发送
****************************************/
void send_signal(void)
{
temp_signals=(0xff<<(8-press_keys)); //使用步进码
}
/****************************************
主程序(查询方式)
****************************************/
void main(void)
{
timer1_int();
timer0_init();
port_int();
SEI();
TIMSK = 0b00000101; //timer interrupt sources
while(1) //等待中断
{
scan_key_press();
if (press_keys!=0)
{
send_signal();
}
}
} |
|