|
|
View previous topic :: View next topic |
Author |
Message |
acel
Joined: 09 Aug 2018 Posts: 8
|
18F06Q40 for UART settings code example |
Posted: Sun Jun 13, 2021 8:55 am |
|
|
18F06Q40 how should the UART settings be? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sun Jun 13, 2021 11:20 am |
|
|
Key thing is it is a PPS chip.
So look at the sticky at the top of this forum on PIN SELECT.
You should select the pins you want and then talk to the peripheral
by name in the setup rather than using the pin names. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Sun Jun 13, 2021 12:48 pm |
|
|
As for baudrate, bits, etc...... that depends on the OTHER device it has to communicate with. Also you'll need a TTL> RS232 converter, typically a 'MAX232'. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Jun 14, 2021 12:18 am |
|
|
Also as a general comment, when using the hardware UART's, you should
always use 'ERRORS' in the setup.
The PIC UART has a behaviour, that is some characters are received, and
not handled, resulting in a buffer overflow, or an error is detected in the
byte format, so a 'framing error' is detected, it disables the UART, till this
error is cleared. Adding 'ERRORS' adds code to automatically handle this.
Your choices are:
1) Don't have ERRORS, and don't handle the errors yourself. Result UART
can become hung.
2) Don't have ERRORS but add your own handling to the receive code. Very
good and gives you complete control of the error handling. Good, but needs
quite a bit of extra code.
3) Have ERRORS. The compiler will automatically clear the errors,
preventing the UART from hanging. The last error detected will be flagged
in the variable RS232_ERRORS. Can be tested if required. This should be
your default for all normal UART usage.
Not having ERRORS, is asking for that odd occasional 'hang'....
It is a 'peeve' of mine, that so many of the CCS examples don't have this.
Repeat 50* "when using the hardware UART, there needs to be something
to handle any errors"....
They 'get away' with not having it, because using RS232 'on the bench',
it is 99.999+% reliable, so the hang is almost never seen. However if
instead you use something like a wireless link, suddenly problems start
to appear.... |
|
|
MikeW
Joined: 15 Sep 2003 Posts: 184 Location: Warrington UK
|
|
Posted: Mon Jun 14, 2021 3:30 am |
|
|
This is what I used at the start of my "C" main program
#include <18F16Q41.h>
#fuses NOWDT,NOPROTECT,PUT_64MS,NOLVP,NOBROWNOUT
#use delay(clock=4M)
#use i2c(Master,Fast,sda=PIN_B4 ,scl=PIN_B6)
#PIN_SELECT U1RX=PIN_C1
#PIN_SELECT U1TX=PIN_C0
#USE RS232 (UART1, BAUD=9600,STREAM=SeriaLPort1, ERRORS)
then to use it
fprintf(SeriaLPort1,"\n\rStart:\n\r"); |
|
|
acel
Joined: 09 Aug 2018 Posts: 8
|
|
Posted: Mon Jun 14, 2021 7:40 am |
|
|
No rx interrupts occur in this program. sending data, but not receiving data.
Code: | /////////////////////////////////////////////////////////////////////////
//// EX_SISR.C ////
//// ////
//// This program shows how to implement an interrupt service ////
//// routine to buffer up incomming serial data. ////
//// ////
//// If the PIC does not have an RDA interrupt pin, B0 may be used ////
//// with the INT_EXT. ////
//// ////
//// Also see EX_RXBUFFER.C for an example where the compiler ////
//// buffers incoming data. ////
//// ////
//// This example will work with the PCM, PCD, and PCH compilers. ////
//// The following conditional compilation lines are used to include////
//// a valid device for each compiler. Change the device, clock and////
//// RS232 pins for your hardware if needed. ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2003,2018 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////
#include <18F06Q40.h>
//#endif
//#use delay(clock=64Mhz,internal)
#fuses NOEXTOSC //Oscillator external is not enabled
#fuses RSTOSC_HFINTRC_64MHZ //HFINTOSC with HFFRQ = 4 MHz and CDIV = 4:1
#fuses NOCLKOUT //CLKOUT function is disabled; I/O or oscillator function on OSC2
#fuses NOPRLOCK1WAY //bit can be set and cleared multiple times (subject to the unlock sequence)
#fuses CKS
#fuses NOFCMEN
#fuses MCLR
#fuses NOPUT
#fuses NOMVECEN
#fuses NOIVT1WAY
#fuses NOLPBOR
#fuses NOBROWNOUT
#fuses BORV28
#fuses NOZCDDIS
#fuses NOPPS1WAY
#fuses STVREN
#fuses NODEBUG
#fuses NOXINST
#fuses WDT64
#fuses NOWDT
#fuses LVP
#FUSES PROTECT
#use delay(INTERNAL=64M)
#PIN_SELECT U1RX=PIN_C5
#PIN_SELECT U1TX=PIN_C4
#USE RS232 (UART1, BAUD=9600,STREAM=SeriaLPort1, ERRORS)
//#Fuses ECL,ECM,ECH,RSTOSC_HFINTRC_64MHZ
//#use rs232(baud=115200, xmit=PIN_C4, rcv=PIN_C5) //Text through the UART
//#pin_select U1TX = PIN_C4 // U1RXPPS portC 0x3ae5 = 0x17
//#pin_select U1RX = PIN_C5
#define BUFFER_SIZE 80
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#byte PIR3 = 0x39A3
#bit U1RXIF = PIR3.3 // UART1 Receive Interrupt Flag bit.
#byte PIE3 = 0x3993
#bit U1IE = PIE3.6
#bit U1RXIE = PIE3.3
//#byte U1TXB = 0x3dea; // 0x3dea U1TXB: UART TRANSMIT REGISTER
//#byte U1RXB = 0x3de8; // 0x3de8 U1RXB: UART RECEIVE REGISTER
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main() {
U1IE = 1; // PIE3.U1IE, 0x3993.6 = 1. PERIPHERAL INTERRUPT ENABLE REGISTER 3.
// bit 6 U1IE: UART1 Interrupt Enable bit
// 1 = Enabled
// 0 = Disabled
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
printf("\r\n\runing..\r\n");
// The program will delay for 10 seconds and then display
// any data that came in during the 10 second delay
do {
disable_interrupts(INT_RDA);
disable_interrupts(GLOBAL);
delay_ms(1000);
printf("\r\nreceived data => ");
while(bkbhit)
putc( bgetc() );
output_toggle(pin_A1);
delay_ms(1000);
output_low(PIN_A1);
output_toggle(pin_a2);
} while (TRUE);
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Jun 14, 2021 8:03 am |
|
|
Yes they do. You are disabling them....
Look at your code. The very first thing it does after printing the 'running'
message, is to disable this interrupt. It is never enabled again. The interrupt
can never trigger unless a character is received while the 'running' is being
printed...
You are delaying _after_ you have disabled the interrupt.
Your approach is also fundamentally wrong in the ISR., Do not use % buffer
size except with a binary buffer size, and the test for full is wrong. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Mon Jun 14, 2021 10:48 am |
|
|
caution !!
this....
#fuses LVP
may cause you problems, sooner or later, unless your programmer really IS a Low Voltage Programmer....
also I find it better to always use
#fuses PUT
the small delay isn't really noticeable but can insure a more reliable startup
Jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|