Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

usart with pic

Status
Not open for further replies.

neelam29

New Member
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 said:
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 said:
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.
 
ravimarcus said:
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 said:
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
 

Attachments

  • 18f4550.GIF
    18f4550.GIF
    8.2 KB · Views: 475
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 said:
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
 
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 said:
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
 
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&=0b11001111
#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&=0b11001111
#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&=0b11001111
#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
 
HELP

I ran the code written in the beginin of the thread and got the following error...
Error - section 'bigdata' can not fit the section. Section 'bigdata' length=0x0000011e
Please help..
 
This is something you can fix on your own
length=0x0000011e is 286.
Ask the world's hotdog eating contest winner what it's like to fit more than you can :)
**broken link removed**
 
Your character array is too bit. You have 11 entries of 25 bytes = 275 bytes. The largest section you can have on that chip is 256 bytes.

Mike.
 
The 1st code written in the thread is copied from:
C:/MCC18/examples/users_guide/example2
(MPLAB compiler)
Still it is having one error:
Error - section 'bigdata' can not fit the section. Section 'bigdata' length=0x0000011e

Please debug it.
 
If you look in the manual you will see that in order to have an array over 256 bytes long you have to combine two sections in the linker script. As you are using a 4550 the simplest solution is to expand section gpr1.

Save this as in your project directory as mod.lkr and add it to your project.
Code:
// File: 18f4550i.lkr
// Sample ICD2 linker script for the PIC18F4550 processor

LIBPATH .

FILES c018i.o
FILES clib.lib
FILES p18f4550.lib

CODEPAGE   NAME=page       START=0x0               END=0x7DBF
CODEPAGE   NAME=debug      START=0x7DC0            END=0x7FFF         PROTECTED
CODEPAGE   NAME=idlocs     START=0x200000          END=0x200007       PROTECTED
CODEPAGE   NAME=config     START=0x300000          END=0x30000D       PROTECTED
CODEPAGE   NAME=devid      START=0x3FFFFE          END=0x3FFFFF       PROTECTED
CODEPAGE   NAME=eedata     START=0xF00000          END=0xF000FF       PROTECTED

ACCESSBANK NAME=accessram  START=0x0            END=0x5F
DATABANK   NAME=gpr0       START=0x60           END=[COLOR="Red"]0xcf[/COLOR]
DATABANK   NAME=gpr1       START=[COLOR="red"]0xd0[/COLOR]           END=0x1FF
DATABANK   NAME=gpr2       START=0x200          END=0x2FF
DATABANK   NAME=gpr3       START=0x300          END=0x3F3
DATABANK   NAME=dbgspr     START=0x3F4          END=0x3FF          PROTECTED
DATABANK   NAME=usb4       START=0x400          END=0x4FF          PROTECTED
DATABANK   NAME=usb5       START=0x500          END=0x5FF          PROTECTED
DATABANK   NAME=usb6       START=0x600          END=0x6FF          PROTECTED
DATABANK   NAME=usb7       START=0x700          END=0x7FF          PROTECTED
ACCESSBANK NAME=accesssfr  START=0xF60          END=0xFFF          PROTECTED

SECTION    NAME=CONFIG     ROM=config
[COLOR="red"]SECTION    NAME=bigdata    RAM=gpr1[/COLOR]
STACK SIZE=0x100 RAM=gpr2

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top