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.

[SOLVED] My first pic microcontroller project refused to work according to design

Status
Not open for further replies.

meche

Junior Member level 2
Joined
Apr 12, 2017
Messages
24
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
252
Please I am new to the pic microcontroller and I just started with pic16f84a. My development platform is the MPLAB IDE, and PICKIT3. After building my hardware on a breadboard, I programmed the chip accordingly but the LED refused to blink. The project is just to blink an LED on and off (port RB0) with a delay of 1000ms. The led comes up but doesn't blink. I used a 8MHz crystal oscillator with two ceramic capacitors of value 20pF. I am just wondering what I am not doing right?
Do I need to set the OSCCON register? does the pic16f84a even have one? Below is my code for the header file:



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection disabled)
 
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
 
#include <xc.h>
 
 
#include "newxc8_header.h"
#define _XTAL_FREQ 8000000
 
void main(void) {
    TRISB0=0;
    while(1)
    {
        RB1=1;
        __delay_ms(1000);
        RB1=0;
        __delay_ms(1000);
    }
    return;

 
Last edited by a moderator:

1. you say you have the LED wired to PORTB.0 but the code toggled PORTB.1
2. check the reset pin (-MCLR) is pulled to VDD. It has to be at logic 1 level to release the internal reset circuit.
3. Check you have capacitors (suggest 100nF) directly across the VSS and VDD pins. Short wire length is essential.
4. Tell us what is in the file "newxc8_header.h", it may be overriding the default settings.
5. Try changing RB1=1; to "PORTB.1 = 1" and RB1=0 to "PORTB.1 = 0".

The 16F84A doesn't have an OSCCON register, it only applies to PICs with internal clock generators.

Brian.
 

Thanks.
1. the mcu is actually wired to PORTB.0 . I later changed to PORTB.1 just to see the difference.
2. The header file " newxc8_header.h" contains the configuration bits settings for pic16f84a that I generated using MPLAB X IDE. Below is the content of the file:

Code:
// PIC16F84 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power-up Timer is disabled)
#pragma config CP = OFF         // Code Protection bit (Code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
 

hello

are some line of code missing ?
main part is not closed .. why return ?

Code:
void main(void) 
{
    TRISB0=0;
    while(1)
    {
        RB1=1;
        __delay_ms(1000);
        RB1=0;
        __delay_ms(1000);
    } 
  }   //<----- last accolade
 

You are using XC8 Compiler and so you have to use

Code:
#define _XTAL_FREQ 8000000

as the first line of code.
 
  • Like
Reactions: meche

    meche

    Points: 2
    Helpful Answer Positive Rating
I would remove the "newxc8_header.h" line.
The contents of a '.h' file are dropped in to the code as though they were typed in at that spot. The '#include' does just that, it includes the contents of the named file at that place in the program. There are some cases where it is essential, for example if the same chunk of code has to included in many files but I fear that in your case, it is just overwriting lines that are already there. Try removing it and see what happens.

Brian.
 

Getting started on Microchip devices can take a bit of work but once you have it worked out then it is (relatively) easier after that.
Betwixt has mentioned the hardware checks that you need to make with this device.
Also is the PicKit3 set to hold the device in reset after it has programmed it in 'release' mode?
Can you compile for 'debug' and make sure that you can step into the code? If nothing else this will confirm that the oscillator is running.

@paulfjujo: unfortunately some compilers complain about the 'main' function not returning a value but as long as you have the main loop set up it does not really hurt and can stop the annoying warning messages.
@Okada: the requirement is that the _XTAL_FREQ symbol is defined before the delay macros are used in the code, not necessarily that it be the first line.
Susan
 

My pic16f84a project is now working thanks to your contributions. I moved the code below [#define __XTAL_FREQ 8000000] from the "newx8_header.h" header file to the c main file. The LED blinks according to the delay i choose.
 

My pic16f84a project is now working thanks to your contributions. I moved the code below [#define __XTAL_FREQ 8000000] from the "newx8_header.h" header file to the c main file. The LED blinks according to the delay i choose.

Good news. These are smart devices and they have their own mind, sort of. You really do not know what will coax them to work.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top