1. 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.
    Dismiss Notice

C18 Questions

Discussion in 'Microcontrollers' started by 3v0, Jun 25, 2009.

  1. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Code (text):
    #include<p18f1320.h>
    char * data=00;// Initiazlizes it to zero so the array is saved on
                   // first location and one can retrieve it.

    void main(void)
    {
    rom near char * pname;
    static rom near char name[]="Hi";
    pname=name;//pointer points to first element of name
    *data++=*pname++;
    *data ++=*pname++;
    [B]data=data+20;;
    pname=name;
    *data++=*pname++;
    *data ++=*pname++;
    while(1);
    }[/B]


    //void main(void)
    //{
    ////static rom near char name[]="Hi";
    //*data++=name[];
    //*data ++=pname[];
    //while(1);
    //}
     
    Hi I was doing little code examples and creating a little pointer flash card like thing for my self in the process.The code in the comments works fine.As you can see that one can initialize a pointer at a specific location but what the problem is that cant seem to reinitialize it with the statement:

    data=0x20;

    It says wrong assignment.But the addition thing works fine.:confused:

    I want to first store it from ram location 00 and then be able to access it using the pointer.Also & is illegal in C18.
     
    Last edited: Aug 11, 2009
  2. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    ONLINE
    When you are working in C, you can't just write to any memory. The C compiler will allocate memory and if you need a block for yourself then you should allocate it as an array. You can do malloc in BoostC but I'm not sure in C18.

    Why do you say & is illegal? You can do,
    Code (text):

    #include<p18f1320.h>

    char chrArray[40];
    char * data=chrArray;

    void main(void)
    {
    rom near char * pname;
    static rom near char name[]="Hi";
        pname=&name[0]      ;//pointer points to first element of name
        *data++=*pname++;
        *data ++=*pname++;
        data=data+20;       //will point to chrArray+22 (20+2 increments)
        pname=name;
        *data++=*pname++;
        *data ++=*pname++;
        while(1);
    }
     
    After running the above, chrArray[0] will contain H and charArray[22] will also contain H.

    Mike.
     
  3. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    Yes. Just add the .c source file to you project. Note that this does not move a copy of the file to your project director it just includes the file from where it is at.

    You can also link in compiled objects by adding .o files to the objects list.

    3v0
     
    Last edited: Aug 12, 2009
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US

    Just to make this clear as possible

    Bit Manipulation:
    PORTAbits.RA1 - Use this to read a bit/pin value (can be used like LATA also)
    LATAbits.LATA1 - Use this to set a pin High or Low
    TRISAbits.TRISA - Use this to set a pin input or output

    Byte(whole port) Manipulation:
    PORTA - Modifying this will alter every pin on PORTA/LATA register. Reading it will result in all PORTA pins data.
    LATA - Modifying this will alter every pin on LATA/PORTA register
    TRISA - Modifying this will alter every pin in TRISA register
     
  6. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Thanks for your replies all.

    Thanks had posted a question here but worked it all out.In order to use multiple source files in our project, we need to have one file with main and all that stuff and others which simply define functions and their respective variables.And arguments to functions need not be declared as global variables.
     
    Last edited: Aug 12, 2009
  7. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    you have to include the PICs header. Or the generic which is p18cxxx.h.

    When you include that it will have all the definitions for the selected device in MPLAB.
    Meaning it looks for your device setting in mplab and uses the appropriate header..

    So if 18f1320 is selected in MPLAB it will use.... p18f1320.h

    Which includes the definitions and info for every register on that pic.

    EDIT:
    Reuse is up to how you code it. If you use definitions in your code then its easy to move over to other PICs and even to AVRs and ARMs.

    I have taken most of my PIC code and turned it into ARM in minutes. Its easy! as long as you code it right.

    Instead of straight calling like

    LATAbits.LATA1 = 0;

    Define it :

    #define LED_Ready LATAbits.LATA1

    and in your code use

    LED_Ready = 0;

    This way when you move it over you can alter just the definition and the rest of the code follows
     
    Last edited: Aug 12, 2009
  8. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Thanks for the pointer Atomsoft.I was editing my previuos query.
     
    Last edited: Aug 12, 2009
  9. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    heh ok just noticed it above :D
     
  10. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    Thanks for the thread 3V0!

    Hi, i wanted a bit to be used for indication but found out that C18 doesnt support the bit data type:(.

    Secondly, a question regarding multiple source files.Is there some way one can have variables that are accessible in both files?
     
  11. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    The extern variable qualifer tells the compiler what the variable looks like but does not allocate any storage. You can declare the same variable in as many files as you like but make it extern in all but one.
    shareStuff.h
    Code (text):
    extern int shareVar;
    shareStuff.c
    Code (text):
    int shareVar;
    All files that use shareVar must include shareStuff.h and shareStuff.c must be in the source file list.
    Maybe if you give a bit more detail on what you mean by indication we can help you on your bit problem.

    3v0
     
    Last edited: Aug 16, 2009
  12. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    I want to avoid using .h files since...thats what i know( dont know whether its wrong or right)...when you call a function from a header file then it is replaced on every line the function is invoked..i-e a macro.I am already low on memory(i will have to use two pic micros if i use that technique).I want to differentiate between two ADC conversions(one for temperature and one for a SG) for which i wanted to use a bit.Secondly about the variable problem with two source files.. since i will store temp values in the internal EEPROM i want to use a variable as a counter till 100 and use that same variable as an address to store data.What i have done is that i have given up checking that variable in main and used it in the ADC routine only.

    Thanks.
     
    Last edited: Aug 16, 2009
    • Like Like x 1
  13. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    make a function like:

    int SetBit(int var, int bit){
    var = var | (1<<bit);
    return var;
    }
    int ClearBit(int var, int bit){
    var = var & ~((1<<bit));
    return var;
    }
     
  14. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan

    I have gone through you cheat sheet(very helpful!). I can just use a 8bit data type cause i can use it for indication.Its the memory i want to save on but already worked around it.

    A question about setting automatic acquisition time.How can we do 1TAD? manually?
    Thanks.
     
    Last edited: Aug 16, 2009
  15. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Which PIC...?
     
  16. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    The method I provided for a variable shared between files will use no additional memory. The variable is allocated in the file shareStuff.c only.

    3v0

     
  17. Wond3rboy

    Wond3rboy Member

    Joined:
    Jun 25, 2008
    Messages:
    762
    Likes:
    7
    Location:
    Sindh,Pakistan
    18LF4620.

    I am just trying it.
     
  18. Triode

    Triode Member

    Joined:
    Feb 11, 2009
    Messages:
    936
    Likes:
    22
    Location:
    Bay Area
    I have a few questions about timers, here's a paste from one of microchips manuals

    I'm just wondering about the usage of some of these commands. I'm pretty sure with the open timer command I'm going to first store a configuration into Config0, or any other variable I want to place there, or just put the configuration commands right there in the open command right?

    What do I do with the read command? Does it place the value in a variable? How do I access the timer value once it has been read? Is the variable inserted in to that command, or is it a predefined one or what?

    I've been away from this for a while with work, so I'm trying to get caught up. Im working on timed multitasking now, but I have to figure out timers and timed interrupts first. I do understand how timed multitasking works in theory, but I'm learning how to actually code it in C18 now.

    Edit: found part of the answer to one of my questions in another microchip document
     
    Last edited: Sep 1, 2009
  19. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    Straight from the manual:
    Code (text):

    1.3.1        ReadTimer0
    For TMR_V1, TMR_V2, TMR_V3, TMR_V4, TMR_V5 and TMR_V6

    Function:
    [B]Reads the value of the timer0.[/B]

    Include:   
    timers.h

    Prototype:
    unsigned int  ReadTimer0( void );

    Remarks:
    This function reads the value of the timer0 register.

    Timer0:                         TMR0L,TMR0H

     

    Note: When using a timer in 8-bit mode that may be configured in 16-bit mode (e.g., timer0), the upper byte is not ensured to be zero. The user may wish to cast the result to a char for correct results. For example:
     

      // Example of reading a 16-bit result
      // from a 16-bit timer operating in
      // 8-bit mode:

      unsigned int result;

     [B] result = (unsigned char) ReadTimer0();[/B]

    Return Value:
       
    [B]The current value of the timer.
    [/B]
    File Name:
    t0read.c
     
     
    Last edited: Sep 1, 2009
  20. Triode

    Triode Member

    Joined:
    Feb 11, 2009
    Messages:
    936
    Likes:
    22
    Location:
    Bay Area
    Thanks, I got that far.

    Which structure do people generally use for multitasking?

    I can picture one where you have a loop that monitors the timer, and when it hits a value it increments all of the variables that are counting up for their respective tasks, and then resets the timer. Or one that has a timer interrupt triggered event that increments the variables then resets the timer. I'm guessing the ladder would be more accurate, but I'll have to learn to use interrupts to try it. I'm working on that right now.
     
  21. AtomSoft

    AtomSoft Well-Known Member

    Joined:
    Feb 7, 2008
    Messages:
    5,670
    Likes:
    41
    Location:
    Brooklyn, NY US
    where did you learn multitasking? From the NET? (everything i learned or know about on any subject i learned online)

    Any good resources you can share?
     

Share This Page