Read Specific Port Bit State Arduino Andset a Variable Equil to It

Atmel AVR Tutorial Accesing GPIO PORTs

Tutorial

Overview

You cannot imagine to apply microcontroller without using any of its i/o pins. Finally its all about : taking input , processing it and generating output ! Thus i/o registers and their correct settings is indispensable part while learning to program whatsoever uC.

Nosotros will larn how to use Atmel AVR's GPIO ports and actually 'code'  for writing/reading data to/from port pins in this AVR tutorial. It is slightly confusing for beginners, however in one case y'all understand it, y'all will certainly appreciate the way it is designed. To go quick background about AVR microcontrollers, refer to Intoduction to AVR Tutorial on this blog.

Contents

  • 1 Tutorial
    • 1.1 Overview
    • 1.2 Registers
    • one.3 DDRx register
    • ane.iv PINx register
    • i.5 PORTx annals
    • 1.6 Summary
  • 2 Hands on …
    • 2.1 Glimmer LED on PB0 at a rate of 1Hz.
    • ii.2 Blink LEDs on PB with  dissimilar patterns co-ordinate to given input.
  • iii Other AVR Tutorials

Annotation : I volition oft refer to  'configuring pin' or but 'pivot'. Remember, a port has multiple pins. Thus in order to modify setting for one port, you have to alter setting for all port pins of that port. To change setting for one single pivot of the port, you lot have to change a particular bit in associated register. Got that ? If non read this para again.

Registers

Atmel AVR is 8 flake microcontroller. All its ports are 8 bit wide. Every port has 3 registers associated with information technology each 1 with eight bits. Every bit in those registers configure pins of detail port. Bit0 of these registers is associated with Pin0 of the port, Bit1 of these registers is associated with Pin1 of the port, …. and like wise for other bits.

These iii registers are every bit follows :
(x tin can be replaced by A,B,C,D as per the AVR y'all are using)
– DDRx register
– PORTx register
– PINx register

DDRx annals

DDRx (Data Direction Register) configures data direction of port pins. Ways its setting determines whether port pins will be used for input or output. Writing 0 to a scrap in DDRx makes corresponding port pin as input, while writing i to a bit in DDRx makes corresponding port pin as output.

Instance:

  • to make all pins of port A as input pins :
    DDRA = 0b00000000;
  • to make all pins of port A as output pins :
    DDRA = 0b11111111;
  • to make lower nibble of port B as output and higher nibble as input :
    DDRB = 0b00001111;

PINx annals

PINx (Port IN) used to read information from port pins. In guild to read the data from port pivot, first yous have to alter port's data direction to input. This is done by setting bits in DDRx to nil. If port is made output, and then reading PINx register will give y'all data that has been output on port pins.

Now there are two input modes. Either you tin can use port pins as tri stated inputs or you tin actuate internal pull up. Information technology will be explained shortly.

Example :

  • to read data from port A.
    DDRA = 0x00;                //Set port a as input                x = PINA;                //Read contents of port a                              

PORTx annals

PORTx is used for two purposes.

1) To output information  :  when port is configured as output

When you prepare bits in DDRx to 1, corresponding pins becomes output pins. Now you tin can write data into respective bits in PORTx register. This will immediately modify state of output pins according to data you have written.

In other words to output data on to port pins, yous take to write it into PORTx register. Notwithstanding exercise non forget to set data management equally output.

Example :

  • to output 0xFF data on port b
    DDRB = 0b11111111;                //set all pins of port b every bit outputs                PORTB = 0xFF;                //write data on port                              
  • to output data in variable ten on port a
    DDRA = 0xFF;                                  //make port a every bit output                PORTA = x;                //output variable on port              
  • to output data on only 0th bit of port c
    DDRC |= 0b00000001;                //fix but 0th pin of port c as output                PORTC |= 0b00000001;                //make information technology loftier.                              

2) To activate/deactivate pull upward resistors – when port is configures as input

When you ready bits in DDRx to 0, i.due east. make port pins as inputs, then corresponding bits in PORTx register are used to activate/conciliate pull-up registers associated with that pivot. In order to actuate pull-up resister, set up bit in PORTx to 1, and to deactivate (i.e to make port pin tri stated) set it to 0.

In input fashion, when pull-upwards is enabled, default country of pin becomes 'ane'. And then fifty-fifty if you don't connect annihilation to pin and if you lot try to read it, it will read equally 1. Now, when you externally drive that pin to zip(i.due east. connect to basis / or pull-downward), simply then it volition be read as 0.

