pic_rc5/src/main.c

195 lines
2.3 KiB
C

#include <htc.h>
#include <pic16F628a.h>
__CONFIG( LVP_OFF & BOREN_OFF & CPD_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT );
unsigned char PhaseDemod();
//Global
unsigned char toggle = 0;
unsigned int CountA, CountB, CountC;
unsigned int somethingpressed = 0;
void samplebit();
void ON()
{
unsigned char i = 0;
do
{
RA0 = 1; // 6-7us on
RA0 = 1;
RA0 = 1;
RA0 = 1;
RA0 = 0; // 21-22 us of
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
RA0 = 0;
i++;
}
while(i<32);
RA0 = 0;
}
void OFF()
{
unsigned char i = 0;
do
{
RA0=0;
}
while(i++<59);
}
void interrupt ISR (void)
{
}
void delay_89ms()
{
#asm
pause89ms
clrf _CountA
clrf _CountB
clrf _CountC
movlw 0x74
movwf _CountB
movlw 0x02
movwf _CountA
movlw 0x01
movwf _CountC
Loop ;Wiederholung der Zeitverzögerung
decfsz _CountA
goto Loop
decfsz _CountB
goto Loop
decfsz _CountC
goto Loop
#endasm
return;
}
void One()
{
OFF();
ON();
}
void Zero()
{
ON();
OFF();
}
void evaluate_tastatur(char *comcode)
{
unsigned char i;
unsigned char a;
unsigned char e;
e = 0;
for(i=3; i != 255; i--)
{
PORTB = 0xFF;
PORTB &= ~(1<<i);
for(a=4; a <= 7; a++)
{
e++;
if( ((PORTB>>a) & 1) == 0)
{
*comcode = e;
somethingpressed = 1;
return;
}
}
}
}
void SendRc5(char* command, char *group)
{
unsigned char i;
One();
One();
toggle = toggle ^ 1 & 1;
if(toggle == 1)
One();
else
Zero();
for(i = 4; i != 255; i--)
{
if(((*group>>i))&1)
One();
else
Zero();
}
for(i = 5; i != 255; i--)
{
if(((*command)>>i)&1)
One();
else
Zero();
}
delay_89ms();
}
void main(void)
{ //Takt des PIC 4MHz, ein Zyklus = 1us
unsigned char comcode = 0;
char subcode = 0;
OPTION_REG = 0b00000010; //Pullups on
CMCON = 0x07; //Turn of Comperators
TRISA = 0x00; //PORTA is Output
TRISB = 0xF0; //PORTB 0...3 Output
GIE = 1; //Global Interrupt Enable
CM0 = 1;
CM1 = 1;
CM2 = 1;
PORTA = 0;
PORTB = 0xFF;
subcode = 29;
INTCON = 0;
RBIE = 1;
char help = 0;
while(1)
{
while(!RBIF)
SLEEP();
PORTB = 0xFF;
evaluate_tastatur(&comcode);
if(somethingpressed == 1)
{
SendRc5(&comcode, &subcode);
somethingpressed = 0;
help = 1;
}
}
}