# How to get 8MHz square signal out a pic18f45

I am using a Pic18F45K40 to control an ST7590 power line networking chip which requires an 8MHz clock signal to function. I read the datasheet and it looks like a 16MHz signal can be generated from the 64MHz microchip.

I set up the Reference Clock Output Module as follows:

#pragma config CLKOUTEN = ON


and

#define oCLK_INIT()      CLKRCLKbits.CLK=1; \
CLKRCONbits.EN=0;  \
CLKRCONbits.DC=2;  \
CLKRCONbits.DIV=3; \
SCANTRIGbits.TSEL=1;
//OSCCON1bits.NOSC=6;
#define oCLK_EN_ON()       CLKRCONbits.EN=1;


and used PPS to pin RB4 by setting

  RB4PPS=0b10100;        /* B4=CLKR 8MHz      */ \


I do get an 8MHz signal out but it looks like this:

This is more sinusoidal than square and might be the reason why I am not able to talk to the ST7590 chip via UART. Can anyone explain what is happening?

EDIT: Result of changing slew rate.

• Do you mean PIC18F45K40? – MIL-SPEC Jun 18 '18 at 8:06
• Could it be that the signal is degraded by the time it get to the oscilloscope inputs? E.g. by capacitance and/or inductance in the wires? – Andrew Morton Jun 18 '18 at 8:07
• Yes, it is PIC18F45K40. It is an 8MHz signal, I don't think the length of the oscilloscope cable would change the signal like this. – Gareth T. Jun 18 '18 at 8:18
• I see you have made progress, but what is the bandwidth of your oscilloscope? If you want it to accurately represent a square wave, the odd harmonics (3x8MHz, 5x8MHz, 7x8MHz etc) need to be within the 'scope bandwidth. For example a 20MHz 'scope (or scope probe) would show an 8MHz square wave looking like a sine wave, and you'd really want 80MHz or more if you are wanting to look at signal integrity of a square wave at 8MHz. – rolinger Jun 18 '18 at 13:39

## 2 Answers

Two possibilities - Stray capacitance or slew rate control.

If you were working on breadboards, I would think it to be unwanted capacitance between the pin strips but due to the package of the STmicro device, I believe you are working with proper PCBs.

That leads to the other possibility - the PIC is limiting slew rates on the port. This is done to reduce EMC from fast switching "bouncing", so the slew rates of the transistors are slowed to create a nice gradient. Works great under 1MHz but you need to be wary of it at 8MHz.

See page 244 for directions to wiping the SLRCONx registers to prevent or reduce slew rate limiting.

15.2.5 Slew Rate Control
The SLRCONx register controls the slew rate option for each port pin. Slew rate for each port pin can be
controlled independently. When an SLRCONx bit is set, the corresponding port pin drive is slew rate
limited. When an SLRCONx bit is cleared, The corresponding port pin drive slews at the maximum rate
possible.

• Oh wow that did have an effect. – Gareth T. Jun 18 '18 at 8:54
• Did it have the intended effect? Completely wiping the register may induce unwanted ringing to the signal, so you may want to actually just speed up the slew rate. – MIL-SPEC Jun 18 '18 at 8:57
• I have added the response to the question. Thnka you, I will do the next lot of testing shortly. – Gareth T. Jun 18 '18 at 9:03
• Assuming you wiped the register completely, this is now what I would expect when seeing stray capacitance i.e. the ramped rising edge, sharp falling edge with a ramp at the bottom. – MIL-SPEC Jun 18 '18 at 9:04
• I agree with its current shape and as Xin on the ST7590 is an analogue pin to it will read the appropriate High and low. I only cleaned the pin connected to the SLRCONB. so the rest of the register will remain active. I still can not get a response from the ST7590 but it is for another reason and not the clock. Thank you. I will post another question shortly. – Gareth T. Jun 18 '18 at 9:15

An ideal square wave signal does not exist. You will always have a certain rise and fall time in your signal. The higher your frequency is, the larger gets the relative amount of rise and fall time compared to your total on and off time respectively. What you suffer from is on one hand the parasitic properties of your path and the internal wiring of the pin including ESD protection and capacitance to ground.

An external oscillator also doesn't produce a clean square wave signal but rather one looking like your scope print, but that's not a problem as you usualy have edge detection function on your clock inputs.