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.

C18 Questions

Status
Not open for further replies.
Code:
#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:
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:
#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.
 
Wond3rboy said:
1)Can we have multiple source codes(not included as functions in header files) in a C18 project.This will help if one is working on multiple devices and rather then going through the process of putting the code in to one file(a huge head ache) one can just add them and link them.If so then how?(sorry my C has just dried up...)
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:
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
 
Thanks for your replies all.

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

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:
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:
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?
 
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?

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:
extern int shareVar;
shareStuff.c
Code:
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:
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:
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;
}
 
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;
}


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:
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

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.
 
I have a few questions about timers, here's a paste from one of microchips manuals

#include <timers.h>
void OpenTimer0( unsigned char config0 ); // Configure and enable timer x.
void OpenTimer1( unsigned char config1 );
void OpenTimer2( unsigned char config2 );
void OpenTimer3( unsigned char config3 );
void WriteTimer0( unsigned int value16 ); // Write a value into timer x.
void WriteTimer1( unsigned int value16 );
void WriteTimer2( unsigned char value_8 );
void WriteTimer3( unsigned int value16 );
unsigned int ReadTimer0( void ); // Read the value of timer x.
unsigned int ReadTimer1( void );
unsigned char ReadTimer2( void );
unsigned int ReadTimer3( void );
void CloseTimer0( void ); // Disable timer x.
void CloseTimer1( void );
void CloseTimer2( void );
void CloseTimer3( void );

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
Timer register(s) for ReadTimerx and WriteTimerx functions:
· Timer0 (16 bit): TMR0L,TMR0H
· Timer1 (16 bit): TMR1L,TMR1H
· Timer2 (8 bit): TMR2
· Timer3 (16 bit): TMR3L,TMR3H
 
Last edited:
Straight from the manual:
Code:
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:
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.
 
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?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top