The document contains several code examples for interfacing microcontrollers with various hardware components:
1. Code to display numbers 0-9 on a 7-segment display by setting the ports of a microcontroller.
2. Code to read a byte of data from a microcontroller pin serially and store in a variable.
3. Code to control the direction of a stepper motor by setting the states of driver pins.
4. Additional code examples for reading inputs, controlling relays, temperature monitoring, and interfacing a stepper motor with a keypad and LCD display.
1. Interface 7 Segment with MC. Display the number from 0-9 on 7 Segment
CODING
#define LED PORTC
int j=0;
int i=0;
void main()
{
unsigned char LD[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
TRISC=0;
LED=0;
while(1)
{
for(i=0;i<10;i++)
{
LED=LD[i];
delay_ms(200);
}
}
}
input byte of data in MC at Rb0 serially one bit at a time. LSB should come first
CODING
#define pb0 portb.f0
void main()
{
unsigned char x;
unsigned char assign=0;
trisb.f0=1;
trisd=0;
for(x=0;x<8;x++)
{
assign=assign>>1;
assign=(pb0 & 0x01)<<7;
}
portd=assign;
}
2. Input byte of data in MC at Rb0 serially one bit at a time. MSB should come first
CODING
#define pb0 portb.f0
void main()
{
unsigned char x;
unsigned char assign=0;
trisb.f0=1;
trisd=0;
for(x=0;x<8;x++)
{
assign=assign<<1;
assign=pb0 & 0x01;
}
portd=assign;
}
Controlling direction of Stepper Motor
CODING
void step(unsigned char);
void main()
{
unsigned char i;
trisd=0;
for(i=1;i<=40;i++)
{
step(0x03);
delay_ms(1000);
step(0x06);
delay_ms(1000);
step(0x0c);
delay_ms(1000);
step(0x09);
delay_ms(1000);
}
}
void step(unsigned char st)
{
portd=st;
}
ON OFF AC BULB 220V USING RELAY N MC
4. Design Temperature control system for furnace, Two temperature switches are installed
in furnace to sense hotness and coldness. Specification for hot sensors is Temperature
greater than 100 degree, For Cold sensors is temperature less than 35 degree. If Furnace is
Hot Make flame off, If furnace is Cold make flame on
CODING
void main(void)
{
TRISC=0;
INTCON.GIE=1;
INTCON.INT0IF=0;
INTCON.INT0IE=1;
INTCON3.INT1IF=0;
INTCON3.INT1IE=1;
INTCON2.INTEDG0=1;
INTCON2.INTEDG1=1;
while(1)
{ }
}
void interrupt (void)
{
Lcd_Init(&PORTC); // Initialize LCD connected to PORTC
Lcd_Cmd(Lcd_CLEAR); // Clear display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
if(INTCON.INT0IF)
{
Lcd_Out(1, 1, "FURNANCE OFF");
INTCON.INT0IF=0;
}
if(INTCON3.INT1IF)
{
Lcd_Out(1, 1, "FURNANCE ON");
INTCON3.INT1IF=0;
}}
5. Design the interface of stepper motor with MC, Degree of circular motion of stepper
motor is entered from keypad and display degree & Motion on LCD
unsigned char kp = ' ';
char keypadPort at PORTB;
void step(unsigned char);
void step(unsigned char st)
{
portc=st;
}
void main(){
unsigned int temp;
unsigned int key;
unsigned char i,n;
TRISD=0;
Keypad_Init();
Lcd_Init();
Lcd_Config(&PORTD,3,1,2,0,7,6,5,4);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1, "LCD ON ");
Delay_ms(500);
do {
temp = Keypad_key_click();
delay_ms(500);
Lcd_Cmd(_LCD_CLEAR);
switch (temp) {
case 5: Lcd_Cmd(_LCD_CLEAR); key=9; Lcd_Out(1, 1, "9"); break;
case 6: Lcd_Cmd(_LCD_CLEAR); key=6; Lcd_Out(1, 1, "6"); break;
case 7: Lcd_Cmd(_LCD_CLEAR); key=3; Lcd_Out(1, 1, "3"); break;
case 9: Lcd_Cmd(_LCD_CLEAR); key=8; Lcd_Out(1, 1, "8"); break;
case 10: Lcd_Cmd(_LCD_CLEAR); key=5; Lcd_Out(1, 1, "5"); break;
case 11: Lcd_Cmd(_LCD_CLEAR); key=2; Lcd_Out(1, 1, "2"); break;
case 12: Lcd_Cmd(_LCD_CLEAR); key=0; Lcd_Out(1, 1, "0"); break;
6. case 13: Lcd_Cmd(_LCD_CLEAR); key=7; Lcd_Out(1, 1, "7"); break;
case 14: Lcd_Cmd(_LCD_CLEAR); key=4; Lcd_Out(1, 1, "4"); break;
case 15: Lcd_Cmd(_LCD_CLEAR); key=1; Lcd_Out(1, 1, "1"); break;
case 16: Lcd_Cmd(_LCD_CLEAR); break;
}
trisd=0;
n=30/key;
for(i=1;i<=n;i++)
{
step(0x03);
delay_ms(300);
step(0x06);
delay_ms(300);
step(0x0c);
delay_ms(300);
step(0x09);
delay_ms(300);
}
} while (1);
}
7. case 13: Lcd_Cmd(_LCD_CLEAR); key=7; Lcd_Out(1, 1, "7"); break;
case 14: Lcd_Cmd(_LCD_CLEAR); key=4; Lcd_Out(1, 1, "4"); break;
case 15: Lcd_Cmd(_LCD_CLEAR); key=1; Lcd_Out(1, 1, "1"); break;
case 16: Lcd_Cmd(_LCD_CLEAR); break;
}
trisd=0;
n=30/key;
for(i=1;i<=n;i++)
{
step(0x03);
delay_ms(300);
step(0x06);
delay_ms(300);
step(0x0c);
delay_ms(300);
step(0x09);
delay_ms(300);
}
} while (1);
}