📄 ir.c
字号:
/* IR.c
* Copyright 2007 by Indigo Manufacturing Inc.
* All rights reserved. Property of Indigo Manufacturing Inc.
*
* Created by Frank Tu
*
* Auguest 28, 2007
*/
//#include <msp430x12x2.h>
#include <msp430x22x4.h>
#include "IR.h"
#include "delay.h"
#include "ASCIIcodeTable.h"
#define IR_COMMAND_MASK 0xffff
extern unsigned long int uliStateTimer; // temporary variable for state timer
/********************************** Private functions ************************/
/******************************************************************************/
struct IR_timerSTRUC {
unsigned long command; // command code
unsigned int delay; // general time delay
unsigned int bitcount; // number of received bits
} IR_timer;
#define IR_BUFF_LENGTH 0x7f
unsigned int IRcommand;
unsigned char repeat_flag;
unsigned int IRpulseBuff[IR_BUFF_LENGTH+1];
unsigned int IRputIndex, IRgetIndex;
unsigned long IR_repeat_counter;
/********************* Interrupt for IR ******************************/
void port_interrupt (void) __interrupt[PORT1_VECTOR]
{
// _DINT();
// P1IFG&=~1;
if (P1IFG & IR_PIN)
{
P1IFG &= ~IR_PIN; // clear interrupt flag
P1IE |= IR_PIN; // IR interrupt enable
// P1IES ^= IR_PIN; //toggle detecting edge
IR_repeat_counter=1000;
repeat_flag=1;
IRputIndex &= IR_BUFF_LENGTH; // set within range
*(IRpulseBuff+IRputIndex) = TAR; // store timer
TAR=0;
IRputIndex++; // next
IRputIndex &= IR_BUFF_LENGTH; // set within range
}
// _EINT();
}
/***************************************************************/
void vIR_CommandDecode(void)
{//return;
// if ((IRputIndex-IRgetIndex)>60) // used for debugging
while (IRgetIndex != IRputIndex) // is there any new data? - process all
{
//IR_repeat_counter=2000;
IR_timer.delay = IRpulseBuff[IRgetIndex]; //
IRgetIndex++;
IRgetIndex &= IR_BUFF_LENGTH; // set whithin limits
//repeat_flag=1;
if (IR_timer.delay < ZERO_PULSE_LOW_RANGE| IR_timer.delay>ONE_PULSE_HIGH_RANGE)
{
// IR_timer.bitcount=31; // if repeat =>
IR_timer.bitcount=0;
IR_timer.command=0; // => clear comand; set to start
return;
}
if (IR_timer.delay>ZERO_PULSE_LOW_RANGE // is it '0'
&& IR_timer.delay<ZERO_PULSE_HIGH_RANGE)
{
IR_timer.command=(IR_timer.command)<<1;
IR_timer.bitcount++;
}
if (IR_timer.delay>ONE_PULSE_LOW_RANGE // is it '1'
&& IR_timer.delay<ONE_PULSE_HIGH_RANGE)
{
IR_timer.bitcount++;
IR_timer.command=IR_timer.command<<1|1;
}
IR_timer.delay=0;
if (IR_timer.bitcount==32) // if we have 10 bits -> valid data
{
// if ((IR_timer.command & 0xffff0000)==0x52ad0000)
{
IRcommand=IR_timer.command & IR_COMMAND_MASK; // set IR_Command variable
// UCA0TXBUF=IRcommand/256;
// vDelay(3000);
// UCA0TXBUF=IRcommand&0xff;
sendasiccode(IRcommand);
IR_timer.command=0;
}
// else
// IRcommand=0;
// IR_timer.command=0;
//P2IE |= IR_PIN; // IR interrupt enable
// IRgetIndex=IRputIndex=0; // initialize buffer
IR_timer.bitcount=0; // reset bit count
while (repeat_flag);
//_DINT();
//vDelay(24000);
vInitIR();
uliStateTimer = 0;
// _EINT();
}
}
}
/************************************************/
void vInitIR(void)
{
IRgetIndex = 0;
IRputIndex = 0;
IR_timer.bitcount=0;
IR_timer.command=0;
P1IES |= IR_PIN; //detect falling edge
P1IFG &= ~IR_PIN; // clear interrupt flag
P1IE |= IR_PIN; // IR interrupt enable
}
//*****************************************
void sendasiccode(unsigned int command )
{ unsigned char temp;
temp=ASCIIcodeTable[command&0xff];
UCA0TXBUF=temp;
switch (command)
{
case one:
UCA0TXBUF=0x31;
break;
case two:
UCA0TXBUF=0x32;
break;
case three:
UCA0TXBUF=0x33;
break;
case four:
UCA0TXBUF=0x34;
break;
case five:
UCA0TXBUF=0x35;
break;
case six:
UCA0TXBUF=0x36;
break;
case seven:
UCA0TXBUF=0x37;
break;
case eight:
UCA0TXBUF=0x38;
break;
case nine:
UCA0TXBUF=0x39;
break;
case zero:
UCA0TXBUF=0x30;
break;
default:
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -