18F46K20 HW UART Oshonsoft CODE

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
The PIC I'm using 18F46K20, isn't fully supported by Oshonsoft, including the TX (Not sure about RX?)

In another thread, I have explored settings till I found that a SW UART needs (oddly) PORTD.4 to be set to INPUT. I've searched the net, contacted Microchip, (No reply yet) and I've been on the Microchip forum. They couldn't help either, but one of them did start me with some CODE in C to help me write a HW UART, in Oshonsoft BASIC

Here is is:
It's going to be difficult for me, so any help would be welcome.
I almost understand the lines marked <<<<<<<<<

The lines marked ?????????? will need some thought.
Camerart.

Code:
//Configure EUSART for 9600 baud on an MCU clock of 16MHz
//Mode is asynch 8 bits no parity
'void config_eusart(void)<<<<<<<<<<<<<<<
'{
//Configure I/O pins
TRISC.6 = 1  'triscbits.RC6 = 1;  //TX pin
TRISC.7 = 1  'triscbits.RC7 = 1;  //RX pin

//Configure EUSART peripheral
//=== TXSTA ===
//x--- ---- CSRC - don't care for asynch
//-0-- ---- TX9 - eight bit mode TX
//--0- ---- TXEN - TX disabled for now
//---0 ---- SYNC - Asynch mode
//---- 0--- SENDB - don't send break
//---- -0-- BRGH - low speed
//---- --x- TRMT - don't care is read only
//---- ---x TX9 - don't care in 8 bit mode
'TXSTA = 0x00;
TXSTA.7 = 0
TXSTA.6 = 0
TXSTA.5 = 0
TXSTA.4 = 0
TXSTA.3 = 0
TXSTA.2 = 0
TXSTA.1 = 0
TXSTA.0 = 0

//=== RCSTA ===
//0--- ---- SPEN - disabled for know
//-0-- ---- RX9 - eight bit mode RX
//--x- ---- SREN - don't care in asynch mode
//---0 ---- CREN - RX disabled for know
//---- x--- ADDEN - don't care in 8 bit asynch mode
//---- -x-- FERR - don't care is read only
//---- --x- OERR - don't care is read only
//---- ---x RX9D - don't care is read only
'RCSTA = 0x00;
RCSTA.7 = 0
RCSTA.6 = 0
RCSTA.5 = 0
RCSTA.4 = 0
RCSTA.3 = 0
RCSTA.2 = 0
RCSTA.1 = 0
RCSTA.0 = 0

//=== BAUDCON ===
//0--- ---- ABDOVF - no auto baud
//-x-- ---- RCIDL - don't care is read only
//--0- ---- DTRXP - data receive normal polarity
//---0 ---- CKTXP - transmit polarity normal (idle high)
//---- 0--- BRG16 - 8 bit baud rate generator
//---- -x-- don't care is unimplemented
//---- --0- WUE - normal operation
//---- ---0 ABDEN - no auto baud
'BAUDCON = 0x00;
BAUDCON.7 = 0
BAUDCON.6 = 0
BAUDCON.5 = 0
BAUDCON.4 = 0
BAUDCON.3 = 0
BAUDCON.2 = 0
BAUDCON.1 = 0
BAUDCON.0 = 0

//Configure baud rate generator for 9600 baud on a 16MHz MCU clock
'SPBRG = 25;  'NEEDS WORKING OUT??????????????????????
'BRGH = 0;'NEEDS WORKING OUT?????????????????????
'}

//Enable transmitter
'void enable_tx(void)<<<<<<<<<<<<<<<<<
'{
TXSTA.5 = 1  'TXEN = 1;<<<<<<<<<<
RCSTA.7 = 1  'SPEN = 1;<<<<<<<<<<<<<<<<<<<<<<<
'}

//Disable transmitter
'void disable_tx(void)<<<<<<<<<<<<<<<<<<<
'{
TXSTA.5 = 0  'TXEN = 0;<<<<<<<<<<<<<<<<<<<<<
'}

//Enable receiver
'void enable_rx(void)<<<<<<<<<<<<<<<<<<<
'{
RCSTA.4 = 1  'CREN = 1;<<<<<<<<<<<<<<<
RCSTA.7 = 1  'SPEN = 1;<<<<<<<<<<<<<<<<<<<
'}

//Disable receiver
'void disable_rx(void)'<<<<<<<<<<<<<<<<<<<<<<<<<<
'{
RCSTA.4 = 0  'CREN = 0;'<<<<<<<<<<<<<<<<<<<
'}

