0% found this document useful (0 votes)
9 views

MCA Codeof pic microcontroller practical

Codes for studying microcontroller

Uploaded by

Tanmay shitole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

MCA Codeof pic microcontroller practical

Codes for studying microcontroller

Uploaded by

Tanmay shitole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Timer With Interrupt

#include <p18f4550.h>
#include <stdlib.h>

extern void _startup (void);


void timer_isr(void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
//The program execution comes to this point when a timer interrupt is generated
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
_asm goto timer_isr _endasm //The program is relocated to execute
the interrupt routine timer_isr
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}

#pragma code
// This function is executed as soon as the timer interrupt is generated due to timer overflow

#pragma interrupt timer_isr

void timer_isr(void)
{
TMR0H = 0XFD; // Reloading the timer values after overflow
TMR0L = 0XA8;
PORTB = ~PORTB; //Toggle the PORTB led outputs RB0 - RB3
INTCONbits.TMR0IF = 0;//Resetting the timer overflow interrupt flag

}
void main()
{
ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O
TRISB = 0; //Configuring the LED port pins as outputs
PORTB = 0xFF; //Setting the initial value of the LED's after reset
T0CON = 0x08; //Set the timer to 16-bit mode,internal instruction cycle clock,no
prescaler
TMR0H = 0x3C;
TMR0L = 0xB0;
INTCONbits.TMR0IF = 0; // Clear Timer0 overflow flag
INTCONbits.TMR0IE = 1; // TMR0 interrupt enabled
T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE = 1; // Global interrupt enabled

while(1); //Program execution stays here untill the timer overflow


interrupt is generated

Led relay buzzer

#include<p18f4550.h>
extern void _startup(void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset(void){
_asm goto _startup _endasm
}
#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void _high_ISR(void){
//_asm goto High_ISR_endasm
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void _low_ISR(void){
// _asm goto Low_ISR _endasm
}
#pragma code

void delay(int n){


int i,j;
for(i=0;i<n;i++)
for(j=0;j<1000;j++);
}
void main(void){
// tris sets the input output port
TRISBbits.RB4=0; //output port
TRISBbits.RB5=0; //output port
TRISBbits.RB6=0; //output port
TRISBbits.RB7=0; //output port
TRISBbits.RB0=1; //input port
TRISBbits.RB1=1; //input port
TRISCbits.RC0=0; //buzzer
TRISCbits.RC1=0; //relay
while(1){
if(PORTBbits.RB0==0){
PORTCbits.RC0=1;
PORTCbits.RC1=1;
while(1){
PORTB = 0x10;
delay(100);
PORTB = 0x20;
delay(100);
PORTB = 0x40;
delay(100);
PORTB = 0x80;
delay(100);
if(PORTBbits.RB1==0){
break;
}
}
}
else if(PORTBbits.RB1==0){
PORTCbits.RC0=0;
PORTCbits.RC1=0;
while(1){
PORTB = 0x80;
delay(100);
PORTB = 0x40;
delay(100);
PORTB = 0x20;
delay(100);
PORTB = 0x10;
delay(100);
if(PORTBbits.RB0==0){
break;
}
}
}
}
}

LCD display

/********************************************************************************/
/*This program demonstrates the interfacing of LCD to PIC18F4550 microcontroller*/
/********************************************************************************/

/*
LCD DATA port----PORT D
signal port------PORT E
rs-------RE0
rw-------RE1
en-------RE2
*/

#include <p18f4550.h>

//LCD data pins connected to PORTD and control pins connected to PORTE
#define LCD_DATA PORTD //LCD data port
#define ctrl PORTE //LCD signal port
#define en PORTEbits.RE2 // enable signal
#define rw PORTEbits.RE1 // read/write signal
#define rs PORTEbits.RE0 // register select signal
#define BUSY PORTDbits.RD7

//LCD function definitions


void LCD_Busy(void);
void LCD_cmd(unsigned char cmd);
void init_LCD(void);
void LCD_write(unsigned char data);
void LCD_write_string(static char *str);

/*The following lines of code perform interrupt vector relocation to work with the USB bootloader.
These must be
used with every application program to run as a USB application.*/
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000

void _reset (void)


{
_asm goto _startup _endasm
}

#pragma code
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
}

#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}
#pragma code
//Function to generate delay
void myMsDelay (unsigned int time)
{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 710; j++);/*Calibrated for a 1 ms delay in MPLAB*/
}

// Function to initialise the LCD


void init_LCD(void)
{
LCD_cmd(0x38); // initialization of 16X2 LCD in 8bit mode
myMsDelay(15);

LCD_cmd(0x01); // clear LCD


myMsDelay(15);

LCD_cmd(0x0C); // cursor off


myMsDelay(15);

LCD_cmd(0x80); // ---8 go to first line and --0 is for 0th position


myMsDelay(15);

// ---8 go to first line and --0 is for 0th position

return;
}
//Function to pass command to the LCD
void LCD_cmd(unsigned char cmd)
{
LCD_DATA = cmd;
rs = 0;
rw = 0;
en = 1;
myMsDelay(15);
en = 0;
myMsDelay(15);
return;
}

//Function to write data to the LCD


void LCD_write(unsigned char data)
{
LCD_DATA = data;
rs = 1;
rw = 0;
en = 1;
myMsDelay(15);
en = 0;
myMsDelay(15);
return ;
}
//Function to split the string into individual characters and call the LCD_write function
void LCD_write_string(static char *str) //store address value of the string in pointer *str
{
int i = 0;
while (str[i] != 0)
{
LCD_write(str[i]); // sending data on LCD byte by byte
myMsDelay(15);
i++;
}
return;
}

