All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 07-08-2012, 04:09 PM
ActivePower's Avatar
ActivePower ActivePower is offline
Senior Member
 
Join Date: Mar 2012
Location: India (GMT + 5.5)
Posts: 152
Blog Entries: 3
Default PIC16F877A LED Blinking Program

I am a complete noob in programming a PIC microcontroller and I have been trying to get the most basic of the tasks (flashing an LED) done.

I am using MPLAB v8.73 with HiTech C Compiler and a PIC16F877A

I found an circuit online (http://justanotherlanguage.org/sites...ed_circuit.jpg) and built the circuit with a few modifications in the component values.

The code is as follows:

Code:
#include<pic.h>
#define _XTAL_FREQ 20000000

void main()
{
     TRISB=0;
     for (;;)
     {
        RB0=1;
        __delay_ms(250);
        RB0=0;
        __delay_ms(250);
     }
}
Select All
The code builds successfully but does not blink the LED Maybe there is a problem with the circuit but I wanted to be sure about the code first.

Thanks
Reply With Quote
  #2  
Old 07-08-2012, 04:16 PM
takao21203's Avatar
takao21203 takao21203 is offline
Senior Member
 
Join Date: Apr 2012
Location: Ireland
Posts: 2,354
Blog Entries: 16
Send a message via Skype™ to takao21203
Default

Good work!

You need to modify the ANSEL register setting for this PIC model, if I guess right. Also configuration needs to be set correctly, including to select HS oscillator.

In the schematic the LED is connected to PORTA.
Reply With Quote
  #3  
Old 07-08-2012, 04:37 PM
ActivePower's Avatar
ActivePower ActivePower is offline
Senior Member
 
Join Date: Mar 2012
Location: India (GMT + 5.5)
Posts: 152
Blog Entries: 3
Default

Hey, thanks! I had changed the LED to the RB0 position on the PIC. Does that make any difference? And I am using a 20 MHz crystal oscillator, so I included 'XT' in the config bits from the datasheet. And what is the syntax for changing the 'ANSEL', I am afraid I am unaware of that.

Thanks!
Reply With Quote
  #4  
Old 07-08-2012, 04:38 PM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

You have not set the config bits as far as I can see. The config bits are related to section 14.0 SPECIAL FEATURES OF THE CPU in the datasheet.
Reply With Quote
  #5  
Old 07-08-2012, 04:45 PM
ActivePower's Avatar
ActivePower ActivePower is offline
Senior Member
 
Join Date: Mar 2012
Location: India (GMT + 5.5)
Posts: 152
Blog Entries: 3
Default

I set the config bits from the MPLAB IDE option 'Configuration Bits' and unchecked the 'Use configuration bits set in code' and selected the XT oscillator from the drop-down menu. Does that not count? I am sorry I do not know the details.
Reply With Quote
  #6  
Old 07-08-2012, 04:53 PM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

Quote:
Originally Posted by ActivePower View Post
I set the config bits from the MPLAB IDE option 'Configuration Bits' and unchecked the 'Use configuration bits set in code' and selected the XT oscillator from the drop-down menu. Does that not count? I am sorry I do not know the details.
It is OK for a beginner to it that way. But it is poor for documentation. Anyway If you are using a 20MHz crystal. The setting should be HS and not XT
Reply With Quote
  #7  
Old 07-08-2012, 05:03 PM
takao21203's Avatar
takao21203 takao21203 is offline
Senior Member
 
Join Date: Apr 2012
Location: Ireland
Posts: 2,354
Blog Entries: 16
Send a message via Skype™ to takao21203
Default

XT and 20 MHz does never works that's that.
Reply With Quote
  #8  
Old 07-08-2012, 05:04 PM
JohnInTX's Avatar
JohnInTX JohnInTX is offline
Senior Member
 
Join Date: Jun 2012
Location: Texas GMT-5
Posts: 974
Default Is the compiler selecting the RAM banks?

RP1/0 in STATUS have to select RAM bank 1 or 3 for TRISB and then change to 0 or 2 for PORTB. Step through your code in MPLAB with a watch window open to be sure that STATUS is correct when addressing the I/O. 'Watch' PORTB as well to see if the LSB is being set/cleared.

You should not have to in this case but instead of RB0 = 1, try writing (unsigned char) 1 and 0 to the port (LSB of char is RB0). Sometimes flipping individual bits on midrange PICs can be problematic.

As others have indicated, HS is the correct oscillator to use for 20MHz. HS drives the XTAL harder than XT.

You can indeed set the config in MPLAB but eventually it should be in your source code.

Finally, you can step in the Disassembly Output window in MPLAB to see what's going on on the code level.
Reply With Quote
  #9  
Old 07-08-2012, 05:11 PM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

Quote:
Originally Posted by JohnInTX View Post
RP1/0 in STATUS have to select RAM bank 1 or 3 for TRISB and then change to 0 or 2 for PORTB. Step through your code in MPLAB with a watch window open to be sure that STATUS is correct when addressing the I/O
The C compiler will take care of this
Reply With Quote
  #10  
Old 07-09-2012, 08:48 AM
ActivePower's Avatar
ActivePower ActivePower is offline
Senior Member
 
Join Date: Mar 2012
Location: India (GMT + 5.5)
Posts: 152
Blog Entries: 3
Default

Here is the code after modifications:

Code:
#include<pic.h>
#define _XTAL_FREQ 20000000

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_OFF & WRT_OFF & DEBUG_ON & CPD_OFF & CP_OFF)

//Should I leave DEBUG ON? I have just a programmer and I do not know what it does apart from programming the PIC

void main()
{
    TRISB=0;
    for (;;)
    {
       RB0=1;
       __delay_ms(250);
       RB=0;
      __delay_ms(250);
    }
}
Select All
This builds successfully but would it run on the PIC?
Also, would it make any difference if I use RA pins or RB pins? The circuit is configured for RA but I wrote the code from the HiTech C Quickstart guide and it suggests RB pins.
On a slightly unrelated note, for my PIC circuit-related queries should I make a post here or in the Projects Forum?

Thanks!
Reply With Quote
Reply   Post New Thread

Tags
, , , , ,


Related Site Pages
Section Title
Worksheet Design Project: Logic probe


Similar Threads
Thread Thread Starter Forum Replies Last Post
Project: PIC LED Sequencer MMcLaren The Completed Projects Collection 23 05-09-2012 03:03 AM
Switch LED on blinking signal/input tunafish24 The Projects Forum 13 05-04-2012 03:11 AM
Simple LED program josephgebran The Projects Forum 4 06-11-2011 08:17 AM
Help to program PIC16F88 Kai-Itza Programmer's Corner 1 08-03-2010 06:55 PM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:44 PM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin
Copyright ©2000 - 2014, vBulletin Solutions, Inc.