Microchip
直播中

何培芬

7年用户 164经验值
私信 关注
[问答]

怎么用PIC12F1572与TSOP2438接收机模块建立一个IR接收机

你好,所以我正在用PIC12F1572与TSOP2438接收机模块建立一个IR接收机。这个项目的目的是通过遥控接收数据,并通过UART发送给PC。我已经完成了代码,我正在测试它,我可以通过UART发送正常值,但是某处是错误的,这样我就不能接收到关于遥控器命令的十六进制值了。谁能看到我的代码并告诉我哪里出错了?感谢是我的密码

以上来自于百度翻译


      以下为原文

    Hello,
So I  am building an IR Receiver with PIC12F1572 with receiver module TSOP2438 My objective of this project is to receive a data by remote control and send it to PC via UART.. I have done the code and I am testing it I can send the normal value through the UART but Somewhere is wrong so that I can not receive the hex values regarding the commands of remote control Can anyone see my code and tell where I am going wrong?

Thanks
Here is my code

void main(void)
{
OSCILLATOR_Initialize(); // 0x78 for Fosc = 16Mhz
PIN_MANAGER_Initialize(); //All port pins Digital and input
EUSART_Initialize();
INTCONbits.IOCIF = 0; // Interrupt on-change Flag
INTCONbits.PEIE = 1; //SEt Peripheral Interrupt
INTCONbits.GIE = 1; //Set Global Interrupt
//while(!OSCSTATbits.HFIOFS); //Check here or wait here to OSC stable/ 0.5% accuracy

TRISAbits.TRISA2 = 1; //Configure R1 as input

// uint16_t Input_buffer [20];

EUSART_Write(0x40); // 0x40 = @ some flag


while(1)
{
count = 0;
//while((IR_PIN)); //IR_PIN receives an IR signal its output pin goes from logic 1 to logic 0
//which causes the microcontroller to start reading the IR signal using the function. decode()
EUSART_Write(0x41);
//while(IR_PIN);

if(Decode()) //check if RC5 decoding- new data is arrived
{

EUSART_Write(0x42);

toggle_bit = bit_test(IR_Code, 11);
address = (IR_Code >> 6) & 0x1F;
command = IR_Code & 0x3F;

EUSART_Write(toggle_bit);
EUSART_Write(address);
EUSART_Write(command);

EUSART_Write(0x43);


}
}
}

/*----------*/
uint8_t Measure_space()
{
TMR0_Initialize();

while(IR_PIN && (count < 2000))
count = TMR0_ReadTimer(); //Read timer value and store it in count value
if((count > 1999) || (count < 700))
return 0; //0 = If width is out of range
if(count > 1200)
return 1; //1 = If width is long
else
return 2; //2 = If the width is short
}

uint8_t Decode()
{
uint8_t i = 0, check;
mid1:
check = Measure_Pulse();
if(check == 0)
return FALSE;
bit_set(IR_Code, 13 - i);
i++;

if(i > 13)
return TRUE;

if(check == 1)
goto mid0;
else
goto start1;

mid0:
check = Measure_space();
if((check == 0) && (i != 13))

return FALSE;

bit_clear(IR_Code, 13 - i);
i++;

if(i > 13) return TRUE;

if(check == 1)
goto mid1;
else
goto start0;

start1:
check = Measure_space();
if(check != 2)
return FALSE;
goto mid1;
start0:
check = Measure_Pulse();
if(check != 2)
return FALSE;
goto mid0;
}   Attachment(s)

RC5.c (2.50 KB) - downloaded 58 times

回帖(1)

李林

2019-5-20 13:07:45
嗨,Garryp,第一件事——你在上面的帖子中列出的代码与附件不同,并且它丢失了你的其他文件。在请求帮助时总是要张贴所有的源代码是很重要的。通常,其他论坛读者可以测试代码,看看它们是否可以复制这个问题。没有完整的源代码,理解代码并指出任何错误可能比较困难。根据上面的说法,文章中的代码将TRISA2设置为输入——好,那么这是怎么做的呢?这是你输入的红外线吗?我看到使用了Time0。Time0是一个8位定时器,所以它将计数到255或0xFF。然后,如果语句查找的值超过count:uint8_t.e_space(){TMR0_Initialize();.(IR_PIN&(count<2000))count=TMR0_ReadTimer();//Read计时器值,如果((count>1999)|(count<700))<--count不能是,则将其存储在count值中高于255返回0;//0=如果宽度超出范围,如果(count>;1200)返回1;//1=如果宽度长,否则返回2;//2=如果宽度短}基于计时器只上升到255的事实,永远不会返回“1”值。如果这是你的意图,可以,但是你可以考虑修改这个值,使它更有意义。你可能想看一下我们的一些与IR通信相关的应用说明和技术简介。Thisone(AN1064)描述了RC5协议,它可能是有用的:http://ww1.microhip.com/downloads/jp/附注/01064 A.PDFCHRIS

以上来自于百度翻译


      以下为原文

    Hi Garryp,
 
First thing - the code you have listed in your post above differs from the attachment, and it is missing your other files. It is important when asking for help to always post ALL of the source code. Often, other forum readers may test the code out to see if they can replicate the issue. Without full source code, it may be challenging to understand the code and point out any errors.
 
With that said, the code in the post shows TRISA2 set as an input - ok so what does this do? Is this your input for the IR?
 
I see that Timer0 is used. Timer0 is an 8-bit timer, so it will count up to 255 or 0xFF. You then have if statements looking for values that would exceed the count:
 
uint8_t Measure_space()
{
TMR0_Initialize();

while(IR_PIN && (count < 2000))
count = TMR0_ReadTimer(); //Read timer value and store it in count value
if((count > 1999) || (count < 700))     <--- count can't be higher than 255
return 0; //0 = If width is out of range
if(count > 1200)
return 1; //1 = If width is long
else
return 2; //2 = If the width is short
}
 
Based on the fact that the timer only goes up to 255, the '1' value would not be returned ever. If that is your intention ok, but you may consider revising that value so that it makes more sense.
 
You may want to take a look at some of our app notes and tech briefs related to IR communications.
 
This one (AN1064) describes the RC5 protocol, which may be useful:
http://ww1.microchip.com/downloads/jp/AppNotes/01064A.pdf
 
Chris
 
 
举报

更多回帖

发帖
×
20
完善资料,
赚取积分