Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Projects using Microcontroller Md. Khalilur Rhaman Credit: Interface Lab.

Similar presentations


Presentation on theme: "Introduction to Projects using Microcontroller Md. Khalilur Rhaman Credit: Interface Lab."— Presentation transcript:

1 Introduction to Projects using Microcontroller Md. Khalilur Rhaman Credit: Interface Lab

2 Basic Input and Output // define Switch pins for input #define SW1 RC0_bit #define SW2 RC1_bit // define Switch pin Direction #define SW1_TRIS TRISC0_bit #define SW2_TRIS TRISC1_bit // define LED pins for output #define LED1 RB7_bit #define LED2 RB5_bit #define LED3 RB6_bit // define LED pin Direction #define LED1_TRIS TRISB7_bit #define LED2_TRIS TRISB5_bit #define LED3_TRIS TRISB6_bit void main() { // make SW1 and SW2 pin as input SW1_TRIS = 1; SW2_TRIS = 1; // make LED1, LED2 and LED3 pin as output LED1_TRIS = 0; LED2_TRIS = 0; LED3_TRIS = 0; // turn off all LED LED1 = 0; LED2 = 0; LED3 = 0; if(SW1 == 0) { LED2 = 0; // lit only LED1, if SW1 pressed LED3 = 0; LED1 = 1; } else if(SW2 == 0) { LED1 = 0; // lit only LED2, if SW2 pressed LED3 = 0; LED2 = 1; } else { LED1 = 0; // lit only LED3, if no switch pressed LED2 = 0; LED3 = 1; }

3 Character LCD Interfacing // LCD module connections sbit LCD_RS at RB4_bit; sbit LCD_EN at RB5_bit; sbit LCD_D4 at RB0_bit; sbit LCD_D5 at RB1_bit; sbit LCD_D6 at RB2_bit; sbit LCD_D7 at RB3_bit; sbit LCD_RS_Direction at TRISB4_bit; sbit LCD_EN_Direction at TRISB5_bit; sbit LCD_D4_Direction at TRISB0_bit; sbit LCD_D5_Direction at TRISB1_bit; sbit LCD_D6_Direction at TRISB2_bit; sbit LCD_D7_Direction at TRISB3_bit; // End LCD module connections void main() { Lcd_Init(); // Initialize LCD Lcd_Cmd(_LCD_CLEAR); // Clear display Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off // Write a Character LCD_Chr(2,1,’W’"); // Write some String LCD_Out(1,1,"Hello!"); }

4 DC Voltmeter void main() { char Voltage; unsigned int AdcValue; while(1) { AdcValue = ADC_Read(0); Voltage = (AdcValue * 5) / 256; Delay_ms(100); /* * Voltage contains the actual Input voltage * in AN0. Show Voltage to user using LCD */ }

5 PWM Capture/Compare/PWM

6 Code Functions: PWM(n)_Init(Frequency); PWM(n)_Set_Duty(Duty_Ratio); PWM(n)_Start(); PWM(n)_Stop(); Implementation: void main() { // Initialize PWM1 and PWM2 module at 5KHz PWM1_Init(5000); PWM2_Init(5000); // start PWM1 and PWM2 PWM1_Start(); PWM2_Start(); // Set current duty for PWM1 and PWM2 PWM1_Set_Duty(0); PWM2_Set_Duty(250); while (1) { // Set duty for PWM1 and PWM2 PWM1_Set_Duty(100); PWM2_Set_Duty(150); Delay_ms(2000); // Set duty for PWM1 and PWM2 PWM1_Set_Duty(130); PWM2_Set_Duty(120); Delay_ms(2000); // Set duty for PWM1 and PWM2 PWM1_Set_Duty(200); PWM2_Set_Duty(70); Delay_ms(2000); // Set duty for PWM1 and PWM2 PWM1_Set_Duty(250); PWM2_Set_Duty(0); Delay_ms(2000); }

7 Another Code while (1) { // endless loop if (RA0_bit) { // button on RA0 pressed Delay_ms(40); current_duty++; // increment current_duty PWM1_Set_Duty(current_duty); } if (RA1_bit) { // button on RA1 pressed Delay_ms(40); current_duty--; // decrement current_duty PWM1_Set_Duty(current_duty); } if (RA2_bit) { // button on RA2 pressed Delay_ms(40); current_duty1++; // increment current_duty1 PWM2_Set_Duty(current_duty1); } if (RA3_bit) { // button on RA3 pressed Delay_ms(40); current_duty1--; // decrement current_duty1 PWM2_Set_Duty(current_duty1); } Delay_ms(5); // slow down change pace a little } unsigned short current_duty, old_duty, current_duty1, old_duty1; void InitMain() { ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; PORTA = 255; TRISA = 255; // configure PORTA pins as input PORTB = 0; // set PORTB to 0 TRISB = 0; // designate PORTB pins as output PORTC = 0; // set PORTC to 0 TRISC = 0; // designate PORTC pins as output PWM1_Init(5000); // Initialize PWM1 module at 5KHz PWM2_Init(5000); // Initialize PWM2 module at 5KHz } void main() { InitMain(); current_duty = 16; // initial value for current_duty current_duty1 = 16; // initial value for current_duty1 PWM1_Start(); // start PWM1 PWM2_Start(); // start PWM2 PWM1_Set_Duty(current_duty); // Set current duty for PWM1 PWM2_Set_Duty(current_duty1); // Set current duty for PWM2

8 PWM Capture/Compare/PWM

9 Code void main() { short current_duty_1 = 16; // initial value for current_duty_1 short current_duty_2 = 16; // initial value for current_duty_2 TRISD = 0xFF; // PORTD as input TRISC = 0x00; // PORTC as output PWM1_Init(5000); // Initialize PWM1 PWM2_Init(5000); // Initialize PWM2 PWM1_Start(); // start PWM1 PWM2_Start(); // start PWM2 PWM1_Set_Duty(current_duty_1); // Set current duty for PWM1 PWM2_Set_Duty(current_duty_2); // Set current duty for PWM2 while (1) // endless loop { if (!RD0_bit) // if button on RD0 pressed { Delay_ms(40); current_duty_1++; // increment current_duty_1 PWM1_Set_Duty(current_duty_1); //Change the duty cycle } if (!RD1_bit) // button on RD1 pressed { Delay_ms(40); current_duty_1--; // decrement current_duty_1 PWM1_Set_Duty(current_duty_1); } if (!RD2_bit) // if button on RD2 pressed { Delay_ms(40); current_duty_2++; // increment current_duty_2 PWM2_Set_Duty(current_duty_2); } if (!RD3_bit) // if button on RD3 pressed { Delay_ms(40); current_duty_2--; // decrement current_duty_2 PWM2_Set_Duty(current_duty_2); } Delay_ms(10); // slow down change pace a little }


Download ppt "Introduction to Projects using Microcontroller Md. Khalilur Rhaman Credit: Interface Lab."

Similar presentations


Ads by Google