PIC18F452 configuration bits setting

(instructions, reset, WDT, specifications...) PIC17Cxx, PIC18Fxxx

PIC18F452 configuration bits setting

Postby Keno » Sat Nov 07, 2020 11:03 am

Hello everyone!

In the last few days I'm trying to set up simple program (blinking LED) with included configuration bits, in order to check if PIC responds correctly. I have already wired all needed components for this project: added R-C (10k, 22pF) to oscillator pin (OSC2/RA6), 10k pull-up resistor on MCLR, LED with 330R in series to RD0 pin and lastly PICKit3 programmer to all needed pins (wired correctly, I double checked it).

image_2020-11-07_104950-min-min.jpg
image_2020-11-07_104950-min-min.jpg (205.69 KiB) Viewed 1695 times


MPLAB tells me that it recognized the device via programmer, and the code is uploaded without errors (see pictures below). However, the problem might lie in me not setting configuration bits correctly. When I open MCC, there is no port A, B, C or D, where I could set them as I/O ports. But PIC18F452 has multiple I/O ports. So, how to I manually set these ports as output or input? Does this need to be defined in configuration bits area or in main.c file? If so, can someone demonstrate, what I should be or shouldn't be doing it this particular situation?


Here is the main.c file:
Code: Select all
#include <xc.h>
#include <htc.h>
#include <p18cxxx.h>
#include "conifg.h"


void delay (void)
{
  int i;
  for (i = 0; i < 10000; i++)
    ;
}


void main( void )
{

   TRISD = 0x00;

   while (1) {
      PORTDbits.RD0 = 1;
      delay();
      PORTDbits.RD0 = 0;
      delay();
   }
   
}


And here is header file (configuration bits) for this project:

Code: Select all
#ifndef CONFIG_H
#define   CONFIG_H

#include <xc.h> // include processor files - each processor file is guarded. 

#define _XTAL_FREQ 1000000
// PIC18F452 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = RCIO         // Oscillator Selection bits (XT oscillator)
#pragma config OSCS = OFF       // Oscillator System Clock Switch Enable bit (Oscillator system clock switch option is disabled (main oscillator is source))
// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = OFF        // Brown-out Reset Enable bit (Brown-out Reset disabled)
#pragma config BORV = 20        // Brown-out Reset Voltage bits (VBOR set to 2.0V)
// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 128      // Watchdog Timer Postscale Select bits (1:128)
// CONFIG3H
#pragma config CCP2MUX = ON     // CCP2 Mux bit (CCP2 input/output is multiplexed with RC1)
// CONFIG4L
#pragma config STVR = OFF       // Stack Full/Underflow Reset Enable bit (Stack Full/Underflow will not cause RESET)
#pragma config LVP = ON         // Low Voltage ICSP Enable bit (Low Voltage ICSP enabled)
// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-001FFFh) not code protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code protected)
// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot Block (000000-0001FFh) not code protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code protected)
// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000200-001FFFh) not write protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write protected)
// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0001FFh) not write protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write protected)
// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000200-001FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from Table Reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from Table Reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0001FFh) not protected from Table Reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#endif   /* XC_HEADER_TEMPLATE_H */


123969157_629365851092873_6909130685936928153_n.jpg
123969157_629365851092873_6909130685936928153_n.jpg (144.99 KiB) Viewed 1694 times


*P.S. I know some basic stuff about coding in C, but know very little to nothing about coding in C for PIC. I also have PIC16F628A; might have less trouble setting up this one.
Keno
 
Posts: 11
Joined: Sat Nov 07, 2020 10:15 am
PIC experience: EE Student

Re: PIC18F452 configuration bits setting

Postby AussieSusan » Sun Nov 08, 2020 3:32 am

I assume the include of the "conifg.h" file is a typo as the code would not compile with that spelling (presumably). However that implies that we are not seeing the actual code and there could be other differences between what you have posted and what you compiled.

If the first 2 include files, the only one you need is "xc.h" - remove the others as they may be interfering with the required definitions.
In 'config.h" you don't need to include "xc.h" twice. In fact, given that you include "xc.h" before "config.h"in the main code file, you don't need to include it at all (although it doesn't hurt to do so).
Your 'delay' function may not be working as you intend. The compiler may well see that there is nothing in the 'for' loop and optimise the whole loop away.
The XC8 compiler does have built in delay macros (which use the _XTAL_FREQ #define value - in fact they are the only things that use that #define value) and you would be better off using those.
The RC oscillator components need to be connected to the pin OSC1, NOT OSC2, as per Figure 2-3 of the data sheet. This may mean that you do not have an oscillator running. (Note that you don't need an oscillator running to program the chip.)
Also I suggest that you add a bypass capacitor to ALL Vdd pins.

Given all of that, I'm not sure what your problem is. You say you can compile the code correctly and program the chip correctly, so what is the problem?
How do you know there is a problem? What is happening that you don't expect?
Susan
AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: PIC18F452 configuration bits setting

Postby Keno » Mon Nov 09, 2020 10:24 am

Hello Susan!

Geez, I did mess up thing by typing in wrong name of header file meant for configuration bits. I really thought it was something else, like not setting configuration bits correctly. I set it up correctly this time and am able to use it "all the way" (referring to pic below). It's all good now. In meantime I set up PIC16F628A, which also corresponded to my code correctly. I'm glad things are going much smoother now :)

124752175_673881716596911_5982010465143542081_n-min.jpg
124752175_673881716596911_5982010465143542081_n-min.jpg (67.66 KiB) Viewed 1680 times


Thank you for all the help I needed in such situation!

Keno
Keno
 
Posts: 11
Joined: Sat Nov 07, 2020 10:15 am
PIC experience: EE Student


Return to 16-Bit Core

Who is online

Users browsing this forum: No registered users and 13 guests

cron