AVR C comunicate with peripherals

Go To Last Post
15 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all, sorry if my question looks kind of stupid, but please help me if you can.

I started learning avr c on spare time and I fo not inderstand how do I implement comunication with other periferals. Like LCD. I do have datasheet and know how to send bits, but how to send them on avr c

For exaple I pull pin high.

PORTB |= (1 << PB5)

Then pull it low

PORTB &= ~(1 << PB5)

What if I need to send 8 bits of data?

Should I use for loop?

Wht about timing of signals? What if period has to be exact something like 15ms?

Last Edited: Mon. Feb 3, 2020 - 02:20 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

VeryFastSnail wrote:
What if I need to send 8 bits of data?
There are many many ways to send N bits of data from one micro to another. Perhaps the easiest of all is to connect 8 wires from the port of one micro to the other. You then write the 8 bits you want to send to the PORT on one and the other just reads the whole PIN register to get the value. Of course this needs some way for the sender to say "just updated it, you need to read again". So often you have a "strobe" line which is a 9th wire and a pulse on that is an indication from sender to receiver to say "pick up new value now".

 

But not all designs can waste an entire 8 (9) lines just to transmit bytes like this. So then you might look at breaking the data up a bit. The first thing you might do is split the 8 wires into just 4 so that now, to send a whole byte you actually send 4 bits, pulse the "strobe" then send another 4 bits and pulse it again. But this still needs 5 wires. So how about splitting that again - have just 2 signal wires and one strobe/clock line. Now you present 2 bits a time and each time you also send a pulse on the strobe/clock line to say "next lot ready to read". But the ultimate is when you finally break this down into just 2 wires - one data line and one clock line. Now you just send a single bit at a time and each time you make a pulse on the other wire (clock) to say "next bit ready". When you get to this stage you have effectively invented SPI. It's a peripheral in the AVR that will do all the splitting and pulsing for you so you don't need to "bit bang" it in software. I2C is a kind of similar system but the electronics are a bit different so that now there can be more than one "listener" on the line.

 

Of course if you think that even 2 wires to send the data is still too much you could do away with the clock line all together. Instead you just make pulses on the single wire to send a bit at a time. But to do this the sender and receiver have to agree at what speed the pulses will be sent as the receiver has no other way to know when one bit ends and the next is read to be read. Also to make sure the receiver starts listening at the right moment you probably want to send an extra bit at the start as a kind of "synchronization" pulse to say "data about to start". That extra pulse is a "start bit". When you get to this stage you just invented UART.

 

BTW in all this there is always (ALWAYS!) one additional wire in addition to all the others I have mentioned and that is "ground" because to measure 0 or 1 pulses (voltage levels) you have to have a ground reference to measure them against. So even in the UART case it is the data line and a Gnd line.

 

So what is it you are trying to connect exactly? Many things you might attach to a micro already have a 8 bit data or SPI or I2C or UART interface!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


VeryFastSnail wrote:
avr c

There is really no such thing as "AVR C": it is the same standard 'C' Programming Language as used on any other target - and as described in any standard 'C' textbook; eg,

 

 

https://www.oreilly.com/library/view/the-c-programming/9780133086249/

 

 

Therefore, if you don't already have a solid grasp of the  'C' Programming Language, it will be a lot easier to learn on a PC or similar than on a small microcontroller.

 

Here are some 'C' learning & reference materials for you - including a free online 'C' textbook:

 

http://blog.antronics.co.uk/2011...

 

VeryFastSnail wrote:
What if I need to send 8 bits of data?

This really has nothing specifically to do with 'C' - it would be the same whatever language you use.

 

Do you want to send the 8 bits in parallel - ie, all 8 pins of the port at once - or serial?

 

VeryFastSnail wrote:
Wht about timing of signals? What if period has to be exact something like 15ms?

Again, nothing specifically to do with 'C'.

 

There are basically 2 ways to give delays:

  1. rely on the time it takes to execute a given number of instructions;
  2. use a Timer.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

AVR's have lots of app notes available on how to talk to peripheral devices, check out the documentation tab on the MicroChip website for your device.

Also look in the tutorials forum here on this site.

As a third alternative, Arduinos are AVR's too, so lots of learning resources there as well.

Asking specific questions will yield you better answers then a generic question.

 

