In: Computer Science
Write the C code required to configure pin 0 on port B to be an input with the pullup resistor enabled.
To make port as a input with pullups enabled and read data from port 0
DDRA = 0x00;
PORTA = 0xFF;
To make port B as a tri stated input
DDRB = 0x00;
PORTB = 0x00;
To make lower nibble of port 0 as a output, higher nibble as a input with pullups enabled.
DDRA = 0x0F;
PORTA = 0xF0;
Blink LED on PB0 at a rate of 1Hz.
#include <mega16.h>
#include <delay.h>
void main()
{
DDRB = 0xFF; //PB as output
PORTB= 0x00; //keep all LEDs off
while(1)
{
PORTB &= 0b11111110; //turn LED off
delay_ms(500); //wait for half second
PORTB |= 0b00000001; //turn LED on
delay_ms(500); //wait for half second
};
}
Blink LEDs on Port B with different patterns according to given input.
– Kit : use SW0, SW1
– No Kit : Connect a 2 micro-switches between pin PC0,PC2 and
ground. So that when you press the switch pin is pulled low.
#include <mega16.h>
#include <delay.h>
//declare global arrays for two patterns
unsigned char p1[4] = { 0b10000001,
0b01000010,
0b00100100,
0b00011000 };
unsigned char p2[4] = { 0b11111111,
0b01111110,
0b00111100,
0b00011000 };
void main()
{
unsigned char i; //loop counter
DDRB = 0xFF; //PB as output
PORTB= 0x00; //keep all LEDs off
DDRC = 0x00; //PC as input
PORTC |= 0b00000011; //enable pull ups for
//only first two pins
while(1)
{
//# if SW0 is pressed show pattern 1
if((PINC & 0b00000001)==0)
{
for(i=0;i<3;i++)
{
PORTB=p1[i]; //output data
delay_ms(300); //wait for some time
}
PORTB=0; //turn off all LEDs
}
//# if SW1 is pressed show pattern 2
if((PINC & 0b00000010)==0)
{
for(i=0;i<3;i++)
{
PORTB=p2[i]; //output data
delay_ms(300); //wait for some time
}
PORTB=0; //turn off all LEDs
}
};