How to Relay interfacing with microcontroller use Mikro C compiler
Welcome to MINA TECHNOLOGY.
In this tutorial we will learn how to Relay interfacing with Microcontroller. Here I am use PIC16F877A Microcontroller and LM16x2 Lcd display. You can watch the video or read the written tutorial below.
Circuit Diagram for Relay Interface:
Mikro C Code:
# define SW PORTD.RD7 // switch is connect to RD7
// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End lcd module connnections
void main() {
TRISC = 0X00; // PORTC in output
TRISD = 0X80; // RD7 is input
PORTC = 0X00; // clear PORTC
PORTD = 0X00; // Clear PORTD
Lcd_Init(); // Initialize Lcd module
Lcd_Cmd(_LCD_CURSOR_OFF); // Lcd cursor off
Lcd_Cmd(_LCD_CLEAR); // Led clear
Lcd_Out(1,3,"DEVELOPED BY"); // Lcd show "DEVELOPED BY"
Lcd_Out(2,1,"MINA TECHNOLOGY"); // Lcd show "MINA TECHNOLOGY"
delay_ms(2000); // 2s delay
Lcd_Cmd(_LCD_CLEAR); // Lcd clear
Lcd_Out(1,1,"Relay Interface"); // Lcd show "LATCH"
Lcd_Out(2,1,"Relay:"); // Lcd show "LED:"
Lcd_Out(2,8,"Deactive");
while(1){ // Endless loop
if(SW==1){
while(SW==1); // wait for SW release
PORTC.F0 = ~PORTC.F0; // Toggle the PORTC.RC0
if(PORTC){
Lcd_Out(2,8,"Active ");
}
else{
Lcd_Out(2,8,"Deactive");
}
// Increase value
}
}
}
Click the download button for program file:
In this tutorial we will learn how to Relay interfacing with Microcontroller. Here I am use PIC16F877A Microcontroller and LM16x2 Lcd display. You can watch the video or read the written tutorial below.
Circuit Diagram for Relay Interface:
Mikro C Code:
# define SW PORTD.RD7 // switch is connect to RD7
// Lcd module connections
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End lcd module connnections
void main() {
TRISC = 0X00; // PORTC in output
TRISD = 0X80; // RD7 is input
PORTC = 0X00; // clear PORTC
PORTD = 0X00; // Clear PORTD
Lcd_Init(); // Initialize Lcd module
Lcd_Cmd(_LCD_CURSOR_OFF); // Lcd cursor off
Lcd_Cmd(_LCD_CLEAR); // Led clear
Lcd_Out(1,3,"DEVELOPED BY"); // Lcd show "DEVELOPED BY"
Lcd_Out(2,1,"MINA TECHNOLOGY"); // Lcd show "MINA TECHNOLOGY"
delay_ms(2000); // 2s delay
Lcd_Cmd(_LCD_CLEAR); // Lcd clear
Lcd_Out(1,1,"Relay Interface"); // Lcd show "LATCH"
Lcd_Out(2,1,"Relay:"); // Lcd show "LED:"
Lcd_Out(2,8,"Deactive");
while(1){ // Endless loop
if(SW==1){
while(SW==1); // wait for SW release
PORTC.F0 = ~PORTC.F0; // Toggle the PORTC.RC0
if(PORTC){
Lcd_Out(2,8,"Active ");
}
else{
Lcd_Out(2,8,"Deactive");
}
// Increase value
}
}
}
Click the download button for program file:
download |
No comments