Jump to content

Recommended Posts

Hi Guys,

 

I am a newbie to PIC microcontroller programming and struggling to understand some the ways pics function.

 

I've got the ECIO40 development board which has a PIC18F4455 chip on board. After reading the data sheet, I was under the impression that the primary clock will default to 48MHz at which both USB and the PIC core and peripherals will run at. I've got a c program written in sourceboost, where all I do is toggle a variable ON-OFF and write it to an output port. Now, I was expecting this generated clock to be in the MHz range, whereas, the output I am actually getting is 600 kHz. I have tried to modify registers etc to no avail. Can someone please explain why the output is at this frequency and not at the higher rate.

 

Regards,

Meththa

Link to post
Share on other sites
Can someone please explain why the output is at this frequency and not at the higher rate.
Would be good if you posted the code you are using, maybe you aren't being very efficient.

 

Regards

Dave

Link to post
Share on other sites

#include <system.h>
#include "Light_Sensor.h"

#pragma CLOCK_FREQ 48000000

//Configuration data
#pragma DATA 0x300000, 0x20 //00100000 CONFIG1L
#pragma DATA 0x300001, 0xe  //00001110 CONFIG1H
#pragma DATA 0x300002, 0x3e //00111110 CONFIG2L
#pragma DATA 0x300003, 0x1e //00011110 CONFIG2H
#pragma DATA 0x300004, 0x0  //00000000 
#pragma DATA 0x300005, 0x81 //10000001 CONFIG3H
#pragma DATA 0x300006, 0x81 //10000001 CONFIG4L
#pragma DATA 0x300007, 0x0  //00000000 
#pragma DATA 0x300008, 0xf  //00001111 CONFIG5L
#pragma DATA 0x300009, 0x80 //10000000 CONFIG5H
#pragma DATA 0x30000a, 0xf  //00001111 CONFIG6L
#pragma DATA 0x30000b, 0xa0 //10100000 CONFIG6H
#pragma DATA 0x30000c, 0xf  //00001111 CONFIG7L
#pragma DATA 0x30000d, 0x0  //00000000 CONFIG7H

void main(void)
{
	trisd = 0;		//All pins configured as output

portd.2 = 0; // initialise outputs
unsigned char x = 1;
while(1){
	x = 1-x;
	portd.0 = x;
	}

}

 

sorry - I should've included this. As you can see, it doesnt do much at all apart from toggle the output pin.

Link to post
Share on other sites

meththas,

 

#include <system.h>
#include "Light_Sensor.h"

#pragma CLOCK_FREQ 48000000

//Configuration data
#pragma DATA 0x300000, 0x20 //00100000 CONFIG1L
#pragma DATA 0x300001, 0xe  //00001110 CONFIG1H
#pragma DATA 0x300002, 0x3e //00111110 CONFIG2L
#pragma DATA 0x300003, 0x1e //00011110 CONFIG2H
#pragma DATA 0x300004, 0x0  //00000000 
#pragma DATA 0x300005, 0x81 //10000001 CONFIG3H
#pragma DATA 0x300006, 0x81 //10000001 CONFIG4L
#pragma DATA 0x300007, 0x0  //00000000 
#pragma DATA 0x300008, 0xf  //00001111 CONFIG5L
#pragma DATA 0x300009, 0x80 //10000000 CONFIG5H
#pragma DATA 0x30000a, 0xf  //00001111 CONFIG6L
#pragma DATA 0x30000b, 0xa0 //10100000 CONFIG6H
#pragma DATA 0x30000c, 0xf  //00001111 CONFIG7L
#pragma DATA 0x30000d, 0x0  //00000000 CONFIG7H

void main(void)
{
	trisd = 0;		//All pins configured as output

portd.2 = 0; // initialise outputs
unsigned char x = 1;
while(1){
	x = 1-x;
	portd.0 = x;
	}

}

 

sorry - I should've included this. As you can see, it doesnt do much at all apart from toggle the output pin.

The code generated should give around double the rate you are seeing, you can check this yourself in the SourceBoost IDE debugger by stepping the code and looking at clock ticks changing at the bottom of the screen.

 

Regards

Dave

Link to post
Share on other sites

Thank you for the quick reply guys.

 

Reynard - could you please clarify to me what you mean please. Do you mean it is taking 36 steps to go through the while loop? Is there anything I can do to minimise that time. Would you also be able to tell me how you calculated the number of cycles please so that I can use the same method in the future.

 

Also what would the best method be for generating a clock signal that is needed to drive another CMOS chip.

 

Thank you for all your help.

 

Meththa

 

 

 

Hi Meththa,

 

Using the default optimization, your loop time is 36 cycles. So at 48MHz your square wave output shuld be 666.666kHz which is what you are seeing.

 

Cheers

 

Reynard

Link to post
Share on other sites
Thank you for the quick reply guys.

 

Reynard - could you please clarify to me what you mean please. Do you mean it is taking 36 steps to go through the while loop? Is there anything I can do to minimise that time. Would you also be able to tell me how you calculated the number of cycles please so that I can use the same method in the future.

 

Also what would the best method be for generating a clock signal that is needed to drive another CMOS chip.

 

Thank you for all your help.

 

Meththa

 

 

 

Hi Meththa,

 

Using the default optimization, your loop time is 36 cycles. So at 48MHz your square wave output shuld be 666.666kHz which is what you are seeing.

 

Cheers

 

Reynard

Reynard means 36 clock ticks (or around 9 pic instructions), and in fact he is right. I measure exactly this in SourceBoost IDE debugger, but I forgot that the port has to be updated twice one time for off and the other for on. So the code goes around the loop at a rate of 1133.333KHz and the port changes state at this rate, but it has to change state twice for one square wave cycle.

 

Regards

Dave

Link to post
Share on other sites

Hi Meththa,

 

To continue with what Dave has said.

 

The loop takes 36 clock cycles at 48MHz. Now there are 4 clock cycles per machine instruction cycle meaning the processor core is actually being clocked at only 12MHz. Most instructions take 1 machine cycles but some take 2 machine cycles. Your loop takes 9 cycles as there are several branch instructions to execute.

 

If you want to generate a fixed clock frequency with very little code and run autonomous to your application, consider using one of the PICs timers. Which timer depends on the frequency you want to set the clock at. Using timer 2 with the compare feature is good for high speed clocks as it is set it and forget it.

 

The PWM feature also creates a regular clock.

 

Cheers

 

Reynard

Link to post
Share on other sites

Hi Dave, Reynard,

 

Thank you so much for your help. I see what you mean about the number of cycles now. I will look at how to get the Timer 2 or the PWM working to generate the clock then.

 

Does the compiler have anything to do with how many cycles it takes to run an instruction? Like I said I am using BoostC compiler with SourceBoost. But I have seen that others recommend using either CC8E (CC5X version for 18F chips) or even something like CCS etc.

 

So from what you have explained to me, the inside of the PIC chip is never going to run as fast as the clock frequency is it? I mean if I am reading data from a port within a while loop, the time it takes to read the data will depend on all the other code I've got and therefore will be at a slower rate than what I am expecting or what I should be reading the data at. In such instances is it better to use interrupts or is there any other method to read data from ports at specific frequencies?

 

I can't thank you guys enough for your help.

 

Meththa

 

Hi Meththa,

 

To continue with what Dave has said.

 

The loop takes 36 clock cycles at 48MHz. Now there are 4 clock cycles per machine instruction cycle meaning the processor core is actually being clocked at only 12MHz. Most instructions take 1 machine cycles but some take 2 machine cycles. Your loop takes 9 cycles as there are several branch instructions to execute.

 

If you want to generate a fixed clock frequency with very little code and run autonomous to your application, consider using one of the PICs timers. Which timer depends on the frequency you want to set the clock at. Using timer 2 with the compare feature is good for high speed clocks as it is set it and forget it.

 

The PWM feature also creates a regular clock.

 

Cheers

 

Reynard

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...