Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

500Kz square wave from pic18f65k22 using xc8 c compiler?

Status
Not open for further replies.
T

treez

Guest
Hello,
I am using MPLAB.X with the microchip XC8 compiler (free version). The target chip is PIC18F65K22.
I want the oscillator of pic18f65k22 to be 4mhz. (1us per instruction)
I wish to pulse a pic18f65k22 pin as an output with a 500KHz square wave. (50% duty)
Will I have to use inline assembler to do this?
What frequency will it be if I do it in C like this for 64 pulses…


For (i = 64, i=0, i--) {
LATE.RE1 = 0;
LATE.RE1 = 1;
}
 

It can't work with a loop, either in C or assembler. A linear sequence of 64 XORWF instructions is the only chance to do it with 1 cycle per port output instruction.

What are you trying to achieve? If you want to perform respective fast action on a µP pin, you'll choose a suitable processor and the right pin (e.g. a PWM respectively OC pin).
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
If you generate the square wave with inline assembly, the dutycycle may not be 50% and you cant exactly set the 500khz output frequency with 1us instruction time.

Better select a controller with pwm or use the controller on its maximum frequency.

The code you shown can not be predicted, which depend on compiler and optimization level. But surely a controller cant generate 500khz from 1MHz instruction cycle by toggling the output pin.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
For 500kHz generation ,,you only need a 1MHz Xtal Osc on an inverter and /2 configured D FF (Q to D feedback)
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Re: 500KHz 64 pulse wave from pic18f65k22 using xc8 c compiler?

we just want 64 pulses of "1us high, 1 us low".
If not , then what is the fastest frequency that c can give us.?......the duty cycle is not that important, its just to send pulses to a digital pot up/down pin so that we can move the wiper of the digital pot....we have to do it quickly as we zero the pot's wiper to the end of the totem pole before setting its position..otherwise we don't now the wipers position.....and we have to do it quick otherwise we get a transient voltage unwantedly at the pot change time and a dip in the led current that's being controlled.
 

may be with a common 20MHz crystal.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
For (i = 64, i=0, i--)
  {
  LATE.RE1 = 0;
  nop();
  nop();
  nop();
  nop();
  LATE.RE1 = 1;
  nop();
  nop();
  }



Adjust the nop()s for duty cycle and output frequency.
 

to be honest, we want to limt to 4mhz, as we fear noise on our noisy, crammed PCB.
 

A number of options has been presented, time for treez to decide what he wants to do.
 

Re: 500KHz 64 pulse wave from pic18f65k22 using xc8 c compiler?

we just want 64 pulses of "1us high, 1 us low".
If not , then what is the fastest frequency that c can give us.?......the duty cycle is not that important, its just to send pulses to a digital pot up/down pin so that we can move the wiper of the digital pot....we have to do it quickly as we zero the pot's wiper to the end of the totem pole before setting its position..otherwise we don't now the wipers position.....and we have to do it quick otherwise we get a transient voltage unwantedly at the pot change time and a dip in the led current that's being controlled.
Noise bandwidth is not the clock rate rather it is the rise time that determines noise bandwidth.

Also Microchip's digital Pots are designed for midscale power on reset. Your design should start at low current for this setting by using a big cap with a slew rate to limit the surge while you set the pot at your leisure.... Being a millisecond, rather than microsecond.

The cap would be tied to Vcc or Gnd depending on which yields zero current to Leds, and the cap only needs to be a millisec. RC value.
 

I reckon simplest way is along the lines of FvM way, but just

LATE.RE1 = 0;
LATE.RE1=1;
LATE.RE1 = 0;
LATE.RE1=1;
LATE.RE1 = 0;
LATE.RE1=1;
LATE.RE1 = 0;
LATE.RE1=1;

...64 times
 

Right, it translates to

BCF xx.x
BSF xx.x
BCF xx.x
...
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
What will be the frequency of this pulse train (64 pulses)?

Hello,
I am coding C (free Microchip XC8 compiler) for PIC18F65K22. (MPLABX environment).
We wish to send a pulse train of 64 pulses (to move a wiper on a MCP4013 digital pot as quickly as possible, but slower than 400KHz)

The PIC oscillator frequency is 4MHz.

Here is the code (below). What frequency will the pulses be at?

for(i==64;i==0;i--){
mcp4013_udout = 0;
mcp4013_udout = 0;
mcp4013_udout = 1;
mcp4013_udout = 1;
}


"mcp_4013_udout" represents a pin of the micro.
 
Last edited by a moderator:

Re: What will be the frequency of this pulse train (64 pulses)?

I don't understand why a digital Pot was used if it is "volatile", when a PWM can emulate a Pot.

How does the system behave in a brown-out or with a dropout less than 10 cycles. Pffft? These must be considered before the design.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
its not volatile as long as the supply stays good to it, and that's fine for us.
 

Re: What will be the frequency of this pulse train (64 pulses)?

Hello,
I am coding C (free Microchip XC8 compiler) for PIC18F65K22. (MPLABX environment).
We wish to send a pulse train of 64 pulses (to move a wiper on a MCP4013 digital pot as quickly as possible, but slower than 400KHz)

The PIC oscillator frequency is 4MHz.

Here is the code (below). What frequency will the pulses be at?




"mcp_4013_udout" represents a pin of the micro.

Try reading the compiler's disassembly of the lines that concern you. I don't know that particular tool, but most compilers will allow you do so.
 

I don't know where the 400 kHz specification comes from, obviously not the MCP4013 datasheet. But code execution timing can be easily determined with the MPLAB simulator stop watch.
 

i'm adding some margin, mcp4013 wiper can be pulsed at 1mhz, but I am dropping back a bit for certainty
 

For 500kHz generation ,,you only need a 1MHz Xtal Osc on an inverter and /2 configured D FF (Q to D feedback)
I would take !MHz pulse from pin#50 (CLKO). Generate a gate pulse of 64 instruction cycles using an internal counter. "And" both pulses to get 64 cycles output.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top