You are on page 1of 9

Led:

#include <htc.h>
#define _XTAL_FREQ 8000000
#define led RB0
void main()
{
TRISB=0x00;
PORTB=0x00;
led=1;
__delay_ms(1000);
led=0;
__delay_ms(1000);
}

Lcd:
#include <htc.h>
#define _XTAL_FREQ

8000000

#define RS RB0
#define EN RB1
#define D0 RC0
#define D1 RC1
#define D2 RC2
#define D3 RC3
#define D4 RC4
#define D5 RC5
#define D6 RC6
#define D7 RC7
void lcd_init(unsigned int );
void lcd_data(unsigned char *);
void lcd_str(unsigned char *);
int main()
{
while(1)
{
TRISB=0x00;
PORTB=0x00;
TRISC=0x00;

PORTC=0x00;
lcd_init(0x38);
__delay_ms(100);
lcd_init(0x0E);
__delay_ms(100);
lcd_init(0x01);
__delay_ms(100);
lcd_init(0x80);
__delay_ms(100);
lcd_str("satya");// 41 line
__delay_ms(100);
lcd_init(0xC0);
__delay_ms(100);
lcd_str("ved");//47 line
__delay_ms(100);
}
}
void lcd_init(unsigned int val)
{
PORTC=val;
//54 line
RS=0;
EN=1;
__delay_ms(100);
EN=0;
__delay_ms(100);
}
void lcd_str(unsigned char *str) // Function to send data to string
{
int i=0;
while(str[i]!='\0')
{
lcd_data(str[i]);

i++;
//delay(10);
}
return;
}
void lcd_data(unsigned char *data)
{
PORTC=data;//71 line
RS=1;
EN=1;
__delay_ms(100);
EN=0;//76 line
__delay_ms(100);
}

Uart:
#include <htc.h>
#include <string.h>
#define _XTAL_FREQ 20000000
#define RS RC0
#define EN RC1
#define TX RC6
#define RX RC7
char lcd_init();
char serial_init();
char lcd_cmd(unsigned char );
char lcd_data(unsigned char);
char lcd_str(unsigned char *);
char lcd_str1(unsigned char *);
char lcd_init()
{
lcd_cmd(0x38); // Function set 8-bit 2-line.
lcd_cmd(0x0e);// Display ON, Cursor ON.
lcd_cmd(0x01);// Clear display.
lcd_cmd(0x80);// Force the cursor to the beginning of the 1st line.
}

char lcd_cmd(unsigned char value)


{
PORTB=value;
RS=0;
EN=1;
__delay_ms(1);
EN=0;
__delay_ms(100);
}
char serial_init()
{
SPBRG=0x1F; // variable to hold baud rate.
BRGH=0;
// High baud rate select bit.
SYNC=0;
// setting to be in Synchronous and Asynchronous mode.
SPEN=1;
// Serial port enable bit.
TRISC6=0;
// TX pin.
TRISC7=1;
// RX pin.
TXEN=1;
// transmit enablle bit.
CREN=1;
// Continuous enable bit.
}
char lcd_data(unsigned char data)
{
PORTB=data;
RS=1;
EN=1;
__delay_ms(1);
EN=0;
__delay_ms(100);
}
char lcd_str(unsigned char *str)
{
while(*str)
lcd_data(*str++);
}
char lcd_str1(unsigned char *str)
{
while(*str){

TXREG=(*str++);
__delay_ms(100);
}
}
char main()
{
unsigned int i;
unsigned char a[6]="satya",b[6];
TRISB=0x00; //lcd port setting as output.
PORTB=0x00;
TRISC0=0;
TRISC1=0;
lcd_init();
serial_init();
while(!TRMT);
lcd_str1("PRINT");
while(1)
{
for(i=0;i<6;i++)
{
while(!RCIF);
b[i]=RCREG;
RCIF=0;
}
lcd_cmd(0x80);// Force the cursor to the beginning of the 1st line.
lcd_cmd(0x01);// CLear display.
lcd_str(b);
if(strcmp(b,a)==0)
{
lcd_cmd(0xc0);
lcd_str("same");
__delay_ms(100);
}
else
{
lcd_cmd(0xc0);

lcd_str("Not same");
}
}
}

