Blink I/O PIC16F887

(instructions, reset, WDT, specifications...) PIC12F6xx, PIC16Fxxx, PIC16F6x, PIC16F7x

Blink I/O PIC16F887

Postby SSAnne_ » Thu Sep 03, 2020 3:49 pm

XC8 compiler/PICKit 4/MPLAB X IDE v5.35

Hi, I'm trying to blink any pin on the Port D pins (I've tried the same method below on ports A & B),
currently have one connected to RD3.

It builds fine, it connects to the board fine.
But I get nothing on RD3 or any of RD0-RD7.

I'm fairly certain it isn't any of my circuit connections,
I've swapped out the chip for a duplicate to no avail.

Below is all of my code.

Code: Select all
/*
 * File:   newmain1.c
 * Author: Eric
 *
 * Created on September 2, 2020, 6:46 PM
 */


#include <xc.h>
#define _XTAL_FREQ (4000000UL)
void main(void) {
    TRISD = 0x00;
    while(1){
        PORTD = 0xFF;
        __delay_ms(100);
        PORTD = 0x00;
    }
    return;
}
Attachments
PIC.jpg
PIC.jpg (101.44 KiB) Viewed 3646 times
SSAnne_
 
Posts: 6
Joined: Thu Sep 03, 2020 3:24 pm
PIC experience: EE Student

Re: Blink I/O PIC16F887

Postby vloki » Thu Sep 03, 2020 4:00 pm

How do you know the PIC is running at all?
Are you programming in debug-mode?

Did you set the configuration bits for internal oscillator?
->Show them
vloki
Verified identity
 
Posts: 186
Joined: Wed May 28, 2014 8:42 am
Location: Germany
PIC experience: Professional 5+ years with MCHP products

Re: Blink I/O PIC16F887

Postby SSAnne_ » Thu Sep 03, 2020 4:12 pm

vloki wrote:How do you know the PIC is running at all?
Are you programming in debug-mode?

Did you set the configuration bits for internal oscillator?
->Show them


I don't really, I don't think it is in debug mode.

I've not done anything beyond the code I posted.
SSAnne_
 
Posts: 6
Joined: Thu Sep 03, 2020 3:24 pm
PIC experience: EE Student

Re: Blink I/O PIC16F887

Postby ric » Thu Sep 03, 2020 9:10 pm

Your code is missing the configuration bit settings.
They are not optional, you MUST specify them.
The _XTAL_FREQ line does NOT set your oscillator speed, it just tells the compiler what you have already set it to.
Have a look at https://microchipdeveloper.com/mplabx:v ... ation-bits
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Blink I/O PIC16F887

Postby SSAnne_ » Fri Sep 04, 2020 1:10 am

ok I've done that now, and still nothing from the chip. I've changed it from blinking
to just being on; to prevent a bug where the led is turning on and off too quickly for me to see.

I think my oscillator is set to 8MHz but, FOSC doesn't specify what
the speed is just what oscillator is being used
Code: Select all
/*
 * File:   newmain2.c
 * Author: Eric
 *
 * Created on September 2, 2020, 6:46 PM
 */

// PIC16F887 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
//#define _XTAL_FREQ (20000000UL)
void main(void) {
    TRISD = 0x00;
    //PORTA = 0xFF;
    //RA7 = 1;
    //*0x05=0xFF
    while(1){
      ///PORTB = 0xFF; 
        PORTD = 0xFF;
        //__delay_ms(10000);
        //PORTD = 0x00;
    }
    return;
}
SSAnne_
 
Posts: 6
Joined: Thu Sep 03, 2020 3:24 pm
PIC experience: EE Student

Re: Blink I/O PIC16F887

Postby ric » Fri Sep 04, 2020 1:51 am

FOSC = HS
specifies an external oscillator. I don't see a crystal on your board. (See the comment on that line)
You probably want the internal oscillator!
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Blink I/O PIC16F887

Postby SSAnne_ » Fri Sep 04, 2020 2:30 am

ric wrote:FOSC = HS
specifies an external oscillator. I don't see a crystal on your board. (See the comment on that line)
You probably want the internal oscillator!


Yeah which is strange, looking in the datasheet its a register that selects what kind of external oscillator you want to use. Weird I don't see an option to choose internal oscillator on any of these.

The register that controls the internal oscillator is "OSSCON", which I've now tried to define in my main() function, still isn't working. I've defined it as 0x75 because I want it to use 8MHz (0111) and I'm trying to use HFINTOSC as the internal oscillator with the 0101 (I'll attach register photo from datasheet).

Although still no luck.

Code: Select all
/*
 * File:   newmain2.c
 * Author: Eric
 *
 * Created on September 2, 2020, 6:46 PM
 */

// PIC16F887 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ (20000000UL)
void main(void) {
    OSCCON = 0x75;
    TRISD = 0x00;
    //PORTA = 0xFF;
    //RA7 = 1;
    //*0x05=0xFF
    while(1){
      ///PORTB = 0xFF; 
        PORTD = 0xFF;
        //__delay_ms(10000);
        //PORTD = 0x00;
    }
    return;
}
Attachments
internal_osc.PNG
internal_osc.PNG (53.59 KiB) Viewed 3633 times
SSAnne_
 
Posts: 6
Joined: Thu Sep 03, 2020 3:24 pm
PIC experience: EE Student

Re: Blink I/O PIC16F887

Postby ric » Fri Sep 04, 2020 3:30 am

You've skipped a step.
Running code cannot write to a register until it is running, so you MUST select a valid oscillator to start with.
Otherwise it's "chicken or the egg"
So, you must select an operaiton oscillator in the config settings. Try:
Code: Select all
#pragma config FOSC = INTOSCIO


Also, change both
Code: Select all
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)

to OFF. You do not want those options on.

WDTE=ON will cause your program to contiinually reset until you learn how to "kick the dog".
LVP=ON will randomly drop your chip into programming mode if the RB3 pin is floating, and stops you from using RB3 as a GPIO pin.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: Blink I/O PIC16F887

Postby AussieSusan » Fri Sep 04, 2020 3:48 am

Go to the data sheet and look up Register 14-1 on page 206. Look at the FOSC field values - you need one of the values INTOSC or INTOSCIO (depending on what you want to do with the RA6 and RA7 pins). That will set the internal oscillator.
Look at Figure 4-1 and you will see how the whole oscillator is configured and which fields you need to set.
Remember that the CONFIG bits will determine how the MCU powers up. By setting FOSC=HS, you are saying that the MCU is to power up and use the crystal to clock the device right from the start. If there is not crystal connected, then the MCU will power up with NO oscillator running and therefore will NOT RUN AT ALL. Therefore, in your case you MUST tell the MCU to power up using the internal oscillator in the CONFIG setting.
Also look at the power-up setting for the SCS bit in the image you posted - it says that the power up value is '0' which is the FOSC clock source. This bit cannot be set except when code is running and therefore when there is a running clock.
Susan
AussieSusan
Verified identity
 
Posts: 173
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Blink I/O PIC16F887

Postby SSAnne_ » Fri Sep 04, 2020 3:51 am

Code: Select all
#pragma config FOSC = INTOSCIO


gives an unknown configuration error. As it isn't one of the options

FOSC_options.png
FOSC_options.png (21.69 KiB) Viewed 3628 times


I tried to use a define statement for OSSCON outside of main, but that also doesn't work
SSAnne_
 
Posts: 6
Joined: Thu Sep 03, 2020 3:24 pm
PIC experience: EE Student

Next

Return to 14-Bit Core

Who is online

Users browsing this forum: No registered users and 3 guests

cron