|
|
View previous topic :: View next topic |
Author |
Message |
djsb
Joined: 29 Jan 2011 Posts: 41
|
PIC18F27J53 LED Blinky code |
Posted: Fri Apr 28, 2023 10:40 am |
|
|
Hi,
I'm trying to get my CCS PCH (V5.115) compiler set up with MPLABX V6.05 (which also has PCM V5.110 installed). I'm using a PIC18F27J53 to start with on a simple PCB breakout. Does anyone have a basic LED Blinky example I can try (or point me to an example, maybe provided with the compiler). Just want to get the basic fuses setup and the external ceramic resonator. I've seen a few more advanced code on the forum that use pin select functionality. I don't need to use that yet, but an example using a switch input would be a bonus. Any help appreciated. Thanks.
David. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Apr 28, 2023 11:03 am |
|
|
Just try.
Any of the flash an LED examples given here ought to work, However
one critical thing is needed, that you have not said. The frequency of your
resonator.
Ceramic resonators are setup exactly the same as crystals.
So start with something like:
[url]
https://www.ccsinfo.com/forum/viewtopic.php?t=59170&highlight=flash+led
[/url]
Change the clock setup to use
CRYSTAL=FREQUENCY_OF_YOUR_RESONATOR
Once this is working, and working at the right frequency, program a
pin for your 'switch'. Have this wired to pull an input low. Add a pullup
on this. Now portB on your chip does support internal programmable
pull-ups. On the whole port though a search on pullup in the manual
will find this. They try changing the delays in the loop to use a variable,
and at the start of each loop, read the input on the pin which you use
for the switch, read this and if it is 1, set one value, and if 0 a
different value. This way you can then see the flash rate change when
your switch is operated. |
|
|
djsb
Joined: 29 Jan 2011 Posts: 41
|
|
Posted: Fri Apr 28, 2023 11:43 am |
|
|
Thanks Ttelmah,
I'll start first with reading the header file to see which fuses to set. I like to proceed with caution in case I lock myself out with code protection. The resonator is 16MHz by the way. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Apr 29, 2023 1:31 am |
|
|
Don't get too neurotic about the protection. A full erase will clear the
protection. |
|
|
djsb
Joined: 29 Jan 2011 Posts: 41
|
|
Posted: Sun May 07, 2023 7:22 am |
|
|
I've just tried this code, which I admit is just a copy and paste from an example in this forum.
Code: |
#include <18F27J53.h>
#device ADC=12
#fuses HSPLL,PLLEN,PLL5, NOCPUDIV, NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void main() {
while(TRUE)
{
output_high(PIN_B6);
delay_ms(1000);
output_low(PIN_B6);
delay_ms(1000);
}
}
|
This gives me a square wave with a period of 832ms (on/off for 416ms). The dev board I'm using has an 8MHz crystal (I changed the board). I'd like to learn how to use both the internal RC oscillator AND the external clock. I'm obviously going to have to read the manual to figure out which fuses to set/clear. Can anyone give me some pointers in the right direction. Thanks.
David. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun May 07, 2023 8:51 am |
|
|
In the datasheet, locate the block diagram for the 'clock'. You'll see all the different paths a 'clock' can follow......
I printed that page of for the 18f46k22 years ago as they're quite complicated compared to the 16C84 PICs...... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun May 07, 2023 10:29 am |
|
|
First thing, on most of the chips you can only use one clock _OR_ the
other. Not AND.
What would be the point anyway?. If you have the better accuracy of
a crystal fitted, using the intenal oscillator would be pointless.
That said, the easiest way to setup for each is:
Code: |
#fuses HSPLL,PLLEN,PLL5, NOCPUDIV, NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(clock=8000000)
//This is wrong. The PLL on this chip is designed to give 96Mhz for the
//USB. The settings are wrong for your clock, and the CPU will not
//be running at 8MHz if it did work.
//For 8MHz crystal, you need to use PLL2 Table 3-5 in the datasheet.
//For an 8MHz CPU frequency you then need CPUDIV6
//The PLL is only designed to accept 4MHz (so 8MHz/2). Then the
//CPU is divided from this /2. So CPUDIV6 gives 96/(2*6) = 8MHz
#fuses HSPLL,PLLEN,PLL2, CPUDIV6, NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(clock=8000000)
//Much easier and safer to use:
#fuses NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(CRYSTAL=8MHz, CLOCK=8MHz, USB_FULL)
//This will automatically set the fuses up for this
//or
#fuses NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(INTERNAL=8MHz, CLOCK=8MHz, USB_FULL, ACT)
//This sets up the internal oscillator to run at 8MHz, and enables
//active clock tuning (needed for USB with the internal oscillator)
//then you have choices of faster clock rates from the same crystal
//8, 16, 24 or 48MHz are supported from this crystal
#fuses NOWDT,NOPROTECT
#fuses ADC12, RTCOSC_INT, DSWDTOSC_T1
#use delay(CRYSTAL=8MHz, CLOCK=16MHz, USB_FULL)
|
You need to really study the datasheet, and look at what options
are actually supported for a particular crystal. Only use the legal
settings if you want it to work.
This PLL will run a small margin away from it's rated frequency, but
not a lot. USB will only work if it is running at 96MHz.
Last edited by Ttelmah on Sun May 07, 2023 10:44 am; edited 2 times in total |
|
|
djsb
Joined: 29 Jan 2011 Posts: 41
|
|
Posted: Sun May 07, 2023 10:42 am |
|
|
Thank you, Ttelmah and temtronic,
I appreciate the clear explanation provided.
I will definitely read the datasheet as suggested, starting with the block diagram and then delving more deeply. Have a nice day. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|