#include <pic.h>
#include "always.h"
#include "delay.h"
#include "usart.h"
#include "clk_freq.h"
unsigned char i,j,k;
const unsigned char * gtck_tx;
unsigned char buff_tx[20],buff_tx_adr;
unsigned char buff_rx[20],buff_rx_adr=0;
unsigned char m,z,gelen_pkt_tmm=0;
signed char kars_sonuc;
const unsigned char * gln_kars;
void port_kur()
{
CMCON=7;
PORTA=0x00;
TRISA=0b00000000;
PORTB=0x00;
TRISB=0b00000010;
seri_temizle();DelayMs(5);
seri_kur(9600);
}
//*****************************************************
void gndr_tx(const unsigned char * gtck_tx)
{
j=0;buff_tx_adr=0;
do
{
buff_tx[j]=*gtck_tx;
j++;
}while(*gtck_tx++);
TXIE=1;
}
unsigned char kars(const unsigned char *gln_kars)
{ m=0;kars_sonuc=0;z=0;
do
{
CLRWDT();
while(!gelen_pkt_tmm);
kars_sonuc=buff_rx[m]-*gln_kars;
m++;gln_kars++; //kars_1++ ile pointerini artırmak yerine *kars_1++ ile pointerin içeriğini artırıyorsunuz.
}
while(*gln_kars);
if(kars_sonuc<0){z=0;}
else if(kars_sonuc>0){z=0;}
else if (kars_sonuc==0){z=1;}
return z;
}
//**************************************************************
void oku(){
if(RCIF)
{
if(kars("a")){RB3=!RB3;if(RB3)gndr_tx("led1 is active\r\n");if(!RB3)gndr_tx("led1 is passive\r\n");}
else if(kars("b")){RB4=!RB4;if(RB4)gndr_tx("led2 is active\r\n");if(!RB4)gndr_tx("led2 is passive\r\n");}
else if(kars("c")){RB5=!RB5;if(RB5)gndr_tx("led3 is active\r\n");if(!RB5)gndr_tx("led3 is passive\r\n");}
else if(kars("d")){RB6=!RB6;if(RB6)gndr_tx("led4 is active\r\n");if(!RB6)gndr_tx("led4 is passive\r\n");}
else return;
}
else return;
}
void main()
{
port_kur();
GIE=1;
PEIE=1;
TMR2IF=0;
TMR2IE=0;
T1CON=0b00110001;
T2CON=0b00000111;
PR2=6; // 230 = 3,68
do
{
oku();
}
while(1);
}
static void interrupt Kesme(void)
{
if(TXIF){
if(buff_tx[buff_tx_adr]!='\0')
{
TXREG=buff_tx[buff_tx_adr++];
}
else {TXIE=0;}
TXIF=0;
}
if(RCIF){
buff_rx[k++]=RCREG;
if(k==5){k=0;gelen_pkt_tmm=1;}
RCIF=0;
}
if(TMR2IF)
{
RB7=!RB7;
buff_rx_adr=0;
PR2=6;
TMR2IF=0;
TMR2IE=0;
}
}