Jim (Not all freaks are named Jim, but it helps)

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

An overview of Serial vs Parallel data transmission:

 

https://www.quantil.com/content-delivery-insights/content-acceleration/data-transmission/

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Forgot to mention above that as soon as you look at splitting 8 bits to send in small chunks (the ultimate being 1 bit at a time) there's then an additional thing you have to consider and that is the order the bits are sent. For exampl if you have bits 76543210 and you send it as 2 bits at a time do you send 76, 54, 32, 10 or 10, 32, 54, 76 or perhaps even 01, 23, 45, 67. When there's just 1 wire do you send 7,6,5,4,3,2,1,0 or do you send 0,1,2,3,4,5,6,7. Rather curiously UART (for example) chooses to send the bits "backwards" (well backwards compared to western left to right reading order) so bit 0 goes out first then bit 1 and so on.

 

Again it's up to sender/receiver to agree on this detail. Also if using clock pulses is the data stable at the rising or the falling clock. Also do you send 0 as 0V and 1 as 5V or perhaps inverted so 0 is 5V and 1 is 0V ?

 

All of these things constitute a "protocol". Both ends have to agree to use the same data transmission protocol.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

ki0bk wrote:
the documentation tab on the MicroChip website for your device

 

aka the "Product Page".

 

If you just put the part number into google, the Product Page will be one of the first hits; eg,

 

 

The Product Page looks like this:

 

 

The 'Documents' Tab gives you the datasheets, application notes, etc:

 

 

Note that this is not unique to AVR or Microchip - most manufacturers have similar arrangements

 

#ProductPage

#UseTheProductPage

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Feb 7, 2020 - 10:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I do have some sensors that uses non stardard comunication not spi i2c and so on. So to make my question clear, i would like to send 8 bits of data sequentially one after another

So for example 00110100

Would be:
__пп_п__

Sonthis signal must have clock to destinguish data bits right?

So whole data would look something like this:

П______П <—— this is used for tellin that data starts and when it ends
__ПП_П__ <—— this is data

П_П_П_П_ <—— clock for bit identification

So for example I just gave you an example of one of protocols that my lcd uses. So my goal is not to get it working. There are lots of libraries, but to understand how to implement it on microprocessor should I use loop and pass data after bit start indentificaror?

Last Edited: Mon. Feb 3, 2020 - 04:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thnak you very informative answer, will look into that book you gave link.

Last Edited: Mon. Feb 3, 2020 - 04:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But does your “protocol “ involve a separate clock line or do you plan to do this async? If clock you have a for(8) loop sampling a bit at a time (some kind of mask/shift), setup the bit the pulse the clock. Else, if async, you need to pepper the data setup with more accurate timing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VeryFastSnail wrote:
non stardard comunication not spi i2c and so on  ...  how to implement it on microprocessor

 

The term you're looking for is "bit-banging"

 

https://en.wikipedia.org/wiki/Bit_banging

 

https://circuitdigest.com/article/introduction-to-bit-banging-spi-communication-in-arduino-via-bit-banging

 

It is just a matter of:

 

  1. Set the communication line to the required state
  2. Wait for the required time
  3. Set the communication line to the next required state
  4. Wait for the required time
  5. rinse & repeat as required ...

 

should I use loop and pass data after bit start indentificaror?

Well, a loop is always an obvious choice for any repeated set of operations - assuming that the loop overhead doesn't compromise the speed you require

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

None of this has anything specifically to do with AVR;  it is all entirely general - applicable to any microcontroller

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VeryFastSnail wrote:
I do have some sensors that uses non stardard comunication

What sensors are those?   Until forthcoming with this info, we can not offer you any help.

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

VeryFastSnail wrote:

П______П <—— this is used for tellin that data starts and when it ends
__ПП_П__ <—— this is data

П_П_П_П_ <—— clock for bit identification

 

For "ASCII Art" like that to work, you need to use a mono-spaced font. And for that, you need to use the 'Add Code' button:

    _              _
___| |____________| |___
    _   _       _
___| |_| |_____| |______
    _   _   _   _
___| |_| |_| |_| |______

 

See Tip #1 in my signature (below; may not be visible on mobile)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
Until forthcoming with this info, we can not offer you any (sic) help.

Well, only very general help - as above.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...