Got it working, at least this works

include jaluino_bee
include delay

onboard_led_direction = output

onboard_led = high
delay_1s(1)
onboard_led = low


var dword serial_hw_baudrate = 9600
include serial_hardwarem
include print

forever loop
serial_hw_baudrate = 2400
serial_hw_init()
print_string(serial_hw_data,"\r\n(C) Sunish Issac baud 2400\r\n Init OK
\r\n")
onboard_led = high
delay_1s(1)
onboard_led = low
delay_1s(1)
serial_hw_baudrate = 4800
serial_hw_init()
print_string(serial_hw_data,"\r\nbaud 4800\r\n Init OK \r\n")
onboard_led = high
delay_1s(1)
onboard_led = low
delay_1s(1)
serial_hw_baudrate = 9600
serial_hw_init()
print_string(serial_hw_data,"\r\nbaud 9600\r\n Init OK \r\n")
onboard_led = high
delay_1s(1)
onboard_led = low
delay_1s(1)

end loop


Here's the modified usartcommon, I commented the bauddeviation check part.

-- Title: USART common
-- Author: Stef Mientki Copyright (c) 2002..2010, all rights reserved.
-- Adapted-by: Sebastien Lelong, Joep Suijs, Rob Hamerling
-- Compiler: 2.4o
-- Revision: $Revision: 3087 $
--
-- This file is part of jallib (http://jallib.googlecode.com)
-- Released under the ZLIB license (
http://www.opensource.org/licenses/zlib-license.html)
--
-- Description: USART common functions
-- This file provides common functions to other libraries.
-- .
-- Baudrate can simply be set through a constant in the application program,
-- because the baudrate depending registers are calculated by this unit.
-- Baudrate is calculated, starting at the high baudrate flag,
-- which will ensure the highest possible accuracy.
--

if (defined(usart_hw_serial) == FALSE) then
   const bit usart_hw_serial = TRUE                -- default is async mode
(not sync)
end if

-- Internal function, calculates and sets baudrate divider
-- and stores it in the appropiate register.
-- the high-speed / low-speed bit is not stored but returned as the result
--
-- Special attention is paid to the highest and lowest baudrates,
-- a deviation of 5% is accepted in these cases
--
-- Asynchronous baudrate settings
-- {{{
-- for ESUART (with 16 bits baudrate generator)
--  (word) <SPBRG> = ( Fosc / ( 4 * Baudrate ) ) -1
--
-- if TXSTA_BRGH = 1 (high speed)
--  (byte) <SPBRG> = ( Fosc / ( 16 * Baudrate ) ) -1
--
-- if TXSTA_BRGH = 0 (low speed)
--  (byte) <SPBRG> = ( Fosc / ( 64 * Baudrate ) ) -1
--
-- }}}
--
-- Synchronous baudrate settings:
--  * TXSTA_BRGH = 0 (low speed)
--  * (byte) <SPBRG> = ( Fosc / ( 4 * Baudrate ) ) -1


-- 
------------------------------------------------------------------------------
-- Title:     Calculate and set baudrate
-- Arguments: (none)
-- Returns:   (nothing)
-- Notes:     - Uses externally defined constant 'serial_hw_baudrate' to
--              set baudrate registers and corresponding control bits
--            - Uses bit-constant 'usart_hw_serial' to determine mode of
operation:
--              * TRUE:  Asynchronous communications (default)
--              * FALSE: Synchronous communications
-- 
------------------------------------------------------------------------------
procedure _calculate_and_set_baudrate() is

   const max_deviation = 5                         -- maximum % deviation
of the desired baudrate

   if (usart_hw_serial == TRUE) then               -- USART in asynchronous
mode

      if (defined(BAUDCON_BRG16) == TRUE) then     -- use 16 bit baudrate
register
         BAUDCON_BRG16 = TRUE
         TXSTA_BRGH = TRUE

         var word  usart_div = ((5 + ( ( 10 * target_clock ) / ( 4 *
serial_hw_baudrate ))  ) / 10 ) - 1
;         var dword real_baud
;         real_baud = target_clock / 4 / ((usart_div & 0xffff) + 1)

;         if (real_baud > serial_hw_baudrate) then
;            if (100 * (real_baud - serial_hw_baudrate) /
serial_hw_baudrate >= max_deviation) then
;               _error "asynchronous baudrate deviation is too large"
;            end if
;         else
;            if (100 * (serial_hw_baudrate - real_baud) /
serial_hw_baudrate >= max_deviation) then
;               _error "asynchronous baudrate deviation is too large"
;            end if
;         end if
;
         SPBRGL = byte(usart_div)                  -- MSB
         SPBRGH = byte(usart_div >> 8)             -- LSB

      else
         -- use classic (8 bit) baudrate register

         -- <SPBRG> = ( Fosc / ( 4 * Baudrate ) ) -1
         -- first try high baudrate, will generate highest accuarcy
         -- to get the right rounding (5 + 10*f(x)) /10
         const usart_div = ((5 + ( ( 10 * target_clock ) / ( 16 *
serial_hw_baudrate ))  ) / 10 ) - 1

         -- special case if divider is 0, test if deviation is not too much
         if usart_div <= 0 then
            if (100 * (serial_hw_baudrate - (target_clock / 16) )) /
serial_hw_baudrate >= max_deviation then
               _error "Asynchronous baudrate is too high"
            end if
         end if

         -- if divider small enough calculate divider and set high-speed
         const real_baud = target_clock / 16 / (usart_div + 1)
         if usart_div <= 255 then
            if (real_baud > serial_hw_baudrate) then
               if (100 * (real_baud - serial_hw_baudrate) /
serial_hw_baudrate >= max_deviation) then
                  _error "Asynchronous baudrate deviation is too large"
               end if
            else
               if (100 * (serial_hw_baudrate - real_baud) /
serial_hw_baudrate >= max_deviation) then
                  _error "Asynchronous baudrate deviation is too large"
               end if
            end if
            if usart_div >= 0 then
               SPBRGL = byte(usart_div)
            else
               SPBRGL = byte(0)
            end if
            TXSTA_BRGH = TRUE
            -- try the low-speed mode
         else
            const usart_div_low = ((((10 * target_clock) / ( 64 *
serial_hw_baudrate )) + 5 ) / 10) - 1
            -- here divider will never be 0
            -- but special case to consider,
            -- if baudrate is just a little too low
            if (usart_div_low > 255) & (100 * ((target_clock / (64 * 256 ))
- serial_hw_baudrate)) / serial_hw_baudrate < max_deviation then
               SPBRGL = byte(255)
               TXSTA_BRGH = FALSE
               -- now calculate divider and set high-speed / low-speed bit
            elsif usart_div_low <= 255 then
               if usart_div_low >= 0 then
                  SPBRGL = byte(usart_div_low)
               else
                  SPBRGL = byte(0)
               end if
               TXSTA_BRGH = FALSE
            else
               _error "Asynchronous baudrate is too low"
            end if
         end if
      end if

   else                                            -- USART in synchronous
mode

      const usart_div_sync = ( target_clock / ( 4 * serial_hw_baudrate )) -
1

      -- special case if divider is 0 or negative
      -- test if baudrate is a little bit too high
      if usart_div_sync <= 0 then
         if (100 * (serial_hw_baudrate - (target_clock / 4 ) )) /
serial_hw_baudrate >= max_deviation then
            _error "Synchronous baudrate is too high"
         end if
      end if

      -- special case to consider, if baudrate is just a little too high
      if (usart_div_sync > 255) & (100 * ((target_clock / (4 * 256)  ) -
serial_hw_baudrate)) / serial_hw_baudrate < max_deviation then
         SPBRGL = byte(255)
      elsif (usart_div_sync <= 255) then
         if (usart_div_sync >= 0) then
            SPBRGL = byte(usart_div_sync)
         else
            SPBRGL = byte(0)
         end if
      else
         _error "Synchronous baudrate is too low"
      end if

   end if

end procedure

Hope it might be of use..

Sunish

On Sun, Feb 24, 2013 at 5:38 PM, Sunish Issac <[email protected]> wrote:

> Thanks everybody. Declared it as variable, not sure its should be word or
> dword could be dword as it can be more tahn 65535. Results in some warnings
> like operation is always true. Yes init will have to be called again.
>
> Will be able to test only later.
>
> Regards,
> Sunish
>
>
> On Sun, Feb 24, 2013 at 3:32 PM, Oliver Seitz <[email protected]> wrote:
>
>>
>> This may be possible, but after altering the baud rate, you will have to
>> call the init procedure again, as the baudrate constant is only used to set
>> the baud rate registers once.
>>
>> Greets,
>> Kiste
>>
>>
>>
>> Hi Sunish,
>>
>> Did not dig into the code, but you could declare baudrate as a variable,
>> not a constant. I had the same issue with ADC libraries. Instead of using
>> constant, I used variables to modify at runtime ADC behavior. I had to
>> modify library itself in the end... I don't think it's not doable, but it
>> may require to change the lib.
>>
>> Let us know
>>
>> Cheers
>> Seb
>>
>>
>> On 24 February 2013 10:51, Sunish Issac <[email protected]> wrote:
>>
>> I have a requirement where the baudrate of the sender can be either
>> 2400,4800 and 9600.
>> The sending device is a weighing machine and it sends data continuosly,
>> so the plan was to try verify the characters and then if its not valid try
>> the next baud.
>>
>> Since the library expects a constant for baudrate, its directly not
>> possible to try other baudrates, Any suggestions ?
>>
>> Sunish
>> --
>> You received this message because you are subscribed to the Google Groups
>> "jallib" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/jallib?hl=en.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>>
>>
>>
>> --
>> Sébastien Lelong
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/jallib?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to