I have a PIC16F877A and I wrote a program in MPLAB X IDE to blink a LED.
This is my program :
#include <xc.h>
#define _XTAL_FREQ 6000000
void main()
{
TRISB7 = 0;
while(1)
{
RB7=1;
__delay_ms(1000);
RB7=0;
__delay_ms(1000);
}
}
I compiled it successfully, but when I load the hex file in PICKit2v2 , it show a warning as below :
enter image description here
And also when I tried to write that hex file on MCU, It show the below error :
enter image description here
So I reopened my project in MPLAB X IDE V2.26 and tried to add configuration data to my code, using the wizard in Windows --> PIC Memory view --> Configuration Bits But when I click it, I see this below window on the bottom of screen :
Question 1 : Why most of memory is equal to 3FFF
in the first and second image that I attached above? Does that mean my program is just about 6 byte? (The first six bytes are different)
Question 2 : The error that I received is about configuration bits? Or it is something else?!
Question 3 : Which one of this optioned must I select for internal clock? I don't want to use any external clock or crystal o RC.
Question 4 : What is the difference between htc.h and xc.h libraries? I some of example code in the internet I saw htc.h included in the above of program? What is it for? Which libraries is for delay() function?
Question 5 : Is the second line in my program mandatory (I mean #define _XTAL_FREQ 6000000
) Does it for external crystal? or ...?
I am a brand new in PIC MCU's programming. Pleas shed any light on this issue for me and for future viewers.
Update :
I added configuration setting to my program : Now this is my program :
/*
* File: newmain.c
* Author: Amir Ebrahim
*
* Created on January 3, 2015, 1:42 PM
*/
#include <xc.h>
#define _XTAL_FREQ 12000000
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
void main()
{
TRISB7 = 0;
while(1)
{
RB7=1;
__delay_ms(1000);
RB7=0;
__delay_ms(1000);
}
}
When I load the hex file in PICKit2v2 I received successful load :
enter image description here
But when I want to write the file on the MCU, I receive the same previous error!
enter image description here
Why?! :(
-
\$\begingroup\$ Have you tried flashing another PIC? \$\endgroup\$Triak– Triak2015年01月02日 14:26:47 +00:00Commented Jan 2, 2015 at 14:26
-
\$\begingroup\$ @Triak No, Unfortunately I have bought only one MCU! FYI, I can erase it and read it successfully. But I can't write to it! May the MCU was corrupted while I can read/erase it? \$\endgroup\$Ebrahim Ghasemi– Ebrahim Ghasemi2015年01月02日 14:31:30 +00:00Commented Jan 2, 2015 at 14:31
-
\$\begingroup\$ Well, then your programmer is fine... Get another PIC (ex.16F628A) and try to burn a 'HelloWorld' program. Your PIC might be dead... What programmer do you use ? \$\endgroup\$Triak– Triak2015年01月02日 14:33:46 +00:00Commented Jan 2, 2015 at 14:33
-
\$\begingroup\$ @Triak I don't know what kind of programmer I have!! On the box is written "PICKIT2- MOdel: PUP0401" and you can see two image of it here : eshop.eca.ir/1673-thickbox_default/pic-usb-programmer.jpg and eshop.eca.ir/1674-large_default/pic-usb-programmer.jpg Successfully reading and erasing doesn't mean that my PIC is not dead? \$\endgroup\$Ebrahim Ghasemi– Ebrahim Ghasemi2015年01月02日 14:43:01 +00:00Commented Jan 2, 2015 at 14:43
-
1\$\begingroup\$ I see that MPLABX v2.26 has 'beta' support for PICkit2 when I set up a dummy project for the PIC16F877A. Try programming it directly from MPLABX instead of with the standalone programming tool. Also, consider investing in a PICkit3 programmer - PICkit2 is largely obsolete these days. \$\endgroup\$Adam Lawrence– Adam Lawrence2015年01月02日 17:20:35 +00:00Commented Jan 2, 2015 at 17:20
1 Answer 1
Question 1 : Why most of memory is equal to 3FFF in the first and second image that I attached above? Does that mean my program is just about 6 byte? (The first six bytes are different)
Scroll down - you'll see more than just those. The first bit is "jump to your code", then there's the vector table for the interrupts, then there's your code.
Question 2 : The error that I received is about configuration bits? Or it is something else?!
That's what it says, isn't it? Without config bits it won't work.
Question 3 : Which one of this optioned must I select for internal clock? I don't want to use any external clock or crystal or RC.
The PIC16F877A has no internal oscillator. For that you want to pick a more modern chip.
Question 4 : What is the difference between htc.h and xc.h libraries? I some of example code in the internet I saw htc.h included in the above of program? What is it for? Which libraries is for delay() function?
htc.h
is for the Hi-Tec compiler. xc.h
is for the XC8 compiler. Pick the one for your compiler.
Question 5 : Is the second line in my program mandatory (I mean #define _XTAL_FREQ 6000000) Does it for external crystal? or ...?
That may be used for the timing of __delay_ms()
depending on your compiler's implementation. If so, it needs to match the crystal you attach to the chip.
-
\$\begingroup\$ Would you please take a look at the Update section in the question? Thanks \$\endgroup\$Ebrahim Ghasemi– Ebrahim Ghasemi2015年01月02日 14:20:11 +00:00Commented Jan 2, 2015 at 14:20