📄 ire.c
字号:
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
//端口初始化
void port_init(void)
{
PORTA = 0xff;
DDRA = 0xff;
PORTB = 0xff;
DDRB = 0xff;
PORTC = 0xff;
DDRC = 0xff;
PORTD = 0xff;
DDRD = 0xff;
}
//T0初始化,产生38k的载波
// actual value: 38.462KHz (1.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0xe8; //set count
OCR0 = 0x34;
TCCR0 = 0x02; //start timer
}
//T0中断
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
TCNT0 = 0xe8; //reload counter value
PORTD^=(1<<1); //引脚输出38K信号
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 588Hz
// actual value: 588.062Hz (0.0%)
timer1_init(uchar th,uchar tl)
{
TIFR|=0X04; //复位溢出标志
TCCR1B = 0x00; //stop
TCNT1H = th; //setup
TCNT1L = tl;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
while(!(TIFR&0X04)); //溢出后离开
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
out_1() //输出1
{
TIMSK=0x01; //发送
timer1_init(0xfb,0xa0); //560us
TIMSK=0x00; //停止发送
timer1_init(0xf2,0xb7); //1650us
}
out_0() //输出0
{
TIMSK=0x01; //发送
timer1_init(0xfb,0xa0); //560us
TIMSK=0x00; //停止发送
timer1_init(0xfb,0xa0); //560us
}
out_f() //输出结束
{
TIMSK=0x01; //发送
timer1_init(0xfb,0xa0); //560us
TIMSK=0x00; //停止发送
}
//发送函数
void send_n(uchar aa,uchar bb) //aa系统码,bb键码
{
uchar cc,i;
TIMSK=0x01;
timer1_init(0xb9,0x9e); //引导码9ms低
TIMSK=0x00;
timer1_init(0xdc,0xcf); //引导码4.5ms高
//发送系统码
cc=0x80;
for(i=0;i<8;i++)
{
if(aa&cc)
{
out_1();
}
else
{
out_0();
}
cc=cc>>1;
}
//发送系统反码
aa=~aa;
cc=0x80;
for(i=0;i<8;i++)
{
if(aa&cc)
{
out_1();
}
else
{
out_0();
}
cc=cc>>1;
}
//发送键码
cc=0x80;
for(i=0;i<8;i++)
{
if(bb&cc)
{
out_1();
}
else
{
out_0();
}
cc=cc>>1;
}
//发送键码反码
bb=~bb;
cc=0x80;
for(i=0;i<8;i++)
{
if(bb&cc)
{
out_1();
}
else
{
out_0();
}
cc=cc>>1;
}
//结束
out_f();
}
main()
{
init_devices();
while(1)
{
send_n(0x00,0x58); //发送遥控码
while(1);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -