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.

Problem with program that toggles portb every second in HI Tech C compiler

Status
Not open for further replies.

annna

Member level 2
Member level 2
Joined
Jun 17, 2010
Messages
52
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
pak
Activity points
1,682
Hi everyone,
I have recently started using PIC with C, but I am having some problems. I was trying to make a first test program that toggles portb every second. The simulation works fine but when I tried it in real world nothing happen. I am a using a JDM programmer and Hi Tech C compiler. Can anyone tell me where I am doing the mistake. Here's the code......


#include<htc.h>


#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif

void main(void)
{

TRISB = 0;

while(1)
{
PORTB = 0xFF;
__delay_ms(1000);
PORTB = 0x00;
__delay_ms(1000);

}

}


waiting for your reply..........................
 

Re: Problem with HI Tech

What is the exact PIC model are you using in your design?

---------- Post added at 22:42 ---------- Previous post was at 22:27 ----------

By the way, have you setup your configuration register correctly, for instance disabled the watchdog timer?
 

Re: Problem with HI Tech

I am using PIC16F877A and yes I disabled it
 

Re: Problem with HI Tech

Your program is fine, therefore the problem must be at the hardware level.

After programming the device, have you ran a verify to ensure the device was indeed programmed? While I am not familiar with the JDK programmer, I use Microchips ICD2s, ICD3s and Real ICE, most programming devices require you to release the device from reset, by pulling the MCLR high. Have you done this after successfully programming the device? Also, make sure you compile the program as a "release" version, not "debug." If you compile the code as "release" if all else fails after programming the device, simply power off the circuit, detach the programmer and power the circuit back up. This should allow the the code to run if you have wired the circuit correctly.

Is this a development board you are using or a circuit you have constructed? If you constructed the circuit, please post or upload the schematic.

How are you detecting changes or lack of on PORTB pins in the physical hardware, LEDs? If so, on all pins, if not what pins? Also if you are using LEDs are they accompanied by appropriate current limiting resisters?

After you response to these questions, I should be able to help you further.

---------- Post added at 01:41 ---------- Previous post was at 01:38 ----------

By the way, how are you configuring the configuration register? I do not see a config line in your code, so are you allowing the MPLAB to provide the settings when the code is compiled?

Maybe you should also provide the full configuration settings or value. There is always a possibility incorrect configs can cause problems as well.
 

Re: Problem with HI Tech

Maybe because you don't add this line __CONFIG(0x3F32);
See my example and video LED.
 

Re: Problem with HI Tech

The problem was with configuration bits thanks to both of you...............
 

Re: Problem with HI Tech

this time the problem is while using timer 0..... The book which I am following teaches to use pic18 but if I try programming pic16 I face problems. This time I used the following code:

#include<pic.h>
#include<htc.h>

__CONFIG(0x3F32);

#define _XTAL_FREQ 4000000

void main(void)
{


TRISAbits.TRISA4 = 1;
TRISB = 0;

T0CON = 0x78;

while(1)
{
do{
T0CONbits.TMR0ON = 1;
PORTB = TMR0L;
}while(INTCONbits.TMR0IF==0);

T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;

}
}

and the compiler gives error "c; 15.1 undefined identifier "T0CON"" and same with T0CONbits, INTCON.... I wonder if these registers are called by some other names in Hi Tech compiler..................
 

Re: Problem with HI Tech

There are quite a few differences between the PIC16 and PIC18 architectures. All the #defines for registers, bits and configuration bits can be found in the appropriate header file located in the "include" directory, in this case pic16f877a.h.

Example Path to "include" directory:
C:\Program Files\HI-TECH Software\PICC\9.81\include

Also, especially when learning the PIC architecture, it is more advantageous to use this form:

Code:
__CONFIG(CP_OFF & CPD_OFF & BOREN_OFF & WDTE_OFF & PWRTE_ON & FOSC_HS & FCMEN_OFF & IESO_OFF); //Only an example not for the PIC16F877A

then this form:

Code:
__CONFIG(0x3F32);

The appropriate configuration bit #defines are located at the top of your device's header file, in this case pic16f877a.h.

Using the former example, allows you to turn on and off features without memorizing the configuration register's bit pattern.


You would most likely benefit from studying the best tutorials I've come across on the NET to date:

**broken link removed**

They are very professionally done, cover both the baseline and midrange PICs, using both MPASM Assembler and Hi-Tech C. Each lesson is in PDF form along with downloadable source code. The midrange C tutorials start off with using the 12F629, but soon move on to the 16F series.

---------- Post added at 23:43 ---------- Previous post was at 23:15 ----------

this time the problem is while using timer 0..... The book which I am following teaches to use pic18 but if I try programming pic16 I face problems. This time I used the following code:

#include<pic.h>
#include<htc.h>

__CONFIG(0x3F32);

#define _XTAL_FREQ 4000000

void main(void)
{


TRISAbits.TRISA4 = 1;
TRISB = 0;

T0CON = 0x78;

while(1)
{
do{
T0CONbits.TMR0ON = 1;
PORTB = TMR0L;
}while(INTCONbits.TMR0IF==0);

T0CONbits.TMR0ON = 0;
INTCONbits.TMR0IF = 0;

}
}

and the compiler gives error "c; 15.1 undefined identifier "T0CON"" and same with T0CONbits, INTCON.... I wonder if these registers are called by some other names in Hi Tech compiler..................

One of the most important differences between the PIC16 and PIC18 series, in regards to your code, is the fact that the PIC18 series has a 16-bit TMR0, while the PIC16 series has an 8-bit TMR0, also the TMR0 in the PIC16 is not controlled by its own "Control Register" as it is in the PIC18. Controlling the TMR0 in the PIC18 is accomplished by manipulating the bits of both the INTCON and OPTION_REG registers.

When first learning to program a new device, it is essential you first obtain/download the devices datasheet:

Microchip's PIC16F877A Info Page

PIC16F87XA Datasheet PDF

There are almost always major differences between different devices of the same series, so studying the datasheet of your actual device is vital.

Hope the suggestions help in your endeavors.
 
Last edited:
  • Like
Reactions: annna

    annna

    Points: 2
    Helpful Answer Positive Rating
Thank you very much. You helped me very much. I am really thankfull
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top