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.

EUART pic16f887 and MikroC not working

Status
Not open for further replies.

kidteam

Junior Member level 3
Joined
Dec 22, 2010
Messages
27
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,420
hi all i'm a newbie
when i use euart's pic16f887 with hi-tech C, it work fine
but it not work when i use mikro C
please help me
code HTC work fine
Code:
#include <htc.h>
//#include <pic.h>
__CONFIG(FOSC_XT&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_OFF&CPD_OFF&BOREN_OFF&IESO_OFF&FCMEN_OFF&LVP_OFF&DEBUG_OFF);
__CONFIG(BOR4V_BOR40V&WRT_OFF);
//dau tien ta thu cho xuat ra 8 led tai cong b
#ifndef _XTAL_FREQ
	#define _XTAL_FREQ 4000000
#endif
unsigned char c;
void main() {
	ANSEL=ANSELH=0;
	TRISB=0;//cong B lam cong ra
	TRISC7=1;//RxD //chan vao
	TRISC6=0;//TxD //chan ra
	//khoi tao toc do baurd
	SYNC=0;
	BRGH=1;
	BRG16=0;
	SPBRG=25;
	//toc do baurd 9600
	SPEN=1;
	RX9=0;
	CREN=1;//cho phep nhan
	RCIF=0;
	TXIF=0;
	TXEN=1;//cho phep truyen
	PORTB=0x00;
	while(1) {
		//chu y o day ta dung hoi vong thoi
		//chua dung den ngat
		while(!RCIF){ //cho cho den khi nhan duoc thi thoi
			
		}//cho
		RCIF=0;
		c=RCREG;
		if(c=='A') {
			PORTB=0xff;
			//__delay_ms(10);
			TXREG='C';
			while(!TXIF);//cho gui xong
			TXIF=0;
		} else {
			if(c=='B') {
				PORTB=0x00;
				//__delay_ms(10);
				TXREG='D';
				while(!TXIF);
				TXIF=0;
			}
		}		
	}
}
and code MikroC not work
Code:
void main(){

  ANSEL  = 0;                             // Configure AN pins as digital I/O
  ANSELH = 0;

  TRISB = 0x00;                           // Set PORTB as output (error signalization)
  PORTB = 0;                              // No error
  UART1_Init(9600);
  Delay_Ms(100);
  while(1) {
      UART1_Write('A');
      Delay_Ms(100);
  }
}
 

So why use the MikroC compiler instead of the Hi-Tech Compiler?

One issue maybe the Configuration Register settings.

Have you ensure the proper Configuration Register settings, in the project edit dialog?



Also be aware MikroC often redefines commonly used #define macros, such as RB0, for other purposes.


BigDog
 

i config as
**broken link removed**
 

Your image URL doesn't appear to be functioning.

Try using the forums Insert Image feature.

BigDog
 

i config as
Fig2.jpg
 

Your Configuration Register settings appear to be set correctly.

According to the MikroC Pro C manual you need a delay of 100ms after initializing the UART.

Try the following example code:

Code:
char uart_rd;

void main() {
  ANSEL  = 0;                     // Configure AN pins as digital
  ANSELH = 0;
  
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);
  
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);       // and send data via UART
    }
  }
}


BigDog
 

Your Configuration Register settings appear to be set correctly.

According to the MikroC Pro C manual you need a delay of 100ms after initializing the UART.

Try the following example code:

Code:
char uart_rd;

void main() {
  ANSEL  = 0;                     // Configure AN pins as digital
  ANSELH = 0;
  
  UART1_Init(9600);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  UART1_Write_Text("Start");
  UART1_Write(10);
  UART1_Write(13);
  
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     // read the received data,
      UART1_Write(uart_rd);       // and send data via UART
    }
  }
}


BigDog
i already tested it
it is in help file of MikroC
it not work
 

Frankly, I avoid using MikroC like the plague, even though I own a license for the software.

I would suggest recompiling it again, ensuring the Configuration Registers and Oscillator frequency are set as you've shown in post #5.

The code provided in the example was specifically for the PIC16F887 and even though I use my own routines, I don't recall any issues with the UART routines provided by MikroC.

Let me know if a recompile doesn't work.


BigDog

- - - Updated - - -

You might try changing the oscillator setting to HS, instead of XT.

A 4MHz crystal is right on the border of these two settings, I usually choose HS.

BigDog

- - - Updated - - -

To ensure the code was viable, I just setup a circuit with a PIC16F887 with a 4MHz crystal and compiled, loaded and tested the example code.

It was successful and performed as expected.

After programming the device, are you properly resetting the PIC before testing the example code?


BigDog
 
doesn't work :(
i don't change config bit
i translate code from HTC to MikroC
Code:
unsigned char cc;
void main(){

        ANSEL=ANSELH=0;
	TRISB=0;//cong B lam cong ra
	TRISC7_bit=1;//RxD //chan vao
	TRISC6_bit=0;//TxD //chan ra
	//khoi tao toc do baurd
	TXSTA.SYNC=0;
	TXSTA.BRGH=1;
	BAUDCTL.BRG16=0;
	SPBRG=25;
	//toc do baurd 9600
	RCSTA.SPEN=1;
	RCSTA.RX9=0;
	RCSTA.CREN=1;//cho phep nhan
	PIR1.RCIF=0;
	PIR1.TXIF=0;
	TXSTA.TXEN=1;//cho phep truyen
	PORTB=0x00;
	while(1) {
		//chu y o day ta dung hoi vong thoi
		//chua dung den ngat
		while(!PIR1.RCIF){ //cho cho den khi nhan duoc thi thoi

		}//cho
		PIR1.RCIF=0;
		cc=RCREG;
		if(cc=='A') {
			PORTB=0xff;
			//__delay_ms(10);
			TXREG='B';
			while(!PIR1.TXIF);//cho gui xong
			PIR1.TXIF=0;
		} else {
			if(cc=='B') {
				PORTB=0x00;
				//__delay_ms(10);
				TXREG='C';
				while(!TXIF);
				PIR1.TXIF=0;
			}
		}
	}
}
and it does work fine, please tell me why????
 

i don't change config bit

What do you mean by this statement?


As I've indicated in Post #8, I have fulled tested the MikroC UART example code using a PIC16F887 and 4MHz crystal.

The example code performed as expected. Absolutely no issues.

What version of the MikroC compiler are you currently using?


BigDog
 
i'm using version 5.01
I don't understand
now it work???
 
Last edited:

Maybe you were mistakenly loading an older version of the HEX file.

You are using a fairly recent version.

Anyway, glad to hear you manage to get the code to function properly.


BigDog
 
Hi

I made a project in real and when I tried to send 8 byte in sequence, I got only 4 byte. the first three and the last. Can you help please?
 

Post your code using CODE tags and I'll take a look at it.

If you have a schematic of your design, post it as well.

BigDog
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top