Starting Programming PIC16f877 with C

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
HI guys,

I want to clear and want to know how to write simple LED blink Code for PIC16F877A ..
so, searching and asking give me idea i ahve installed Hi tech C compiler MAP lab.

so,

i have learn this but how to write further code for blinking LEd i want your help.




Rich (BB code):
 This is for 4MHz xtal
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_XT);

*/
Low Voltage Programming Disabled (LVP_OFF)
Brown Out Reset Disabled (BOREN_OFF)
Power Up Timer Enabled (PWRTE_ON)
Watchdog Timer Disabled (WDT_OFF)
Standard Crystal Oscillator (FOSC_XT)
/*

#define _XTAL_FREQ 4000000

//start code here






#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_HS);

*/
Low Voltage Programming Disabled (LVP_OFF)
Brown Out Reset Disabled (BOREN_OFF)
Power Up Timer Enabled (PWRTE_ON)
Watchdog Timer Disabled (WDT_OFF)
High Speed Crystal Oscillator (FOSC_HS)
/*

#define _XTAL_FREQ 6000000

//start code here
 

absf

Joined Dec 29, 2010
1,942
This program has 2 LED and 2 switches. The comments would self-explain itself. It was taken from my PIC development board and I removed the LCD part. Try it and it may required minor modifications if it doesnt work straight away....

Rich (BB code):
#include <htc.h>

__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDT_OFF & FOSC_HS);

*/
Low Voltage Programming Disabled (LVP_OFF)
Brown Out Reset Disabled (BOREN_OFF)
Power Up Timer Enabled (PWRTE_ON)
Watchdog Timer Disabled (WDT_OFF)
High Speed Crystal Oscillator (FOSC_HS)
/*

#define _XTAL_FREQ 4000000

//start code here

//configuration
//==========================================================================
//__CONFIG ( 0x3F32 );		//configuration for the  microcontroller

//	define
//==========================================================================

#define	SW1		RB0			
#define	SW2		RB1			

#define	LED1		RB6			
#define	LED2		RB7				

//	function prototype		(every function must have a function prototype)
//==========================================================================
void delay(unsigned long data);			

//	main function					(main fucntion of the program)
//==========================================================================
void main()
{
	unsigned long delay_time=5000;

	//set I/O input output
	TRISB = 0b00000011;				//configure PORTB I/O direction
	
	LED1=0;								//OFF LED1
	LED2=0;								//OFF LED2

	while(1)							//Infinite loop
	{
		if(!SW1)						//check if SW1 is pressed
		{
			while (!SW1);				//wait SW1 pressed
			{
				delay_time+=1000;		//delay
			}
		}
		else if(!SW2)					//check if SW2 is pressed
		{
			while (!SW2);				//wait SW2 pressed
			{
				delay_time-=1000;		//delay
			}
		}
		
		LED1^ = 1;					//toggle LED1
		delay(delay_time);				
		LED2^ = 1;					//toggle LED2			
		delay(delay_time);			
	}
}

//	functions
//==========================================================================
void delay(unsigned long data)			//delay function, the delay time
{										//depend on the given value
	for( ;data>0;data--);
}
Sw1 would make the LEDs blink slower and Sw2 make them blink faster. The program also works without the switches installed.

Allen
 
Last edited:

t06afre

Joined May 11, 2009
5,936
Have you programmed your chip with assembler so you know the basics about configuration bits? And also please state what kind of crystal you are using. It has been a lot of changes in Hi-Tech C. So you must also tell us which version you are using of the C-compiler
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
Hi,

I am using Hi tech ANSI C compiler with MAP lab 8.33V IDE...
and if i want to check like simulate code what to do??
 

t06afre

Joined May 11, 2009
5,936
The program I posted was compiled on HiTech C V9.83 (Lite) free version.
MPLab IDE v8.80. http://
Kind of awckward program. You do know that Hi-Tech C have native delay functions?
Here is the link i am using window one..!!
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en542849

and please start telling me how to write code!
Have you looked in the C:\Program Files\HI-TECH Software\PICC\9.83\docs folder. Recommended for reading is the quickstart.pdf and of course the manual.pdf. Every time you do some work in MPLAB. ALWAYS build a project. That will make things more easy for you. Also in post #3 I asked some questions that you have not answered. Please do so
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
Have you programmed your chip with assembler so you know the basics about configuration bits? And also please state what kind of crystal you are using. It has been a lot of changes in Hi-Tech C. So you must also tell us which version you are using of the C-compiler
Yes, in PIc16F676 with ASM code PWM the code was not of mine but i have litle knowledge of bits and programming in Asm now i want to start in C ..

here is linkl of my video..

http://www.youtube.com/watch?v=W0ZoAzAbi_o
http://www.youtube.com/watch?v=uQ7nw9mTevI
 

absf

Joined Dec 29, 2010
1,942
Yes, I know <htc.h> does have a built-in __delay_ms() function.

But I guess the programmer who wrote the program wants to demonstrate how to use SW1 and SW2 to increase and decrease the blinking rate of the 2 LED, so that's why he didnt use the built-in function.

And it was originally using pic.h and I changed it to htc.h because the OP was using it.

Allen
 

t06afre

Joined May 11, 2009
5,936
A good help is also to open your PIC include file in folder "...\HI-TECH Software\PICC\9.83\include" Here you will find the naming for the registers both at byte and bit level. At the top of the file. You also have the config-bits names. It looks like you have most of them correct. But looking at the header file is WDTE_OFF and not WDT_OFF
Rich (BB code):
#include <htc.h>
Remember to always put this line in any program you write.
I can not learn you how to write C. That job you have to do by coding and learning on the way. But I can recommend some good books.
This is a program that should blink the bits on PORTB
Rich (BB code):
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
/*
Low Voltage Programming Disabled (LVP_OFF)
Brown Out Reset Disabled (BOREN_OFF)
Power Up Timer Enabled (PWRTE_ON)
Watchdog Timer Disabled (WDT_OFF)
High Speed Crystal Oscillator (FOSC_HS)
*/

