Program:
//Author : Palak Patel
//Contact No:9173211683
//Title:USART Transmission and Receiver For PIC18F4520
//Platform: PIC18f4520
//Software:MPLAB
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#define LCD PORTD
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2
// start ISR code
#pragma code isr = 0x08 // store the below code at address 0x08
#pragma interrupt isr // let the compiler know that the function isr() is an interrupt handler
void delay();
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);
void isr(void)
{
unsigned char lcddata;
if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
{
if(PIR1bits.TXIF == 1) // check if the TXREG is empty
{
lcddata=RCREG;
dat(lcddata);
}
}
}
void main()
{
unsigned char palak[]="Palak Patel";
unsigned int i=0;
TRISCbits.TRISC0=0;
TRISCbits.TRISC1=0;
TRISCbits.TRISC2=0;
TRISCbits.TRISC6 = 0; // make the TX pin a digital output
TRISCbits.TRISC7 = 1; // make the RX pin a digital input
TRISD=0x00;
cmd(0x01); //Clear Lcd
cmd(0x02); //4 Bit Specification
cmd(0x38); //4 Bit specification
cmd(0x0c);
cmd(0x80); //Cursor starting point
cmd(0x06);
TXSTAbits.CSRC=1; //Master Mode
TXSTAbits.TX9=0; //8 Bit
TXSTAbits.TXEN=1; //Transmit enabled
TXSTAbits.SYNC=0; //Asynchronous Mode
TXSTAbits.SENDB=0; //Sync Break transmission completed
TXSTAbits.BRGH=1; //High Speed
RCSTA=0x90;
SPBRG=100;
RCONbits.IPEN = 0; // disable interrupt priority
INTCONbits.GIE = 1; // enable interrupts
INTCONbits.PEIE = 1; // enable peripheral interrupts.
PIE1bits.RCIE = 1; // enable USART receive interrupt
while(palak[i]!='\0')
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=palak[i]; //Transmit buffer
i++;
}
while(1);
}
void delay()
{
unsigned int j;
for(j=0;j<100;j++);
}
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void dat(unsigned char k)
{
LCD=k;
RS=1;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void Delay_ms(unsigned char p)
{
unsigned char i,j;
for(i=0;i<p;i++)
{
for(j=0;j<20;j++);
}
}
Simulation:
//Author : Palak Patel
//Contact No:9173211683
//Title:USART Transmission and Receiver For PIC18F4520
//Platform: PIC18f4520
//Software:MPLAB
#include<p18f4520.h>
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#define LCD PORTD
#define RS PORTCbits.RC0
#define RW PORTCbits.RC1
#define EN PORTCbits.RC2
// start ISR code
#pragma code isr = 0x08 // store the below code at address 0x08
#pragma interrupt isr // let the compiler know that the function isr() is an interrupt handler
void delay();
void cmd(unsigned char);
void dat(unsigned char);
void Delay_ms(unsigned char);
void isr(void)
{
unsigned char lcddata;
if(PIR1bits.RCIF == 1) // if the USART receive interrupt flag has been set
{
if(PIR1bits.TXIF == 1) // check if the TXREG is empty
{
lcddata=RCREG;
dat(lcddata);
}
}
}
void main()
{
unsigned char palak[]="Palak Patel";
unsigned int i=0;
TRISCbits.TRISC0=0;
TRISCbits.TRISC1=0;
TRISCbits.TRISC2=0;
TRISCbits.TRISC6 = 0; // make the TX pin a digital output
TRISCbits.TRISC7 = 1; // make the RX pin a digital input
TRISD=0x00;
cmd(0x01); //Clear Lcd
cmd(0x02); //4 Bit Specification
cmd(0x38); //4 Bit specification
cmd(0x0c);
cmd(0x80); //Cursor starting point
cmd(0x06);
TXSTAbits.CSRC=1; //Master Mode
TXSTAbits.TX9=0; //8 Bit
TXSTAbits.TXEN=1; //Transmit enabled
TXSTAbits.SYNC=0; //Asynchronous Mode
TXSTAbits.SENDB=0; //Sync Break transmission completed
TXSTAbits.BRGH=1; //High Speed
RCSTA=0x90;
SPBRG=100;
RCONbits.IPEN = 0; // disable interrupt priority
INTCONbits.GIE = 1; // enable interrupts
INTCONbits.PEIE = 1; // enable peripheral interrupts.
PIE1bits.RCIE = 1; // enable USART receive interrupt
while(palak[i]!='\0')
{
while(PIR1bits.TXIF==0);// TMRT Empty
TXREG=palak[i]; //Transmit buffer
i++;
}
while(1);
}
void delay()
{
unsigned int j;
for(j=0;j<100;j++);
}
void cmd(unsigned char k)
{
LCD=k;
RS=0;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void dat(unsigned char k)
{
LCD=k;
RS=1;
RW=0;
EN=1;
Delay_ms(20);
EN=0;
}
void Delay_ms(unsigned char p)
{
unsigned char i,j;
for(i=0;i<p;i++)
{
for(j=0;j<20;j++);
}
}
Simulation:
No comments:
Post a Comment