Chapter2 2014 cont

527 views
420 views

Published on

chapter 2 (cont.), microcontroller (dae 32203)

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

  • Be the first to like this

No Downloads
Views
Total views
527
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
31
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Chapter2 2014 cont

  1. 1. MICROCONTROLLER 16F877A USING C LANGUAGE (MIKROC VER. 8.0) CONT…
  2. 2. MICROCONTROLLER • The microprocessor can provide, in a simplified form, all the main elements of the conventional microprocessor system on one chip. • Used to control a small system.
  3. 3. INPUT /OUTPUT PORTS • Each microcontroller has one or more registers (called ports) connected to the microcontroller pins. • Why input/output? Because you can change a pin function as you wish. • For example, suppose you want your device to turn on/off three signal LEDs and simultaneously monitor the logic state of five sensors or push buttons. • Some of the ports need to be configured so that there are three outputs (connected to LEDs) and five inputs (connected to sensors). • It is simply performed by software, which means that a pin function can be changed during operation.
  4. 4. INPUT /OUTPUT PORTS • For practical reasons, many I/O pins have two or three functions. If a pin is used as any other function, it may not be used as a general purpose input/output pin; and • Every port has the corresponding TRIS register: TRISA, TRISB, TRISC etc. which determines performance, but not the contents of the port bits. By clearing some bit of the TRIS register (bit=0), the corresponding port pin is configured as output. Similarly, by setting some bit of the TRIS register (bit=1), the corresponding port pin is configured as input. This rule is easy to remember 0 = Output, 1 = Input. In order pins’ operation can match internal 8-bit organization, all of them are, similar to registers, grouped into five so called ports denoted by A, B, C, D and E. They all have several features in common:
  5. 5. PROGRAMMING LANGUAGE  The microcontroller executes the program loaded in its Flash memory.  A specialized program in a PC called compiler is in charge of compiling program into machine language (hex file).  The main advantage of C languages is simplicity of program writing.
  6. 6.  A rough illustration of what is going on during the process of compiling the program from higher to lower programming language.
  7. 7. Simplify of C compared to assembly Same program compiled into assembly code
  8. 8. THE BASICS OF C PROGRAMMING LANGUAGE /* Text between these signs is not compiled into executable code and represents a comment. */ //This sign is used for short comments //within one program line
  9. 9. MikroC Ver. 8.0 First Look • mikroC for PIC is a full-featured ANSI C compiler for PIC devices from Microchip®. • It is the best solution for developing code for PIC devices. It features intuitive IDE, powerful compiler with advanced optimizations, lots of hardware and software libraries, and additional tools that will help you in your work. • Compiler comes with comprehensive Help file and lots of ready-to-use examples designed to get you started in no time. Press F1 to get help.
  10. 10. Case 1 : Turn On LED Special Function RegistersSpecial Function Registers InfoInfo TRISB xxxxxxxx PORTB xxxxxxxx void main() { TRISB = 0b00000000; PORTB = 0b00000001; } C programming code to turn the LED on. LED is connected to PORTB bit 0
  11. 11. Case 1 : Turn On LED Special Function RegistersSpecial Function Registers InfoInfo TRISB xxxxxxxx PORTB xxxxxxxx void main() { TRISB = 0b00000000; PORTB = 0b00000001; } As soon as the microcontroller is turned on, it will execute the main() function
  12. 12. Case 1 : Turn On LED Special Function RegistersSpecial Function Registers InfoInfo TRISB 00000000 PORTB xxxxxxxx void main() { TRISB = 0b00000000; PORTB = 0b00000001; } Set all bits TRISB register to 0. This makes the whole bit of PORTB as output. * 0 = Output, 1 = Input
  13. 13. Case 1 : Turn On LED Special Function RegistersSpecial Function Registers InfoInfo TRISB 00000000 PORTB 00000001 void main() { TRISB = 0b00000000; PORTB = 0b00000001; } • Set binary value 00000001 to PORTB. • LED D1 lights up. * 0 = 0Volt, 1 = 5Volt Red indicator: 5V = Digital 1 Blue indicator: 0V = Digital 0
  14. 14. Case 1 : Turn On LED Special Function RegistersSpecial Function Registers InfoInfo TRISB 00000000 PORTB 00000001 void main() { TRISB = 0b00000000; PORTB = 0b00000001; } • Program END • LED D1 light up forever •Code execution ended.
  15. 15. Case 1 : Turn On LED void main() { TRISB = 0b00000000; PORTB = 0b00000001; } Various styles of code programming. Each of them produce the same output. void main() { TRISB.F0 = 0; //Only bit 0 set as output PORTB.F0 = 1; } = #define LED PORTB.F0 //LED at PORTB bit 0 void main() { TRISB = 0b00000000; LED = 1; } = #define LED PORTB void main() { TRISB = 0b00000000; LED = 0b00000001; } =
  16. 16. Software Delay Software delay is a special function to make the flow of the system hold/pause to specific times. The delay can be set in microsecond and millisecond. • Delay_us(10); /* 10 microseconds pause */ • Delay_ms(1000); /* 1000ms ~ 1 second pause */ List of delay function codes in MikroC:
  17. 17. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB xxxxxxxx PORTB xxxxxxxx •Blink the LED1 and LED2 for 1 second •LED1 at PORTB bit 1 •LED2 at PORTB bit 3 20Mhz
  18. 18. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB xxxxxxxx PORTB xxxxxxxx •Define PORTB as LEDS 20Mhz
  19. 19. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB xxxxxxxx PORTB xxxxxxxx •Execute the main() function 20Mhz
  20. 20. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB 00000000 PORTB xxxxxxxx Set all bits TRISB register to 0. This makes the whole bit of PORTB as output. * 0 = Output, 1 = Input 20Mhz
  21. 21. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } 20Mhz TRISB 00000000 PORTB 00001010 LED1 = ON LED2 = ON * 0 = 0V, 1 = 5V 20Mhz
  22. 22. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } 20Mhz TRISB 00000000 PORTB 00001010 LED1 and LED2 still ON and wait for 1 second 20Mhz
  23. 23. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB 00000000 PORTB 00000000 20Mhz LED1 = OFF LED2 = OFF * 0 = 0V, 1 = 5V
  24. 24. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB 00000000 PORTB 00000000 20Mhz LED1 and LED2 still OFF and wait for 1 second
  25. 25. Case 2 : LED Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } TRISB 00000000 PORTB 00000000 20Mhz • After wait for 1 second the program END. • LED1 and LED2 not blink anymore • The LEDs only ON and OFF one time. Code execution ended
  26. 26. Case 2 : LED Blinking #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); }
  27. 27. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB Void main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB xxxxxxxx PORTB xxxxxxxx 20Mhz • Make LED1 and LED2 blinking forever. From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  28. 28. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB xxxxxxxx PORTB xxxxxxxx 20Mhz •Define PORTB as LEDS From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  29. 29. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB xxxxxxxx PORTB xxxxxxxx 20Mhz Execute the main() function From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  30. 30. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB xxxxxxxx 20Mhz Set all PORTB bits as output From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  31. 31. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB xxxxxxxx 20Mhz The condition of while is always 1 (true). So this is an infinite loop. From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  32. 32. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB 00001010 20Mhz LED1 = ON LED2 = ON * 0 = 0V, 1 = 5V 20Mhz From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  33. 33. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB 00001010 20Mhz LED1 and LED2 still ON and wait for 1 second 20Mhz From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  34. 34. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB 00000000 20Mhz 20Mhz LED1 = OFF LED2 = OFF * 0 = 0V, 1 = 5V From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  35. 35. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB 00000000 20Mhz 20Mhz Wait for 1 second From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever.
  36. 36. Case 3 : LED Always Blinking Special Function RegistersSpecial Function Registers InfoInfo #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); } } TRISB 00000000 PORTB 00001010 From the previous code, the LEDs only blink for 1 time. To make the LEDs always blink, add “while(1)” command to make the program looping forever. 20Mhz • After wait for 1 second, program goes to loop and start again to light up the LEDs.
  37. 37. • As a result, the two led flashes all the time. • The use of while(1) is essential for the continuation of the program. Case 3 : LED Always Blinking “Animated Gif”
  38. 38. Various styles of code programming. Each of them produce the same output. Case 3 : LED Always Blinking #define LEDS PORTB main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LEDS = 0b00001010; //D1 and D2 light up. Delay_ms(1000); //Pause for 1 sec LEDS = 0b00000000; Delay_ms(1000); //Pause for 1 sec } } #define LED1 PORTB.F1 #define LED2 PORTB.F3 main() { TRISB = 0b00000000; //All PORTB bits are output while(1) // endless loop { LED1 = 1; //D1 ON LED2 = 1; //D2 ON Delay_ms(1000); //Pause for 1 sec LED1 = 0; //D1 OFF LED2 = 0; //D2 OFF Delay_ms(1000); //Pause for 1 sec } } =
  39. 39. Case 4 : LED Running Light “Animated Gif” • LED in running light with 1 second delay. InfoInfo
  40. 40. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC xxxxxxxx PORTC xxxxxxxx • Press the button to light up LED and LED off when release the button. B1 (Input) D1 (Output) 1 (5V) 0 (0V) 0 (0V) 1 (5V) void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } }
  41. 41. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC xx1xxxxx void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } Set PORTC bit 5 as input, the rest as output
  42. 42. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00100000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } Clear all bits at PORTC
  43. 43. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00100000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } The condition of while is always 1 (true). So this is an infinite loop.
  44. 44. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00100000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •Check if input at bit 5 get 0 or 1, in this case button is not press. So bit 5 got 1. Digital 1 = 5Volt
  45. 45. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00100000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •The if command got false signal, so jump to else.
  46. 46. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00100000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •Clear bit 2 (LED off)
  47. 47. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00000000 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •Program goes to loop and start again to check the button •This time button is pressed and bit 5 got 0 (TRUE signal) Digital 0 = 0Volt
  48. 48. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00000100 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •PIC send 5Volt to PORTC bit 5 •D1 light up.
  49. 49. Case 5 : LED and Button Special Function RegistersSpecial Function Registers InfoInfo TRISC 00100000 PORTC 00000100 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, the // rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button1 pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button1 not pressed) { PORTC.F2 = 0; //LED1 OFF } } } •Program will goes to loop and start again to check the button •This process is repeated like previous.
  50. 50. Case 5 : LED and Button “Animated Gif” * LED is always ON as long as the button is pressed and OFF when the button is released.
  51. 51. Various styles of code programming. Each of them produce the same output. void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, //the rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (PORTC.F5 == 0) //if got 0 or 0V (button is pressed) { PORTC.F2 = 1; //LED1 ON } else // got 1 or 5V (button is not pressed) { PORTC.F2 = 0; //LED1 OFF } } } #define BUTTON PORTC.F5 #define LED1 PORTC.F2 void main(){ TRISC = 0b00100000; //Bit 5 PORTC set to Input, //the rest as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (BUTTON==0) //if got 0 or 0V (button is pressed) { LED1 = 1; //LED1 ON } else // got 1 or 5V (button is not pressed) { LED1 = 0; //LED1 OFF } } } = Case 5 : LED and Button
  52. 52. Case 6 : LED and Button Method 2 InfoInfo •In this example, LED will emit light for 1 second only when button is released. After that LED will OFF. •Unlike the previous methods ,when a button is pressed, the LED lights up quickly.
  53. 53. Case 7 : 2 LEDs and 2 Buttons InfoInfo •In this example, there are two buttons that use different circuits. B1 and B2 respectively give the values of 0​​ and 1 when pressed. “Animated Gif”
  54. 54. Case 8 : Relay #define RELAY PORTC.F5 //Relay at PORTC bit 5 #define LED PORTB.F0 //LED1 at PORTB bit 0 #define BON PORTA.F0 //Button ON at PORTA bit 0 #define BOFF PORTA.F2 //Button OFF at PORTA bit 2 void main(){ TRISA = 0b11111111; //Set all bits of PORTA as input ADCON1 = 0b00000110; //Set PORTA as digital input //ADCON1 is only for PORTA TRISB = 0b00000000; //Set all bits of PORTB as output PORTB = 0b00000000; //Clear all bits of PORTB TRISC = 0b00000000; //Set all bits of PORTC as output PORTC = 0b00000000; //Clear all bits of PORTC while(1) //infinite loop { if (BON == 1) //if Button ON is pressed { RELAY = 1; LED = 1; } if (BOFF == 1) //if Button OFF is pressed { RELAY = 0; LED = 0; } } //end of while } //end of main() “Animated Gif” Info: Two buttons to activate and deactivate relay and LED. Relay connected to 12V lamp.
  55. 55. #define sevensegment PORTB void main() { TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { //Using dec2bcd.Convert 8 bit decimal to binary code decimal //example: dec2bcd(13) = 00010011, dec2bcd(88) = 10001000 sevensegment = dec2bcd(1); //binary code decimal of no. 1 (00000001) Delay_ms(1000); sevensegment = dec2bcd(2); //binary code decimal of no. 2 (00000010) Delay_ms(1000); sevensegment = dec2bcd(3); //binary code decimal of no. 3 (00000011) Delay_ms(1000); } } Case 9 : Seven Segment with decoder “Animated Gif”
  56. 56. Various styles of code programming. Each of them produce the same output. = Case 9 : Seven Segment with decoder #define sevensegment PORTB void main() { TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { //Using dec2bcd.Convert 8 bit decimal to binary code decimal //example: dec2bcd(13) = 00010011, dec2bcd(88) = 10001000 sevensegment = dec2bcd(1); //binary code decimal of no. 1 (00000001) Delay_ms(1000); sevensegment = dec2bcd(2); //binary code decimal of no. 2 (00000010) Delay_ms(1000); sevensegment = dec2bcd(3); //binary code decimal of no. 3 (00000011) Delay_ms(1000); } } #define sevensegment PORTB int i; void main() { TRISB = 0b00000000; //PORTB as output PORTB = 0b00000000; //Clear PORTB while(1) //infinite loop { for (i=1;i<=3;i++) //using “for loop” { sevensegment = dec2bcd(i); Delay_ms(1000); } } }
  57. 57. Interrupt

×