Digital I/O on AVR in mikroPascal

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Digital I/O on AVR in mikroPascal

#1 Post by mikroSeven » 20 Nov 2016 22:15

Programming mikroPascal ( I love it )
for a while but I don't get a clear total overview about digital I/O.

Configuring single bits or complete byte as input or output.


This example confuses me "Button" ( from mikroPascal PRO for AVR Help )

1) DDB0_bit := 0; // set PORTB to be input
2) DDRC := 0xFF; // configure PORTC as output
3) PORTC := 0xAA; // initial PORTC value


Makes sence: DDR makes a complete output on port C ( complete byte, 8bit )


Whole port B is set to be input, but it shows DDB followed by 0_bit...
Why/how?

Tnx in advance.
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#2 Post by mikroSeven » 21 Nov 2016 11:52

What I have working since starting mikroPascal-AVR on my 2560:

Digital-out ( by byte and/or single bit )
LCD by 6 wires ( 4-data, 2-signal )
RS232 uart: to from pc (Lazarus)
ADC: 8 channels, still have to find out how to reach all 16 channels

Digital input: I don't get it.

Can't be that hard...
To DIY or not to DIY

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: Digital I/O on AVR in mikroPascal

#3 Post by Aleksandar.Mitrovic » 21 Nov 2016 16:33

Hi,

If you take a look at the datashet for the ATmega2560 you will find :
The DDxn bit in the DDRx Register selects the direction of this pin. If DDxn is written logic one,
Pxn is configured as an output pin. If DDxn is written logic zero, Pxn is configured as an input
pin.
So if you want to use pin as a button it should be input, or in this case DDxn = 0.
Number "0" will seet all register to the value 0, no matter how big it is.
If you are setting whole port to "1" we suggest you to use 0xFF.

Can you please explain it which problem exactly do you have with Digital Inputs and ADC?

Kind regards,
Aleksandar

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#4 Post by mikroSeven » 21 Nov 2016 20:19

Tnx.

For the moment, I will study the datasheet.

Want to find out how to acces all the 16 ADC inputs ( channel 8 to 15 ) of the 2560
and a ( better ) understanding of declaration/use of digital input


All regards.
To DIY or not to DIY

HardWareMan
Posts: 57
Joined: 09 Oct 2014 17:39

Re: Digital I/O on AVR in mikroPascal

#5 Post by HardWareMan » 21 Nov 2016 22:17

Code: Select all

DDRx  := %00110000; <= Input mode where '0', output mode where '1'
PORTx := %01010101; <= In output mode set output level, in input mode '1' enables pullup for input pin
Temp  := PINx;      <= Read pin state, whatever it mode is.
Just like that:
Image
If you want one bit - you should mask it. F.e. PINx and $01.

Some AVRs support the Pin toggling while it in output mode (2560 does). Writing a logic one to PINxn toggles the value of PORTxn, independent on the value of DDRxn. Note that the SBI instruction can be used to toggle one single bit in a port. That's right: PINx can be written.

As for ADC analog inputs, you should set up DIDR0 and DIDR2 registers to switch off digitall part of port. See ADC section in datasheet.

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#6 Post by mikroSeven » 22 Nov 2016 11:27

The data sheet makes sence!.

Slowly things are getting clear!

Before I started to use 2560 ( or better said: microcontrollers at all ),
I had only experience with the 82c55a I/O-periphical chip's on my DIY-ISA-slot-pcb's, programmed in TurboPascal 7.
( http://europakade.nl/motion-control-and-robots )
( https://www.youtube.com/playlist?list=P ... uG4mvriLgJ )

Marcel.
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#7 Post by mikroSeven » 22 Nov 2016 11:34

See here the ADC's ( channel 0 to 7 ) reading and send by RS232-uart to pc-Lazarus:

( topic: http://forum.mikroe.com/viewtopic.php?f ... 6&start=30 )
screen ADC values.jpg
screen ADC values.jpg (17.76 KiB) Viewed 7818 times
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#8 Post by mikroSeven » 22 Nov 2016 12:44

It's functioning!

Here full byte as input:

Code: Select all


// init

  DDRC:= 0;          
  PORTC:= 255;     


// some code

  for shCntrButton:= 4 to 7 do       // hardware 4..7 connected
    begin
       if ( pinC.shCntrButton )
         then stOut[shPosInStOut]:= '1'
            else stOut[shPosInStOut]:= '0';

       inc(shPosInStOut);
     end;

Don;t mind 'inc[shPosInStOut], it's of the 'upper-loop'.
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#9 Post by mikroSeven » 22 Nov 2016 21:35

Ok!

It works in different ways of coding:

Input 'copied' to an LED-output:

Code: Select all

// init
  portH6_bit:= 0;

  DDRC:= %00000000
  portC:= %00000000
One way:

Code: Select all

// some code
  portH6_bit:= pinC3_bit;  

Another way:

Code: Select all

// some code
  portH.6:= pinC.3;
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#10 Post by mikroSeven » 23 Nov 2016 11:39

My understanding of the ADC-functioning has been increased a lot also!

for example:
Reading the ADC channel above [0..7] shouldn't be a problem anymore...
Need MUX bit5.

Made an overview:
ADC briefly a.jpg
ADC briefly a.jpg (247.26 KiB) Viewed 7807 times
ADC briefly b.jpg
ADC briefly b.jpg (103.06 KiB) Viewed 7807 times
To DIY or not to DIY

mikroSeven
Posts: 161
Joined: 14 Mar 2016 10:24
Contact:

Re: Digital I/O on AVR in mikroPascal

#11 Post by mikroSeven » 23 Nov 2016 11:53

Thanks to subscriber voodle for the code above.

From that point, I contineu and add more code.
To DIY or not to DIY

Post Reply

Return to “User Projects”