firmware/rc5.c
author slime@unimatrix01.gamma-quadrant.de
Tue, 29 Jan 2008 22:31:52 +0100
changeset 2 2f55e5dd591d
permissions -rw-r--r--
inital checkin
slime@2
     1
/*
slime@2
     2
 * Project Frontplatte
slime@2
     3
 *
slime@2
     4
 * rc5.c  -  decode rc5 signals
slime@2
     5
 *
slime@2
     6
 * This file is released under the GNU General Public License. Refer
slime@2
     7
 * to the COPYING file distributed with this package.
slime@2
     8
 *
slime@2
     9
 * (c) 2007 Carsten Presser cpresser AT fsing.uni-sb.de
slime@2
    10
 */
slime@2
    11
slime@2
    12
#include <p18cxxx.h>
slime@2
    13
#include "rc5.h"
slime@2
    14
#include "io_cfg.h"
slime@2
    15
#include "delay.h"
slime@2
    16
#include "user.h"
slime@2
    17
slime@2
    18
#include <timers.h>
slime@2
    19
slime@2
    20
/** V A R I A B L E S **********************************************/
slime@2
    21
unsigned char rc5_status;
slime@2
    22
unsigned int  rc5_receive_buffer;
slime@2
    23
unsigned char gg_ir;
slime@2
    24
unsigned char gg_ir_address;
slime@2
    25
slime@2
    26
/** C O D E ********************************************************/
slime@2
    27
void rc5_init(void)
slime@2
    28
{
slime@2
    29
	OpenTimer0(TIMER_INT_ON & T0_8BIT & T0_SOURCE_INT & T0_PS_1_256);
slime@2
    30
	// SOLLTE SEIN: ein Timer Takt = 51,2 E-6 sec. (mit prescaler 256)
slime@2
    31
	// ist aber 25,6usec [woher kommt das 1/2??]
slime@2
    32
slime@2
    33
/*	kurze pulsdauer: 		444us	bis		1333us		mitte:	889us
slime@2
    34
	entspricht in timer0:	17,3			52,0				34,7
slime@2
    35
slime@2
    36
	lange pulsdauer:		1334us	bis		2222us				1778us
slime@2
    37
	in timer0:				52,1			87					69,5*/
slime@2
    38
slime@2
    39
	INTCONbits.TMR0IF	= 0;	// clear flag
slime@2
    40
	INTCON2bits.TMR0IP	= 1;	// make TMR0 a high-priority-interrupt
slime@2
    41
slime@2
    42
	rc5_receive_buffer	= 0x0000;
slime@2
    43
	gg_ir_address		= 0xFF;
slime@2
    44
	gg_ir 				= 0xFF;
slime@2
    45
	rc5_status			= RC5_UNDEF;
slime@2
    46
}
slime@2
    47
slime@2
    48
void rc5_decode(void)
slime@2
    49
{
slime@2
    50
	unsigned char tmr0;
slime@2
    51
	unsigned char pulse = 0xFF;
slime@2
    52
slime@2
    53
/* G E T   P U L S E W I D T H   &  T Y P E */
slime@2
    54
	tmr0 = (unsigned char)(ReadTimer0());
slime@2
    55
	// decide if this is long or short...
slime@2
    56
	if ((tmr0 > 20) && (tmr0 < 49)) {pulse = RC5_SPACE_SHORT;}
slime@2
    57
	if ((tmr0 > 55) && (tmr0 < 82)) {pulse = RC5_SPACE_LONG;}
slime@2
    58
slime@2
    59
	// if RB0 = 1 -> this is a space, not a pulse -> add one
slime@2
    60
	pulse += PIN_IR;
slime@2
    61
slime@2
    62
/* S T A T U S    M A C H I N E */
slime@2
    63
	switch (rc5_status)
slime@2
    64
	{
slime@2
    65
		case RC5_MID1:
slime@2
    66
			if (pulse == RC5_PULSE_SHORT)
slime@2
    67
			{
slime@2
    68
				rc5_status = RC5_START1;
slime@2
    69
			}
slime@2
    70
			else if (pulse == RC5_PULSE_LONG)
slime@2
    71
			{
slime@2
    72
				rc5_status = RC5_MID0;
slime@2
    73
				rc5_receive_buffer = rc5_receive_buffer << 1;		// emit zero
slime@2
    74
			}
slime@2
    75
			else
slime@2
    76
				rc5_scrap();
slime@2
    77
			break;
slime@2
    78
slime@2
    79
		case RC5_MID0:
slime@2
    80
			if (pulse == RC5_SPACE_SHORT)
slime@2
    81
			{
slime@2
    82
				rc5_status = RC5_START0;
slime@2
    83
			}
slime@2
    84
			else if (pulse == RC5_SPACE_LONG)
slime@2
    85
			{
slime@2
    86
				rc5_status = RC5_MID1;
slime@2
    87
				rc5_receive_buffer = rc5_receive_buffer << 1;
slime@2
    88
				rc5_receive_buffer |= 0x01;							// emit one
slime@2
    89
			}
slime@2
    90
			else
slime@2
    91
				rc5_scrap();
slime@2
    92
			break;
slime@2
    93
slime@2
    94
		case RC5_START1:
slime@2
    95
			if (pulse == RC5_SPACE_SHORT) 
slime@2
    96
			{
slime@2
    97
				rc5_status = RC5_MID1;
slime@2
    98
				rc5_receive_buffer = rc5_receive_buffer << 1;
slime@2
    99
				rc5_receive_buffer |= 0x01;							// emit one
slime@2
   100
			}
slime@2
   101
			else
slime@2
   102
				rc5_scrap();
slime@2
   103
			break;
slime@2
   104
		
slime@2
   105
		case RC5_START0:
slime@2
   106
			if (pulse == RC5_PULSE_SHORT)
slime@2
   107
			{
slime@2
   108
				rc5_status = RC5_MID0;
slime@2
   109
				rc5_receive_buffer = rc5_receive_buffer << 1;		// emit zero
slime@2
   110
			}
slime@2
   111
			else
slime@2
   112
				rc5_scrap();
slime@2
   113
			break;
slime@2
   114
slime@2
   115
		case RC5_DONE:
slime@2
   116
		case RC5_UNDEF:
slime@2
   117
		default:
slime@2
   118
			rc5_scrap();
slime@2
   119
			break;
slime@2
   120
	}
slime@2
   121
	
slime@2
   122
	// i recieved 13 + startbit bits...
slime@2
   123
	if (rc5_receive_buffer & 0x6000) 
slime@2
   124
	{
slime@2
   125
		rc5_status = RC5_DONE;
slime@2
   126
		gg_ir 			= (unsigned char)(rc5_receive_buffer & 0x3F);			// lower 6 bits -> command
slime@2
   127
		gg_ir_address	= (unsigned char)((rc5_receive_buffer & 0x032) >> 6);	// upper 6 bits -> address + toogle
slime@2
   128
slime@2
   129
		gg_ioflags		|= FLAG_IR;
slime@2
   130
	}
slime@2
   131
slime@2
   132
	// reset timer and wait for next pulse....
slime@2
   133
	WriteTimer0(0x00);
slime@2
   134
}
slime@2
   135
slime@2
   136
slime@2
   137
void rc5_scrap(void) 
slime@2
   138
{
slime@2
   139
	// enter start-state and emit one
slime@2
   140
	rc5_status = RC5_MID1;
slime@2
   141
	rc5_receive_buffer = 0x0001;
slime@2
   142
}