Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * tutorial_1.c
- *
- * Created: 2016/08/15 12:04:52 PM
- * Author: Kai von Fritschen 214200809
- 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
- */
- #include <avr/io.h>
- #define F_CPU 16000000UL
- #include <util/delay.h>
- void outputs_off (void); // function prototype
- void outputs_on (void); // function prototype
- void sequence1 (void); // function prototype
- void sequence2 (void); // function prototype
- int main(void)
- {
- DDRB = 0xff; // PORT B is configured to be outputs
- DDRD = 0x00; // PORT D is configured to be inputs
- PORTD = 0x07; // PORT D is configured to use ALL input's pullup resistors in PORT D (floating input means input is HIGH)
- while(1)
- {
- if (PIND == 0x00){ // if digital pin 0,1,2 are all low
- outputs_off(); // calls function to turn off all outputs
- }
- else if (PIND == 0x07){ // if DI pins 0,1,2 are all HIGH
- sequence2(); // calls function to run sequence 2
- }
- else if (PIND == 0x03){ // if DI pins 0 and 1 are HIGH
- sequence1(); // calls function to run sequence 1
- }
- 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
- { outputs_on(); // turn all outputs HIGH
- }
- }
- }
- void outputs_off (){
- PORTB= 0x00; // turns all outputs off
- }
- void outputs_on (){
- PORTB= 0x3E; // turn digital pins 13,12,10,9 and 8 HIGH
- }
- void sequence1 (){
- PORTB= 0x2A; // turn digital pins 13, 11 and 9 HIGH
- _delay_ms(2000);
- PORTB= PORTB>>1; // shift bits to the left by one, making pins 11 and 10 HIGH
- _delay_ms(2000);
- }
- void sequence2 (){
- PORTB= 0x02; // sets pin 9 high
- _delay_ms(1000);
- PORTB = PORTB<<2; // shifts bit by two (to the left), making only pin 11 high
- _delay_ms(1000);
- PORTB= PORTB<<2; // shifts bit by two (to the left), making only pin 13 high
- _delay_ms(1000);
- PORTB = PORTB>>1; // shifts bit by one (to the right), making only pin 12 high
- PORTB |= (1<<2); // sets bit 2 high instantly after previous command, making pin 10 high
- _delay_ms(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement