|
|
View previous topic :: View next topic |
Author |
Message |
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Fri Feb 27, 2015 9:23 am |
|
|
Ttelmah, i'm sorry:
The definition of the RS232_ERRORS is as follows:
No UART:
· Bit 7 is 9th bit for 9 bit data mode (get and put).
· Bit 6 set to one indicates a put failed in float high mode.
With a UART:
· Used only by get: <<<<<<<<<<<<<<<<<<<<<< !!!!!!
· Copy of RCSTA register except:
· Bit 0 is used to indicate a parity error. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Feb 27, 2015 9:49 am |
|
|
Use this instead then:
Code: |
#use rs232(uart1, baud=9600, bits =9, stop =1, ERRORS, LONG_DATA, stream=str1)
int8 find_parity(int8 data)
{
#asm
swapf data, W
xorwf data, F
rrf data, W
xorwf data, F
btfsc data, 2
incf data, F
movf data, W
andlw 1
movwf _return_
#endasm
}
void fput_chr( char data)
{
int16 to_send;
//add 9th bit as parity to the data
to_send=make16(!find_parity(data),data);
fputc(to_send,str1);
}
//Then you can just use
printf(fput_chr,"What you want to send/n");
//and 9bit data will automatically be sent, and efficiently. :)
|
The RS232_ERRORS route, certainly did work at one time, but it's been probably at least ten years since I had to touch 9bit. I think it was before they added the 'LONG_DATA' option. |
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Fri Feb 27, 2015 10:01 am |
|
|
I will try, but i really like this style
Code: |
void out_spi(char s){
spi_write2(s);
}
///////////////////////
setup_spi2(SPI_MASTER|SPI_MODE_2|SPI_CLK_DIV_64);
delay_ms (500); // give some time to 12f1822
output_low(CS);
delay_ms (1); // mandatory
spi_write2(0x74); //"t"
spi_write2(0x65); //"e"
spi_write2(0x73); //"s"
spi_write2(0x74); //"t"
spi_write2(0x0A); //LF
spi_write2(0x0D); //CR
printf(out_spi, "let's see this\n\r"); // Oooh Yeah
spi_write2(0x78); //"x"
delay_ms (1); // or wait empty flag ?
output_high(CS);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Fri Feb 27, 2015 11:33 am |
|
|
Makes it bulkier, easier to go wrong, and harder to read....
The actual 'out_spi' function is pointless, You can just use spi_write2. The encapsulation is only needed if you want to make some form of change to the data. Hence needed for 9bit. The whole thing just becomes:
Code: |
setup_spi2(SPI_MASTER|SPI_MODE_2|SPI_CLK_DIV_64);
delay_ms (500); // give some time to 12f1822
output_low(CS);
delay_ms (1); // mandatory
printf(spi_write2, "test\n\rlet's see this\n\rx"); // Oooh Yeah
delay_ms (1); // or wait empty flag ?
output_high(CS);
|
|
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Sat Feb 28, 2015 1:51 am |
|
|
I give up. I have not enough brain to understand these #use statements. I guess they are amazing for software communication,
but i can't get working hardware peripheral with them.
Code: |
void out_spi(char s){
spi_write2(s);
}
/////
#use spi(spi2, master, baud= 100000, mode=3, bits=8, stream=my_spi)
setup_spi2(SPI_MASTER|SPI_H_TO_L | SPI_XMIT_L_TO_H|SPI_CLK_DIV_64);
delay_ms (500);
output_low(CS);
delay_ms (1);
printf(out_spi, "test1\n\r"); // this works
//printf(spi_write2, "test1\n\r"); // undefined identifier
printf(my_spi, "test3\n\r"); // something wrong with mode
spi_xfer(my_spi, "test4\n\r"); // something wrong with mode
delay_ms (1);
output_high(CS);
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Feb 28, 2015 2:00 am |
|
|
Look at the thread 'SPI Enable pin'.
You are doing exactly what is causing the problem here. |
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Sat Feb 28, 2015 5:51 am |
|
|
With my minor XP here, i can point what puzzle me in these #use statements.
I had no success to run a hardware spi or uarts on 18F23K22 with them.
1.
#USE SPI (options)
SPI1 Use the hardware pins for SPI Port 1 // Only the pins or whole hw ????
SPI2 Use the hardware pins for SPI Port 2
FORCE_HW Use the pic hardware SPI. // Which one if there are 2 or 4?
result = spi_xfer(stream, data, bits) // looks perfect
2.
This works well and do not require #USE. But require separate spi_write() and spi_read() ???
setup_spi (mode)
setup_spi2 (mode)
3.
#USE RS232 (options)
BITS =X Where x is 5-9 (5-7 may not be used with the SCI).
LONG_DATA Makes getc() return an int16 and putc accept an int16. This is for 9 bit data formats.
Completely unclear for me.
BITS =X ? Which one, transmit, receive or both ? I guess both. But on PIC i can set them separately. I can receive 9 bits and
send 8. Which i am doing in my case.
LONG_DATA ? Do i have to specify the bits beforehand ? Ok, i will check
4.
set_uart_speed (baud, [stream, clock])
Availability: This function is only available on devices with a built in UART.
Requires: #USE RS232 ???
As comparison with SPI i do not expect this, but something as setup_uart2 (baud, bits, [stream]).
Let me clear this. I do not criticize anyone or anything!
The compiler looks great. I wrote working test for 3 days without having any expirience
with C!! In comparison i lost 3 weeks for my real asm code. Imagine printf(fname, cstring, values...) function in assembler
I just ask questions.
fobi
PS How to attach file here ?
http://postimg.org/image/8a1kpxpwh/ |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Feb 28, 2015 8:47 am |
|
|
1) The SPI port on the pins you are using.
2) Just use spi_read(val) to send and receive a value at the same time.
3) No you can't. The UART hardware can only be set to one data format on any selected port. If you are setting two different word lengths, the one will use the hardware, and the other a software implementation. The abilities of the commands are those of the hardware. One port can only have one data format.
4) If you want to change the bits, then just use another #use rs232. The point about set_uart_speed is it allows on the fly changing without stopping the UART |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Feb 28, 2015 9:52 am |
|
|
As a comment, you could save yourself an enormous amount of hassle, by switching up to a PIC24 or above.
Unlike the PIC16/18 chips, these have _hardware_ parity generators, so the parity problems you have seen, would not apply. Settings would still apply to the whole port, but a lot of fiddling would be saved. You could even change parity by just changing the PDSEL bits in UxMODE . |
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Sat Feb 28, 2015 12:06 pm |
|
|
I can receive 9 bits and send 8bits with same PIC hw uart. There is separate TXSTA.TX9 and RCSTA.RX9 bits.
I just wanted to see how looks C code and learn something. I found some benefits and little drawbacks. I choose CCS as good looking and PIC with more peripherals. I choose already written by me assembler code for test. I could choose another mcu or Arduino, FlowCode, CodeWarrior, PicBasic and who knows whatever more. CCS is fine. I have full low level control of the PIC and some ... pamperings here Any time i can write my own functions and use them or convert macros to C functions. I have few free days (i.e. nights ) more and i will fun with this. But my real code will stay in pure assembler. I can not use C only because it works. I have to know how exactly works, because my projects are for customers.
Thank you for the answers, you were very kind.
fobi |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sat Feb 28, 2015 1:53 pm |
|
|
just to let you know...
You can printout the 'filename.lst' and get the complete program in asm. this will allow you to see how CCS codes their functions. Very often they are most efficient code possible.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sat Feb 28, 2015 2:21 pm |
|
|
If you rem out the #nolist near the top of the processor's include file, this will include the core functions as well. |
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Sun Mar 01, 2015 2:18 am |
|
|
Yes, thanks, i do that. I found this :
Code: |
.................... void out_spi2(char s){
.................... spi_write2(s);
*
03A0: MOVF SSP2BUF,W
03A2: MOVFF s,SSP2BUF
03A6: RRCF SSP2STAT,W
03A8: BNC 03A6
.................... }
03AA: GOTO 03C2 (RETURN)
|
Nice. But this reads the buffer before sending.
I would do that in reverse order - read the buffer after sending
Code: |
movff s,SSP2BUF
bf:btfss SSP2STAT.BF
bra bf
movff SSP2BUF,s
|
May be spi_xfer(data) is doing exactly that.
I will write new test to see most of the functions and my questions will fall sharply and become more meaningful :)) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Sun Mar 01, 2015 3:07 am |
|
|
No.
If you want to read the buffer after sending, then use spi_read(val), which will write then read.
They read the buffer before sending, because if something is in the buffer there will be an overflow flagged. Data sheet quote:
"The user must read the SSPBUF, even if only transmitting data, to avoid setting overflow".
To read after sending, you have to wait for the transmission to complete. They don't. They let the hardware get on sending while you can carry on doing other things. The function returns as soon as the byte is loaded. |
|
|
fobi
Joined: 26 Feb 2015 Posts: 15 Location: BG
|
|
Posted: Sun Mar 01, 2015 4:00 am |
|
|
you are right, yes .....
Ttelmah !!!
I found the bug. Parity works ok, but it is confused!
Parity=EVEN mean Parity=ODD.
After Code: |
#use rs232(uart2, baud=9600, bits =8, parity=o, stop =1, stream=cash2 )
fprintf(cash2,"test"); |
I got this Code: |
0464 @PUTCHAR_BIU_2
.................... #use rs232(uart2, baud=9600, bits =8, parity=o, stop =1, stream=cash2, ERRORS )
*
045E: MOVLW 08
0460: MOVWF @01
0462: CLRF @@x8B
0464: INCF @@x8B,F
0466: MOVFF ??65535,00
046A: MOVF @00,W
046C: XORWF @@x8B,F
046E: RRCF @00,F
0470: DECFSZ @01,F
0472: BRA 046A
0474: BTFSS PIR3.TX2IF
0476: BRA 0474
0478: MOVLW FE
047A: ANDWF TXSTA2,F
047C: BTFSC @@x8B.0
047E: BSF TXSTA2.TX9D
0480: MOVFF ??65535,TXREG2
0484: GOTO 049C (RETURN)
....................
|
This calculate parity indeed.
Then i used this Code: |
#use rs232(uart1, baud=9600, bits =9, parity=n, stop =1, stream=cash1 )
#use rs232(uart2, baud=9600, bits =9, parity=n, stop =1, stream=cash2 )
#use rs232(uart2, baud=9600, bits =8, parity=e, stop =1, stream=cash3 )
|
with this calls
Code: |
printf(out_chr2,"test fname1\n\r");
printf(out_chr1,"test fname2\n\r");
fprintf(cash3,"test putc?"); |
And this works LOL :))))))))))))))))))))))))))) |
|
|
|
|
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
|