Atmega2560 base course (3) - I / O input and output port
Configuration I / O, typically requires three registers DDRx, PINx, PORTx, to I / O port A Case Study
1.I / O port output
Configuring the I / O direction is output, a high DDRx direction register their respective locations, determined by the level of the output PORTx
/*
PA0输出引脚,10ms翻转一次电平
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRA|=_BV(DDA0);
PORTA|=_BV(DDA0);
_delay_ms(10);
while (1)
{
PORTA&=~_BV(DDA0);
_delay_ms(10);
PORTA|=_BV(DDA0);
_delay_ms(10);
}
}
Waveform diagram as follows:
2.I / O port input
Configuring I / O port as an input, low DDRx direction register their respective locations, the input level can be read by a PINx, PORTx determined level when no input
/*
PA0输入引脚,无输入时为高电平,PD0输出引脚,PA0输入低电平时PD0输出低电平,
PA0输入高电平时PD0输出高电平
*/
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/sfr_defs.h>
int main(void)
{
DDRA&=~_BV(DDA0);
PORTA|=_BV(PA0);
DDRD|=_BV(DDD0);
PORTD|=_BV(PD0);
while (1)
{
if (bit_is_clear(PINA,PINA0))
{
PORTD&=~_BV(PD0);
}
else
{
PORTD|=_BV(PD0);
}
}
}