about avr-gcc

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

hi 

i'm using avr-gcc first time and i have problem in last i used  keil  

ex.

 char bdata flag=0;

sbit   lvl    at flag.b0;

but how do that in avr-gcc??????

 

Last Edited: Sat. May 12, 2018 - 03:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

Get hold in a ANSI C book!

 

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

engmahmoud wrote:
last i used  keil 

Presumably, Keil C51 - for 8051-based micros?

 

 char bdata flag=0;

sbit   lvl    at flag.b0;

These are proprietary language extensions - so non-standard and not portable to any other compiler.

 

Also, they reflect specific features of the 8051 architecture - so aren't going to be relevant to other processors anyhow.

 

As sparrow2 says, GCC focuses a lot more on being standards-compliant - so most of what you need will be found in any standard 'C' textbook.

 

Probably the main extensions needed to accommodate AVR are the modifiers for accessing data in the Flash and EEPROM ...

 

Here are the manual pages:

 

https://www.nongnu.org/avr-libc/

 

https://gcc.gnu.org/wiki/avr-gcc

 

Note that Atmel Studio uses GCC.

 

See also the Tutorials forum on this site.

 

EDIT

 

Specifically about manipulating bits, see: [TUT] [C] Bit manipulation (AKA "Programming 101") - although note that this is all standard 'C'; nothing specific to GCC or AVR

 

EDIT 2

 

typo

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: Sat. May 12, 2018 - 03:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Search here for sbit.h for an easy solution.

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

thank 

but i want define like a flag and than define bit bit inside this flag like my ex  in 8051

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

"bit inside flag" doesn't make sense. In C, if you want to use single bits, you use them explicitly by doing bitwise operations.

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

Simple

 

uint8_t flag;
#define set_sbit0 (flag |= (1<<0))
#define clr_sbit0 (flag &= ~(1<<0))
#define val_sbit0 (flag & (1<<0))
#define set_sbit1 (flag |= (1<<1))
#define clr_sbit1 (flag &= ~(1<<1))
#define val_sbit1 (flag & (1<<1))
....

These are simple to use. To set sbit5, for example, just do

 

set_sbit5;

Similarly, to clear. To test for whether a bit is set or clear:

 

if (val_sbit3) {
    ...
}

Don't be afraid of all those shift operations. They are resolved at compile time and do not take any computation time.

 

"The Real Seebs" complaint is that, normally, in code, the term "flag" is used to refer to a bit. Here, the term is used for "a byte that contains flags". The big problem is that there is no bit mechanism like there is in 8051 family. That mechanism is, I think, unique to the 8051 architecture. You certainly do not find it in AVRs or PICs or MSP430 devices. So, you need to find a way to achieve the same end effect differently.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. May 12, 2018 - 06:07 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

engmahmoud wrote:
but i want define like a flag and than define bit bit inside this flag like my ex  in 8051

But, again, that relied upon a specific hardware feature peculiar to the 8051 architecture.

 

I don't think the AVR has such a feature?

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

I'm guessing you didn't even bother to look at sbit.h  It makes things like

PortC.b5 = 1;

quite possible. 

 

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

I think what the OP's talking about is having actual single-bit variables in RAM - not just accessing the bits of port registers ?

 

This is a proprietary feature that Keil C51 gives you - relying upon the bit-addressable RAM area of the 8051 ...

 

http://www.keil.com/support/man/docs/c51/c51_le_bdata.htm

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

I believe that is what the OP is looking for, and which I provided a template for. 

 

The 8051 has a whole block of ram (several bytes) that are addressable by bit number. eg, bit9 or bit33. There are unique operations that set, clear, and test these bits, again, by bit name (without any reference to a "containing" byte, even though that region of memory is also byte addressable).

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. May 12, 2018 - 08:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

But sbit.h can be used just as easily for that? (All it's really doing is casting a struct full of 1 bit fields onto a byte - that byte can be SFR or RAM)

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

It's not quite the same - it's the closest you can get!

 

Again, the 8051 has a specific hardware feature that lets individual bits be directly accessed - you can't do that on an AVR (can you?) so you have to simulate it by masking bits in a byte (or larger). That's what sbit.h gives you.

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

You have the same set/clear bit instructions in a AVR (but the Carry as 1 bit register you can use for logic things it don't have).

 

The problem is that ANSI C don't have them!

Last Edited: Mon. May 14, 2018 - 07:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:
The problem is that ANSI C don't have them!
]

Indeed - that's why it is a proprietary extension in Keil!

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

sparrow2 wrote:
You have the same set/clear bit instructions in a AVR

Do you?

 

Don't they all work on registers - not directly on RAM ?

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

You have 256 bit in the registers, and  256 bit in the IO array , so it cover better than than the 8051 that have 128 in RAM and 128 in IO.

 

 

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

but it's still not bit-addressable RAM.

You are "hijacking" things for unintended purposes:  you can't just arbitrarily use any one of those bits as single-bit variables in your program - some will have dedicated hardware purposes; some are not readable; some are not writable; etc, ...

 

You can, of course, use the same trick to extend the 8051's "bdata" - bit-addressable RAM.

 

 

I guess, in summary, the answer to the specific question in the OP is: "no, you can't do exactly the same on an AVR - but there are other ways to achieve similar results"

 

 

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

Sorry but I don't see the what the RAM in 8051 have to do with this!

It's not like you can make an array of bits and then say that bit[n]=1;  on a 8051  (perhaps the keil compiler can do it but then it will have to mask bit's out as well).

