Author Topic: counting actions ? please explane  (Read 5624 times)

0 Members and 1 Guest are viewing this topic.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
counting actions ? please explane
« on: June 21, 2008, 05:15:26 PM »
Hi, i`m trying to flash more leds changing portd, copying and other combining, but its not working, Ben gaved some definitions and i think thouse definitions make problem to define new open ports
Quote
#define sbi(a, b)  (a |= (1 << b))
#define cbi(a, b)  (a &= ~(1 << b))
could some one explain code above ?


allso i`m looking for cycle counting, my goal is to flash PD4 x times and after PD5 x times, mixing and making something interesting, untill now i have found some c++ page writen in my language, but its hard to understand what the author thinks, i guess its because i dont understand the syntax - for now, i`m willing and i will learn it, i just need a bit help

Karlis

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #1 on: June 21, 2008, 06:50:25 PM »
#define is a precompiler directive. it replaces x with y.

C++ is a "pure" universal language. it has no idea what hardware is running on, what ports are out there, what hardware we have and anything about interrupts.
The standard way to control a port is something like
Code: [Select]
PORTD = 0xF0 //7-4 on, 3-0 off

If you want to control a single pin, intuition tells you to read the port, make the changes on a shadow register and write it back. since you mention PD5. Since you are mentioning pins as "P" and not "R" i will asume AVR architecture.

So, changing a single byte (7)
Code: [Select]
uint8_t gigi; //our shadow buffer is named gigi
gigi = PORTD;
gigi = PORTD | 0x80; (binary: 10000000)
PORTD = gigi; //bit 7 is now set

//wait

gigi = PORTD;
gigi = PORTD & 0x7F; (binary: 01111111)
PORTD = gigi //bit 7 is now clear
Try to understand what's happening in the ^^^ piece of code. using OR you can set bytes, using AND you can clear bits. The "mask", or the pattern which is used in the OR/AND defines which bits remain the same, and which are changed

Now, it's cumbersome to remember what 0x80 and 0x78 stands for.

So, the shift operator comes in help. Basically, it shifts a variable x places
If you had: 01011100
and you shift by one
you'll have: 10111000

Why this is of help is that you can generate 80 and 7F (10000000 and inverted ) by shifting stuff.
So, 80 is exactly: (1 << 7)  // one shifted seven times
Also, to get 7F, we just have to invert 80. For each 0, we get a 1, and for each one, we get a 0.

So, using something like
Code: [Select]
PORTD |= (1 << 7)
will set bit 7.

What Ben did was gave you simpler defines for these operations
sbi(a, b)
will be replaced by
a |= (1 << b)

so, sbi(PORTD, 7) //set pin 7 of portd
will be translated as
a |= (1 << 7)

which, as we learnt above, will set only bit 7, disregarding the rest.

I hope this helps :P

edit: these are called binary operators, except for ~ which is a unary operator. You will need this big time for developing uc software, and it's a nice thing to know (although, not so used) for general C++ programming.
Don't move on until you fully understand these, and you can set/clear bits without any problem.
Another cool thing is ^ (XOR) which will toggle the masked bits.
« Last Edit: June 21, 2008, 06:52:36 PM by izua »
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline emmannuel

  • Full Member
  • ***
  • Posts: 87
  • Helpful? 0
Re: counting actions ? please explane
« Reply #2 on: June 21, 2008, 07:04:16 PM »
thx for that izua, it helps a lot :D

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #3 on: June 22, 2008, 02:12:43 AM »
Thanks iuza it helped a litle,but i still dont know what i should change in code to open other ports since there are no nummbers >.<
are there any libary who i could inclide, and c will know what port i`m gona use ? like just writing
Code: [Select]
PORTD.3=1 // on
PORTD.3=0 // off
or similar ?? atleast on previus test codes i have seen that, but they didnt work :(

but since i`m new in coding all signs ( | << ~ $ # etc) are making me confused   :-\

i know a bit aout binnary - have learned it in schoold all year ( binnary hexadec. math ) allso a bit of algorithms i know :)

Code: [Select]
uint8_t gigi; //our shadow buffer is named gigi
gigi = PORTD;
gigi = PORTD | 0x80; (binary: 10000000)
PORTD = gigi; //bit 7 is now set

//wait

gigi = PORTD;
gigi = PORTD & 0x7F; (binary: 01111111)
PORTD = gigi //bit 7 is now clear
please tell me if i understand this or not: befour wait you opened PD, and after closed it, right ? but was it portD7 ?

Please tell me if i`m doing it correctly or not
Code: [Select]
PORTD |= (1 << 6) i`m opening portD 6 correct ? but what i have to define to do so? i think if i do so there will be bunch of errors :)
« Last Edit: June 22, 2008, 02:14:17 AM by karlis »

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #4 on: June 22, 2008, 03:03:40 AM »
yes. in the first example, with the "gigi" shadow port, you first set pin 7 (setting means writing one to something, and setting a pin means sourcing, or +5V), after the delay, it is cleared (writing 0 to it, a pin sinks, or is at ground). we are only altering port 7.
the important thing to notice about that example is that, we are operating on the whole port
"portd = gigi"
but with the help of the mask
"gigi = gigi | 80"
we only set bit 7 of gigi, which of course, contains portd. so the new portd value will only have bit 7 set, all the others, have the same value. this is very important! it works the same way for clearing a bit (& ~(mask)) and toggling (^ (mask)).

in your second example
Code: [Select]
PORTD |= (1 << 6)

you are creating the following mask: 1 moved 6 bits to the left, that is 01000000 (hex 0x40). you are using the logical "or" operator, so bit 6 will be set (because at least one bit is set, and that bit is in your mask!). please refrain from using "opening" and "closing" and use instead "set" and "clear", respectively "source" and "sink". opening and closing may have different meanings, like a high impedance state.

you do not need to include anything to use these constructs. they are basic C, and any compatible ANSI C compiler should support them.
The real fun starts when you get them.
Basically, you have
| = or (at least one bit has to be 1 to result in 1)
& = and (both bits have to be 1 to result 1)
^ = xor (only one of the bits has to be 1 to result 1)
~ = nor (1 will turn to 0, 0 to 1)
>>, << = bit shifting.

I'd recommend Dean Camera's Bit manipulaton (aka programming 101) for way more info.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: counting actions ? please explane
« Reply #5 on: June 22, 2008, 03:32:10 AM »
Thanks iuza it helped a litle,but i still dont know what i should change in code to open other ports since there are no nummbers >.<
are there any libary who i could inclide, and c will know what port i`m gona use ? like just writing
Code: [Select]
PORTD.3=1 // on
PORTD.3=0 // off
or similar ?? atleast on previus test codes i have seen that, but they didnt work :(

Check out this wikipedia page on bit fields:

http://en.wikipedia.org/wiki/Bit_field

It explains a little bit about bit manipulation using bitmasks, and it shows you how you can define a struct that gives you direct access to specific bits of a byte.

- Ben
« Last Edit: June 22, 2008, 03:33:27 AM by bens »

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #6 on: June 22, 2008, 07:26:44 AM »
i understand operands |&^~ i have learned them at school ( i`m learning about computers so i should know that, now i understand where and why :) )

correct me if i understod wrong:

c does not know what is port D, and if i want to output something like +5v from portd4 then i use masks
portd = gigi
gigi4 = portd 4
true ?

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #7 on: June 23, 2008, 04:24:52 AM »
or i`m teribly wrong ?

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #8 on: June 23, 2008, 06:57:21 AM »
nope. unless you declare gigi4 to something, a c compiler won't know it.
PORTx is a one byte variable. It will always be on byte, you can't make it something else.
The fact that what is written to PORTx will be reflected on the pins is not of importance.

The important thing is to understand how to set only some bits, or how to clear them using operators.
gigi = portd 4
does not use any operators.

get a compiler and experiment, outputing data to cout.
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #9 on: June 23, 2008, 12:30:39 PM »
i`m using AVR studio 4 for compiling and uploading stuff.. i just need realy good sample and explanation for code some thing like
| = or
& =  and and so on..

i have allmost understud bit's but something is mising

one byte is per pin, is it so ?

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #10 on: June 24, 2008, 10:38:47 AM »
yesterday something got in my mind... maybe there are some libary which tells compiler what i want with ports ?

like
#include <MCU pins.h>

portD.4=1 // on
delat_ms(200)
portD.4=0 // off

or its only my stupid idea ?

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #11 on: June 24, 2008, 09:28:59 PM »
use the defines someone posted for you
Code: [Select]
#define sbi(a, b)  (a |= (1 << b))
#define cbi(a, b)  (a &= ~(1 << b))
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #12 on: June 25, 2008, 05:14:16 AM »
i`m allredy using them, i cant understand what i have to change to define new pins aka portd5/6 and other port pins

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #13 on: June 25, 2008, 05:59:00 PM »
you don't have to change the defines.
do something like sbi (PORTD, 5)

dude, switch to basic. really. until you get a better grasp of programming, there's really no point in torturing yourself. or just look for bitwise operations tutorials (like the one i recommended you initially)
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #14 on: June 25, 2008, 11:15:38 PM »
i did and AVR Studio sayd something about sbi/cbi allredy defined ...

i`m not at moment at my pc, so i cant tell exact error, but i will leather today

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #15 on: June 26, 2008, 12:48:46 AM »
Code: [Select]
[code]not in avr, ffs. you should have mentioned that. do instead of
[code]
#define sbi(a, b)  (a |= (1 << b))
#define cbi(a, b)  (a &= ~(1 << b))
this
Code: [Select]
#define set_bits(a, b)  (a |= (1 << b))
#define clear_bits(a, b)  (a &= ~(1 << b))

it's pretty obvious that you haven't understood most of the stuff explained above. so here's the deal. you wanna learn programming and understand AVR architecture? learn its assembly language at first. don't work with any other language, unless you get the grasp of it. then, try making something of medium complexity. let's say a alphanumeric lcd which behaves like a terminal (esc-[2J clears, it shows incoming data, etc) must be done completly in assembly. of course, you should do it from scracth, not using other people's libs.
when it works, switch to C. try to redo it from scratch in C, using the same logic you have used in assembly. now the theophany will come, you'll understand most of the C constructs (including ~, | and &) and what's their logic.[/code][/code]
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: counting actions ? please explane
« Reply #16 on: June 26, 2008, 01:00:15 AM »
i did and AVR Studio sayd something about sbi/cbi allredy defined ...
I gave you those #defines because you said you were getting some error about AVR Studio not knowing what sbi and cbi are.  If for some reason now you have it set up so that AVR Studio knows what sbi and cbi are, delete those #defines from your program and use the ones AVR Studio thinks are in conflict.

- Ben

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #17 on: June 26, 2008, 02:33:18 AM »
well it seems so izua is true, i thot i understud it, but i was wrong, but its not true that i`m total noob and dont know anything.. i allso know php/mysql, how it works, but i dont know anything usefull to code... its similar here i know and i dont know, i cant understand without working example, now you are saying go learn asambler and code LCD in it, but i dont even have an LCD attached to my MCU, its only my future plan to add one..it would be cool if some one could drop link where is it explained (which wiire goes where) but i allso understand that its hard to teach other who have no understanding what is he doing, in my case i`m understanding atleast something...i`m not hopeless, just new into c

i have google'd for syntax (c syntax) to know what parameters, definitions etc are, but i have faild or i`m just dumbass >.< if i could get some example like photowore, but not so complicated ( i have seen how much files there are and explored some files) its difficult for newbie in programming understand what process is going on


ps here is the code for multiple led's but only led on PD4 is working i added izua last bit sets, deleted them nothing change PD5 does not flash
Code: [Select]
/*********************************************
* Author: Karlis
* This program is to test the led connected to
* PD4.
* Chip type           : ATmega16
* Clock frequency     : 16,000000 MHz
*********************************************/
#include <avr/io.h>
#include <mega16.h>
#include <util/delay.h>
#define sbi(a, b)  (a |= (1<< b))
#define cbi(a, b)  (a &= ~(1 << b))
#define set_bits(a, b)  (a |= (1 << b))
#define clear_bits(a, b)  (a &= ~(1 << b))

void delay_ms(unsigned int time_ms)
{
  while (time_ms--)
    _delay_ms(1);
}

int main(void)
{
/* enable  PD4 as output */
sbi(DDRD,PD4);
while (1) {
/* led on, pin=0 */
cbi(PORTD,PD4);
delay_ms(50);
/* set output to 5V, LED off */
sbi(PORTD,PD4);
delay_ms(50);}
//test flash 2 led on port D5
sbi(PORTD,PD5);
while (1) {
delay_ms(100);
cbi(PORTD,PD5);
delay_ms(100);
sbi(PORTD,PD5);
delay_ms(100);

}
}

there are no warnings or errors!

edite: just mesured what current is comming out from bouth pins 4 and 5, on pd4 is 0-4.5v on pd5 are only 0.15v no voltage jumps no nothing
« Last Edit: June 26, 2008, 03:01:03 AM by karlis »

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #18 on: June 26, 2008, 04:53:12 AM »
are you using the defines given above or not?
if you are using those, you can only do sbi(PORT, PD5)
if you are not using those, you can only do sbi(PORT, 1 << PD5).

simple, eh? the defines to the << by themselves :P
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #19 on: June 26, 2008, 05:59:10 AM »
you can just look at the code, fater last comment is code for PD5 its seems ok to me, but no current is comming out from it :(

Offline izua

  • Supreme Robot
  • *****
  • Posts: 682
  • Helpful? 0
    • izua electronics
Re: counting actions ? please explane
« Reply #20 on: June 26, 2008, 06:29:55 AM »
put a led on it!
i looked over the code, that question was for you and the solutions for each case were below (in case you wanted to mangle with the code)
Check out my homepage for in depth tutorials on microcontrollers and electronics.

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #21 on: June 26, 2008, 01:52:55 PM »
the led is allredy soldered in place, i`m manipulating with code above and no results apear, only led on PD4 is changing its on of intervals, but i dont even touch the code where its about portD4

izua sorry i did not understud your post about definitions, am i doing something wrong?

i even changed led to weaker and nothing changes >.<

Offline bens

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 335
  • Helpful? 3
Re: counting actions ? please explane
« Reply #22 on: June 26, 2008, 02:09:56 PM »
Karlis, it seems like you are currently way out of your element and I don't think many people here have time to walk you through the absolute basics of AVR programming.  I strongly suggest you search around online for some basic AVR tutorials or consider buying a book that will give you the introduction you clearly need.  I've heard good things about the book C Programming for Microcontrollers.

- Ben

Offline karlisTopic starter

  • Full Member
  • ***
  • Posts: 79
  • Helpful? 0
    • sctg clans website
Re: counting actions ? please explane
« Reply #23 on: June 26, 2008, 02:57:22 PM »
Thanks Ben, no more questions for now ^__^

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: counting actions ? please explane
« Reply #24 on: July 04, 2008, 11:06:23 AM »
Look at the file SoR_Utils.h for lots of examples:

http://www.societyofrobots.com/axon/downloads/Axon_Source_062008.zip

 


Get Your Ad Here