Sunday, April 20, 2008

Exercise 1: My way...


Different approach... same result... mainly to show that in programming, there is no right way or wrong way... just either more compact (less codes space) or faster execution... This code compile to 206 bytes only as opposed to over 1000 bytes on 9w2dtr version...

13 comments:

9w2dtr said...

tak aci!!!!!! pakai cacing...

Unknown said...

Got cacing and panah oso...

9w2gu said...

Cacing and panah was going to me my next lesson.. :)

Good try dexter. The idea is to make the code small. Thats why we have loops. It reduces repetitive task and expands on it. But at least i know, we have an audiance now. I will try to post more info, when i get the time. If it was MikroBasic it would be easier. :) bcos you can use variable to access individual bit on portX.

in basic you could do portb.bit where bit is a variable from 0 to 7.
Unfortunately in MikroC this does not work.

Maybe i should not have posted the programming lesson until i explained bit access..


Hey how you guys capture screen? what software you use?

9w2gu said...

This is the code for blinking one way with out using >> and ~.

main() {
unsigned char mbit, count,x;
trisb=0;
while (1) {
for (count=1; count<=8; count++) {
mbit=1;
if(count!=1) {

for (x=1; x<=count; x++) {
mbit=mbit*2;
}
}
portb=mbit;
delay_ms(100);
}
}
}

Basir you too advance...Where is uncle Chow and Yau?

9W2BSR said...

Hamid,
Let it be advance... can use to introduce some more C operators... and push them to learn and explore more...

I used screen capture from here...
http://www.mirekw.com/winfreeware/mwsnap.html

Freeware... but very good...

9w2gu said...

I know...I havn't explained the cacing and panah. :) i tot they could write the code with what they learned so far. It is easy for, if we dont know how to do something, we know what term to look for in the help file, but they dont.

On the other hand, they get exposed to new commands and functions. And since it is a hands on approach, they should understand faster.

I am still working on the conditional, bitwise, if(, baseN and boolean operators. All on paper not to pc yet. too busy...

9w2dtr said...

Actually, I am confused because I don't have any reference and explanation for C syntax and usage..
Although Hamid did some explanation earlier, it's confusing cos it is not separated by making the syntax as "Bold" and the explanation in normal.

9w2dtr said...

Actually, I am confused because I don't have any reference and explanation for C syntax and usage..
Although Hamid did some explanation earlier, it's confusing cos it is not separated by making the syntax as "Bold" and the explanation in normal.

9w2dtr said...

Actually, I am confused because I don't have any reference and explanation for C syntax and usage..
Although Hamid did some explanation earlier, it's confusing cos it is not separated by making the syntax as "Bold" and the explanation in normal.

Unknown said...

Hamid... are you writing the post in Open Office? Doing it using blogger interface sometimes doesn't give the expected results once you do heavy editing...

On the cacing and panah, I just wanted to throw some spanners into dexter's head... just for fun... hi hi...

ADC lesson being written-up... need to test it on the DevBoard before putting it up...

Sparky said...

My code makes me look quite silly but I am a newbie at this.

int a = 1;

void main ()

{
TRISB = 0;
PORTB = 0;

while (1);
{
do
{
PORTB = a;
Delay_ms(100);
a <<= 1; //shift bit to the left
}
while(a<255);

a >>= 2; //stops bit 7 double flashing between do loops

do
{
PORTB = a;
Delay_ms(100);
a >>= 1;
}
while(a>1);
}

}

Sparky said...

Sorry there was a small mistake in my last post.
Should be:

int a = 1;

void main ()

{
TRISB = 0;
PORTB = 0;

while (1)
{
do
{
PORTB = a;
Delay_ms(50);
a <<= 1; //shift bit to the left
}
while(a<255);

a >>= 2; //stops bit 7 double flashing between do loops

do
{
PORTB = a;
Delay_ms(50);
a >>= 1;
}
while(a>1);
};

}

9W2BSR said...

Hi there Sparky,

Everyone's a newbie at sometime so no need to feel silly... :-)

What's important is you can do it... get a working code and you're learning in the process....

We're no expert... just a bunch of guys who like to tinker around... Good to know someone else is benefiting from this blog...