However, if you configure pin every bit tri-state. Then pin goes into country of loftier impedance. We can say, it is now simply connected to input of some OpAmp inside the uC and no other circuit is driving it from uC. Thus pivot has very high impedance. In this example, if pin is left floating (i.e. kept unconnected) then even small static charge present on surrounding objects tin can change logic state of pin. If you try to read corresponding bit in pin register, its country cannot exist predicted. This may cause your programme to go haywire, if it depends on input from that item pin.

Thus while, taking inputs from pins / using micro-switches to take input, e'er enable pull-upward resistors on input pins.

Note: while using on-bit ADC, ADC port pins must exist configured as tri-stated input.

Example :

  • to make port a as input with pull-ups enabled and read data from port a
    DDRA = 0x00;                //make port a as input                PORTA = 0xFF;                //enable all pull-ups                                y = PINA;                //read data from port a pins              
  • to make port b as tri stated input
    DDRB  = 0x00;                //make port b every bit input                PORTB = 0x00;                //disable pull-ups and make it tri state              
  • to make lower crumb of port a as output, higher nibble every bit input with pull-ups enabled
    DDRA  = 0x0F;                //lower beak> output, college nib> input                PORTA = 0xF0;                //lower pecker> ready output pins to 0,                                //higher pecker> enable pull-ups              

Summary

Following table lists annals chip settings and resulting function of port pins. Annotation: Convention of "10.n" is a psuedo code. You need to use appropriate AND (&), OR (|), and bitshift (<<, >>) operations to extract the desired bit field.

register bits →
pin function↓
DDRx.n PORTx.n PINx.due north
tri stated input 0 0 read data scrap
x = PINx.n;
pull-up input 0 1 read information scrap
x = PINx.due north;
output ane write data bit
PORTx.n = ten;
writing 1 toggles the
state of the output pivot.
PINx.n = 1;

Easily on …

  • Yous tin blazon post-obit programs in CodeVisionAVR or Atmel Studio and test them on your development kit .
  • To know, how to burn lawmaking into uC using PonyProg2000, refer to the post on Making and Using Simple AVR Developer. You tin also use USBasp and Avrdude to program AVR controllers. Refer to the Avrdude tutorial to understand how to download/burn programs on AVR microcontroller.
  • If you don't have development kit, only connect 8 LEDs to Pb (i.east port b).
  • In Project>Configure>C compiler do non forget to set following options :
    Scrap   : ATmega16 (or whatever you are using)
    Clock : 16MHz (or whatever you are using)
  • Experiment with these programs and try to learn more from information technology.

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;            //plow LED off            delay_ms(500);            //wait for one-half 2d            PORTB |= 0b00000001;            //turn LED on                        delay_ms(500);            //wait for half second            };         }          

Blink LEDs on PB with  different patterns according to given input.

– Kit : utilise SW0, SW1
– No Kit : Connect a 2 micro-switches between pin PC0,PC2 and footing. So that when you lot press the switch pin is pulled low.

#include <mega16.h> #include <delay.h>            //declare global arrays for 2 patterns            unsigned            char            p1[4] = { 0b10000001,                         0b01000010,                             0b00100100,                         0b00011000 };  unsigned            char            p2[iv] = { 0b11111111,                         0b01111110,                             0b00111100,                         0b00011000 };            void            master() { unsigned            char            i;            //loop counter            DDRB = 0xFF;            //Lead every bit output            PORTB= 0x00;            //keep all LEDs off            DDRC = 0x00;            //PC equally input            PORTC |= 0b00000011;            //enable pull ups for            //simply 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 fourth dimension            }             PORTB=0;            //plough off all LEDs            }            //# if SW1 is pressed evidence pattern 2            if((PINC & 0b00000010)==0)                    {            for(i=0;i<3;i++)             {                 PORTB=p2[i];            //output information            delay_ms(300);            //look for some fourth dimension            }             PORTB=0;            //turn off all LEDs            }      };         }          

Other AVR Tutorials

  • Lot more examples of AVR GPIO setup and command are listed in this post: ATMEL AVR Tips – Input Output Ports Code Snippets
  • A Video lecture on Introductory AVR programming and GPIO configuration is here: AVR microcontroller video tutorial – Function ane

#atmel-avr #avr #gpio #pin #ddr #port #tutorial

newmanancen1954.blogspot.com

Source: https://www.elecrom.com/avr-tutorial-2-avr-input-output/

0 Response to "Read Specific Port Bit State Arduino Andset a Variable Equil to It"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel