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 settings for 18F2455

Status
Not open for further replies.

parts-man73

New Member
I have a Junebug and I decided to give C programming a try on the 18F series.

I programmed the onboard 18F1320 without issues with a simple LED blinker.

I had a 18F2455 around from another project (one that provided a HEX file, so I didn't write the code myself). I plugged it into a breadboard, and jumpered the programming and power wires to the breadboard.

The code builds successfully, and the PIC programs successfully, but I do not get the intended results. I want to turn on RB0, which I have an LED properly attached to.

Code:
// My first C program */

#include <p18f2455.h>

#pragma config WDT = OFF


void main(void)
{
  TRISB = 0b00000000;
  PORTB = 0b00000001;
  while(1);
}

First of all...I'm wondering, since I'm not utilizing the USB, is a crystal required, as I just want to use the internal oscillator?

btw...I manually set the configuration bits in MPLAB to make PORTB all digital I/O, until I figure out the correct way to do it in code.

I try to set the OSC with
Code:
#pragma config OSC = INTHS
but the compiler kicks out "Error [1224] configuration setting 'OSC' not recognized"

Any good references to where to look for #pragma config settings? and just general C help? I've looked at a few tutorials I found with Google, including 3vO's tutorial that are in development. I've never used C before, but I have programmed in other langauges, including some assembler, Pascal, basic and other similar.

Thanks in advance,
Brian
 
Wahoo! that fixed it right up. That was exactly the information I need.

btw.... do you plan on continuing work on the tutorials? I just found them so I don't know how long or if they have been in the works. I saw your post a few months ago asking for volunteers for your team.

Thanks for the help
Brian
 
The tutorials are are work in progress. The truth is I am still playing with format and content. You may have noticed that they differ signifigantly. I have a class that is just starting programming. This gives me some insight into what works outside my mind. :) If there is any specific area you would like to see let me. No prommises but you never can tell.

3v0

Wahoo! that fixed it right up. That was exactly the information I need.

btw.... do you plan on continuing work on the tutorials? I just found them so I don't know how long or if they have been in the works. I saw your post a few months ago asking for volunteers for your team.

Thanks for the help
Brian
 
I'm looking for tutorials covering the basic structure and operators of the language. I've figured out a simple "While" loop, but I haven't tried a "For" loop or function calls yet. Interrupts are something else I'll have to check out soon.
 
I'm looking for tutorials covering the basic structure and operators of the language. I've figured out a simple "While" loop, but I haven't tried a "For" loop or function calls yet. Interrupts are something else I'll have to check out soon.

At this point an interesting exercise is to write the same loop using goto, for and while. First up and then down. Once you do that looping should be clear.
I have attached a 2 pages on the subject in open office format.

Functions are easy but look at data types and their modifiers first. It will help you understand the return type and parameter list/passing.
 

Attachments

  • loops.zip
    22.8 KB · Views: 305
Last edited:
Excellent, thank you, I'll work through that tonight after I put the kids to bed.

