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.

UART/CODE problem Oshonsoft

Status
Not open for further replies.

camerart

Well-Known Member
Hi,
When setting the BAUD rate on a PIC, one of the settings is BRGH, the H stans for High.
what I want to know is, what is high and what is low?
In other words when is BRGH used?
Camerart
 
Maybe trying to force a language change on somebody whose to struggling to understand a language he has used for years, and who has adamantly stated he is unwilling to try something new is a waste of bits.
 
Maybe trying to force a language change on somebody whose to struggling to understand a language he has used for years, and who has adamantly stated he is unwilling to try something new is a waste of bits.
Totally agree. Oshonsoft works for C so we'll use that.

C' can you send me the entire pic18f4431 code and I'll sim it in real time. I can inject GPS sentences and see if the pic's getting pummeled or not setting SPI up properly..
 
I think you'll find that's probably a waste of time.

The code he has works "most of the time", so it's likely a matter of finding out what error/timing conditions cause it to fail. That's pretty hard to guess at simulating, and since it involves three different processors that's going to be almost impossible to simulate.

But hey, don't let me burst the bubble.
 
Maybe trying to force a language change on somebody whose to struggling to understand a language he has used for years, and who has adamantly stated he is unwilling to try something new is a waste of bits.

"Force", huh and I thought exposing folks to new ideas was a duty of a professional. a
progressive way of helping. I actually had a great career doing this. Note I qualified the
OP post with comments that are not typical of an Enforcer, just a consider this......

We know that people learn in different ways, eg. visual learners, audible learners,
written.....etc..

So I will stick with my sense of showing folks, in some discussions, what I refer to as
ticklers, "gee Bob did you know in the near future you will be able to dictate/speak
to an AI machine and have it create the code for you". Already we have AI from chat
input generating code, albeit not always right, but like regular programmers who do
not have it right from the start its a step to solution solving.

So until the "Force" police catch up with me I am going to continue to "Force" people to
do it a certain way. And that way will continue to evolve as the tools, ideas, methods of
engineering evolve.

"May the Force be with us Grasshopper".......


Regards, Dana, the enForcer :)
 
Last edited:
@camerart

This is an aside on block based / visual programming.

There are a number of block programmers developed for folks that learn in visual fashion.
MIT with its Scratch, mBlock, Snap4arduino, Flowcode, Nodered, Visuino, Tuniot.....

Here is an example of what a simple voltage to pulse width converter looks like (just 5 blocks) :

View attachment 142812
You take blocks out of second window, place them in design area (3'rd window),
configure them, and then hit the connect button and mBlock generates C/C++
code and then uses the Arduino IDE programmer to program the target board/part.
The rightmost window is Arduino C/C++ code that mBlock generates from your block
configuration.

I am not advocating this as a way to learn C, rather an adjunct to easily program
simple designs and be able to look at what C/C++ code looks like. Its not optimal
code by any stretch but for simple one off projects fast to get something going.
Some examples :




Flow code is done with a flowchart like interface (multiple ways, mix and match) :

View attachment 142813

A number of folks experimenting with ChatGPT and other AI programs to generate code. Its a
new world in which complex ideas are getting whittled down into simple code methods. C/C++
was/is a strongly typed language, meaning its exact at how you enter and code with it, not forgiving,
"do it my way or die" type language. These new languages are born out of that and making simple
designs a lot easier. Talking voltmeter above link great example of a complex design reduced to
simple block approach.

C/C++, Python, still the "professional" methods, but block and AI catching up rapidly.

Have some fun, try it out on the simple stuff.


Regards, Dana.
Hi D,
This is really good, however, programming and maths are not my strength, even though I have a maths 'O' level, I'm not good and avoid it, in a similar way to learning new programming skills.

Apart from this I am fitting a Purlin in my house that has to pass an inspection, and it's also complicated, and I HAVE to get it right. Too much going on!

Thanks for trying.
C
 
Hi,
We're trying this in the SIM:
It passes the $, then blocks on the 1st CHAR, 'G' any ideas?
C
Code:
If GPS_String(GPS_LAST) = 0 Then  'GPS_LAST is the last char in the GPS STRING (E/W)

    ' We are transferring data  so look for the $ start character
    If PIR1.RCIF = 1 Then
        rxirqchar = RCREG  'read the received char, clears RCIF
        If rxirqchar = "$" Then
            GPS_String(0) = rxirqchar  ' store the $ in the GPs string at the start of the string
            gps_string_position = 0  ' reset the counter
        Else  ' we are adding to the string
            gps_string_position = gps_string_position + 1  ' point to next location
            GPS_String(gps_string_position) = rxirqchar  ' and store the data
        Endif  ' rxirqchar = $
        If gps_string_position = GPS_LAST Then  ' we are at the end of the buffer
            If GPS_String(GPS_LAST) = 0 Then  ' Buffer has under run so continue searching for the $
                gps_string_position = 0  ' reset the counter
            Endif
        Endif
    Endif
Endif  'GPS_String(GPS_LAST)
 

Attachments

  • UART G.jpg
    UART G.jpg
    170.7 KB · Views: 205
Rearrange the tests so that if RCIF is set you read RCREG.
You also have other bits in PIE1 enabled... are you handling them?

Don't forget to check for OERR.
 
Recommend you try this and then continue.


#define CONFIG1L = 0x00
#define CONFIG1H = 0x28
#define CONFIG2L = 0x1e
#define CONFIG2H = 0x2a
#define CONFIG3L = 0x00
#define CONFIG3H = 0x3f
#define CONFIG4L = 0x80
#define CONFIG4H = 0x00
#define CONFIG5L = 0x0f
#define CONFIG5H = 0xc0
#define CONFIG6L = 0x0f
#define CONFIG6H = 0xe0
#define CONFIG7L = 0x0f
#define CONFIG7H = 0x40
'***************************
#define CLOCK_FREQUENCY = 64
'Define SIMULATION_WAITMS_VALUE = 1
'*******************************************************************
'Example_01
'BPSI, Pic18F46K22, 24/09/2023
'*******************************************************************
'Buffer uart
'*******************************************************************
'********************************************************************
Include "_FuncionesPic18F46K22.bas"
Include "_FuncionesUartRingBuffer.bas"
'Configuración
AllDigital
TRISA = 255
TRISB = 255
TRISD = 255
TRISC = %10111111 'C.6 = Tx, C.7 = Rx
TRISE = %1111

UART1_Init 9600
UART2_Init 9600 '115200
'Call _set_pullup(_pullup_on)
Call _setup_oscillator(_osc_16mhz_hf)
Call _setup_oscillator_mode(_primary_clock)
Call _set_pll4(_pll_on)
Call _interrupts_mode(_disable_priority)
'Call _setup_timer1(_tmr1_internal, _tmr1_div1)
'Call _set_timer1(0xc180)
'Call _timer1(_on)
'Call _enable_interrupts(_int_timer1)
Call _setuprbf(10) 'mSec. Delay Read usart, if read buffer = 0
Call _enable_interrupts(_int_rda)

Dim gps_string(80) As Byte
Dim gps_last As Byte
Dim rxirqchar As Byte
Dim gps_string_position As Byte
gps_string_position = 0
gps_last = 0x0a '??? End trama NMEA.
'$GPRMC,192247.825,A,3643.4983,N,00332.0126,W,0.00,197.95,301204,,*11, CrLf

Call _enable_interrupts(_global_high)
'WaitMs 100
While True 'Loop

'If gps_string(gps_last) = 0 Then 'GPS_LAST is the last char in the GPS STRING (E/W)

' We are transferring data so look for the $ start character
If _buffer > 0 Then
rxirqchar = _receive() 'RCREG 'read the received char, clears RCIF
Hserout rxirqchar 'Test, output
If rxirqchar = "$" Then
gps_string(0) = rxirqchar ' store the $ in the GPs string at the start of the string
gps_string_position = 0 ' reset the counter
Else ' we are adding to the string
gps_string_position++ ' point to next location
gps_string(gps_string_position) = rxirqchar ' and store the data
Endif ' rxirqchar = $
If rxirqchar = gps_last Then 'we are at the end of the Trama
gps_string(gps_last) = 0x0a 'If end trama NMEA = 0x0a
gps_string_position = 0 ' reset the counter
Hserout "END", CrLf 'Test output
Endif
Endif
'Endif 'GPS_String(GPS_LAST)
Wend
End
'*************************************************
On High Interrupt
Save System
'Control buffer serie
If PIR1.RC1IF = 1 Then
Call _ringbuffer()
Endif
Resume
 

Attachments

  • Example_01.bas
    2.9 KB · Views: 208
  • _FuncionesPic18F46K22.bas
    32.8 KB · Views: 210
  • _FuncionesUartRingBuffer.bas
    3.6 KB · Views: 204
  • Example.jpg
    Example.jpg
    1.1 MB · Views: 201
Last edited:
I think you'll find that's probably a waste of time.

The code he has works "most of the time", so it's likely a matter of finding out what error/timing conditions cause it to fail. That's pretty hard to guess at simulating, and since it involves three different processors that's going to be almost impossible to simulate.

But hey, don't let me burst the bubble.
Hi T,
Just a note: There are only 2x PICs on each PCB.
C
 
Rearrange the tests so that if RCIF is set you read RCREG.
You also have other bits in PIE1 enabled... are you handling them?

Don't forget to check for OERR.
Hi T,
Thanks, I'll try that.
C.
Recommend you try this and then continue.


#define CONFIG1L = 0x00
#define CONFIG1H = 0x28
#define CONFIG2L = 0x1e
#define CONFIG2H = 0x2a
#define CONFIG3L = 0x00
#define CONFIG3H = 0x3f
#define CONFIG4L = 0x80
#define CONFIG4H = 0x00
#define CONFIG5L = 0x0f
#define CONFIG5H = 0xc0
#define CONFIG6L = 0x0f
#define CONFIG6H = 0xe0
#define CONFIG7L = 0x0f
#define CONFIG7H = 0x40
'***************************
#define CLOCK_FREQUENCY = 64
'Define SIMULATION_WAITMS_VALUE = 1
'*******************************************************************
'Example_01
'BPSI, Pic18F46K22, 24/09/2023
'*******************************************************************
'Buffer uart
'*******************************************************************
'********************************************************************
Include "_FuncionesPic18F46K22.bas"
Include "_FuncionesUartRingBuffer.bas"
'Configuración
AllDigital
TRISA = 255
TRISB = 255
TRISD = 255
TRISC = %10111111 'C.6 = Tx, C.7 = Rx
TRISE = %1111

UART1_Init 9600
UART2_Init 9600 '115200
'Call _set_pullup(_pullup_on)
Call _setup_oscillator(_osc_16mhz_hf)
Call _setup_oscillator_mode(_primary_clock)
Call _set_pll4(_pll_on)
Call _interrupts_mode(_disable_priority)
'Call _setup_timer1(_tmr1_internal, _tmr1_div1)
'Call _set_timer1(0xc180)
'Call _timer1(_on)
'Call _enable_interrupts(_int_timer1)
Call _setuprbf(10) 'mSec. Delay Read usart, if read buffer = 0
Call _enable_interrupts(_int_rda)

Dim gps_string(80) As Byte
Dim gps_last As Byte
Dim rxirqchar As Byte
Dim gps_string_position As Byte
gps_string_position = 0
gps_last = 0x0a '??? End trama NMEA.
'$GPRMC,192247.825,A,3643.4983,N,00332.0126,W,0.00,197.95,301204,,*11, CrLf

Call _enable_interrupts(_global_high)
'WaitMs 100
While True 'Loop

'If gps_string(gps_last) = 0 Then 'GPS_LAST is the last char in the GPS STRING (E/W)

' We are transferring data so look for the $ start character
If _buffer > 0 = 1 Then
rxirqchar = _receive() 'RCREG 'read the received char, clears RCIF
Hserout rxirqchar 'Test, output
If rxirqchar = "$" Then
gps_string(0) = rxirqchar ' store the $ in the GPs string at the start of the string
gps_string_position = 0 ' reset the counter
Else ' we are adding to the string
gps_string_position++ ' point to next location
gps_string(gps_string_position) = rxirqchar ' and store the data
Endif ' rxirqchar = $
If rxirqchar = gps_last Then 'we are at the end of the Trama
gps_string(gps_last) = 0x0a 'If end trama NMEA = 0x0a
gps_string_position = 0 ' reset the counter
Hserout "END", CrLf 'Test output
Endif
Endif
'Endif 'GPS_String(GPS_LAST)
Wend
End
'*************************************************
On High Interrupt
Save System
'Control buffer serie
If PIR1.RC1IF = 1 Then
Call _ringbuffer()
Endif
Resume
Hi D,
That looks great.
It will take a little time to get it working as I'm using a different PIC 18F4431, and there are a few differences.
Thanks.
C
 
Recommend you try this and then continue.


#define CONFIG1L = 0x00
#define CONFIG1H = 0x28
#define CONFIG2L = 0x1e
#define CONFIG2H = 0x2a
#define CONFIG3L = 0x00
#define CONFIG3H = 0x3f
#define CONFIG4L = 0x80
#define CONFIG4H = 0x00
#define CONFIG5L = 0x0f
#define CONFIG5H = 0xc0
#define CONFIG6L = 0x0f
#define CONFIG6H = 0xe0
#define CONFIG7L = 0x0f
#define CONFIG7H = 0x40
'***************************
#define CLOCK_FREQUENCY = 64
'Define SIMULATION_WAITMS_VALUE = 1
'*******************************************************************
'Example_01
'BPSI, Pic18F46K22, 24/09/2023
'*******************************************************************
'Buffer uart
'*******************************************************************
'********************************************************************
Include "_FuncionesPic18F46K22.bas"
Include "_FuncionesUartRingBuffer.bas"
'Configuración
AllDigital
TRISA = 255
TRISB = 255
TRISD = 255
TRISC = %10111111 'C.6 = Tx, C.7 = Rx
TRISE = %1111

UART1_Init 9600
UART2_Init 9600 '115200
'Call _set_pullup(_pullup_on)
Call _setup_oscillator(_osc_16mhz_hf)
Call _setup_oscillator_mode(_primary_clock)
Call _set_pll4(_pll_on)
Call _interrupts_mode(_disable_priority)
'Call _setup_timer1(_tmr1_internal, _tmr1_div1)
'Call _set_timer1(0xc180)
'Call _timer1(_on)
'Call _enable_interrupts(_int_timer1)
Call _setuprbf(10) 'mSec. Delay Read usart, if read buffer = 0
Call _enable_interrupts(_int_rda)

Dim gps_string(80) As Byte
Dim gps_last As Byte
Dim rxirqchar As Byte
Dim gps_string_position As Byte
gps_string_position = 0
gps_last = 0x0a '??? End trama NMEA.
'$GPRMC,192247.825,A,3643.4983,N,00332.0126,W,0.00,197.95,301204,,*11, CrLf

Call _enable_interrupts(_global_high)
'WaitMs 100
While True 'Loop

'If gps_string(gps_last) = 0 Then 'GPS_LAST is the last char in the GPS STRING (E/W)

' We are transferring data so look for the $ start character
If _buffer > 0 = 1 Then
rxirqchar = _receive() 'RCREG 'read the received char, clears RCIF
Hserout rxirqchar 'Test, output
If rxirqchar = "$" Then
gps_string(0) = rxirqchar ' store the $ in the GPs string at the start of the string
gps_string_position = 0 ' reset the counter
Else ' we are adding to the string
gps_string_position++ ' point to next location
gps_string(gps_string_position) = rxirqchar ' and store the data
Endif ' rxirqchar = $
If rxirqchar = gps_last Then 'we are at the end of the Trama
gps_string(gps_last) = 0x0a 'If end trama NMEA = 0x0a
gps_string_position = 0 ' reset the counter
Hserout "END", CrLf 'Test output
Endif
Endif
'Endif 'GPS_String(GPS_LAST)
Wend
End
'*************************************************
On High Interrupt
Save System
'Control buffer serie
If PIR1.RC1IF = 1 Then
Call _ringbuffer()
Endif
Resume
Hi D,
I tried your prog in the SIM, and it works fine.
As mentioned, I'm using the 18F4431 as it has a quadrature encode HW block.
I'll try to modify your prog to fit.
Thanks again.
C
 
The file containing the ring buffer functions appears to be 100% compatible.

'Include "_FuncionesUartRingBuffer.bas"
Hi D,
I now having your CODE working on an 18F4431.
I am workig through to remove the unwanted sections for my project, and need to step through the code to see what's happening. some of your code is in INCLUDES, that I always have difficulty with, but what I want to know id: when stepping through in the SIM, can the includes be viewed stepping through at the same time?

EDIT: I put both INCLUDES at the bottom of the program, so I can step through and view each step.
C
 
Last edited:
The "Includes" are simulated but cannot be seen in the tracking.

That's right, if you want to see them you have to paste them into the program.
 
Last edited:
The "Includes" are simulated but cannot be seen in the tracking.

That's right, if you want to see them you have to paste them into the program.
Hi D,
Ok, thanks.
Looking through your CODE, is educating, as it has many variations in the INCLUDES Procedures etc, that can be used for other projects. I'll have to remove a lot of the Procedures etc, so I can 'contain' the program, else there's too much, for my few cells :0
but and interesting anyway.
C.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top