MCA Codeof pic microcontroller practical
MCA Codeof pic microcontroller practical
MCA Codeof pic microcontroller practical
#include <p18f4550.h>
#include <stdlib.h>
#pragma code
// This function is executed as soon as the timer interrupt is generated due to timer overflow
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
#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
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
/*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
#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*/
}
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;
}
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
Timer With
#include <p18f4550.h>
#include <stdlib.h>
#pragma code
// This function is executed as soon as the timer interrupt is generated due to timer overflow
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
}
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
#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 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 ***************************************/
/** V E C T O R R E M A P P I N G *******************************************/
#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);
}