Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

compiler for c

Status
Not open for further replies.
Yes, in C18 you use the format of PORTAbits.RA0 to control RA0, PORTBbits.RB4 to control RB4, etc.

Gobbledok, it is is important for me to point out that you can say something like PORTAbits.RA0 = 1 to set RA0 to logic 1, or PORTAbits.RA0 = 0 to clear RA0. It is not only for reading a bit.

Regards, and happy 2012! :D

Der Strom
 
Gobbledok, it is is important for me to point out that you can say something like PORTAbits.RA0 = 1 to set RA0 to logic 1, or PORTAbits.RA0 = 0 to clear RA0. It is not only for reading a bit.

Yes I should have elaborated. I should also point out that you should use the LATx.LATxx registers to write to a port, and the PORTx.Rxx to read from a port to prevent Read-Modify-Write errors ;)

Happy new year to you too mate :)
 
HAPPY NEW YRS!! 3

nickelflippr- the probs were few and far between and i was able to get around them mostly:

A) i learnd in c that in a sub there is BYVAL and BYSTR(i think) where one copies the variable and sends to the sub, GCGB manipulates the variables while in the sub; i think that was the problem, since vars were getting lost until i used another varaible to back-it-up before sending it to the sub,

B) repeat/for/loops are running too deep(>?), i would use a repeat, inside of a repeat , inside of a repeat i forget what layer exactly i could go to, but there is points of my code where i'd need to repeat the code manually, instead of using an increment with a repeat.
ALSO when i say repeat i could mean(for, do, repeat, (all related loops)), if you look in my code(link at bottom) you will see several points where this should be simplified()

C)any complation i have ever done on the 18f4620 has said "error:hex is larger than device" when i load it to the pickit, however this error has never effected performances; and even generates when my only line of code is "set port on";
i think we looked in to this and there was something wrong in a h file or something; but like i said it was no real problem

D)no select case icon:(

E)um complex variables would breakup ie; var(cnt -(1 + dat)) = newval(xx) just doesnt work after saving and loading the equation would split

F) my latest and greatest and most mysterious problem can be found here:
https://www.vbforums.com/showthread.php?t=667160
at the begining of the thread doogle helped me getting the code on the VB(PC) side together, but it extended in to unknown waters, after hitting a wall i decided to give the compiler an upgrade(here).

although ALL OF THESE could just be my bad syntax! or limitations of the chip.
 
Yes, in C18 you use the format of PORTAbits.RA0 to control RA0, PORTBbits.RB4 to control RB4, etc.

Gobbledok, it is is important for me to point out that you can say something like PORTAbits.RA0 = 1 to set RA0 to logic 1, or PORTAbits.RA0 = 0 to clear RA0. It is not only for reading a bit.

Regards, and happy 2012! :D

Der Strom

Funny how this is not mentioned in the other thread (https://www.electro-tech-online.com/threads/c18-structure-for-bits.94878/).
Of course that is not a compiler feature. Those definitions are in the library. But then again, probably every C18 user pretty much have the same libraries.
 
Last edited:
In C18 the normal way to pass variables is ByVal, I.E. a copy is sent to the function.
Code:
void Function(char dat){
    dat = 0x45;
}
char Passed=0x23;
    Function(&Passed);
    //Passed = 0x23
Or, by reference I.E the actual address of the variable is used (known as a pointer)
Code:
void Function(char *dat){
    *dat=0x45;
}
char Passed=0x23;
    Function(&Passed);
    //Passed will now equal 0x45

The way to nest loops and keep track of them is by indenting the code.
Here is an LCD scroll routine,
Code:
void ScrollDisplay(){
char i,j,chr;
    for(i=0;i<20;i++){        //start of for in column 4
        for(j=0;j<3;j++){     //start of for in column 8
            SetPos(i,j+1);
            chr=ReadChar();
            SetPos(i,j);
            WriteChar(chr);
        }                     //end also in column 8
    }                         //end also in column 4
    SetPos(0,3);
    for(i=0;i<20;i++)
        WriteChar(' ');
    SetPos(0,3);
}

The SelectCase function in C is called switch.

I don't understand the other things without seeing example code.

HTH,

Mike.
 
@doggy
Thanks for the reply. I have to say that I am not that familiar with the graphical version of GCBasic. Learned the text based version, and have stuck with that. Yes, you have to go "off road" some times to make things work the way you want to.

A) Every time I use a sub I copy the variable, and at least the text version of GCB does the same.
B) I can see where using nested wait (delay) routines could really add up, or perhaps some other libraries could bust the stack budget of a PIC.
C) Sounds like a bad xxxx.dat file, or a mishandle by the GCGB compiler? I have loads of code for the 18f4620 in GCB without this error.
D) Ooops, a manual text insertion GCB style.
E) Can't comment on that
F) I'll take a look if I find the time.

Either way, all the best with the "C" adventure.
 
like i said none of it has been a real problem, I really do like that little compiler espically since it sets up so easially(w hdw settings included)
except for B) , i am not loosing track of the "LOOP NESTING" but GCGB is, and i find that during the compiler, you need to watch where it will just stop and close without an error, but no hex file either, so if there is old one there; it(obviously) doesn't get overwritten. if you look in my main, my loops are made with variable counts inside a main loop, where i would have liked to just use a few FOR statements. Again I "think" it may have to do with too many subroutines and loops and such.

a)exactly :p
c)all good, it doesnt really effect anything, FYI - the error shows when i import on the pickit2 programmer (but only that chip type)
D)scared of manual!! I like objects due to poor spelling an punctuation, (which is going to kill me in c!)
E)again no biggie just adds another line


F) * update i found the problem in my code on the other forum; variable BLOCKEE was just acting out;

thnx again thou everyone!; im definitely going to try out the c, now that 'v learned this new lang anyway!
 
Last edited:
like i said none of it has been a real problem, I really do like that little compiler espically since it sets up so easially(w hdw settings included)
except for B) , i am not loosing track of the "LOOP NESTING" but GCGB is, and i find that during the compiler, you need to watch where it will just stop and close without an error, but no hex file either, so if there is old one there; it(obviously) doesn't get overwritten. if you look in my main, my loops are made with variable counts inside a main loop, where i would have liked to just use a few FOR statements. Again I "think" it may have to do with too many subroutines and loops and such.

a)exactly :p
c)all good, it doesnt really effect anything, FYI - the error shows when i import on the pickit2 programmer (but only that chip type)
D)scared of manual!! I like objects due to poor spelling an punctuation, (which is going to kill me in c!)
E)again no biggie just adds another line


F) * update i found the problem in my code on the other forum; variable BLOCKEE was just acting out;

thnx again thou everyone!; im definitely going to try out the c, now that 'v learned this new lang anyway!
I have not looked at your code in depth, but appears like you were getting some quality help on the other forum. Lots of bit toggling going on, and plenty of places to get caught out.

D) Thank god for the in-built debuggers, else I would be seriously in trouble with C :).

Congrats on getting it working, I have to say that is the biggest interrupt routine I have seen.

P.S. Not real thrilled with the USART receive routine timing, even if it does work. Following Nigel's tutorial, I would prefer to sample the received bits in the middle of the bit timing. In the future you could use the hardware USART of the 18f4620, with a crystal, and up the baud rate.
 
um, actually it doesn't work, and doogle was great help, espically with the VB side of things, :(, um maybe i should look in to having a debugger myself? what would be good to get?

maybe I should start a new thread for the pic code since there have been several changes(or just put it here)...?

also, im not even sure if that is the right usart pin, I just took the remaining pins avail, plus im not comfortable with crystals yet, but USART was working fine for me, until recently,
 
um, actually it doesn't work, and doogle was great help, espically with the VB side of things, :(, um maybe i should look in to having a debugger myself? what would be good to get?

maybe I should start a new thread for the pic code since there have been several changes(or just put it here)...?

also, im not even sure if that is the right usart pin, I just took the remaining pins avail, plus im not comfortable with crystals yet, but USART was working fine for me, until recently,

I would highly recommend grabbing a PK2 because it has a built-in, in-circuit debugger. It comes in very handy, and the whole thing only costs $35 USD. IMO it is one of the best purchases I have ever made.
 
Last edited:
in mplab? i got pickit2 programmer

but mplab is:pK2Error0028: Unable to enter debug mode

it checks out, enters debug, asks to reprogram(yes), asks to turn off watchdog(yes), asks to turn off lvp(yes),

PK2Error0024: unable to establish vdd on target... 0v

??????????????

mplab is powering the circuits but not after i reprogram it to go in to debug??


Programming Target (1/3/2012 5:39:21 PM)
PIC18F4620 found (Rev 0x7)
Erasing Target
Programming Debug Executive (0x-FDC0 - 0xFFFF)
Verifying Debug Executive (0xFDC0 - 0xFFFF)
Programming Debug Vector
Verifying Debug Vector
Programming Configuration Memory
Verifying Configuration Memory
PK2Error0028: Unable to enter debug mode
PICkit 2 Ready


also is there a way to watch the variables as they change in debug?
 
Last edited:
in mplab? i got pickit2 programmer

but mplab is:pK2Error0028: Unable to enter debug mode

it checks out, enters debug, asks to reprogram(yes), asks to turn off watchdog(yes), asks to turn off lvp(yes),

PK2Error0024: unable to establish vdd on target... 0v

??????????????

mplab is powering the circuits but not after i reprogram it to go in to debug??


Programming Target (1/3/2012 5:39:21 PM)
PIC18F4620 found (Rev 0x7)
Erasing Target
Programming Debug Executive (0x-FDC0 - 0xFFFF)
Verifying Debug Executive (0xFDC0 - 0xFFFF)
Programming Debug Vector
Verifying Debug Vector
Programming Configuration Memory
Verifying Configuration Memory
PK2Error0028: Unable to enter debug mode
PICkit 2 Ready


also is there a way to watch the variables as they change in debug?

Make sure that you have DEBUG=ON listed in the #pragma configs. This should allow you to enter debug mode.
 
k got it!

also is there a way to watch the variables as they change in debug?

AND i noticed that void main loops, is there a way to only run it once?
 
First, yes, you can monitor the variables by going to view-->watch. This will bring up a watch window. Then, select the variables you want to monitor and add them to the screen. you can choose what type of value you watch it in--binary, decimal, hex, etc.

Also, I don't believe the main function loops in real life. It might only do that in the debugger. What you can do, though, is put a Nop() at the very end, and put a breakpoint on it. That way the program will stop when it reaches that point.
 
Main doesn't loop but does terminate and just runs through memory until it wraps around and resets. To stop main from exiting add a while(1) at the end.

I.E.
Code:
void main(void){
    //your code here.
    while(1);
}

You can also view variables by hovering over them after the code has hit a break point.

Mike.
 
You can also use:

LATAbits.LATA0 = 1; to set a bit, or:

PORTAbits.RA0; to read a bit.


I only just learnt this today but you can also use (for example):

Code:
variable = _RB5;       // Reads PortB, 5.

_LATB5 = x;            // Sets PortB, 5.

_TRISB5 = x;           // Sets TrisB, 5.

_ADON = 1;             // Sets the ADCON1.ADON bit.

_T1CKPS = 3;           // Sets the prescaler for Timer1

These are just a couple of examples. It's exactly the same as before but saves you typing out the whole name for the register you want.
 
Last edited:
well now thats handy stuff(all you guys!) epically with the variables. and this conversion is going quite fast with that replace box!, however I seem to have stumbled upon a minor bump, im not sure if im declaring/setting up my functions proper, but compiler is saying error on line 172 (at the bottom of the page), im sure it something simple im missing, also could we just check if my "if" statements are ok(a few before line 172), i think i seen it somewhere like that?

also c book is saying that c variables are all short or long, does that mean to get i bit i break it up like the ports, and bytes?

oh an also all those int's at the beginning, do i need to "DIM" bytes,arrays?
 

Attachments

  • SAMPLE.txt
    3.7 KB · Views: 157
well now thats handy stuff(all you guys!) epically with the variables. and this conversion is going quite fast with that replace box!, however I seem to have stumbled upon a minor bump, im not sure if im declaring/setting up my functions proper, but compiler is saying error on line 172 (at the bottom of the page), im sure it something simple im missing, also could we just check if my "if" statements are ok(a few before line 172), i think i seen it somewhere like that?

also c book is saying that c variables are all short or long, does that mean to get i bit i break it up like the ports, and bytes?

oh an also all those int's at the beginning, do i need to "DIM" bytes,arrays?

That's not your complete code, is it? It seems to end mid-function. Also, the prototypes should usually be put before the "main" function. And what is your "/*wait one second */" doing there? I don't see anything that tells it to wait. If you put some code in there that you didn't post, that's probably where the error is. It's probably on the line right before 172, if you put code before the last bracket.
 
Last edited:
no thats just a chunk, and /*wait*/ is waiting to be replaced with a command....

prototypes? is that what a subroutine is? I moved it up, but the error followed it, and all that is before it is another similar bracket, and both brackets have corresponding brackets?

i trimmed that snip so it has only one sub and one main, could you set it up just to get this first sub goin, just so i can template the rest? I updated the sample so there is only one sub and one main
 

Attachments

  • SAMPLE.txt
    3.6 KB · Views: 142
Status
Not open for further replies.

Latest threads

Back
Top