Beginnersish coding discussion

This forum is for anything that doesn't specifically have to do with Better Than Wolves
Post Reply
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Beginnersish coding discussion

Post by The_Ender_lord »

WOOHOO!
It taken about 2 hours but I've finally done it!
I've made a light blink on and off without ending.
:)

Here's the code for MPLAB X IDE v1.41:
Spoiler
Show

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <p18f87k22.h>
#pragma config XINST = OFF
/*
 *
 */
void main(void) {
    int max = 10000;
    int i;

    LATD=0;
    TRISD=1;
    LATD=1;

  for(i = 0; i < max;i++){

    PORTDbits.RD1=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD1=0;
    }
    }
This is probably one of my biggest achievements yet.(I'm not much of an achiever)

I'm happy.
Last edited by The_Ender_lord on Sun Sep 16, 2012 11:56 am, edited 2 times in total.
All you need is bacon and duct tape.
Glace1221
Posts: 32
Joined: Mon Mar 12, 2012 10:51 am

Re: The *very* beginninings of my coding career

Post by Glace1221 »

What am I looking at?
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

You aren't required to comprehend it. Just to be able to understand the fact that its progress
All you need is bacon and duct tape.
User avatar
Gormador
Posts: 713
Joined: Mon Dec 26, 2011 1:09 pm
Location: France

Re: The *very* beginninings of my coding career

Post by Gormador »

Nice ! It's a first time job but I see no reason why this shouldn't work !

May I ask what is your final goal in learning micro controller programing ?
FlowerChild wrote:If you have a suggestion, I recommend that it have pure manna from heaven flowing forth from its vagina to warrant posting it at this point in time.
starkand
Posts: 7
Joined: Thu Sep 13, 2012 11:00 pm

Re: The *very* beginninings of my coding career

Post by starkand »

KUDOS Ender_lord.
Let me just say, that's a huge beast you have there for prototyping "Hello 10" ;D

Is it just for an academic project or like your favourite hobby?

Glace1221 wrote:What am I looking at?
That's just a red stone clock btw. Remember when you built your first one?
Sig is overrated.
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

Stark: its just a hobby.
Gormador: i think my goal is to just learn how to read code.
All you need is bacon and duct tape.
User avatar
Gormador
Posts: 713
Joined: Mon Dec 26, 2011 1:09 pm
Location: France

Re: The *very* beginninings of my coding career

Post by Gormador »

Seriously ? Then I don't understand. Because the ability to read code is useless if you don't intend to write some at some point.

EDIT : However, your professional life could require you to... yeah, my brain's not quite functional right now... sorry ^^
FlowerChild wrote:If you have a suggestion, I recommend that it have pure manna from heaven flowing forth from its vagina to warrant posting it at this point in time.
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

Well, after a bit of googling I found a bit of code to create an infinite loop. After a bit of work i made this:
Spoiler
Show

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <p18f87k22.h>
#pragma config XINST = OFF
/*
 *
 */
void main(void) {
    int max = 10000;
    int i;

    LATD=0;
    TRISD=1;
    LATD=1;

  for(i = 0; i < max;i++){

    PORTDbits.RD0=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD0=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD1=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD1=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD2=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD2=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD3=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD3=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD4=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD4=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD5=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD5=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD6=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD6=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD7=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD7=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD7=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD7=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD6=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD6=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD5=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD5=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD4=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD4=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD3=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD3=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD2=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD2=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD1=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD1=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD0=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD0=0;
    }
That is the code to make a line of 8 lights blink on and off in succession going one way and then the other.
I'm a bit annoyed as to how much code it takes for such a relatively simple task.
Last edited by The_Ender_lord on Sun Sep 16, 2012 11:57 am, edited 1 time in total.
All you need is bacon and duct tape.
User avatar
ignika42
Posts: 82
Joined: Mon Mar 12, 2012 6:38 pm
Location: Gallifrey

Re: The *very* beginninings of my coding career

Post by ignika42 »

The_Ender_lord wrote: I'm a bit annoyed as to how much code it takes for such a relatively simple task.

Reminds me of something my instructor told us in my first programming class, writing code is like explaining something to a child, you've gotta be very exact if you want them to understand you, because you can't assume they know anything.


Not familiar with micro-controller programming, but if you are going for an infinite loop, can't you just use a while loop?
Spoiler
Show
int y = 1;

while (y = 1)
{
//code that does stuff, aside from set y to something other than 1
}
A madman sat in his empire of dust and ashes. Little knowing of the glory he would achieve. Far away, the idiots and fools dreamt of a shining new future. A future now doomed to never happen.
Image
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

Yeah I've used while loops before.
All you need is bacon and duct tape.
Glace1221
Posts: 32
Joined: Mon Mar 12, 2012 10:51 am

Re: The *very* beginninings of my coding career

Post by Glace1221 »

The_Ender_lord wrote:You aren't required to comprehend it. Just to be able to understand the fact that its progress
Good enough for me! You have more patience than I do, I can tell you that! I don't think I would ever want to be a coder, even though I still might have to do some if I ever want to get into game making.

And,yes! I do remember the first redstone clock I made! I remember getting confused on how to get it going, too!
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

How did redstone clocks get into it?
All you need is bacon and duct tape.
User avatar
Xekrom337
Posts: 154
Joined: Fri Oct 28, 2011 7:59 am

Re: The *very* beginninings of my coding career

Post by Xekrom337 »

The_Ender_lord wrote:How did redstone clocks get into it?
Becuase that's what that codey looking stuff does!
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

...
Off-topic post in the off-topic forum is off-topic
...
...
...
OXYMORONS!
All you need is bacon and duct tape.
User avatar
Xekrom337
Posts: 154
Joined: Fri Oct 28, 2011 7:59 am

Re: The *very* beginninings of my coding career

Post by Xekrom337 »

The_Ender_lord wrote:...
Off-topic post in the off-topic forum is off-topic
...
...
...
OXYMORONS!
Wat.

Also, YOU DONT KNOW YOUR OWN CREATION, DO YOU? TIS NOW UNDER MY COMMAND! MUWAHAHAHHAHA!!!!!!!!
SpaceGuyR
Posts: 57
Joined: Thu Jan 05, 2012 8:29 am

Re: The *very* beginninings of my coding career

Post by SpaceGuyR »

The_Ender_lord wrote:I'm a bit annoyed as to how much code it takes for such a relatively simple task.
Sounds like you need to do functions and arguments. It should be possible to make one combined thing like "set light [position] [on/off]" and then you can easily do patterns instead of copying similar sections in main() every time.
haphazardnuke
Posts: 119
Joined: Mon Apr 16, 2012 12:55 pm

Re: The *very* beginninings of my coding career

Post by haphazardnuke »

I don't know microcontroller-specific code, but I know Java fairly well, so I can get the gist of that code.

Wouldn't it be simpler to do the following (in Java syntax, but it should be readable):

Code: Select all

while (true) {
     thingToToggle = 1;
     someWaitFunction();
     thingToToggle = 0;
}
The delay would only be needed if the LED toggles too fast to see. If the microcontroller has a built-in way to specify processing speed, you could just slow that down.

Regardless of all that, your first successful program is still a cool accomplishment. Tutorial code isn't supposed to be tidy or efficient anyway; those are things tutorial code writers expect you to do on your own, mostly. So, good job and welcome to the world of programming!
PizzaSHARK wrote:It's fun trying to quietly assassinate your kids and family members
starkand
Posts: 7
Joined: Thu Sep 13, 2012 11:00 pm

Re: The *very* beginninings of my coding career

Post by starkand »

redstone? my bad, just throwing sand in the gears. nvm

Allow me to share a pseudo code block. Taking the ideas from SpaceGuyR and haphazardnuke we get:
Spoiler
Show

Code: Select all

void main(void) {
int max = 10000;
int i;

void rotatelamp(void) {
   static BYTE lamps = 0;              // the lamps state
   static int dir = 1;                 // a direction flag
   
   /*
   detect lamps overflow or underflow
   and re-set directions
   */

   if (dir == 1) {
      // rotate byte lamps;
   }
   if (dir == -1) {
      // counter rotate byte lamps;
   }
// PORTDbits = lamps;                   // taking advantage of a full byte write
}
//// I used TECH C compiler and it allowed me to write PORTD = lamps;
//// but in MC C the ports are named differently.


while(1) {                                    // a main loop,
   for(i = 0; i < max;i++){                   // some delay
   }
   rotatelamp();                              // and the lamp rotator pseudo function call
}
}
Another way to set a steady time-base is to use interrupt feature.

Having previous coding experience is a huge leap into MCU's ><
Looking at the thread title... I can't believe this is your first contact with coding syntax.

I'll be around in case you wanna trade any tips.
GL with the bit bang.
The_Ender_lord
Posts: 50
Joined: Mon Sep 10, 2012 10:35 pm
Location: Behind that tree, Wearing a Herobrine skin

Re: The *very* beginninings of my coding career

Post by The_Ender_lord »

haphazardnuke wrote:I don't know microcontroller-specific code, but I know Java fairly well, so I can get the gist of that code.

Wouldn't it be simpler to do the following (in Java syntax, but it should be readable):

Code: Select all

while (true) {
     thingToToggle = 1;
     someWaitFunction();
     thingToToggle = 0;
}
The delay would only be needed if the LED toggles too fast to see. If the microcontroller has a built-in way to specify processing speed, you could just slow that down.

Regardless of all that, your first successful program is still a cool accomplishment. Tutorial code isn't supposed to be tidy or efficient anyway; those are things tutorial code writers expect you to do on your own, mostly. So, good job and welcome to the world of programming!
That was how I started coding.
All you need is bacon and duct tape.
whitechaos35
Posts: 205
Joined: Wed Nov 30, 2011 4:19 am

Re: The *very* beginninings of my coding career

Post by whitechaos35 »

The_Ender_lord wrote:Well, after a bit of googling I found a bit of code to create an infinite loop. After a bit of work i made this:
Spoiler
Show

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <p18f87k22.h>
#pragma config XINST = OFF
/*
 *
 */
void main(void) {
    int max = 10000;
    int i;

    LATD=0;
    TRISD=1;
    LATD=1;

  for(i = 0; i < max;i++){

    PORTDbits.RD0=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD0=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD1=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD1=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD2=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD2=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD3=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD3=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD4=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD4=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD5=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD5=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD6=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD6=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD7=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD7=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD7=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD7=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD6=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD6=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD5=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD5=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD4=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD4=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD3=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD3=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD2=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD2=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD1=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD1=0;
    }
      for(i = 0; i < max;i++){

    PORTDbits.RD0=1;
  }
  for(i = 0; i < max;i++){
    PORTDbits.RD0=0;
    }
That is the code to make a line of 8 lights blink on and off in succession going one way and then the other.
I'm a bit annoyed as to how much code it takes for such a relatively simple task.
Use bit shifting.
Spoiler
Show

Code: Select all

void main(void)
{
    byte dir = 1;
    byte portState = 0x10;

    LATD=0;
    TRISD=1;
    LATD=1;

    while(true)
    {
        PORTDbits = portState;
        if( dir == 1 )
        {
            if( portState == 0x01 )
            {
                dir = -1;
                portState <<= 1;
            }
            else
            {
                portState >>= 1;
            }
        }
        else
        {
            if( portState == 0x10 )
            {
                dir = 1;
                portState >>= 1;
            }
            else
            {
                portState <<= 1;
            }
        }
        delay();
    }
}
Post Reply