but like a AVR you can set/clr a fixed port pin. and other bit's. 

 

add:

like the AVR the n in bit[n]=1; has to be known at compile time!

Last Edited: Mon. May 14, 2018 - 08:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sparrow2 wrote:
Sorry but I don't see the what the RAM in 8051 have to do with this!

because that is specifically what the OP was asking about!

 

See the link in #10

 

See also: http://www.8052mcu.com/tutmemor.phtml#Bit%20Memory

 

It's not like you can make an array of bits and then say that bit[n]=1;  on a 8051

No, you can't make an array, but you can have directly-accessible individual bits in the bit-addressable area - what Keil calls "bdata

 

 (perhaps the keil compiler can do it but then it will have to mask bit's out as well).

No; that's the whole point: the bits are directly accessible without having to mask them out from a byte!

 

 

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

engmahmoud wrote:
in last i used  keil  

ex.

 char bdata flag=0;

sbit   lvl    at flag.b0;

Err ... no - that is not the correct syntax for doing this in Keil!!

 

surprise

 

So that would not have worked - even in Keil!!

 

See: http://www.keil.com/support/man/docs/c51/c51_le_bitaddrobj.htm

 

So I think you are having us on ... ?

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

The closest feature AVR has are the GPIO registers, however, which ones are in range of the bit addressing instructions is implementation specific.

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

The 8051 is similar in that respect - SFRs within a certain range are bit-addressable.

 

This is distinct from the "bdata" bit-addressable RAM area.

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

I'm not familiar with the 8051. In the AVR, I/O and RAM are in the same address space, so they are not actually different in terms of how they are accessed.

 

The difference is that I/O usually has side effects, can be changed from outside the CPU (volatile), and may be read only. But this is not the case for the GPIO registers, they behave exactly as RAM, the problem is they are scarce and sometimes not contiguous.

However, the new tinyAVR-1 and MegaAVR-0 models have an array of 4 contiguous GPIORs (32 bits) within range of the bit addressing instructions.

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

With your logic it's the same as say that an AVR can't do fo++ because it can't inc direct on RAM !!!  

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

I guess so.

 

It's a feature that the 8051 has, but other processors don't.

 

So, when you move from an 8051 to another processor, you have to find a different way to do it.

 

Whether or not that is actually a (significant) problem in a given application is, of course, an entirely different matter.

 

Given that the OP didn't even get her/his example correct, it seems unlikely that it is key to her/his requirement ...

 

 

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

it's a feature others controllers also have, some more intelligent than others.

the 4 bit samsung controller I once used had a mapping array, so 16 bit either could be 4x4bit or 16x1 bit and it was just a memory mapper, so store one place would reflect both, and that way you could make an array bit[n]=1 .

 

But here the main problem still you can't write it in ANSI C.

 

But the AVR GCC use it. PORTB |=0x10;  will use a set bit instruction and not <load> <or> <store>.

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

The OP's specific question was about the 8051's bdata - and how to do the "same" in avr-gcc.

 

sparrow2 wrote:
But here the main problem still you can't write it in ANSI C.

Agreed - see #10.

 

But the AVR GCC use it. PORTB |=0x10;  will use a set bit instruction and not <load> <or> <store>.

Indeed - but not for bit flags in RAM.

 

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

Depends what you call "RAM" ;-)

 

