本帖最后由 mars4zhu 于 2014-9-24 23:40 编辑
五、红外代码的发射瑞萨的R7FC08芯片有两个Timer,刚好用一个Timer生成38KHz载波,另一个Timer生成调制波的时间。
软件流程为:
基于R7FC08的智能遥控器的软件流程图
函数分别如下:
__interrupt void interrupt_inttm00(){
baseFreq ++;
switch(baseFreq){
case 1:
// write 0
// P0 = (P0 &~DAT_BIT) | (switcher & DAT_BIT) ; // switcher lock it ? 0xFF for lock on high level(write 1), 0 for not
P0.DAT = switcherOn ;
break;
case 2:
// write 1
P0.DAT = 1;
break;
case 3:
// Keep 1
baseFreq = 0;
}
}
__interrupt void interrupt_inttm01(){
if(emitted ){
return;
}
if( tmpData[emitIdx] & emitMask ){
switcherOn = YES;
}else{
switcherOn = NO;
}
emitMask >>= 1;
if( emitMask == 0){
emitMask = 0x80;
emitIdx ++;
}
if( emitIdx == expandedLen && emitMask == expandedMask ){
emitIdx = 0;
emitMask = 0x80;
if( !repeat ) {
repeat = YES;
}else{
emitted = YES;
repeat = NO;
}
}
}
之后采用一个按键的高低电平作为开或关键的发射选择
void scan_keys(){
if( !P0.KEY_A && lastCurLevel == HIGH){
// User pressed key ON, should turn on air-condition
lastCurLevel = LOW ;
expandedLen = expand(turnOnCodes, LEN);
emitted = NO;
}else if( P0.KEY_A && lastCurLevel == LOW) {
// User pressed key OFF, should turn on air-condition
lastCurLevel = HIGH ;
expandedLen = expand(turnOffCodes, LEN);
emitted = NO;
}
}
最核心的红外键码调制和发送函数
UINT8 expand(UINT8 codes[], UINT8 arrlen){
// this function is ugly
UINT8 i, j, tmpIdx=2, tmpBitMask=0x80, bitMask=0x80;
memset(tmpData+2, 0xff, sizeof(UINT8) * (MAXLEN-2) ); // skip the preamble
for(i=0;i
for(bitMask=0x80,j=0; j<8; j++){
if( (codes & bitMask) ){ // 1
/* write_1(tmpData, &tmpIdx, &tmpBitMask); */
tmpData[tmpIdx] &= ~tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
}else{// 0
tmpData[tmpIdx] &= ~tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
}
bitMask >>= 1;
}
for(bitMask=0x80,j=0; j<8; j++){
if( !(codes & bitMask) ){ // 1
tmpData[tmpIdx] &= ~tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
}else{// 0
tmpData[tmpIdx] &= ~tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
tmpData[tmpIdx] |= tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
}
bitMask >>= 1;
}
}
tmpData[tmpIdx] &= ~tmpBitMask;
tmpBitMask >>= 1;
if( tmpBitMask == 0){ tmpIdx ++; tmpBitMask = 0x80; }
expandedMask = tmpBitMask;
return tmpIdx+1;
}
基于R7FC08的智能遥控器原型实物图