//Enable EUSART - receiver and transmitter controlled by CREN and TXEN
'void enable_eusart(void)
'{
RCSTA.7 = 1  'SPEN = 0;'?????????????????????????
'}

//Disable EUSART - stops receiver and transmitter
'void disable_eusart(void)<<<<<<<<<<<<<<<<<<<<<<<<<<
'{
RCSTA.7 = 0  'SPEN = 0;<<<<<<<<<<<<<<<<<<<<<<<
'}

//Check for a received byte
//Return: 1 - A received byte is available to be fetched
//0 - No received byte available
'int rx_byte_available()'????????????????????????????
'{
'If(pir1bits.RCIF)'????????????????????
'{
'Return 1;  //data available'???????????????????
'}
'Else'<<<<<<<<<<<<<<<<<<<<
'{
'Return 0;  //no data available'<<<<<<<<<<<<<<<<<<<
'}
'}

//Fetch a received byte
//Parameter: *c - pointer to a buffer for the received byte
//Return: 1 - Valid byte received
//0 - No valid byte received
'int get_rx_byte(unsigned char * C)'????????????????
'{
'unsigned char tempc;'?????????????????????

'If(rcstabits.OERR == 1)'?????????????????
'{
//Overrun error - clear
'rcstabits.CREN = 0;'????????????????????????
'nop() ;?????????????????????????????
RCSTA.4 = 1  'rcstabits.CREN = 1;'<<<<<<<<<<<<<<<<<<<<<<<<
'Return 0;  //No valid byte has been received'?????????????????
'}

'While(rcstabits.FERR == 1)'?????????????????????
'{
'tempc = RCREG;  //reject bytes with framing error'????????????????????
'}

'If(pir1bits.RCIF)  //Check if there is any byte available'?????????????
'{
'tempc = RCREG;  //return valid byte'?????????????????????
'* C = tempc;???????????????????
'Return 1;  //a valid byte has been received       '????????????????????
'}
'Return 0;  //no valid byte received               '???????????????
'}

//Send a byte
//Parameter: c - byte to be sent
//Return: 1 - Byte sent
//0 - Transmitter busy
'int send_tx_byte(unsigned char C)'????????????????
'{
'If(pir1bits.TXIF == 1)'????????????????????????
'{
//Wait for full an empty transmit shift register (errata workaround)
'While(txstabits.TRMT == 1) ;'??????????????????
'TXREG = C;'????????????????????
'Return 1;  //byte sent '??????????????????
'}
'Return 0;  //transmitter busy    '?????????????
'}
.
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
Here's todays iteration:

I'm checking each 'C' question, then changing it to OSH BASIC bit by bit.
It's compiling, but there's a lot not done yet.
I don't know if it's possible to use the simulator on it?
Quite a puzzle.
C


Code:
'18f46k20 8mHz INT PCB_8 HW_UART 171221 0900
Define CONFIG1L = 0x00
Define CONFIG1H = 0x08  '2=8mHz XTL  '6=8mHz XTL x4=32'8=INT 8mHz
Define CONFIG2L = 0x1e
Define CONFIG2H = 0x00
Define CONFIG3L = 0x00
Define CONFIG3H = 0x83
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 = 8

OSCCON = %01110010  'internal 8Mhz clock
'IO Config

Include "Main MCU config.bas"  '[[[ DONT USE THESE INCLUDEs IN DAVES PROGRAMS ]]]
Include "Peripheral setup.bas"

'Init LATx flip flops before TRIS regs
    LATA = latainit
    LATB = latbinit
    LATC = latcinit
    LATD = latdinit
    LATE = lateinit

    TRISA = trisainit
    TRISB = trisbinit
    TRISC = triscinit
    TRISD = trisdinit
    TRISE = triseinit
    
'START UP LEDS
yled = 1
    WaitMs 1000
    yled = 0
    WaitMs 1000
    yled = 1
    WaitMs 1000
    yled = 0
    WaitMs 1000

PIR1 = 0
PIR2 = 0
PIE1 = 0
PIE2 = 0
IPR1 = 0
IPR2 = 0

AllDigital

Define SEROUT_DELAYUS = 1000

Const PR2set = 125  'use this for the live board: gets 5ms per interrupt
'Const PR2set = 2  'use this for fast timing to speed up simulator
'Define SIMULATION_WAITMS_VALUE = 1  'Comment in for SIM out for PIC

Dim tempc As Byte  '??????????

//Configure EUSART for 9600 baud on an MCU clock of 16MHz
//Mode is asynch 8 bits no parity
'void config_eusart(void)<<<<<<<<<<<<<<<
'{
//Configure I/O pins
TRISC.6 = 1  'triscbits.RC6 = 1;  //TX pin
TRISC.7 = 1  'triscbits.RC7 = 1;  //RX pin

//Configure EUSART peripheral
TXSTA.7 = 0  'CSRC DONT CARE
TXSTA.6 = 0  'TX9 = Selects 8-bit transmission
TXSTA.5 = 0  'TXEN = Transmit disabled
TXSTA.4 = 0  'SYNC = Asynchronous mode
TXSTA.3 = 0  'SENDB = Sync Break transmission completed
TXSTA.2 = 0  'BRGH = Low speed
TXSTA.1 = 0  'TRMT DONT CARE
TXSTA.0 = 0  'TX9D DONT CARE 8 BIT MODE

RCSTA.7 = 0  'SPEN = Serial port enabled (configures RX/DT and TX/CK pins as serial port pins),  0 = serial port disabled (held in reset)
RCSTA.6 = 0  'RX9 = Selects 8-bit reception
RCSTA.5 = 0  'SREN = DONT CARE
RCSTA.4 = 0  'CREN - 0 = Disables receiver
RCSTA.3 = 0  'ADDEN - DONT CARE
RCSTA.2 = 0  'FERR - DONT CARE
RCSTA.1 = 0  'OERR - DONT CARE
RCSTA.0 = 0  'RX9D - DONT CARE

BAUDCON.7 = 0  'ABDOVR - NO AUTOBAUD
BAUDCON.6 = 0  'RCIDL - DONT CARE
BAUDCON.5 = 0  'DTRXP - 0 = Receive data (RX) is not inverted (active-high)
BAUDCON.4 = 0  'CKTXP - 0 = Idle state for transmit (TX) is high
BAUDCON.3 = 0  'BRG16 - 0 = 8-bit Baud Rate Generator is used (SPBRG)
BAUDCON.2 = 0  'UNIMP - DONT CARE
BAUDCON.1 = 0  'WUE - 0 = Receiver is operating normally
BAUDCON.0 = 0  'ABDEN - 0 = Auto-Baud Detect mode is disabled

//Configure baud rate generator for 9600 baud on a 16MHz MCU clock
'SPBRG = 25;  'NEEDS WORKING OUT??????????????????????
'BRGH = 0;'NEEDS WORKING OUT?????????????????????
'}

//Enable transmitter
'void enable_tx(void)<<<<<<<<<<<<<<<<<
TXSTA.5 = 1  'TXEN = 1;<<<<<<<<<<
RCSTA.7 = 1  'SPEN = 1;<<<<<<<<<<<<<<<<<<<<<<<

//Disable transmitter
'void disable_tx(void)<<<<<<<<<<<<<<<<<<<
TXSTA.5 = 0  'TXEN = 0;<<<<<<<<<<<<<<<<<<<<<

//Enable receiver
'void enable_rx(void)<<<<<<<<<<<<<<<<<<<

RCSTA.4 = 1  'CREN = 1;<<<<<<<<<<<<<<<
RCSTA.7 = 1  'SPEN = 1;<<<<<<<<<<<<<<<<<<<

//Disable receiver
'void disable_rx(void)'<<<<<<<<<<<<<<<<<<<<<<<<<<

RCSTA.4 = 0  'CREN = 0;'<<<<<<<<<<<<<<<<<<<

//Enable EUSART - receiver and transmitter controlled by CREN and TXEN
'void enable_eusart(void)

RCSTA.7 = 1  'SPEN = 0;'?????????????????????????

//Disable EUSART - stops receiver and transmitter
'void disable_eusart(void)<<<<<<<<<<<<<<<<<<<<<<<<<<

RCSTA.7 = 0  'SPEN = 0;<<<<<<<<<<<<<<<<<<<<<<<

//Check for a received byte
'Return:  '1 - A received byte is available to be fetched KEYWORD????????
//0 - No received byte available
'int rx_byte_available()'????????????????????????????

If PIR1.5 = 1 Then  'RCIF = 1  'If(pir1bits.RCIF)'????????????????????

'Return 1;  //data available'???????????????????
'Else'<<<<<<<<<<<<<<<<<<<<
'Return 0;  //no data available'<<<<<<<<<<<<<<<<<<<
Endif  '???????????????


//Fetch a received byte
//Parameter: *c - pointer to a buffer for the received byte
//Return: 1 - Valid byte received
//0 - No valid byte received
'int get_rx_byte(unsigned char * C)'????????????????

'unsigned char tempc;'?????????????????????

If RCSTA.1 = 1 Then  'If(rcstabits.OERR == 1)'?????????????????

//Overrun error - clear
RCSTA.4 = 0  'rcstabits.CREN = 0;'????????????????????????
WaitUs 100  'shorten  'nop() ;?????????????????????????????
RCSTA.4 = 1  'rcstabits.CREN = 1;'<<<<<<<<<<<<<<<<<<<<<<<<
'Return 0;  //No valid byte has been received'?????????????????
Endif

While RCSTA.2 = 1  'While(rcstabits.FERR == 1)'0 or 1  ?????????????????????
Wend

'tempc = RCREG;  //reject bytes with framing error'????????????????????



If PIR1.5 = 1 Then  'If(pir1bits.RCIF)  //Check if there is any byte available'?????????????

'tempc = RCREG;  //return valid byte'?????????????????????
'* C = tempc;???????????????????
'Return 1;  //a valid byte has been received       '????????????????????

'Return 0;  //no valid byte received               '???????????????

Endif  '???????????

//Send a byte
//Parameter: c - byte to be sent
//Return: 1 - Byte sent
//0 - Transmitter busy
'int send_tx_byte(unsigned char C)'????????????????

If PIR1.4 = 1 Then  'If(pir1bits.TXIF == 1)'????????????????????????

//Wait for full an empty transmit shift register (errata workaround)
'While TXSTA.1 = 1 Then  'While(txstabits.TRMT == 1) ;'??????????????????
'TXREG = C;'????????????????????
'Return 1;  //byte sent '??????????????????

'Return 0;  //transmitter busy    '?????????????
Endif
main:


Goto main
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
As I'm working through the C CODE, I notice REGISTERS etc that I've come accross before. e,g, RCREG rings a bell. I searched my programs, and found similar CODE, for READing the $sentences from a GPS.
C.
 
Last edited:

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
As mentioned above, I'm trying to write HW-UART CODE for an 18F46K20 PIC, that isn't fully supported by Oshonsoft.
I'm about to test what I've done so far.

Will DATA be shown in the Oshonsoft Simulator (Hardware UART) or must it be 'live'?
C
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
I did this long hand, next I've got to figure out how to send text with DATA. I think this is where CHAR comes in?
If anyone has BASIC routines for this, please let me know.
C
 

Attachments

Thread Starter

camerart

Joined Feb 25, 2013
2,979
hi C,
I don't follow what you are asking for.?
E
Hi E,
To recall: Vlad doesn't support 18F46K20 EUSART, so I'm trying to write one in software to the HW. (very confusing!)
Anyway, as you can see in #8, I've managed to type in ASCII long hand, but now I've got to 'automate' it. so that I can type [ HSEROUT "DATA =",crlf ] and send DATA as before, if that's possible.

We have previously done similar using CHAR, but this has always been RECEIVE, so TX is kind of backwards.
C
 

ericgibbs

Joined Jan 29, 2010
15,378
hi,
We seem to have some confusion, I have a rough idea of what you are wanting to do, I just want to see the bit of Code you used for post #8.

To be sure, are you trying to transmit data OUT of the PIC or into the PIC and expect it to show in an IDE window.
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
hi,
We seem to have some confusion, I have a rough idea of what you are wanting to do, I just want to see the bit of Code you used for post #8.

To be sure, are you trying to transmit data OUT of the PIC or into the PIC and expect it to show in an IDE window.
Hi E,
I'm trying to transmit "DIGITS" + DATA from the PIC, in order to replicate the missing HW UART. HSEROUT.
I've posted the CODE in #8
C
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
The modified CODE from above is now sending DATA. (This is software controlling the PIC hardware)
At the moment it's a bit too messy to post it!

How do I add CRLF to each message?

[ sMessage = "TEST" ]

C
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
msg="TEST"+CRLF
Hi J,
I sometimes use different forums, for information, and this time got help from Microchip forum. The VARIABLES were written by one of the members, so I left them, but as I 'tidy' up I'll change them to more familiar ones.
C.
 

Thread Starter

camerart

Joined Feb 25, 2013
2,979
Hi,
With the program in #18 on an 18F46K20, I can SEND Decimal WORDS and DATA from a computer terminal, via radio link, and the PIC ECHOs the same.

I now have a BASE unit and a REMOTE unit set-up and want to send WORDS and DATA between them. e.g. (As HSEROUT) [ "$BASE ", #data, crlf ]
any ideas please.
C
 
Top