Maybe you could wrap your tutorials up into book form.... call it "When in doubt, PIC C" (unless that's already taken) it was my motto in high school when taking multiple choice tests.
 
You can also get the settings by opening a msdos box (run cmd) and typing "mcc18 --help-config -p=18f2455". You can also create a shortcut to mcc18 with the same parameters and you will get the following,
Code:
C:\MCC18>mcc18 --help-config -p=18f2455 
Configuration settings available for processor 18F2455
  PLL Prescaler Selection bits:
    PLLDIV = 1     	No prescale (4 MHz oscillator input drives PLL directly)
    PLLDIV = 2     	Divide by 2 (8 MHz oscillator input)
    PLLDIV = 3     	Divide by 3 (12 MHz oscillator input)
    PLLDIV = 4     	Divide by 4 (16 MHz oscillator input)
    PLLDIV = 5     	Divide by 5 (20 MHz oscillator input)
    PLLDIV = 6     	Divide by 6 (24 MHz oscillator input)
    PLLDIV = 10    	Divide by 10 (40 MHz oscillator input)
    PLLDIV = 12    	Divide by 12 (48 MHz oscillator input)

  CPU System Clock Postscaler:
    CPUDIV = OSC1_PLL2	[OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]
    CPUDIV = OSC2_PLL3	[OSC1/OSC2 Src: /2][96 MHz PLL Src: /3]
    CPUDIV = OSC3_PLL4	[OSC1/OSC2 Src: /3][96 MHz PLL Src: /4]
    CPUDIV = OSC4_PLL6	[OSC1/OSC2 Src: /4][96 MHz PLL Src: /6]

  USB Clock Selection bit (used in Full Speed USB mode only; UCFG:FSEN = 1):
    USBDIV = 1     	USB clock source comes directly from the primary oscillator block with no postscale
    USBDIV = 2     	USB clock source comes from the 96 MHz PLL divided by 2

  Oscillator Selection bits:
    FOSC = XT_XT   	XT oscillator, XT used by USB
    FOSC = XTPLL_XT	XT oscillator, PLL enabled, XT used by USB
    FOSC = ECIO_EC 	External clock, port function on RA6, EC used by USB
    FOSC = EC_EC   	External clock, CLKOUT on RA6, EC used by USB
    FOSC = ECPLLIO_EC	External clock, PLL enabled, port function on RA6, EC used by USB
    FOSC = ECPLL_EC	External clock, PLL enabled, CLKOUT on RA6, EC used by USB
    FOSC = INTOSCIO_EC	Internal oscillator, port function on RA6, EC used by USB
    FOSC = INTOSC_EC	Internal oscillator, CLKOUT on RA6, EC used by USB
    FOSC = INTOSC_XT	Internal oscillator, XT used by USB
    FOSC = INTOSC_HS	Internal oscillator, HS used by USB
    FOSC = HS      	HS oscillator, HS used by USB
    FOSC = HSPLL_HS	HS oscillator, PLL enabled, HS used by USB

  Fail-Safe Clock Monitor Enable bit:
    FCMEN = OFF    	Fail-Safe Clock Monitor disabled
    FCMEN = ON     	Fail-Safe Clock Monitor enabled

  Internal/External Oscillator Switchover bit:
    IESO = OFF     	Oscillator Switchover mode disabled
    IESO = ON      	Oscillator Switchover mode enabled

  Power-up Timer Enable bit:
    PWRT = ON      	PWRT enabled
    PWRT = OFF     	PWRT disabled

  Brown-out Reset Enable bits:
    BOR = OFF      	Brown-out Reset disabled in hardware and software
    BOR = SOFT     	Brown-out Reset enabled and controlled by software (SBOREN is enabled)
    BOR = ON_ACTIVE	Brown-out Reset enabled in hardware only and disabled in Sleep mode (SBOREN is disabled)
    BOR = ON       	Brown-out Reset enabled in hardware only (SBOREN is disabled)

  Brown-out Voltage bits:
    BORV = 0       	Maximum setting
    BORV = 1       	
    BORV = 2       	
    BORV = 3       	Minimum setting

  USB Voltage Regulator Enable bit:
    VREGEN = OFF   	USB voltage regulator disabled
    VREGEN = ON    	USB voltage regulator enabled

  Watchdog Timer Enable bit:
    WDT = OFF      	HW Disabled - SW Controlled
    WDT = ON       	HW Enabled - SW Disabled

  Watchdog Timer Postscale Select bits:
    WDTPS = 1      	1:1
    WDTPS = 2      	1:2
    WDTPS = 4      	1:4
    WDTPS = 8      	1:8
    WDTPS = 16     	1:16
    WDTPS = 32     	1:32
    WDTPS = 64     	1:64
    WDTPS = 128    	1:128
    WDTPS = 256    	1:256
    WDTPS = 512    	1:512
    WDTPS = 1024   	1:1024
    WDTPS = 2048   	1:2048
    WDTPS = 4096   	1:4096
    WDTPS = 8192   	1:8192
    WDTPS = 16384  	1:16384
    WDTPS = 32768  	1:32768

  MCLR Pin Enable bit:
    MCLRE = OFF    	RE3 input pin enabled; MCLR disabled
    MCLRE = ON     	MCLR pin enabled; RE3 input pin disabled

  Low-Power Timer 1 Oscillator Enable bit:
    LPT1OSC = OFF  	Timer1 configured for higher power operation
    LPT1OSC = ON   	Timer1 configured for low-power operation

  PORTB A/D Enable bit:
    PBADEN = OFF   	PORTB<4:0> pins are configured as digital I/O on Reset
    PBADEN = ON    	PORTB<4:0> pins are configured as analog input channels on Reset

  CCP2 MUX bit:
    CCP2MX = OFF   	CCP2 input/output is multiplexed with RB3
    CCP2MX = ON    	CCP2 input/output is multiplexed with RC1

  Stack Full/Underflow Reset Enable bit:
    STVREN = OFF   	Stack full/underflow will not cause Reset
    STVREN = ON    	Stack full/underflow will cause Reset

  Single-Supply ICSP Enable bit:
    LVP = OFF      	Single-Supply ICSP disabled
    LVP = ON       	Single-Supply ICSP enabled

  Extended Instruction Set Enable bit:
    XINST = OFF    	Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
    XINST = ON     	Instruction set extension and Indexed Addressing mode enabled

  Background Debugger Enable bit:
    DEBUG = ON     	Background debugger enabled, RB6 and RB7 are dedicated to In-Circuit Debug
    DEBUG = OFF    	Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins

  Code Protection bit Block 0:
    CP0 = ON       	Block 0 (000800-001FFFh) code-protected
    CP0 = OFF      	Block 0 (000800-001FFFh) not code-protected

  Code Protection bit Block 1:
    CP1 = ON       	Block 1 (002000-003FFFh) code-protected
    CP1 = OFF      	Block 1 (002000-003FFFh) not code-protected

  Code Protection bit Block 2:
    CP2 = ON       	Block 2 (004000-005FFFh) code-protected
    CP2 = OFF      	Block 2 (004000-005FFFh) not code-protected

  Boot Block Code Protection bit:
    CPB = ON       	Boot block (000000-0007FFh) code-protected
    CPB = OFF      	Boot block (000000-0007FFh) not code-protected

  Data EEPROM Code Protection bit:
    CPD = ON       	Data EEPROM code-protected
    CPD = OFF      	Data EEPROM not code-protected

  Write Protection bit Block 0:
    WRT0 = ON      	Block 0 (000800-001FFFh) write-protected
    WRT0 = OFF     	Block 0 (000800-001FFFh) not write-protected

  Write Protection bit Block 1:
    WRT1 = ON      	Block 1 (002000-003FFFh) write-protected
    WRT1 = OFF     	Block 1 (002000-003FFFh) not write-protected

  Write Protection bit Block 2:
    WRT2 = ON      	Block 2 (004000-005FFFh) write-protected
    WRT2 = OFF     	Block 2 (004000-005FFFh) not write-protected

  Boot Block Write Protection bit:
    WRTB = ON      	Boot block (000000-0007FFh) write-protected
    WRTB = OFF     	Boot block (000000-0007FFh) not write-protected

  Configuration Register Write Protection bit:
    WRTC = ON      	Configuration registers (300000-3000FFh) write-protected
    WRTC = OFF     	Configuration registers (300000-3000FFh) not write-protected

  Data EEPROM Write Protection bit:
    WRTD = ON      	Data EEPROM write-protected
    WRTD = OFF     	Data EEPROM not write-protected

  Table Read Protection bit Block 0:
    EBTR0 = ON     	Block 0 (000800-001FFFh) protected from table reads executed in other blocks
    EBTR0 = OFF    	Block 0 (000800-001FFFh) not protected from table reads executed in other blocks

  Table Read Protection bit Block 1:
    EBTR1 = ON     	Block 1 (002000-003FFFh) protected from table reads executed in other blocks
    EBTR1 = OFF    	Block 1 (002000-003FFFh) not protected from table reads executed in other blocks

  Table Read Protection bit Block 2:
    EBTR2 = ON     	Block 2 (004000-005FFFh) protected from table reads executed in other blocks
    EBTR2 = OFF    	Block 2 (004000-005FFFh) not protected from table reads executed in other blocks

  Boot Block Table Read Protection:
    EBTRB = ON     	Boot block (000000-0007FFh) protected from table reads executed in other blocks
    EBTRB = OFF    	Boot block (000000-0007FFh) not protected from table reads executed in other blocks

I use PFE and have this as the default dos command to run.

Mike.
 
As far as operators are concerned, do standard C or C++ operators apply in C18. I found no C18 specific list of operators (arithmetic and Boolean comparision)

I found this Wiki https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B are those all valid C18 operators?

Also - i looked over the config help file, I ended up using "FOSC = INTOSC_HS" but I tried a few different choices, and I played with the PLL settings (both pre and post scaler) - The LED always blinked at the same rate. What speed is the internal osc running at with the "FOSC = INTOSC_HS" setting, and how can I change that?

Thanks for all your help guys!
Brian
 
Unfortunately, the pll cannot be used with the internal oscillator on the 2455 (it can on later chips). If you want to vary the frequency then you can change it with the OSCCON register. The default speed is 1MHz.

Mike.
 
Pommie,

Thank you, I see the OSCCON settings I need to change now in the datasheet. Looks like bits 4..6 need to be changed to 111 for full speed 8mhz operation.

Can I just say OSCCON = 0b01110000; without regard to the other bits in OSCCON?

Or should I do a bitwise AND with 0b01110000 ?
 
I have trouble accessing this link. can some one tell me how eactly to access this
MPLAB
HELP>TOPICS>LANGUAGE_TOOLS>C18_CONFIG_SETTINGS
 
I have trouble accessing this link. can some one tell me how eactly to access this
MPLAB
HELP>TOPICS>LANGUAGE_TOOLS>C18_CONFIG_SETTINGS

MPLAB HELP>TOPICS>LANGUAGE TOOLS>PIC18 Config Settings> select ur pic of choice from the drop down menu at the left
 
in 18f2455, i want the settings for the internal oscillator for my project. i am not using the usb module of 18f2455....
please give me the code in C...
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top