Advertisement
kyos

GPIO tut 1

Aug 17th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * tutorial_1.c
  3.  *
  4.  * Created: 2016/08/15 12:04:52 PM
  5.  *  Author: Kai von Fritschen   214200809
  6.  
  7.  In this prac, we must use 3 digital inputs in order to generate 8 different states. 5 LED's are connected to the digital outputs and must light up according to the different states on the inputs. I used the internal pull up resistors on inputs 0,1 and 2 on PORT D
  8.  */
  9.  
  10.  
  11. #include <avr/io.h>
  12. #define F_CPU 16000000UL
  13. #include <util/delay.h>
  14.  
  15.  
  16.  
  17.  
  18. void outputs_off (void);        // function prototype
  19. void outputs_on (void);         // function prototype
  20. void sequence1 (void);          // function prototype
  21. void sequence2 (void);          // function prototype
  22.  
  23. int main(void)
  24. {
  25.     DDRB = 0xff;        // PORT B is configured to be outputs
  26.     DDRD = 0x00;        // PORT D is configured to be inputs
  27.     PORTD = 0x07;       // PORT D is configured to use ALL input's pullup resistors in PORT D (floating input means input is HIGH)
  28.    
  29.     while(1)
  30.     {
  31.    
  32.      if (PIND == 0x00){     // if digital pin 0,1,2 are all low
  33.         outputs_off();  // calls function to turn off all outputs
  34.         }
  35.             else if (PIND == 0x07){     // if DI pins 0,1,2 are all HIGH
  36.                     sequence2();        // calls function to run sequence 2
  37.                        
  38.                     }
  39.                 else if  (PIND == 0x03){    // if DI pins 0 and 1 are HIGH
  40.                         sequence1();            // calls function to run sequence 1
  41.                        
  42.                         }
  43.                     else if ((PIND == 0x01) || ( PIND == 0x02) ||( PIND == 0x04) || (PIND == 0x05) || (PIND == 0x06))    // if any other states are met on the DI pins 0,1,2
  44.                             {   outputs_on();       // turn all outputs HIGH
  45.                             }
  46.        
  47. }
  48. }
  49. void outputs_off (){
  50.     PORTB= 0x00;            // turns all outputs off   
  51.     }
  52.  
  53. void outputs_on (){
  54.     PORTB= 0x3E;            // turn digital pins 13,12,10,9 and 8 HIGH
  55.     }
  56. void sequence1 (){
  57.     PORTB= 0x2A;            // turn digital pins 13, 11 and 9 HIGH
  58.     _delay_ms(2000);
  59.     PORTB= PORTB>>1;        // shift bits to the left by one, making pins 11 and 10 HIGH
  60.     _delay_ms(2000);
  61.     }
  62. void sequence2 (){
  63.     PORTB= 0x02;            // sets pin 9 high
  64.     _delay_ms(1000);   
  65.     PORTB = PORTB<<2;       // shifts bit by two (to the left), making only pin 11 high
  66.     _delay_ms(1000);
  67.     PORTB= PORTB<<2;        // shifts bit by two (to the left), making only pin 13 high
  68.     _delay_ms(1000);
  69.     PORTB = PORTB>>1;       // shifts bit by one (to the right), making only pin 12 high
  70.     PORTB |= (1<<2);        // sets bit 2 high instantly after previous command, making pin 10 high
  71.     _delay_ms(1000);
  72.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement