Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 4th April 2008, 01:04 PM   (permalink)
Default usart with pic

dear all
m designing a project using pic18f4550 with usart . m not able to get proper data on hyperterminal. m using 4 mhz crystal.bellow is the code .please help me find mistake.


#include <p18f4550.h>
#include <stdio.h>
#include <usart.h>
/* Set configuration bits for use with ICD2 / PICDEM2 PLUS Demo Board:
* - set HS oscillator
* - disable watchdog timer
* - disable low voltage programming
* - enable background debugging
*/
//#pragma config OSC = HS
//#pragma config WDT = OFF
//#pragma config LVP = OFF
//#pragma config DEBUG = ON
void rx_handler (void);
#define BUF_SIZE 25
/*
* Step #1 – The data is allocated into its own section.
*/
#pragma idata bigdata
char data[11][BUF_SIZE+1] = {
{ "String #0\n\r" },
{ "String #1\n\r" },
{ "String #2\n\r" },
{ "String #3\n\r" },
{ "String #4\n\r" },
{ "String #5\n\r" },
{ "String #6\n\r" },
{ "String #7\n\r" },
{ "String #8\n\r" },
{ "String #9\n\r" },
{ "Invalid key (0-9 only)\n\r" }
};
#pragma idata
#pragma code rx_interrupt = 0x8
void rx_int (void)
{
_asm goto rx_handler _endasm
}
#pragma code
#pragma interrupt rx_handler
void rx_handler (void)
{
unsigned char c;
/* Get the character received from the USART */
c = ReadUSART();
if (c >= '0' && c <= '9')
{
c -= '0';
/* Display value received on LEDs */
PORTB = c;
/*
* Step #2 – This example did not need an additional
* pointer to access the large memory because of the
* multi-dimension array.
*
* Display the string located at the array offset
* of the character received
*/
putsUSART (data[c]);
}
else
{
/*
* Step #2 – This example did not need an additional
* pointer to access the large memory because of the
* multi-dimension array.
*
* Invalid character received from USART.
* Display error string.
*/
putsUSART (data[10]);
/* Display value received on LEDs */
PORTB = c;
}
/* Clear the interrupt flag */
PIR1bits.RCIF = 0;
}
void main (void)
{

TRISB = 0;

OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 103);/* Display a prompt to the USART */

putrsUSART (
(const far rom char *)"\n\rEnter a digit 0-9!\n\r");/* Enable interrupt priority */

RCONbits.IPEN = 1;/* Make receive interrupt high priority */

IPR1bits.RCIP = 1;/* Enable all high priority interrupts */

INTCONbits.GIEH = 1;/* Loop forever */

while (1)
;
}

thanks
neelam29 is offline   Reply With Quote
Old 4th April 2008, 02:16 PM   (permalink)
Default

You appear to have all the config lines commented!!

Mike.
Pommie is offline   Reply With Quote
Old 5th April 2008, 05:17 AM   (permalink)
Default

Quote:
Originally Posted by neelam29
void main (void)
{

TRISB = 0;

OpenUSART (USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH, 103);/* Display a prompt to the USART */
IMHO, the USART is in PORTC and by default all the port pins are configured as input on power up. You will have to make the TX pin as output.

TRISC = b'10111111'

Cheers

Ravi
ravimarcus is offline   Reply With Quote
Old 5th April 2008, 05:51 AM   (permalink)
Default

Quote:
Originally Posted by ravimarcus
IMHO, the USART is in PORTC and by default all the port pins are configured as input on power up. You will have to make the TX pin as output.

TRISC = b'10111111'

Cheers

Ravi
The TX pin has to be input for the UART to work properly.

Mike.
Pommie is offline   Reply With Quote
Old 5th April 2008, 05:56 AM   (permalink)
Default

Quote:
Originally Posted by Pommie
The TX pin has to be input for the UART to work properly.

Mike.
The TX pin has to be OUTPUT for the USART to work properly :-). RX pin RC7 and Tx pin RC6.

Cheers

Ravi
ravimarcus is offline   Reply With Quote
Old 5th April 2008, 06:16 AM   (permalink)
Default

Quote:
Originally Posted by ravimarcus
The TX pin has to be OUTPUT for the USART to work properly :-). RX pin RC7 and Tx pin RC6.

Cheers

Ravi
Check the data sheet for the 18f4550, I think you will find I am correct.

Mike.
Pommie is offline   Reply With Quote
Old 5th April 2008, 06:57 AM   (permalink)
Default

Quote:
Originally Posted by Pommie
Check the data sheet for the 18f4550, I think you will find I am correct.

Mike.
Please check the attachment. If I am wrong, please tell me where it is mentioned that the TX pin has to be an input. How can a uC transmit if the pin is defined as input?

Cheers

Ravi
Attached Images
File Type: gif 18f4550.GIF (8.2 KB, 5 views)
ravimarcus is offline   Reply With Quote
Old 5th April 2008, 07:08 AM   (permalink)
Default

It's mentioned in section 20.0 in the datasheet.

The microcontroller will automatically configure the TX pin as an output pin for transmission, but - as Mike said - you set bits TRISC,6 and TRISC,7 when you initialize the EUSART.
eng1 is offline   Reply With Quote
Old 5th April 2008, 07:16 AM   (permalink)
Default

Quote:
Originally Posted by eng1
It's mentioned in section 20.0 in the datasheet.

The microcontroller will automatically configure the TX pin as an output pin for transmission, but - as Mike said - you set bits TRISC,6 and TRISC,7 when you initialize the EUSART.
Thank you for this information and correcting me. It had totally missed me :-). This is a new feature in 18F where the EUSART control will automatically reconfigure the pin from input to output as needed. Normally in 16F we are used to set the TX pin as output.

Cheers

Ravi
ravimarcus is offline   Reply With Quote
Old 5th April 2008, 07:48 AM   (permalink)
Default

A lot of the 16 series chips also require them to be input, even the 16F628A does. I think the only downside to not configuring as input is an increase in power consumption.

Mike.
Pommie is offline   Reply With Quote
Old 5th April 2008, 08:06 AM   (permalink)
Default

Quote:
Originally Posted by Pommie
A lot of the 16 series chips also require them to be input, even the 16F628A does. I think the only downside to not configuring as input is an increase in power consumption.

Mike.
Yes, the data sheet mentiones SET. For example I tried this out in MPLAB for 16F877. The TRISC = b'11xxxxxx'. Initialised the UART registers. The TRISC is still b'11xxxxxx'. Practically too I have seen, if the TX pin is not set as output, the transmission will not occure.

For the 18F series, the behaviour is the same in MPLAB. I will have to check it out practically.

Cheers

Ravi
ravimarcus is offline   Reply With Quote
Old 9th April 2008, 01:09 PM   (permalink)
Default

Thanks pommie and ravi for replies ..i tried uncommenting the oscillator settings and configuration bits settings but it gave error .i tried changing the oscillator from xt to hs but still error persisted so i commented only that line....still it is not working.bellow m sending the usart.h file.does this file needs some changes..please help...

#ifndef __USART_H
#define __USART_H


#define PARAM_SCLASS auto


#define USART_TX_INT_ON 0b11111111 // Transmit interrupt on
#define USART_TX_INT_OFF 0b01111111 // Transmit interrupt off
#define USART_RX_INT_ON 0b11111111 // Receive interrupt on
#define USART_RX_INT_OFF 0b10111111 // Receive interrupt off
#define USART_BRGH_HIGH 0b11111111 // High baud rate
#define USART_BRGH_LOW 0b11101111 // Low baud rate
#define USART_CONT_RX 0b11111111 // Continuous reception
#define USART_SINGLE_RX 0b11110111 // Single reception
#define USART_SYNC_MASTER 0b11111111 // Synchrounous master mode
#define USART_SYNC_SLAVE 0b11111011 // Synchrounous slave mode
#define USART_NINE_BIT 0b11111111 // 9-bit data
#define USART_EIGHT_BIT 0b11111101 // 8-bit data
#define USART_SYNCH_MODE 0b11111111 // Synchronous mode
#define USART_ASYNCH_MODE 0b11111110 // Asynchronous mode


#if defined(__18F6585) || defined(__18F6680) || \
defined(__18F8585) || defined(__18F8680) || \
defined(__18F2480) || defined(__18F2580) || \
defined(__18F4480) || defined(__18F4580) || \
defined(__18F2585) || defined(__18F2680) || \
defined(__18F4585) || defined(__18F4680) || \
defined(__18F64J15) || defined(__18F65J10) || defined(__18F65J15) || \
defined(__18F66J10) || defined(__18F66J15) || defined(__18F67J10) || \
defined(__18F84J15) || defined(__18F85J10) || defined(__18F85J15) || \
defined(__18F86J10) || defined(__18F86J15) || defined(__18F87J10) || \
defined(__18F1220) || defined(__18F1320) || \
defined(__18F6525) || defined(__18F6621) || \
defined(__18F8525) || defined(__18F8621) || \
defined(__18F2515) || defined(__18F2525) || \
defined(__18F2610) || defined(__18F2620) || \
defined(__18F4515) || defined(__18F4525) || \
defined(__18F4610) || defined(__18F4620) || \
defined(__18F6310) || defined(__18F6390) || \
defined(__18F6410) || defined(__18F6490) || \
defined(__18F8310) || defined(__18F8390) || \
defined(__18F8410) || defined(__18F8490) || \
defined(__18F2455) || defined(__18F2550) || \
defined(__18F4455) || defined(__18F4550) || \
defined(__18F2510) || defined(__18F2520) || \
defined(__18F2410) || defined(__18F4410) || defined(__18F4420) || \
defined(__18F4510) || defined(__18F4520) || \
defined(__18F6527) || defined(__18F6622) || \
defined(__18F6627) || defined(__18F6722) || \
defined(__18F8527) || defined(__18F8622) || \
defined(__18F8627) || defined(__18F8722) || \
defined(__18F24J10) || defined(__18F25J10) || \
defined(__18F44J10) || defined(__18F45J10)


#define BAUD_IDLE_RX_PIN_STATE_HIGH 0b11011111 // idle state for RX pin is high level
#define BAUD_IDLE_RX_PIN_STATE_LOW 0b11111111 // idle state for RX pin is low level

#define BAUD_IDLE_TX_PIN_STATE_HIGH 0b11101111 // idle state for TX pin is high level
#define BAUD_IDLE_TX_PIN_STATE_LOW 0b11111111 // idle state for TX pin is low level

#define BAUD_IDLE_CLK_HIGH 0b11111111 // idle state for clock is a high level
#define BAUD_IDLE_CLK_LOW 0b11101111 // idle state for clock is a low level

#define BAUD_16_BIT_RATE 0b11111111 // 16-bit baud generation rate
#define BAUD_8_BIT_RATE 0b11110111 // 8-bit baud generation rate

#define BAUD_WAKEUP_ON 0b11111111 // RX pin monitored
#define BAUD_WAKEUP_OFF 0b11111101 // RX pin not monitored

#define BAUD_AUTO_ON 0b11111111 // auto baud rate measurement enabled
#define BAUD_AUTO_OFF 0b11111110 // auto baud rate measurement disabled

#endif


#if defined(__18F6520) || defined(__18F6620) || defined(__18F6720) || \
defined(__18F8520) || defined(__18F8620) || defined(__18F8720) || \
defined(__18F64J15) || defined(__18F65J10) || defined(__18F65J15) || \
defined(__18F66J10) || defined(__18F66J15) || defined(__18F67J10) || \
defined(__18F84J15) || defined(__18F85J10) || defined(__18F85J15) || \
defined(__18F86J10) || defined(__18F86J15) || defined(__18F87J10) || \
defined(__18F6527) || defined(__18F6622) || \
defined(__18F6627) || defined(__18F6722) || \
defined(__18F8527) || defined(__18F8622) || \
defined(__18F8627) || defined(__18F8722) || \
defined(__18F6525) || defined(__18F6621) || \
defined(__18F8525) || defined(__18F8621) || \
defined(__18F6310) || defined(__18F6390) || \
defined(__18F6410) || defined(__18F6490) || \
defined(__18F8310) || defined(__18F8390) || \
defined(__18F8410) || defined(__18F8490)


/* ***** USART1 ***** */

/* status bits */
union USART1
{
unsigned char val;
struct
{
unsigned RX_NINE:1; // Receive Bit 8 if 9-bit mode is enabled
unsigned TX_NINE:1; // Transmit Bit 8 if 9-bit mode is enabled
unsigned FRAME_ERROR:1; // Framing Error for USART
unsigned OVERRUN_ERROR:1; // Overrun Error for USART
unsigned fill:4;
};
};


void Open1USART (PARAM_SCLASS unsigned char config, PARAM_SCLASS char spbrg);
#if defined(__18F6525) || defined(__18F6621) || \
defined(__18F8525) || defined(__18F8621) || \
defined(__18F64J15) || defined(__18F65J10) || defined(__18F65J15) || \
defined(__18F66J10) || defined(__18F66J15) || defined(__18F67J10) || \
defined(__18F84J15) || defined(__18F85J10) || defined(__18F85J15) || \
defined(__18F86J10) || defined(__18F86J15) || defined(__18F87J10) || \
defined(__18F6527) || defined(__18F6622) || \
defined(__18F6627) || defined(__18F6722) || \
defined(__18F8527) || defined(__18F8622) || \
defined(__18F8627) || defined(__18F8722)

#define DataRdy1USART( ) (PIR1bits.RCIF)
#else
#define DataRdy1USART( ) (PIR1bits.RC1IF)
#endif
char Read1USART (void);
void Write1USART (PARAM_SCLASS char data);
void gets1USART (PARAM_SCLASS char *buffer, PARAM_SCLASS unsigned char len);
void puts1USART (PARAM_SCLASS char *data);
void putrs1USART (PARAM_SCLASS const MEM_MODEL rom char *data);
#define getc1USART Read1USART
#define putc1USART Write1USART
#define Close1USART( ) RCSTA1&=0b01001111,TXSTA1bits.TXEN=0,PIE1&=0b11001 111
#define Busy1USART( ) (!TXSTA1bits.TRMT)
#if defined(__18F6525) || defined(__18F6621) || \
defined(__18F8525) || defined(__18F8621) || \
defined(__18F6310) || defined(__18F6390) || \
defined(__18F6410) || defined(__18F6490) || \
defined(__18F8310) || defined(__18F8390) || \
defined(__18F8410) || defined(__18F8490) || \
defined(__18F64J15) || defined(__18F65J10) || defined(__18F65J15) || \
defined(__18F66J10) || defined(__18F66J15) || defined(__18F67J10) || \
defined(__18F84J15) || defined(__18F85J10) || defined(__18F85J15) || \
defined(__18F86J10) || defined(__18F86J15) || defined(__18F87J10) || \
defined(__18F6527) || defined(__18F6622) || \
defined(__18F6627) || defined(__18F6722) || \
defined(__18F8527) || defined(__18F8622) || \
defined(__18F8627) || defined(__18F8722)

void baud1USART (PARAM_SCLASS unsigned char baudconfig);
#endif


/* ***** USART2 ***** */

/* status bits */
union USART2
{
unsigned char val;
struct
{
unsigned RX_NINE:1; // Receive Bit 8 if 9-bit mode is enabled
unsigned TX_NINE:1; // Transmit Bit 8 if 9-bit mode is enabled
unsigned FRAME_ERROR:1; // Framing Error for USART
unsigned OVERRUN_ERROR:1; // Overrun Error for USART
unsigned fill:4;
};
};

void Open2USART (PARAM_SCLASS unsigned char config, PARAM_SCLASS char spbrg);
#define DataRdy2USART( ) (PIR3bits.RC2IF)
char Read2USART (void);
void Write2USART (PARAM_SCLASS char data);
void gets2USART (PARAM_SCLASS char *buffer, PARAM_SCLASS unsigned char len);
void puts2USART (PARAM_SCLASS char *data);
void putrs2USART (PARAM_SCLASS const MEM_MODEL rom char *data);
#define getc2USART Read2USART
#define putc2USART Write2USART
#define Close2USART( ) RCSTA2&=0b01001111,TXSTA2bits.TXEN=0,PIE3&=0b11001 111
#define Busy2USART( ) (!TXSTA2bits.TRMT)
#if defined(__18F6525) || defined(__18F6621) || \
defined(__18F8525) || defined(__18F8621) || \
defined(__18F64J15) || defined(__18F65J10) || defined(__18F65J15) || \
defined(__18F66J10) || defined(__18F66J15) || defined(__18F67J10) || \
defined(__18F84J15) || defined(__18F85J10) || defined(__18F85J15) || \
defined(__18F86J10) || defined(__18F86J15) || defined(__18F87J10) || \
defined(__18F6527) || defined(__18F6622) || \
defined(__18F6627) || defined(__18F6722) || \
defined(__18F8527) || defined(__18F8622) || \
defined(__18F8627) || defined(__18F8722)
void baud2USART (PARAM_SCLASS unsigned char baudconfig);
#endif

#else


/* ***** USART (TXSTA, RCSTA, etc.) ***** */

/* status bits */
union USART
{
unsigned char val;
struct
{
unsigned RX_NINE:1; // Receive Bit 8 if 9-bit mode is enabled
unsigned TX_NINE:1; // Transmit Bit 8 if 9-bit mode is enabled
unsigned FRAME_ERROR:1; // Framing Error for USART
unsigned OVERRUN_ERROR:1; // Overrun Error for USART
unsigned fill:4;
};
};

void OpenUSART (PARAM_SCLASS unsigned char config, PARAM_SCLASS unsigned spbrg);
#define DataRdyUSART( ) (PIR1bits.RCIF)
char ReadUSART (void);
void WriteUSART (PARAM_SCLASS char data);
void getsUSART (PARAM_SCLASS char *buffer, PARAM_SCLASS unsigned char len);
void putsUSART (PARAM_SCLASS char *data);
void putrsUSART (PARAM_SCLASS const MEM_MODEL rom char *data);
#define getcUSART ReadUSART
#define putcUSART WriteUSART
#define CloseUSART( ) RCSTA&=0b01001111,TXSTAbits.TXEN=0,PIE1&=0b1100111 1
#define BusyUSART( ) (!TXSTAbits.TRMT)
#if defined(__18F6585) || defined(__18F6680) || \
defined(__18F8585) || defined(__18F8680) || \
defined(__18F2480) || defined(__18F2580) || \
defined(__18F4480) || defined(__18F4580) || \
defined(__18F2585) || defined(__18F2680) || \
defined(__18F4585) || defined(__18F4680) || \
defined(__18F1220) || defined(__18F1320) || \
defined(__18F2515) || defined(__18F2525) || \
defined(__18F2610) || defined(__18F2620) || \
defined(__18F4515) || defined(__18F4525) || \
defined(__18F4610) || defined(__18F4620) || \
defined(__18F2455) || defined(__18F2550) || \
defined(__18F4455) || defined(__18F4550) || \
defined(__18F2410) || defined(__18F2510) || defined(__18F2520) || \
defined(__18F4410) || defined(__18F4510) || defined(__18F4520) || \
defined(__18F24J10) || defined(__18F25J10) || \
defined(__18F44J10) || defined(__18F45J10)
void baudUSART (PARAM_SCLASS unsigned char baudconfig);
#endif

#endif

#endif
neelam29 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Drive 33 Servos With One PIC + USART wschroeder Micro Controllers 40 14th July 2008 02:22 PM
Quik PIC Programming kit Krumlink General Electronics Chat 5 27th January 2008 11:27 PM
Capturing and reproducing audio with a PIC Fred.Amoson Micro Controllers 14 14th December 2007 08:21 PM
RF receive using PIC USART col_implant Electronic Projects Design/Ideas/Reviews 11 15th November 2007 04:05 PM
High ADC sampling rate PIC, 18F needed? bananasiong Micro Controllers 24 28th October 2007 12:13 PM



All times are GMT. The time now is 12:30 AM.


Electronic Circuits  |  Electronics Wiki
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.