Spi_Master:
#include <htc.h>
#define _XTAL_FREQ 8000000
#define SCK RC3
#define SDO RC5
#define RCI RC4
void init_ports()
{
TRISC3=0;
TRISC4=1;
TRISC5=0;

// bit0=output, bit 1 =input


// setting RC3,RC4,RC5 as input bits making all the remaining as output bits.

}
void init_SPI_setbits()
{
SSPEN=0;
SSPSTAT=0b11000000; //making CKE=1,SMP=1.
SSPCON=0b10110010; //making SSPEN=1,CKP=1,SSPM mode is master mode with Fosc/64.
SSPEN=1;
}
void data_spi_w(unsigned char data)
{
SSPBUF=data; // the data will be going on to the sspbuf which means that the data will be
moving it on to the shift register and it will shift the data to the receiver.
while(!TRMT); // waiting till the data to be send into the buffer, the data will be send in to the
buffer if and only if all the 8-bits should be send.
//
__delay_ms(100);
}
int main()
{

//

unsigned char x=0x00;


init_ports();
init_SPI_setbits();
while(1)
{
data_spi_w(x);
__delay_ms(100);
}

}
Spi-Slave:
#include <htc.h>
#define _XTAL_FREQ 8000000
#define SCK RC3
#define SDO RC5
#define SDI
RC4
#define Led RB1
void init_ports()
{
TRISC3=1;
TRISC4=1;
TRISC5=0;// making RC3,RC4,RC5 as input bits and making all the bits as output bits.
TRISB1=0;
//RB1=0
}
void init_SPI_setbits()
{
SSPEN=0;
SSPSTAT=0b11000000; // making CKE=1,SMP=1.
SSPCON =0b00110100; // making SSPEN=1,CKP=1,SSMP mode as
SSPEN=1;
}
void init_spi_r()
{
unsigned char x;
while(!RCIF); // Waiting till receiving all the 8-bit from the tx is received
while(!BF);
// Checking whether the buffer is full or not.;

//RCIF=0;
x=SSPBUF;

// The data from the shift register form the Tx will be coming it in to the// Rx side shift register and from the shift registre it is transfered on to

the SSPBUF
if(x==0xFF){
Led=1;
RCIF=0;
}
else{
Led=0;
RCIF=0;
}
}
int main()
{
init_ports();
init_SPI_setbits();
while(1)
{
init_spi_r();
}
}

Bluetooth:
#include <htc.h>
#define _XTAL_FREQ 8000000
#define SCK RC3
#define SDO RC5
#define SDI
RC4
#define Led RB1
void init_ports()
{
TRISC3=1;
TRISC4=1;
TRISC5=0;// making RC3,RC4,RC5 as input bits and making all the bits as output bits.
TRISB1=0;

//RB1=0
}
void init_SPI_setbits()
{
SSPEN=0;
SSPSTAT=0b11000000; // making CKE=1,SMP=1.
SSPCON =0b00110100; // making SSPEN=1,CKP=1,SSMP mode as
SSPEN=1;
}
void init_spi_r()
{
unsigned char x;
while(!RCIF); // Waiting till receiving all the 8-bit from the tx is received
while(!BF);
// Checking whether the buffer is full or not.;
//RCIF=0;
x=SSPBUF;
// The data from the shift register form the Tx will be coming it in to the// Rx side shift register and from the shift registre it is transfered on to
the SSPBUF
if(x==0xFF){
Led=1;
RCIF=0;
}
else{
Led=0;
RCIF=0;
}
}
int main()
{
init_ports();
init_SPI_setbits();
while(1)
{
init_spi_r();
}
}

You might also like