#define _XTAL_FREQ 6000000
//start code here
void main (void)
{
TRISB=0b00000000;//all pins set as ouput
PORTB=0;
while (1)
 {//endless loop
  PORTB = !PORTB;
  __delay_ms(1000);
 }
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
Please explain this as u have written portb=0 outside the loop, why??
and what does this mean PORTB=!PORTB; as i have done prog in spin it mean logical not gate...
please explain bit more....


thanks

PORTB=0;
WHILE(1); /* FOR LOOP ENDLESS*/
{
PORTB=!PORTB;
__delay_ms(1000);
}
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
I got it!!
OK, i have found 4Mhz crystal so, now i will change ...
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_XT);
 

t06afre

Joined May 11, 2009
5,936
Please explain this as u have written portb=0 outside the loop, why??
and what does this mean PORTB=!PORTB; as i have done prog in spin it mean logical not gate...
please explain bit more....


thanks

PORTB=0;
WHILE(1); /* FOR LOOP ENDLESS*/
{
PORTB=!PORTB;
__delay_ms(1000);
}
I can not learn you C as I said. But maybe this link can be of some help http://www.google.com/search?q=free+books+c
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,823
HI,

while building project it show error...why?
step i have done...
1. create new project PIc16F877A next
2.save a file with testpic next
4.next ( I have not add any file)next
5.finish
6.open new file paste my code
7.save code by testpic.c
8. then right click i add to project
9.after that i build it show this error, why??
Build C:\Users\John\Desktop\maplab\pictest for device 16F877A
Using driver C:\Program Files (x86)\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe

Make: The target "C:\Users\John\Desktop\maplab\testpic.p1" is out of date.
Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" --pass1 C:\Users\John\Desktop\maplab\testpic.c -q --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
Executing: "C:\Program Files (x86)\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe" -opictest.cof -mpictest.map --summary=default --output=default testpic.p1 --chip=16F877A -P --runtime=default --opt=default -D__DEBUG=1 -g --asmlist "--errformat=Error [%n] %f; %l.%c %s" "--msgformat=Advisory[%n] %s" "--warnformat=Warning [%n] %f; %l.%c %s"
HI-TECH C PRO for the PIC10/12/16 MCU family (Lite) V9.65
Copyright (C) 1984-2009 HI-TECH SOFTWARE
(1273) Omniscient Code Generation not available in Lite mode (warning)
Error [1274] C:\Users\John\Desktop\maplab\testpic.c; 20. delay exceeds maximum limit of 197120 cycles
 

t06afre

Joined May 11, 2009
5,936
Using driver C:\Program Files (x86)\HI-TECH Software\PICC\PRO\9.65\bin\picc.exe


You have sevral versions of Hi-Tech C installed. Remove all but version 9.83
 
Top