//LED AND BUZZER INTERFACING

CIRCUIT DIAGRAM

CODE

#include<P18F4550.h>
#pragma config FOSC = HS  // Oscillator selection
#pragma config WDT = OFF // Disable watchdog timer
#pragma config LVP = OFF  // Low voltage Programmming set to OFF
#pragma config PBADEN = OFF // Disable PORTB Analog Inputs

#define lrbit PORTBbits.RB4 // SW0 interfaced to RB4
#define rlbit PORTBbits.RB5  // SW1 Interfaced to RB5

#define buzzer PORTCbits.RC2  // Buzzer interfaced to RC2
#define relay PORTCbits.RC1  // Relay Interfaced to

void msdelay(unsigned int time){
unsigned int i, j;
for(i=0; i<time; i++)     // Delay configured for given time in milliseconds
for(j=0; j<=275; j++);  // Predefined delay configured for 1 milliseconds
}

void main(){
// initialise the ports

TRISBbits.TRISB4 = 1;    // Input port Connected to SW0
TRISBbits.TRISB5 = 1;    // Input Port Connected to SW1
TRISCbits.TRISC2 = 0;    // Output for Buzzer
TRISCbits.TRISC1 = 0;    // Output for Relay
TRISD = 0x00; // Set Port D as Output port for LEDs
PORTD = 0X00; // Turn Off all the LED i.e. Write data to port

buzzer=0;
relay=0;

while(1){
if(!(lrbit)){
//Switch zero is pressed i.e. connected to ground
relay=1;   // Turn on the relay
buzzer=1; // Turn on the buzzer

// Initiate L-R LED sequence
PORTD = PORTD >> 1; // Right Shift

if(PORTD == 0x00) // Initial Condition
PORTD = 0x80; // 1000 0000

msdelay(250);          // Delay of 250 us

} //End If
else if(!(rlbit)){
// Initiate R-L LED Sequence
relay = 0; // Turn Off the Relay
` buzzer = 0; // Turn Off the Buzzer
PORTD = PORTD << 1; // Left shift

if(PORTD == 0x00) // Initial Condition
PORTD = 0x01; // 0000 0001

msdelay(250); // Delay of 250 us

}  // End Else
} // End While
}


//SERIAL COMMUNICATION

CIRCUIT DIAGRAM

CODE

#include <p18f4550.h> //Include library
#pragma config FOSC = HS //Oscillator Selection
#pragma config WDT = OFF //Disable Watchdog timer
#pragma config LVP = OFF //Disable Low Voltage Programming
#pragma config PBADEN = OFF //Disable PORTB Analog inputs


//Variables
#pragma idata
unsigned char msg1[]={"\n\r 1 2 3 Reciever testing\n\r "};
unsigned char msg2[]={"\n\rUART Test Code"};
unsigned char msg3[]={"\n\rSend  < 5 character message to the Micro-controller \n\r"};
unsigned char msg4[]={"\n\rTransmitted Characters are:\n\r"};
unsigned char msg5[]={"\n\rYou have successfully completed the Serial communication experiment\n\r"};


//Function Prorotypes
void TXbyte(unsigned char data); //To transmit single character
void TXstring (unsigned char *string); //To transmit string


//Start of Main Program
void main()
{
unsigned char i=0;
unsigned char rx_data1 [20]; // Buffer to store received data


TRISCbits.TRISC7=1; // RXD line as input
TRISCbits.TRISC6=0; // TXD line as output


SPBRG = 0x07;
SPBRGH = 0x02; // 0x0208 for 9600 baud

TXSTA = 0x24; // TX enable BRGH=1, SPEN=1
RCSTA = 0x90; // SPEN= 1, continuous RX = 1
BAUDCON = 0x08; // BRG16 = 1

TXstring (msg1); // Transmit string 1
TXstring (msg2); // Transmit string 2
TXstring (msg3); // Transmit string 3

for (i=0; i<10; i++)
{
while (PIR1bits.RCIF == 0); // Wait until data received
rx_data1[i]= RCREG;
// TXbyte(rx_data[i]) ;
//TXbyte(RCREG); // Read the received data
}

TXstring (msg4); // Transmit string 4
TXstring (rx_data1); // Transmit received data
TXstring (msg5); // Transmit string 5
while(1); // loop forever
}
void TXbyte(unsigned char data)
{
while(TXSTAbits.TRMT==0); //wait till transmit buffer is not empty
TXREG = data; // Transmit Data
}
void TXstring(unsigned char *string)
{
unsigned char i=0;
for(i=0;string[i]!='\0';i++) //loop till end of the string
TXbyte(string[i]); //Send single character
}

//ADC INTERFACING

CIRCUIT DIAGRAM

CODE

#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
//Declarations for LCD Connection
#define LCD_DATA PORTD
#define en PORTEbits.RE2
#define rw PORTEbits.RE1
#define rs PORTEbits.RE0
//Function Prototypes
void ADC_Init(void);
unsigned int Get_ADC_Result(void);
void Start_Conversion(void);




void msdelay (unsigned int time);
void init_LCD(void);
void LCD_command(unsigned char cmd);
void LCD_data(unsigned char data);
void LCD_write_string(static char *str);
//Start of main program
void main()
{
char msg1[] = "Result is";
char msg2[] = "ADC O/P:";
unsigned char i, Thousands,Hundreds,Tens,Ones;
unsigned int adc_val;
ADCON1 = 0x0F;
TRISD = 0x00;
TRISE = 0x00;
ADC_Init(); // Init ADC peripheral
init_LCD(); // Init LCD Module
LCD_write_string(msg1); // Display Welcome Message
LCD_command(0xC0); // Goto second line, 0th place of LCD
LCD_write_string(msg2); // Display Message "ADC O/P"
while(1)
{
Start_Conversion(); //Trigger conversion
adc_val= Get_ADC_Result(); //Get the ADC output by polling GO bit
LCD_command (0xC8); //Goto 9th place on second line of LCD
i = adc_val/1000 ; //Get the thousands place
Thousands = i + 0x30; // Convert it to ASCII
LCD_data (Thousands); // Display thousands place
i = (adc_val%1000)/100; //Get the Hundreds place
Hundreds = i + 0x30; // Convert it to ASCII
LCD_data (Hundreds); //Display Hundreds place
Tens = i + 0x30; // Convert it to ASCII
LCD_data (Tens); //Display Tens place
i = adc_val%10 ; //Get the Ones place
Ones = i + 30; // Convert it to ASCII
LCD_data (i + 0x30); //Display Ones place
msdelay(300); //Delay between conversions.
}
}

void ADC_Init()
{
ADCON0=0b00000100; //A/D Module is OFF and Channel 1 is selected
ADCON1=0b00001110; // Reference as VDD & VSS, AN0 set as analog pins
ADCON2=0b10001110; // Result is right Justified
//Acquisition Time 0TAD
//ADC Clk FOSC/64
ADCON0bits.ADON=1; //Turn ON ADC module
}
void Start_Conversion()
{
ADCON0bits.GO=1;
}
//Poll till the conversion complete
unsigned int Get_ADC_Result()
{
unsigned int ADC_Result=0;
while(ADCON0bits.GO);
ADC_Result=ADRESL;
ADC_Result|=((unsigned int)ADRESH) << 8;
return ADC_Result;
}
void msdelay (unsigned int time) //Function to generate delay
{
unsigned int i, j;
for (i = 0; i < time; i++)
for (j = 0; j < 710; j++);//Calibrated for a 1 ms delay in MPLAB
}
void init_LCD(void) // Function to initialise the LCD
{
LCD_command(0x38); // initialization of 16X2 LCD in 8bit mode
msdelay(15);
LCD_command(0x01); // clear LCD
msdelay(15);
LCD_command(0x0C); // cursor off
msdelay(15);
LCD_command(0x80); // go to first line and 0th position
msdelay(15);
}
void LCD_command(unsigned char cmd) //Function to pass command to the

{
LCD_DATA = cmd; //Send data on LCD data bus
rs = 0; //RS = 0 since command to LCD
rw = 0; //RW = 0 since writing to LCD
en = 1; //Generate High to low pulse on EN
msdelay(15);
en = 0;
}
void LCD_data(unsigned char data)//Function to write data to the LCD
{
LCD_DATA = data; //Send data on LCD data bus
rs = 1; //RS = 1 since data to LCD
rw = 0; //RW = 0 since writing to LCD
en = 1; //Generate High to low pulse on EN
msdelay(15);
en = 0;
}
//Function to write string to LCD
void LCD_write_string(static char *str)
{
int i = 0;
while (str[i] != 0)
{
LCD_data(str[i]); // sending data on LCD byte by byte
msdelay(15);
i++;
}
}


//DC MOTOR CONTROL

CIRCUIT DIAGRAM

CODE

#include <p18f4550.h>
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF
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
}
void main()
{
TRISCbits.TRISC2 = 0 ; // Set PORTC, RC2 as output (CCP1)
TRISCbits.TRISC6 = 0 ; // Set PORTC, RC6 as output (DCM IN3)
TRISCbits.TRISC7 = 0 ; // Set PORTC, RC6 as output (DCM IN4)
PR2 = 0xBA; // set PWM Frequency 4KHz
CCP1CON = 0x0C; // Configure CCP1CON as PWM mode.
T2CON = 0x07; //Start timer 2 with prescaler 1:16
PORTCbits.RC6 = 1; // Turn ON the Motor
PORTCbits.RC7 = 0;
while(1) // Endless Loop
{
// ----------Duty Cycle 20%-----------
CCP1CONbits.DC1B0 = 1;
CCP1CONbits.DC1B1 = 0;
CCPR1L = 0x25;
// -----------------------------------
// ----------Duty Cycle 40%-----------
myMsDelay(2000);
CCP1CONbits.DC1B0 = 0;
CCP1CONbits.DC1B1 = 0;
CCPR1L = 0x4B;
myMsDelay(2000);
// -----------------------------------

// ----------Duty Cycle 80%-----------

// -----------------------------------
}
}