void main(void)
{
char var1[] = "PVG COET";
ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O
TRISD = 0x00; //Configuring PORTD as output
TRISE = 0x00; //Configuring PORTE as output

init_LCD(); // initialization of LCD


myMsDelay(50); // delay of 50 mili seconds
LCD_write_string(var1);
while (1);
}

Timer With

#include <p18f4550.h>
#include <stdlib.h>

extern void _startup (void);


void timer_isr(void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
//The program execution comes to this point when a timer interrupt is generated
#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008
void high_ISR (void)
{
_asm goto timer_isr _endasm //The program is relocated to execute
the interrupt routine timer_isr
}
#pragma code _LOW_INTERRUPT_VECTOR = 0x1018
void low_ISR (void)
{
}

#pragma code
// This function is executed as soon as the timer interrupt is generated due to timer overflow

#pragma interrupt timer_isr

void timer_isr(void)
{
TMR0H = 0XFD; // Reloading the timer values after overflow
TMR0L = 0XA8;
PORTB = ~PORTB; //Toggle the PORTB led outputs RB0 - RB3
INTCONbits.TMR0IF = 0;//Resetting the timer overflow interrupt flag

}
void main()
{
ADCON1 = 0x0F; //Configuring the PORTE pins as digital I/O

TRISB = 0; //Configuring the LED port pins as outputs


PORTB = 0xFF; //Setting the initial value of the LED's after reset
T0CON = 0x08; //Set the timer to 16-bit mode,internal instruction cycle clock,no
prescaler
TMR0H = 0x3C;
TMR0L = 0xB0;
INTCONbits.TMR0IF = 0; // Clear Timer0 overflow flag
INTCONbits.TMR0IE = 1; // TMR0 interrupt enabled
T0CONbits.TMR0ON = 1; // Start timer0
INTCONbits.GIE = 1; // Global interrupt enabled

while(1); //Program execution stays here untill the timer overflow


interrupt is generated

}
sqrwave.c
Displaying sqrwave.c.

Transmitter &Reciever

#include<p18f4550.h>

#pragma udata
#pragma idata

extern void _startup (void); // See c018i.c in your C18 compiler dir
void High_ISR(void);
void Low_ISR(void);
/** V E C T O R R E M A P P I N G *******************************************/
extern void _startup (void);
#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008


void _high_ISR (void)
{

#pragma code _LOW_INTERRUPT_VECTOR = 0x1018


void _low_ISR (void)
{

}
#pragma code

#pragma code

//#pragma code
unsigned char String[]={"WELCOME \n\rPress any key from PC\n\r"};
unsigned char String1[]={"\n\rUART Tested\n\r"};

void TXbyte(char data)


{
while(TXSTAbits.TRMT==0); //wait tiiltransmit buffer in not empty
TXREG = data; // Transmit Data
}//end TXbyte

void main()
{
unsigned char temp;
unsigned char i=0;
SSPCON1 = 0; // Make sure SPI is disabled //Refer Datasheet
TRISCbits.TRISC7=1; // RX
TRISCbits.TRISC6=0; // TX
SPBRG = 0x71;
SPBRGH = 0x02;// 0x0271 for fosc :48MHz -> 19200 baud XTAL=20MHz,
TXSTA = 0x24; // TX enable BRGH=1
RCSTA = 0x90; // continuous RX
BAUDCON = 0x08; // BRG16 = 1
temp = RCREG; // Empty buffer

for(i=0;String[i]!='\0';i++)
{
TXbyte(String[i]);
}
while(PIR1bits.RCIF==0); //Wait util data from PC is received

for(i=0;String1[i]!='\0';i++)
{
TXbyte(String1[i]);
}
while(1); //loop forever
}
PWM Square Wave

/** I N C L U D E S ********/

#include<p18f4550.h>

/** P R I V A T E P R O T O T Y P E S ***************************************/

void myMsDelay (unsigned int time);

/** V E C T O R R E M A P P I N G *******************************************/

extern void _startup (void); // See c018i.c


#pragma code _RESET_INTERRUPT_VECTOR = 0x1000
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code

#pragma code _HIGH_INTERRUPT_VECTOR = 0x1008


void _high_ISR (void)
{
//_asm goto High_ISR _endasm
}

#pragma code _LOW_INTERRUPT_VECTOR = 0x1018


void _low_ISR (void)
{
// _asm goto Low_ISR _endasm
}

#pragma code

/** D E C L A R A T I O N S **************************************************/

#pragma code

void main()
{
TRISCbits.TRISC2 = 0 ; // Set PORTC, 2 as output
TRISCbits.TRISC6 = 0 ;
TRISCbits.TRISC7 = 0 ;
PR2 = 0x4A; // set PWM period to Maximum value
CCPR1L = 0x12; // Initalise PWM duty cycle to 00
CCP1CON = 0x3C; // Configure CCP1CON as explained above.
T2CON = 0x07;
PORTCbits.RC6 = 1;
PORTCbits.RC7 = 0;
while(1)
{
CCPR1L = 0x12; // Initalise PWM duty cycle to 00
CCP1CON = 0x2C;
myMsDelay(2000);
CCPR1L = 0x25; // Initalise PWM duty cycle to 00
CCP1CON = 0x0C;
myMsDelay(2000);
CCPR1L = 0x37; // Initalise PWM duty cycle to 00
CCP1CON = 0x2C;
myMsDelay(2000);
//CCPR1L = 0xCC; // Initalise PWM duty cycle to 00
//CCP1CON = 0x3C;
//myMsDelay(2000);
}

void myMsDelay (unsigned int time)


{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 710; j++);/*Calibrated for a 1 ms delay in MPLAB*/
}

You might also like