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.

serial communicatin with pc in C18 compiler

Status
Not open for further replies.

Munib

Advanced Member level 4
Joined
Jun 11, 2004
Messages
114
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Activity points
1,129
how to set pll in c18 compiler

im interfacing my PIC 18f452 with my computer with windows XP as OS.

the problem is that i can not send my data correctly to the computer via COM1,

the data which uis received by the hyperterminal is not correct, i wrote a program IN MY COMPUTER WHICH GENRATED AN INTEGER , send it to my mcu via the serial port , the mcu got the data and then send the same data back t the PC,


now when my pc send "1" to mcu, it received 1 but it returned 129 to pc,
when send 2, the mcu returned 130,
and i concluded that the mcu returned the value with an addition of 128, but i was wrong this factor of 128 was not cnstant and it kept on changing

so far i was using C 18 compiler, when i used my mcu with pic basic pro, this problem didnt occur ,
is there ne thing wrong with C 18 or if iam doing ne thing wrong
 

c18 serial

Can you post MCC 18 code, when receive data from PC and when send it back to PC. How is defined your data: unsigned char or char ?
 

rcsta port c18

cause of error may ..
1 error in broadrate timming
2 differance between sign data and unsign data
3 number of stop bit
4 number of bit per transmit
 

delay10ktcyx(8) - compiler (c18)

Code:
#include <p18f452.h>  /* for the special function register declarations */
#include <delays.h>
#pragma config OSC = HS

#pragma config WDT = OFF
#pragma config LVP = OFF

//char data_string[100];
 unsigned char i = 0;
unsigned char ch=0;
//int r = 0;

//~~~~~ START OF MAIN~~~~~~

void main()
{
TRISB=0;
PORTB=0;

//for ( r = 0 ; r < 100 ; r++)
//	data_string[r] = r;


TMR0H=0x00;
TMR0L=0x00;
SPBRG=0x0B;
RCSTA=0b10011001;
TXSTA=0b00100010;


//for (i=0 ; i<123 ; i++)
while(1)
{	
	while(PIR1bits.RCIF==0);
	
	//if(PIR1bits.RCIF==1)
	ch = RCREG;
	PORTB = ch;
    Delay10KTCYx(50);
	if (PIR1bits.TXIF)
	{	
/*			if(i%5 == 1 )
			{
				TXREG = '\n';
				while (TXSTAbits.TRMT==0);
			}//end of small if*/
			TXREG = (ch&0xFF);
			while (TXSTAbits.TRMT==0);

//			PORTB=data_string[i];
	}		

		
	// end of big if


}//end of for

/*	TXREG = '\0';
	while (TXSTAbits.TRMT==0);
*/
	//PORTB = 0xAA;
} // end of main



here is the cde which i have used for communication of pic with PC



here the baudrate is set by
SPBERG=0x0B

which is for 7.37MHz crystal, i have with me 8Mhz crystal, will it affect me, it didnot when i compiled this code in MIKRObasic, but answer goes awry if i use C18 compiler

, Also can any one give me some sample code which worked correctly for data transfer from MCU to PC using the C18 compilerr
 

7 bit serial c18

Munib said:
SPBERG=0x0B

Use SPBRG = 0xC for 9600 baud @ 8MHz, BRGH=0
This will probably fix the problem.

Munib said:
TXSTA=0b00100010;


This is wrong. Bit-1 is read only. Why are you writing a '1' to it?

RCSTA=0b10011001;

Now this is just silly. Did you read the data sheet?

Try
RCSTA = 0b10010000

(and do read the data sheet.) ;-)

Regards,

Jim Robertson
NEWFOUND ELECTRONICS
 

calculating baud rate in mcc18 compiler

ok
i will try out these new settings
but i would like to know
that how u concluded taht instead of 0x0B should be replaced 0x0C

ne way me out to check whether these new settings work or not,

Added after 1 hours 39 minutes:

and also if i want to know baud rates at other crystal configuartions

for example : for 7.73Mhz baudrate is 0x0B and u told that use 0x0C for 8 Mhz,
now if i also use PLL mode too, then what would be the baudrate then :S
 

pir1bits.rcif mikrobasic

Munib said:
ok
i will try out these new settings
but i would like to know
that how u concluded taht instead of 0x0B should be replaced 0x0C

ne way me out to check whether these new settings work or not,

Added after 1 hours 39 minutes:

and also if i want to know baud rates at other crystal configuartions

for example : for 7.73Mhz baudrate is 0x0B and u told that use 0x0C for 8 Mhz,
now if i also use PLL mode too, then what would be the baudrate then :S

Read the data sheet! ;-)

BRGH = 0

Baud Rate = FOSC/(64(x+1)

= 8000000/(64(13))

= 8000000/(832) = 9615 baud

With PLL

Baud Rate = FOSC/(64(x+1)

= 32000000/(64(13))

= 32000000/(832) = 38462 baud

Have a look at this file. **broken link removed**

It may also help.


Regards,

Jim Robertson
NEWFOUND ELECTRONICS
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top