USART issue

Thread Starter

riad_00

Joined Feb 9, 2008
19
Hi,

I am trying to send data over RS232. The analouge signal is coming from a temperature sensor (LM-35) and is being dizitised by PICC18F4520. Then it should send the data via RS232 port to Hyper Terminal. But I can't see any thing. Is any thing wrong with my codes.

Thanks in advance

P:S: I am using a seven segment as well, for the privious effort - so the codes remained.

#include <p18f4520.h>
#include <delays.h>

#include <usart.h>

#pragma config OSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config DEBUG = ON


const char LED[10] = {

0b01111111, /*0*/

0b00001101, /*1*/

0b10110111, /*2*/

0b10011111, /*3*/

0b11001101, /*4*/

0b11011011, /*5*/

0b11111011, /*6*/

0b00001111, /*7*/

0b11111111, /*8*/

0b11001111, /*9*/

};






void main (void)
{
unsigned short result = 0;
unsigned char temp;
unsigned char units;
unsigned char tens;
unsigned char nunits;
unsigned char ntens;
TRISC = 0b11000000;

TRISD = 0b00000000;
ADCON0 = 0b00000000;
ADCON1 = 0b00001110;
ADCON2 = 0b10001100;
ADCON0bits.ADON = 1;
OpenUSART(USART_TX_INT_OFF & USART_BRGH_HIGH & USART_ASYNCH_MODE & USART_EIGHT_BIT,25);
TXSTA = 0b00100100; //8bit-trsnmit disable-high baud rt-asynchronous

RCSTAbits.CREN = 0; //Disable Reciever.
BAUDCON = 0b00000000; //8bit




while (1)
{
RCSTAbits.SPEN = 1; //Serial Port Enable - TX/DT
ADCON0bits.GO = 1;
while(ADCON0bits.GO);


result = ADRESH;
result = result << 8;
result = result + ADRESL;


temp = (result*0.4882815 + 2);
units = temp%10;
tens = (temp/10)%10;
PORTC = ~LED[units];
PORTD = ~LED[tens];

TXREG = temp;
PORTCbits.RC6 = TXREG;








}

}
 

beenthere

Joined Apr 20, 2004
15,819
Think about the absolute output of the LM35 in the range of temperature being measured and the sensitivity of your A to D conversion. Is the output enough to change even one bit? You probably need an amplifier to give the A to D enough voltage to distinguish a change in temperature.
 

Thread Starter

riad_00

Joined Feb 9, 2008
19
Think about the absolute output of the LM35 in the range of temperature being measured and the sensitivity of your A to D conversion. Is the output enough to change even one bit? You probably need an amplifier to give the A to D enough voltage to distinguish a change in temperature.
Thanks for the reply.

It works fine with 7-segment. I still can manage to put an op-amp if required. But I am getting digits and I want to send them to RS232.

P:S: I am an entry level PIC programmer.:)
 

AlexR

Joined Jan 16, 2008
732
If you are trying to send RS232 data to a PC running Hyperterminal you must first convert your data to an ASCII string, then use the USART library routines (assuming they are present in your compiler) or write your own routine to send out the string of ASCII characters over the usart port.
You will also have to use a MAX232 or similar chip to convert the logic level signal from the PIC to RS232 signal levels.
 

Thread Starter

riad_00

Joined Feb 9, 2008
19
If you are trying to send RS232 data to a PC running Hyperterminal you must first convert your data to an ASCII string, then use the USART library routines (assuming they are present in your compiler) or write your own routine to send out the string of ASCII characters over the usart port.
You will also have to use a MAX232 or similar chip to convert the logic level signal from the PIC to RS232 signal levels.
Thanx,

I changed the codes a bit. I am using 'usart.h' and because 'temp' is char type I guess putcUSART() is good enough. I have got MAX202ecpe chip to make RS232 level. I have checked TX pin (25) with a scope, there is serial data there and output of the MAX chip, there is also serial output there. Doesn't make sence - why I don't see anything in hyperterminal! grrrr:mad:

Plz help me - I am pulling my hair off.

Rich (BB code):
void main (void)
{
	unsigned short result = 0;
	unsigned char temp;
	unsigned char units;
	unsigned char tens;
	unsigned char nunits;
	unsigned char ntens;
	TRISC = 0b11000000;
               TRISD = 0b00000000;
	ADCON0 = 0b00000000;
	ADCON1 = 0b00001110;
	ADCON2 = 0b10001100;
	ADCON0bits.ADON = 1; 
	OpenUSART(USART_TX_INT_OFF & USART_BRGH_HIGH & USART_ASYNCH_MODE & USART_EIGHT_BIT & 0b11011111,25);
	//TXSTA = 0b00100100; //8bit-trsnmit disable-high baud rt-asynchronous
	
	//RCSTAbits.CREN = 0; //Disable Reciever.
	//BAUDCON = 0b00000000; //8bit 
	

	

	while (1)
	{
		//RCSTAbits.SPEN = 1; //Serial Port Enable - TX/DT
		ADCON0bits.GO = 1;
		while(ADCON0bits.GO);
	               result = ADRESH;
		result = result <<  8;
		result = result + ADRESL;
                               temp = (result*0.4882815 + 2);
		units = temp%10;
		tens = (temp/10)%10;
		PORTC = ~LED[units];
		PORTD = ~LED[tens];
		
		//TXREG = temp;
		//PORTCbits.RC6 = TXREG;
		putcUSART(temp);	
	}

}
 

Markd77

Joined Sep 7, 2009
2,806
Have you got pin 5 on the serial cable connected to your circuit ground and the output of the max232 to pin 2?
If there are any hardware handshaking settings on hyperterminal they need turning off.
I use realterm software - it's free - it has indicators that show the status of the serial pins so you can tell if anything at all is getting in, even if it isn't valid data.
 

nhenri

Joined Oct 18, 2010
3
simple code for pic18f26j50

void Init_Console(void)
{
TRISCbits.TRISC7=1; //input rx

TRISCbits.TRISC6=0; //output TX

TXSTA1bits.SYNC=0; //asynchrone
TXSTA1bits.TXEN=1; //tx enable
TXSTA1bits.BRGH=1; //high speed

RCSTA1bits.SPEN=1; //uart on
RCSTA1bits.CREN=0; //Receive on
RCSTA1bits.CREN=1; //Receive on !!

SPBRG1=103; //115200 @ 48mhz
SPBRGH1=0;

BAUDCON1bits.BRG16=1; //16bits
BAUDCON1bits.ABDEN=0; //dont auto-detect baud rate

_delay_ms(10);

}
void Console_SendROM(const rom char *str)
{
while(*str)
{
while(!PIR1bits.TX1IF );
TXREG1=*str++;
}
 

eblc1388

Joined Nov 28, 2008
1,542
Have you got pin 5 on the serial cable connected to your circuit ground
Reminded me of an incident years ago, where I forgot to connect the ground connection to the RS232 DB9 socket on the project board. I only connected the Rx and Tx.

With my ICSP programmer connected, the serial port on the project works faultlessly in communication with the PC, where I done all my testing and troubleshooting.

After everything checked out, I powered down and pack up the project into its own case.

Then the communication does not work anymore.

I then dumped the flash content of the MCU and it match with the source.

Now the communication suddenly works again(because I have the ICSP connected).

Ultimately after repeating the same three times and a lot of swearing, the culprit was found. :):):)
 

Thread Starter

riad_00

Joined Feb 9, 2008
19
::D

I had a usb to serial converter and for the extension from the board - I was using a null modem (stupid me) all day without realising. And I had to change the codes a bit at the end
Rich (BB code):
	printf("%u \r\n" , temp);
using 'stdlib.h' instead 'putcUSART()'. Works like a dream.

Next one would be drawing a nice analouge meter in .NET and connect it through serial.

Tanks a lot.
 
Top