Microcontroller Programming Assignment

935 views
702 views

Published on

Microcontroller,

Published in: Education, Business, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
935
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
67
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Microcontroller Programming Assignment

  1. 1. Program 1: Write program to on off LED with some delay. LED is connected to port Rb0 #define LED PORTB.F0 void main() { TRISB.F0=0; LED=0; while(1) { LED=1; delay_ms(1000); LED=0; delay_ms(1000); } } Design: Program 2: Interface 7 Segment with MC. Display the number from 0-9 on 7 Segment #define LED PORTC int j=0,i=0; void main() { unsigned char arr[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; TRISC=0; LED=0; while(1) { for(i=0;i<10;i++) { LED=arr[i]; delay_ms(100); } } } Design: Program 3: Write a program to count numbers from 00 to FF on PORT B #define LED PORTB unsigned char i=0x00; void main(void) { TRISB=0; LED=OFF; while(i<0xFF) { LED=1; Delay_ms(100); LED=0;
  2. 2. Delay_ms(100); i++; } } Design: Program 4: Write Program to read data on PORT B and send it on PORT C #define a PORTB #define b PORTC void main() { TRISC=0X00; TRISB=0XFF; while(1) { b=a; } } Design: Program 5: Write Program to read data on PORT B and send it on PORT C in inverted form #define a PORTB #define b PORTC void main() { TRISC=0X00; TRISB=0XFF; while(1) { b=~a; } } Design: Program 6: Write a program to input byte of data in MC at Rb0 serially one bit at a time. LSB should come first #define pb0 portb.f0 void main() { unsigned char x; unsigned char assign=0; trisb.f0=1; trisd=0;
  3. 3. for(x=0;x<8;x++) { assign=assign>>1; assign=(pb0 & 0x01)<<7; } portd=assign; } Design: Program 7: Write a program to input byte of data in MC at Rb0 serially one bit at a time. MSB should come first #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; } Design: Program 8: Write a program to output byte of data in MC at Rb0 serially one bit at a time. MSB should go first #define LED PORTB.F0 void main() { unsigned char a,x; unsigned int i; x=0X45; TRISB.F0=0; LED=0; a=x; while(1) { for(i=7;i>=0;i--) { a=x; LED=a>>i; delay_ms(100); if(i==0) { i=7; a=x;
  4. 4. break; } } } } Design: Program 9: Write a program to input byte of data in MC at Rb0 serially one bit at a time. LSB should go first #define LED PORTB.F0 int i=0; void main() { unsigned char x,a; x=0xAA; a=x; TRISB.F0=0; while(1) { a=a>>1; LED=a; delay_ms(100); i++; if(i==7) { i=0; a=x ; } } } Design: Program 10: Write a program to on off AC Bulb 220v using relay n MC # define r PORTB.F0 # define s PORTB.F1 void main() { TRISB.F0=0; TRISB.F1=1; relay=0; while(1) { if(s==0) { r=1; } else {
  5. 5. r=0; } } } Design: Program 11: Write a program to on off DC motor using switch. If switch is on, Motor moves clockwise and vise versa # define S PORTB.F0 # define C PORTC.F0 # define A PORTC.F1 void main(void) { TRISB.F0=1; TRISC.F0=0; TRISC.F1=0; C=0; A=0; S=0; while(1) { if(S==1) { C=1; A=0; } else { C=0; A=1; } } } Design: Program 12: Write a program to control direction of Stepper Motor 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);
  6. 6. step(0x09); delay_ms(1000); } } void step(unsigned char st) { portd=st; } Design: Program 13: Design pressure controller using switch. Specifications of pressure switch is given, At 100 Bar switch is Break. At 5 Bar Switch is Make.. Interface Pressure switch with MC to perform the following tasks. 1.If switch is make, Start the Motor 2.If switch is break, Off the Motor #define M PORTD.F0 unsigned int r=0; void interrupt(void) { M=1; if(INTCON.INT0IF==1) { if(r==1) { INTCON2.INTEDG0=1; r=0; } else { M=1; INTCON2.INTEDG0=0; r=1; } } } void main() { TRISB.F0=1; TRISD.F0=0;
  7. 7. INTCON=0x90; INTCON2.INTEDG0=1; M=0; while(1){ } } Design: Program 14: Write a program to display number of persons entering in building on LCD Program 15: 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 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");
  8. 8. INTCON.INT0IF=0; } if(INTCON3.INT1IF) { Lcd_Out(1, 1, "FURNANCE ON"); INTCON3.INT1IF=0; } } Design: Program 16: Design the interface of keypad with MC & display pressed key on LCD unsigned char kp = ' '; unsigned int get_Kp(); char keypadPort at PORTB; sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD4_bit; sbit LCD_D5 at RD5_bit; sbit LCD_D6 at RD6_bit; sbit LCD_D7 at RD7_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD4_bit; sbit LCD_D5_Direction at TRISD5_bit; sbit LCD_D6_Direction at TRISD6_bit; sbit LCD_D7_Direction at TRISD7_bit; void main() { unsigned int temp; Keypad_Init(); Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1, 1, "LCD ON "); Delay_ms(500);
  9. 9. do { temp = get_Kp(); Lcd_Cmd(_LCD_CLEAR); // Clear display switch (temp) { case 1: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "/"); break; // / case 2: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "*"); break; // * case 3: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "-"); break; // - case 4: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "+"); break; // + case 5: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "9"); break; // 9 case 6: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "6"); break; // 6 case 7: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "3"); break; // 3 case 8: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "="); break; // = case 9: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "8"); break; // 8 case 10: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "5"); break; // 5 case 11: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "2"); break; // 2 case 12: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "0"); break; // 0 case 13: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "7"); break; // 7 case 14: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "4"); break; // 4 case 15: Lcd_Cmd(_LCD_CLEAR); Lcd_Out(1, 1, "1"); break; // 1 case 16: Lcd_Cmd(_LCD_CLEAR); break; // clear } Lcd_Out(2, 1, kp); } while (1); } unsigned int get_Kp() { unsigned int kp1 = 0; do{ kp1 = Keypad_Key_Click(); }while (!kp1); return kp1; } Program 17: 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
  10. 10. unsigned char kp = ' '; unsigned int get_Kp(); char keypadPort at PORTB; sbit LCD_RS at RD0_bit; sbit LCD_EN at RD1_bit; sbit LCD_D4 at RD4_bit; sbit LCD_D5 at RD5_bit; sbit LCD_D6 at RD6_bit; sbit LCD_D7 at RD7_bit; sbit LCD_RS_Direction at TRISD0_bit; sbit LCD_EN_Direction at TRISD1_bit; sbit LCD_D4_Direction at TRISD4_bit; sbit LCD_D5_Direction at TRISD5_bit; sbit LCD_D6_Direction at TRISD6_bit; sbit LCD_D7_Direction at TRISD7_bit; void step(unsigned char); void step(unsigned char st) { portc=st; } void main(){ unsigned int temp; unsigned int key; unsigned char i,n; TRISC=0; Keypad_Init(); Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF); Lcd_Out(1, 1, "LCD ON "); Delay_ms(500); do { temp = get_Kp(); Lcd_Cmd(_LCD_CLEAR); switch (temp) {
  11. 11. 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; 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); } unsigned int get_Kp(){ unsigned int kp1 = 0; do{ kp1 = Keypad_Key_Click(); }while (!kp1); return kp1; }

×