pic_rc5/src/main.c

198 lines
3.3 KiB
C

/* MIT License
Copyright (c) 2016 Daniel Treitinger
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
#include <htc.h>
#include <pic16F628a.h>
__CONFIG( LVP_OFF & BOREN_OFF & CPD_OFF & MCLRE_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT );
//Global
unsigned char toggle = 0;
unsigned int CountA, CountB, CountC;
unsigned int somethingpressed = 0;
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;
while(1)
{
while(!RBIF) {
SLEEP(); }
PORTB = 0xFF;
evaluate_tastatur(&comcode);
if(somethingpressed == 1)
{
SendRc5(&comcode, &subcode);
somethingpressed = 0;
}
}
}