Hot!Help with getting started (PIC16F877A LED Blinking Program in C)

Author
Smashbrohammer
Starting Member
  • Total Posts : 17
  • Reward points : 0
  • Joined: 2016/12/04 22:31:22
  • Location: 0
  • Status: offline
2017/08/12 15:03:29 (permalink)
0

Help with getting started (PIC16F877A LED Blinking Program in C)

I was familiar with a 18pin series in assembly but I am moving onto the PIC16F877A writing in C and I am having a hard time getting from ground zero.
 
Just some facts:
1. I am using a development board that was designed for my school program.  It is uing PICkit 2
2. I am using MPLAB IDE v8.92.   I am unaware if my development board will be able to handle MPLAB X.
3. I am using the High-Tech C compiler
 
At this point I am trying to keep it as basic as possible just to make sure I can get started.  All I am trying to do is set all PORTB pins high to make my LEDS turn on.
 
below is my code.
 
#include <htc.h>   //uses non-legacy pic16f77a.h
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 20000000  //sets up the oscillation frequency
#endif

__CONFIG (0X03F32);
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF & CP_OFF); //this is the non-legacy version
 
//***********************************************************

//MAIN PROGRAM
void main (void)
{
 TRISB = 0b00000000;  //all PORTB configured as outputs

 for (;;){ 
 PORTB = 0b11111111;
 }
}   //end of main
 
I am getting build successful output windows in MPLAB:
 
 
 
I am also getting successful programming on the PICkit 2.
 
 
I am hoping it is something wrong with my code that is an easy fix.
#1

5 Replies Related Threads

    dan1138
    Super Member
    • Total Posts : 2542
    • Reward points : 0
    • Status: offline
    Re: Help with getting started (PIC16F877A LED Blinking Program in C) 2017/08/12 22:15:17 (permalink)
    +1 (1)
    This may be one possible way to do what you want:
    /*
     * Author: DAN1138
     * Date: 2017-AUGUST-12
     * File: main.c
     * Target: PIC16F877A
     * OS: Win7 Pro, 64-bit
     * MPLAB: 8.92
     * Compiler: HTC PICC v9.83 (Free version)
     *
     * Description:
     *  Blinky LED demo program.
     *
     *  Set PORTB to all outputs
     *  Set PORTB to all high
     *  Wait for half a second
     *  Set PORTB to all low
     *  Wait for half a second
     *  Do it again
     *
     */
    #include <htc.h>

    /* Set up the configuration bits */
    __CONFIG( FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & CPD_OFF & WRT_OFF & CP_OFF );

    #define FOSC 20000000L      /* target device system clock freqency */
    #define FCYC (FOSC/4L)      /* target device instruction clock freqency */

    #define _XTAL_FREQ FOSC     /* required for HITECH PICC delay macros */
    /*----------------------------------------------*/
    /* Initialize this PIC                          */
    /*----------------------------------------------*/
    void Init_PIC(void)
    {

        /* disable all interrupt sources */
        INTCON = 0x00;
        PIE1 = 0x00;
        PIE2 = 0x00;

        CMCON = 0x07;           /* turn off comparators */
        ADCON1 = 0x06;          /* Turn off ADC */

        OPTION_REG = 0b11011110;/* PORTB weak pull ups disabled */
                                /* Interrupt on rising edge of RB0/INT pin */
                                /* T0 internal clock source */
                                /* T0 clock edge high-to-low */
                                /* Prescaler assigned to WDT */
                                /* Prescale 1:64 for WDT */
        /* turn on the interrupt system */
        PEIE = 1;
        GIE  = 1;
    }
    /*----------------------------------------------*/
    /* Main                                         */
    /*----------------------------------------------*/
    void main (void)
    {
        Init_PIC();

        TRISB = 0b00000000; /* make all pins of PORTB output pins */
        while(1)
        {
            PORTB = 0b11111111; /* set all PORTB outputs high */
            __delay_ms(500);
            PORTB = 0b00000000; /* set all PORTB outputs low */
            __delay_ms(500);
        }
    }
    /*----------------------------------------------*/
    /* Interrupt handlers                           */
    /*----------------------------------------------*/
    void interrupt Interrupt_Handlers(void)
    {
        /* Handle TIMER0 interrupt request */
        if(T0IE)
        {
            if(T0IF)
            {
                T0IF = 0;
            }
        }
    }

    #2
    NKurzman
    A Guy on the Net
    • Total Posts : 14692
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Help with getting started (PIC16F877A LED Blinking Program in C) 2017/08/13 00:29:58 (permalink)
    +2 (2)
    define _XTAL_FREQ 20000000 //sets up the oscillation frequency
    No it does not. It tell the compiler the frequency you are using. You have a crystal right?
    #3
    Smashbrohammer
    Starting Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2016/12/04 22:31:22
    • Location: 0
    • Status: offline
    Re: Help with getting started (PIC16F877A LED Blinking Program in C) 2017/08/13 07:02:48 (permalink)
    0
    Yes the development board comes with a crystal.
     
    I will try the Dans code in a little bit.
    #4
    Smashbrohammer
    Starting Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2016/12/04 22:31:22
    • Location: 0
    • Status: offline
    Re: Help with getting started (PIC16F877A LED Blinking Program in C) 2017/08/13 09:11:08 (permalink)
    0
    So I tried Dans code...no difference.  I can't seem to get any LED to turn on.
     
    Assuming Dans code is correct...this makes me believe I have a hardware issue now.  Ugh.
    #5
    Smashbrohammer
    Starting Member
    • Total Posts : 17
    • Reward points : 0
    • Joined: 2016/12/04 22:31:22
    • Location: 0
    • Status: offline
    Re: Help with getting started (PIC16F877A LED Blinking Program in C) 2017/08/13 09:26:24 (permalink)
    +1 (1)
    False alarm everyone.  I got my original program to work and Dans.
     
    I had my oscillator miswired.  The thing was...the development board is designed for 18pin/28pin/&40pin.
     
    I was originally wired for the 18pin configuration...I made the stupid assumption that my oscillator wiring would be the same!  Stupid newbie mistake!
     
    Thanks for all of your help!!  Both of your responses helped me fix this problem.
     
    Dans code made me realize I had a hardware issue.
    NKurzman made me start thinking about my oscillator :)
     
    Just an FYI for anyone who is having the same issue, my original code did work.  Of course it doesn't blink the LEDS but I was just trying to see if I could make an LED turn on.
    #6
    Jump to:
    © 2017 APG vNext Commercial Version 4.5