Jump to content

Recommended Posts

Can any one tell me which timer is used to generate the baud rate for the software uart?

If i use both timer0 and timer1 for other timing application can i still use rs232 communication?

 

Raghunathan.

Share this post


Link to post
Share on other sites

ra68gi,

Can any one tell me which timer is used to generate the baud rate for the software uart?

If i use both timer0 and timer1 for other timing application can i still use rs232 communication?

Neither timer is used. The timing is acheived using time wasting software loops.

 

Regards

Dave

Share this post


Link to post
Share on other sites

Can i not implement software usart on pic16 chips which doesn't have baud rate generator register(SPBRG), say PIC16F84 ? ;)

 

Raghunathan.

Share this post


Link to post
Share on other sites

It's been a while since I looked at the UART library, but I don't think the software UART uses the USART SFR's (like SPBRG) at all.

Share this post


Link to post
Share on other sites
It's been a while since I looked at the UART library, but I don't think the software UART uses the USART SFR's (like SPBRG) at all.

 

It took me a while to understand that these hardware registers are implemented in the RAM locations.& i had to include....

// RAM used by the software USART driver to emulate the equivalent serial hardware registers
// Note this section is really just reserving the space. The defines in the subsequent section
// overlay these RAM locations					
unsigned short sw_SPBRG@0x47;	// define location for the emulated SSPCON1
unsigned short sw_RCREG@0x48;	// define location for the emulated SSPCON2
unsigned short sw_TXREG@0x49;	// define location for the emulated SSPSTAT
unsigned short sw_TXSTA@0x4A;	// define location for the emulated SSPBUF
unsigned short sw_RCSTA@0x4B;	// define location for the emulated SSPADD
unsigned short sw_TXIF_PIR@0x4C;// define location for the emulated TXIF_PIR1
unsigned short sw_RCIF_PIR@0x4C;// define location for the emulated RCIF_PIR1

 

Regards

Raghunathan.

Share this post


Link to post
Share on other sites

I got it to compile but it failed to build. My target chip is PIC16F84A & crystal freq is 4MHZ. any help appreciated. Baud rate used 2400.

#include <system.h>

#pragma CLOCK_FREQ 4000000  // config clock to 4mhz.

// Set configuration fuse.

#pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF


// RAM used by the software USART driver to emulate the equivalent serial hardware registers
// Note this section is really just reserving the space. The defines in the subsequent section
// overlay these RAM locations                    
unsigned short sw_SPBRG@0x47;    // define location for the emulated SSPCON1
unsigned short sw_RCREG@0x48;    // define location for the emulated SSPCON2
unsigned short sw_TXREG@0x49;    // define location for the emulated SSPSTAT
unsigned short sw_TXSTA@0x4A;    // define location for the emulated SSPBUF
unsigned short sw_RCSTA@0x4B;    // define location for the emulated SSPADD
unsigned short sw_TXIF_PIR@0x4C;// define location for the emulated TXIF_PIR1
unsigned short sw_RCIF_PIR@0x4C;// define location for the emulated RCIF_PIR1


// variables cannot be passed as template arguments. The following constants map to
// the PIC registers and software emulated USART RAM locations. These constants are
// then used by the templated functions. When changing the address of an emulated
// register the corresponding constant mapping must also be changed.
// Sample PIC16F defaults for software emulated USART support
#define TX_PORT        0x06
#define TX_TRIS        0x85
#define TX_BIT            6
#define RX_PORT        0x06
#define RX_TRIS        0x85
#define RX_BIT            7
#define e_SPBRG        0x99
#define e_RCREG        0x1a
#define e_TXREG        0x019
#define e_TXSTA        0x98
#define e_RCSTA        0x18
#define e_TXIF_PIR        0x0c
#define e_RCIF_PIR        0x0c
#define e_TXIF_BIT        4
#define e_RCIF_BIT        5
#define MODE        (USART_reset_wdt)


// bit_time = FOSC / 4 / BAUDRATE 
#define bit_time 416   // for 2400bauds with 4mhz crystal

#include <rs232_driver.h>

void main()
{
    // for the hardware UART the paramter passed is the divisor
    // the hardware usart enables dynamic reconfiguration of the baud rate
    // the software emulated USART baud rate is defined by constants in
    // serial_driver.h - this file also contains the address mapping for
    // RAM required to support the software emulated USART

    uart_init(0,21);  // 1 for high speed divisor mode and divisor value
    puts("Hello, world");
    while (1)
    {
        if (kbhit())
        {
            putc(getc());
        }
    }
}

 

Is uart_init() function required for software usart?

 

Raghunathan.

 

error reported..

 

Building...
BoostC Optimizing C Compiler Version 6.60 (for PIC16 architecture)
http://www.sourceboost.com
Copyright(C) 2004-2006 Pavel Baranov
Copyright(C) 2004-2006 David Hobday

Single user Lite License (Unregistered) for 0 node(s)
Limitations: PIC12,PIC16 max code size:2048 words, max RAM banks:2, Non commercial use only


rs232.test1.c

success
BoostLink Optimizing Linker Version 6.60
http://www.sourceboost.com
Copyright(C) 2004-2006 Pavel Baranov
Copyright(C) 2004-2006 David Hobday




Failure
Error: Unable to successfully create 'delay_us' for target with clock freq 4000000 Hz
Error: Unavailable timing functions used!
"D:\Program Files\SourceBoost\boostc.pic16.exe" rs232.test1.c -t PIC16F84A 
"D:\Program Files\SourceBoost\boostlink.pic.exe" /ld "D:\Program Files\SourceBoost\lib" libc.pic16.lib rs232.test1.obj /t PIC16F84A /d "D:\Program Files\SourceBoost\Samples\C\BoostC\my test files" /p rs232.test1
Exit code was -2.
Removing target: rs232.test1.hex
Failed to locate output file 'D:\Program Files\SourceBoost\Samples\C\BoostC\my test files\rs232.test1.hex'
Done

Failed

Edited by ra68gi

Share this post


Link to post
Share on other sites

The above code builds when i changed the clock frequency to 20MHz.

Does that mean i wouldn't be able to implement SB's suart or huart lib on PICs operating on less than 20MHz ?

 

Is there anyway i can get it to work with a 4MHz clock ?

 

Raghunathan

Share this post


Link to post
Share on other sites

ra68gi,

Is there anyway i can get it to work with a 4MHz clock ?

 

Raghunathan

Yes.

1) Implement your own delay.

2) Wait for next release V6.70 which does still generate delays at lower clock frequencies.

 

Regards

Dave

Share this post


Link to post
Share on other sites
Yes.

1) Implement your own delay.

2) Wait for next release V6.70 which does still generate delays at lower clock frequencies.

 

Regards

Dave

 

To change the delays in rs232_driver.h file is a very difficult task for me. Since i haven't understood the code completely. I will sit back and wait for the release of v6.70. Mean while i will keep working with 20Mhz chips. At this stage i think its going to be good form me see some asm program examples(Microcontrol'n Apps written by David Benson & serial communication by Roger Stevens) )

 

Regards

Raghunathan.

Edited by ra68gi

Share this post


Link to post
Share on other sites
To change the delays in rs232_driver.h file is a very difficult task for me. Since i haven't understood the code completely.

Me neither, the delay is used in just one place in the code. I actually don't quite see the point of this delay. I think it can be safely commented out.

 

Regards

Dave

Share this post


Link to post
Share on other sites
I actually don't quite see the point of this delay. I think it can be safely commented out.

I believe 1 us at 4 MHz is 1 cycle, so if you wanted to be safe, you could put a nop there instead. I don't see the purpose either, so it would still be fine to run at other frequencies.

Share this post


Link to post
Share on other sites
I actually don't quite see the point of this delay. I think it can be safely commented out.

I believe 1 us at 4 MHz is 1 cycle, so if you wanted to be safe, you could put a nop there instead. I don't see the purpose either, so it would still be fine to run at other frequencies.

 

Yes. I did see the 1us delay in the code. I will try replacing it with a nop and see if it works.

Regards

Raghunathan,

Share this post


Link to post
Share on other sites
To change the delays in rs232_driver.h file is a very difficult task for me. Since i haven't understood the code completely.

Me neither, the delay is used in just one place in the code. I actually don't quite see the point of this delay. I think it can be safely commented out.

 

The serial driver was written in such a way as to minimize the peripherals required. This is why it does not use any timers and the same reason it can be easily used on any PIC. The division step immediately before the delay_us line will introduce rounding errors. The rounding error is a function of the clock frequency used and the divisor selected. The additional 1us delay prevents the next character from being serialized out within the timing window of the previous character as a result of the potential rounding error.

 

Most UART receivers do NOT look at the stop bit timing and therefore would not cause a problem. However, other may do this and as a result could potentially generate a framing error. Some UARTS will lock up in the presence of a framing error.

 

The real problem comes about because the compiler does the daft thing of not producing any delay code because it cannot accurately produce a 1us delay at 4HMz yet it obvioulsy could because, as already mentioned, 1us = 1 NOP at 4MHz.

 

Should the delay be dropped from the code? No. It performs a valid operation in the context of the drivers implementation.

 

Would the driver work without it? Maybe yes and maybe no.

Share this post


Link to post
Share on other sites
Should the delay be dropped from the code? No. It performs a valid operation in the context of the drivers implementation.

 

Would the driver work without it? Maybe yes and maybe no.

 

Thanks Andrew, for that nice bit of information.

 

Regards

Raghunathan

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...