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.

pic16f877 hardware problem

Status
Not open for further replies.

sanaprog

Newbie level 6
Joined
Aug 5, 2011
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,395
hi everybody...i am beginner of pic...i have made my own pic trainer cct...with 20MHz crytal.(also check with 8MHz)...but the problem is when i burn some example of mikroC n run the cct..it works good..bt when i burn my own simplest program of just sending any value to port d it doesn't work...:-(..... even the crystal shows no oscillations...

EXAMPLE code:
void main()
{
trisd=0;
portd=0;

while(1)
{
portd=~portd;
delay_ms(1000);
}
}
//////////////////////////////////////////
my code:
void main()
{
trisd=0;
portd=0x3f;
}

so simple but..not working:-( .........i am using mikro C8.....
 

ARE you sure that it will work with small letters for TRISD PORTD etc?

---------- Post added at 20:26 ---------- Previous post was at 20:22 ----------

But any way, in Hi Tech C, it will not accept 'trisd' 'portd' etc , instead I must use 'TRISD', 'PORTD' etc..
 

but since example program is working,
so I think this may be the problem.

void main()
{
trisd=0;
portd=0x3f;
}

This code will not blink the led...
 

so simple but..not working

Your simple program has few issues which can produce unexpected results:

Original Code:

Code:
void main()
{
   trisd=0;
   portd=0x3f;
}


Modified Code:

Code:
void main()
{
   trisd=0;

   portd=0x3f;

   [COLOR="#FF0000"]while(1);[/COLOR]
   
}

The main omitted item is the while loop which prevents the execution flow from exiting the main program. This is necessary due to the fact there is no OS to pass execution.

BigDog
 

All MikroE compilers support to end the program without unlimited while cycle. Even Keil normally works after main program executed. So, it is not a problem.
 

Do you have any reference on that?
I have never heard of an embedded system being allowed to exit the main loop unless you are suggesting that the compiler of the IDE you mentioned adds this automatically.
If you exit the main loop it will generate unexpected results unlike an actual operating system like windows where there are proper handlers for that.

Alex
 

I don't think omitting the while(1); statement is a good practice. I remember I was told (but I can not tell by which/who) the program will run back to the beginning of the program. I'm not clear to which chip/compiler this applies.

That for most of the cases is not the desired behavior. However, we could be even more unfortunate with some complier/chip when the program runs on to the unprogrammed space or anywhere else (who knows?) which I believe is absolutely not a desired behavior.
 
Last edited:

Good practice or not - doesn't matter. In this case it is not a problem. This code must work. I'm using this compiler.
 

main.c
*****************
Code:
#include <stdio.h>
#include <htc.h>
#include "usart.h"

/* A simple demonstration of serial communications which
 * incorporates the on-board hardware USART of the Microchip
 * PIC16Fxxx series of devices. */

 void interrupt isr(void );
void main(void){
	unsigned char input;
	int i;

	INTCON=0x00;	// purpose of disabling the interrupts.

	init_comms();	// set up the USART - settings defined in usart.h

	// Output a message to prompt the user for a keypress	
		
        sendstring("welcome to MegaByte Technologies\n");
	while(1){
         sendstring("welcome to MegaByte Technologies\n");
	   //      if(PIR1 & 0x20)
 		//	interrupt isr();
     //   sendstring("welcome to MegaByte Technologies\n");
   //     for(i=0;i<200;i++);
		//input = getch();	// read a response from the user
	//	printf("\rI detected [%c]",input);	// echo it back
	//	putch(input);
	}
}

 void interrupt isr(void )
{
unsigned char s;
s=getch();
putch(s);

}
******************

uart.c
*********************
#include <htc.h>
#include <stdio.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

unsigned char 
getch() {
	/* retrieve one byte */
		while(!RCIF)	/* set when register is not empty */
		continue;
	return RCREG;	
}

unsigned char
getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}
void sendstring(unsigned char *s)
{
while(*s)
putch(*s++);
}
****************************
uart.h
***************************
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600      
#define FOSC 4000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#if defined(_16F87) || defined(_16F88)
	#define RX_PIN TRISB2
	#define TX_PIN TRISB5
#else
	#define RX_PIN TRISC7
	#define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
	RX_PIN = 1;	\
	TX_PIN = 1;		  \
	SPBRG = DIVIDER;     	\
	RCSTA = (NINE_BITS|0x90);	\
	TXSTA = (SPEED|NINE_BITS|0x20); \
    INTCON=0xc8; \
    PIE1=0x20  \
	

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);
void sendstring(unsigned char *s);
#endif
*****************************


try with this code and see will it works or not this for simple serial data trasforr and read
 
Last edited by a moderator:

In mikroC, by default, the registers are not case sensitive. So, it does not matter if you write portd or PORTD. Same thing.
The main problem, as stated before, is that the while loop has been omitted. This means that after the execution of all the statements, the program goes back to the beginning, goes through all initialization and stuff and this causes undesired results. Always keep the program in the main loop.
Also, check the configuration settings. You may have improper configuration settings, like watchdog timer enabled, or wrong oscillator setting. Check and fix configuration settings by going to Project > Edit Project.

Hope this helps.
Tahmid.
 
Last edited:

thnx everybody...........mikroC compiler is case insensitive...so portd or PORTD is not the issue...(in proteus this code works)....WDT yes can be the problem..il check it out...
 

Code:
Code:
void main() 
{
 ansela=0;
 TRISA=0;
 portb=0;
 PORTA=0;
}

Makes asm listing:
Code:
_main:

;Oven1.c,5 :: 		void main()
;Oven1.c,7 :: 		ansela=0;
	CLRF       ANSELA+0
;Oven1.c,8 :: 		TRISA=0;
	CLRF       TRISA+0
;Oven1.c,9 :: 		portb=0;
	CLRF       PORTB+0
;Oven1.c,10 :: 		PORTA=0;
	CLRF       PORTA+0
;Oven1.c,11 :: 		}
L_end_main:
	GOTO       $+0
; end of _main
 

Easyrider, that's a good proof of your claim. Glad to know that.

Thanks.
 

thnx everybody.....yes it worked,,,,problem was, setting configuration bits WDT,LVP etc....while(1) is also not necessary for keeping the control in main all the time...:)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top