Program compiled in Hi-Tech C for PIC16F877A, no result obtained

Status
Not open for further replies.

archanak2511

New Member
Hi,
I've tried this simple embedded C Program using Hi-Tech C for PIC16F877A.
The aim was to obtain a logic high as output on 3 pins (pins D7, D2, D3 of port D), if one input pin (pin B6, of Port B) is driven to logic 1. The program was built successfully but after burning it to the microcontroller I got no result.

Program
Code:
#include<htc.h>
#define _XTAL_FREQ 20000000      //Clock frequecy=20Mhz
main()
{
 TRISB=0b11111111;                     //Set Port B as input port
 TRISD=0b00000000;                     //Set Port D as output port
 while(1)                                          //forever loop
 	{if(RB6==1)                             //if pin B6 is high
  		{RD7=1;                          //set pin D7 as high
	 	 RD3=1;                          //set pin D3 as high
	 	 RD2=1;                         //set pin D2 as high
		}
 	 else                                     //  if pin B6 is low
   		{RD7=0;                       // set pin D7 as low
		 RD3=0;                        // set pin D3 as low
		 RD2=0;                      //set pin D2 as low
   		}
	    
	}
}
Please tell me if there is any mistake in the code or the logic.

Thanks
 
Last edited by a moderator:
Just stick the following line at the top of the code.
Code:
__CONFIG(0x37F2);

The thing will be set for XT crystal and won't start...
 
Ok, I had not read about such fuse bits. Will try to find out more about what these are how to set these bits. Thank you for the information
 
Just stick the following line at the top of the code.
Code:
__CONFIG(0x37F2);

The thing will be set for XT crystal and won't start...


I'll copied that and pasted it at the top of the code. But now when I compile it, its showing an error.

Error [194] C:\Users\TOSHIBA\Documents\project\test.c; 1.10 ")" expected

So I tried typing it with an extra bracket ")"
__CONFIG(0x37F2));
Now it was compiled successfully. I can't test if it works right now because I don't have the hardware or software required for burning the program into the microcontroller. But anyhow, isn't that a bit weird, one open bracket and 2 close brackets? :/
 
Last edited:
archana, its the same, the config bit of pic ( i'm more of an avr guy.. in avr config bits are called as fuse bits. ) refer page 146 of the PIC 16f877a datasheet for more info.
 
The config bits ARE the fuses... (Named a long while ago when they were fuses)

The code I pasted was ok... Show me where you put it!! It has to be at the top BELOW #include<htc.h>...
 
yeah both are the same but in pic datasheet there is no such thing as a fuse word. so to avoid confusion, I said so.. thanks Ian for the new info about fuse bits.
 
The config bits ARE the fuses... (Named a long while ago when they were fuses)

The code I pasted was ok... Show me where you put it!! It has to be at the top BELOW #include<htc.h>...

I was silly enough to paste it at the very top. Now that I've pasted it below the include statement it works fine. Thank you.
 
I was silly enough to paste it at the very top. Now that I've pasted it below the include statement it works fine. Thank you.

Could you repost the code with the correction. I'm a noob trying to learn the flow. It would really help me.

Regards,
kv
 
Hi KV


Code:
#include<htc.h>
#define _XTAL_FREQ 20000000      //Clock frequecy=20Mhz
__CONFIG(0x37F2);                 // Goes here...ish 

main()
{
 TRISB=0b11111111;                     //Set Port B as input port
 TRISD=0b00000000;                     //Set Port D as output port
 while(1)                                          //forever loop
 	{if(RB6==1)                             //if pin B6 is high
  		{RD7=1;                          //set pin D7 as high
	 	 RD3=1;                          //set pin D3 as high
	 	 RD2=1;                         //set pin D2 as high
		}
 	 else                                     //  if pin B6 is low
   		{RD7=0;                       // set pin D7 as low
		 RD3=0;                        // set pin D3 as low
		 RD2=0;                      //set pin D2 as low
   		}
 
	}
}

The _CONFIG(); is in the headers... Once you use <htc.h> it is automatically defined so you can use it..
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…