Hello Rob,
Thank you for your cooperation, yesterday I had already seen the code.
Now let's figure out how I can fit it into my program.
Maybe my question was not entirely clear so I send a piece of the code to
show how I imagined it.
Kind regards, Bill.
MCU = 16F876A 20 MHz The problem here is that var "set" must be a
constant.
Short vars for testing only.
var byte c1,c2,c3
var byte set -- Baud indicator
const serial[8]={1200,2400,4800,9600,19200,38400,57600,115200}
;var dword serial[8]={1200,2400,4800,9600,19200,38400,57600,115200}
pin_c1_direction = INPUT
pin_c2_direction = INPUT
pin_c3_direction = INPUT
include serial_hardware
if pin_c1 then
c1 = 1
else
c1 = 0
end if
if pin_c2 then
c2 = 2
else
c2 = 0
end if
if pin_c3 then
c3 = 4
else
c3 = 0
end if
set = c1 + c2 + c3 + 1
const serial_hw_baudrate = serial[set]
;var dword serial_hw_baudrate = serial[set]
serial_hw_init()
On Tuesday, October 18, 2022 at 7:14:34 AM UTC+2 [email protected] wrote:
> Hello Bill,
>
> I posted an example but never received it my self so this is a retry.
>
> Kind regards,
>
> Rob
>
>
> Op maandag 17 oktober 2022 om 19:52:25 UTC+2 schreef [email protected]:
>
>> Hi Bill,
>>
>> I applied the suggestion of Kiste in one of my programs. Here is a part
>> of that program. This is for a PIC running at 48 MHz target clock.
>>
>> I had to look up a specific baudrate to see if it was valid. If so I set
>> the new baudrate.
>>
>> You see 4 parts (not all needed)
>>
>> 1. Define valid bauddrates. You could skip this
>> 2. Check if a given baudrate is valid. You could skip this too.
>> 3. The baudrate calculation based on the given baudrate + initialize
>> the USART. You need this. Note: baudrate calculation is of type dword.
>> 4. The intiallization routine of the USART with the calculation. You
>> need this.
>>
>>
>> ; List of baudrates supported. See page 262 of the datasheet.
>> ; Formula for baudrate calclation for 8 byte asynchronous mode
>> ; with for sync = FALSE and brg16 = TRUE and brgh = FALSE:
>> ; brg = target_clock/(baudrate * 16) - 1
>> const dword SUPPORTED_BAUDRATES[] = {
>> 110, 300, 600, 1200, 2400, 4800, 9600, 14400,
>> 19200, 28800, 38400, 57600, 115200, 230400
>> }
>>
>> ; Check if the given baudrate is supported and return TRUE if so.
>> function supported_baudrate(dword in baudrate) return bit is
>>
>> var byte index
>> var bit found = FALSE
>>
>> for count(SUPPORTED_BAUDRATES) using index loop
>> if (SUPPORTED_BAUDRATES[index] == baudrate) then
>> found = TRUE
>> end if
>> end loop
>>
>> return found
>>
>> end function
>>
>> baudrate_calculation = (target_clock/(current_baudrate * 16))
>> - 1
>> usart_init(word(baudrate_calculation))
>>
>> ; Initialize the USART with the given baudrate value. The USART is
>> enabled.
>> procedure usart_init(word in baudrate_value) Is
>>
>> usart_disable()
>> ; Use 16 bit baudrate generator and set baudrate
>> BAUDCON = 0b0000_1000
>> SPBRG = baudrate_value
>> ;Initialise transmitter, 8 bits, asynchronous mode, low speed.
>> TXSTA = 0b0000_0000
>> ; Initialise receiver and serial port, 8 bits
>> RCSTA = 0b1000_0000
>> usart_enable()
>>
>> end procedure
>>
>>
>> Hope this helps.
>>
>> Kind regards,
>>
>> Rob
>> ------------------------------
>> *Van:* [email protected] <[email protected]> namens Bill
>> Beek <[email protected]>
>> *Verzonden:* maandag 17 oktober 2022 19:40
>> *Aan:* jallib <[email protected]>
>> *Onderwerp:* Re: [jallib] Baudrate control
>>
>> Thanks Kiste for your reply. My teacher said more than 50 years ago that
>> poking in memory was not a good way of programming,
>> but I did think that this was also a possibility. If this turns out to be
>> the only possibility,
>> I'm not sure how to go about it I don't see anything in the
>> documentation about writing directly in the PIC memory
>> except that you can use assembler instructions. Unfortunately I am not
>> well informed about the PIC asm code.
>> A long time ago I programmed various micro processors.
>> I would like to hear if you want to share a piece of code what i can
>> alter for my needs.
>>
>> Thanks in advance, Bill.
>>
>>
>> On Monday, October 17, 2022 at 4:47:20 PM UTC+2 Kiste wrote:
>>
>> Hi Bill,
>>
>> I've used different baud rates, but I did not modify the serial libraries
>> for that. I just calculated the register values by hand and poked the
>> different values to the different registers. The baud rate calculation in
>> the libraries is not a thing which is good to be done at runtime. For
>> certain baud rates, the lib will refuse to compile and inform you, that
>> your desired baud rate cannot be set due to a deviation from the desired
>> value which is to large to reliably work. That can't be done at runtime for
>> a start.
>>
>> Greets,
>> Kiste
>>
>> Am Montag, 17. Oktober 2022, 15:39:54 MESZ hat Bill Beek <
>> [email protected]> Folgendes geschrieben:
>>
>>
>> Hello everyone, Is there anyone who has a solution to my problem. I am
>> working on a serial LCD now that I have this working, I want to select the
>> baud rate before running the program. Unfortunately, the "const
>> serial_hw_baudate" do not easily change. In the library "usart_common" I
>> changed all the constants in a variable "var dword" but I keep getting
>> error messages from this lib. Maybe I'm overlooking something, or is there
>> a simple method to set the Baud rate under software control.
>> Kind regards, Bill
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/jallib/2b1be8c6-547c-4f0c-97bb-43fc851e4bf4n%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/jallib/2b1be8c6-547c-4f0c-97bb-43fc851e4bf4n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> --
>> 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 view this discussion on the web visit
>> https://groups.google.com/d/msgid/jallib/81855104-44d2-4941-b199-807927d341dan%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/jallib/81855104-44d2-4941-b199-807927d341dan%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>
--
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 view this discussion on the web visit
https://groups.google.com/d/msgid/jallib/8852c568-89d0-4224-95fa-ecd30b6cdc3an%40googlegroups.com.