Does GPIOR0 count as "RAM"? It's an addressable location which latches all 8 bits that are written to it. What's more it has a low enough address that it is in range of SBI/CBI. (on older AVR like mega16 I've also used TWAR for this as it has the same attributes).

 

In this case:

#include <avr/io.h>

typedef struct {
  int b0:1;
  int b1:1;
  int b2:1;
  int b3:1;
  int b4:1;
  int b5:1;
  int b6:1;
  int b7:1;
} bits_t;

#define flags (*(volatile bits_t *)&GPIOR0)

int main(void) {
  flags.b0 = 1;
  flags.b5 = 0;
  while(1);
}
C:\SysGCC\avr\bin>avr-gcc -Os -mmcu=atmega328p -g avr.c -o avr.elf

C:\SysGCC\avr\bin>avr-objdump -S avr.elf

yields:

int main(void) {
  flags.b0 = 1;
  80:   f0 9a           sbi     0x1e, 0 ; 30
  flags.b5 = 0;
  82:   f5 98           cbi     0x1e, 5 ; 30
  84:   ff cf           rjmp    .-2             ; 0x84 <main+0x4>

And, yes folks, all I am doing here is EXACTLY the same as sbit.h that I mentioned in #4 so when OP said:

thank 

but i want define like a flag and than define bit bit inside this flag like my ex  in 8051

then, for my money anyway, this delivers it. Like I say, if using an "old" AVR that does not have a General Register in SBI/CBI range then pick something that works the same like:

#define flags (*(volatile bits_t *)&TWAR)
C:\SysGCC\avr\bin>avr-gcc -Os -mmcu=atmega16 -g avr.c -o avr.elf
int main(void) {
  flags.b0 = 1;
  6c:   10 9a           sbi     0x02, 0 ; 2
  flags.b5 = 0;
  6e:   15 98           cbi     0x02, 5 ; 2

BTW if you have an XMEGA you have SIXTEEN (yes count them!) 8 bit registers in range of SBI/CBI for this.

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

We've already been through that one - see #18.

 

Although the OP doesn't actually seem be showing much interest - or even have any deep understanding of what Keil does!

 

So the fine detail is probably all wasted.

 

frown

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

Depends what you call "RAM" ;-)

And I will add to that on "normal" AVR's there are 256 bits for this in RAM, that happens also to be the 32 registers.

How many that in realty can be used will be depending on the compiler and/or programmer.   

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

i asked this qu. because  i want interface 74hc151 with atmega8 

the 151 have 8 input how define in my prog.

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

You have to be more specific. The atmega8 outputs are connected to 74hc151 inputs? Or (more likely) the atmega8 is controlling the select pins and you want to store data from the 74hc151 in bit addressable memory?

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

It would probably be better to start from first principles, rather than try to emulate something proprietary from an unrelated toolchain specific to an unrelated processor family/architecture.

 

Have you done the basics first - eg, lighting individual LEDs connected to port pins ... ?

 

Probably best to start a new thread, with an appropriate title, explaining what you're actually trying to do:  "interface 74hc151 with atmega8" doesn't tell us what you're trying to achieve - why do you want to use an 74hc151 ?

 

You can always give a link to this thread, for reference...

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

i will connect 8 switches in input 74hc151 (x0-x7) and i will connect (s0-s2) to micro and connect o/p 151 to micro 

if i press sw0 write word in lcd  i will using scan line (s0-s2)

ex iwant write this

if(scan_line==0){s0L();s1L();s2L(); nop(); sw0=~(o/p151);}

how define sw0 in avr-gcc ?

define sw0 to sw7 in avr-gcc

 

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

From OP point of view I just really think that it starts now!

 

I guess that you have done 8051 in the way past, and used TTL for port extenders etc. But I would first see if a cpu with more port pins would do the job.

 

But if you want to use a AVR like a 8051 you would need to read up on how DDR,PORT and PIN works on a AVR (very different from an org 8051) and then it should be easy. 

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

sparrow2 wrote:
From OP point of view I just really think that it starts now!

Indeed - hence the suggestion to start a new thread with the real question.

 

The discussion here has been a bit of a red herring now that the actual requirement is revealed!

 

frown

 

 

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

It seems OP is only trying to identify which one of 8 switches is on.

He wants to use 74151 to save some pins in Avr.

It is a 8bit multiplexer. It has 8 inputs i0-i7, 3 select pins s0-s2 and one output pin Y.

According to state of select bits one of inputs is connected to output.

 

Not a happy choice, encoder 8 to 3, eg. 74148 would be better.

 

The pseudocode for 74151 could be

(suppose switches active low)

   while(1)
   {
      for(int i=0; i<8; i++)
      {
         select_bits = i;
         if(Y==0) switch = i;
      }
   }

 

As it was said, it would be better first to read something about bit manipulation and start with simple Button-Led examples.

 

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

how define sw0 in avr-gcc ?

define sw0 to sw7 in avr-gcc

unsigned char switches; 
...

switches |=  (1<<0)           // set bit switches.0
switches &= ~(1<<5)           // clear bit switches.5

if(switches & (1<<0))         // if bit switches.0 is set
{
   //do something
}   

if( !(switches & (1<<4)) )   // if bit switches.4 is clear
{
   // do something
}