#include <htc.h>
#include <stdlib.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
void interrupt ISR(void);
unsigned char input[10]; // Transmision buffer
void HSerinit(void);
unsigned char Hserin(void); // transmission functions
int getInt(unsigned char* str); // a sort of atoi
int rxdata,inchar; // incomming data store
unsigned char rxcommand; // incomming command store
void main(void)
{
unsigned char portbuf;
PR2 = 0b11111001 ;
T2CON = 0b00000100 ;
CCP1CON = 0b00111100;
ADCON1 = 0x6;
HSerinit();
TRISD=0x00;
PORTD = 0x0;
__delay_ms(502);
PORTD=0x5; // for testing hardware it is working till here..
__delay_ms(502);
PORTD =0xA;
__delay_ms(502);
PORTD = 0x0;
PEIE = 1;
GIE = 1;
while(1)
{
CCPR1L = (unsigned char)rxdata ;// set PWM
portbuf = 0;
switch(rxcommand)
{
case ('a'):
portbuf += 1;
break;
case ('b'):
portbuf += 2;
break;
case ('c'):
portbuf += 4;
break;
case ('d'):
portbuf += 8;
break;
case ('e'):
portbuf += 10;
break;
case ('f'):
portbuf += 5;
break;
default:
portbuf = 0;
}
PORTD = portbuf;
__delay_ms(2);
}
}
void interrupt ISR()
{
int x;
if(OERR)
{
CREN = 0;
CREN = 1;
return;
}
input[inchar] = RCREG;
if(input[inchar] == 0x0D)
{
if(inchar == 1)
{
if(input[inchar-1] > 0x39)
rxcommand = input[inchar-1];
}
else
rxdata = getInt(input);
for(x = 0;x<10;x++)
input[x] = 0;
inchar = 0;
return;
}
inchar++;
}
int getInt(unsigned char* str) // sort of atoi
{
int result = 0;
while(*str !=0x0D) // until we see carriage return
{
if(*str >0x29 && *str < 0x3A) // Is it a number
{
result*=10;
result+= *str - 0x30; // convert to int
}
str++;
}
return result; // all done
}
void HSerinit(void)
{
TRISC = 0x80; // TX was an input!
SPBRG = 129; // 20Mhz xtal 9600 BAUD
TXSTA = 0x24; // TXEN and BRGH
RCSTA = 0x90; // SPEN and CREN
RCIE = 1;
}