View previous topic :: View next topic |
Author |
Message |
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
UART on PIC16F688 |
Posted: Mon Jun 28, 2004 10:06 am |
|
|
Hi, I'm new to both PIC and CCS. I have a problem with using the UART transmission working. I use a 20 MHz oscillator, the delay is working fine (verified), the hardware seems OK, I toggled the pin 5V - 0V with a square wave using the delay and everything was right. Now, I try to use the same pin for a uart and the idle level is 1.2 V. The signal for the byte I send is also different from what I expect. Here is the code I use:
Code: | #include <16F688.h>
#fuses EC,NOWDT,NOPROTECT,BROWNOUT,NOMCLR,NOCPD,NOWDT,NOPUT,NOIESO,BROWNOUT_NOSL
#use standard_io(C)
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C5, rcv=PIN_C4)
void main() {
do
{
delay_ms(50);
putc( 'A' );
} while (TRUE);
} |
If anyone knows what I'm doing wrong, please tell me.
Thank you,
Simon Martineau |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2004 10:59 am |
|
|
If you look at the Pin Diagram in the 16F688 data sheet, it shows
the following labels for the pins connected to the hardware USART:
RC5/RX/DT
RC4/C2OUT/TX/CK
This means the transmitter (TX) is on pin C4, and the receiver (RX)
is on pin C5. But you have them reversed in your sample program.
To use the hardware USART, you should change your #use rs232()
statement to be like this:
#use rs232(baud=9600, xmit=PIN_C4, rcv=PIN_C5)
-------------------
You should also make sure that your RS232 level translator chip
(MAX232, etc) is connected to the PIC and to your PC correctly. |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Mon Jun 28, 2004 11:22 am |
|
|
Hi,
1 - I tried to put TX on RC4 and RX on RC5 as described in the datasheet but my scope showed nothing on Pin #6 (RC4): 1.2V constant. Then, I thought that the TX and RX names might be seen from the PC or something like that, I tried to invert it. Then, I saw 1.2V with a small glitch (not the data byte) each 50 ms. I thought I was closer to my goal but maybe not.
2- I currently use a probe on the pin #6, so there is no need for level translator. I'm not connected to anything, I removed the load to be sure that was not the problem and I'm looking right on the pin.
Thank you,
Any other idea ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2004 12:04 pm |
|
|
I took a little bit closer look at the data sheet, and I can see
that this is the typical PIC with a comparator module.
I then looked at the .LST file to check the startup code.
For PCM vs. 3.202, the startup code is a total mess for
the A/D converters. It's just totally wrong.
I have made some changes to your demo program
that will probably allow it to work now. The changes
are shown in bold:
#include <16F688.h>
#fuses EC,NOWDT,NOPROTECT,BROWNOUT,NOMCLR,NOCPD,NOWDT,NOPUT,NOIESO,BROWNOUT_NOSL
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C4, rcv=PIN_C5)
#byte ADCON0 = 0x1F
#byte ANSEL = 0x91
void main()
{
ANSEL = 0; // Configure all A/D inputs as Digital
ADCON0 = 0; // Disable the ADC
setup_comparator(NC_NC_NC_NC); // Disable the comparators
do
{
delay_ms(50);
putc( 'A' );
}while (TRUE);
} |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Mon Jun 28, 2004 12:28 pm |
|
|
I cut and paste your program, compile it and it still do the same thing. On the other hand, if I try to use another pin (software UART), then it works but I don't know why.
Thank you. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2004 12:48 pm |
|
|
Quote: | I cut and paste your program, compile it and it still do the same thing |
I looked at the code produced by PCM vs. 3.202 for the hardware
USART in a 16F688. It's all messed up.
The startup code is not using the proper register addresses
for the USART registers. Basically, it's using the addresses
for the 16F877 for the 16F688. But they're all different.
It can't possibly work.
I don't really want to beat my way through all of CCS's messed
code for this PIC. Maybe if I have time later in the day, I could
fix it for you. Who knows how many problems vs. 3.202 has ?
One million problems ? |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Mon Jun 28, 2004 12:53 pm |
|
|
Are you a simple customer as I am or do you work for CCS ? Because if you are just a user as I am, maybe you are not the one who should have to do that.
Anyway, thanks a lot for your time.
Simon Martineau |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jun 28, 2004 12:55 pm |
|
|
I'm a customer of CCS. I don't work for them. |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Tue Jun 29, 2004 8:19 am |
|
|
I tried redoing it without using the CCS templates, by directly accessing the registers but it still doesn't work.
If anyone can get a look at the code, I zipped the project at:
ftp://209.148.86.214
The important part is in the Init.c file ( I think the problem is there) so I include it's code here:
Code: | void Init(void)
{
ANSEL = 0; // Configure all A/D inputs as Digital
ADCON0 = 0; // Disable the ADC
VRCON = 0; // Disable voltage reference
Init_UART_9600();
CMCON0=0x00; // Disable the comparators
}
void Init_UART_9600(void)
{
RCSTA=0x90;
BAUDCTL=0x08;
SPBRG=0x08;
SPBRGH=0x02;
TXSTA=0x06;
} |
The clock is at 20 MHz. Anyway, my problem is not a bad baud rate but no output data at all.
Thank you,
Simon Martineau |
|
|
Ttelmah Guest
|
|
Posted: Tue Jun 29, 2004 8:56 am |
|
|
Simon Martineau wrote: | I tried redoing it without using the CCS templates, by directly accessing the registers but it still doesn't work.
If anyone can get a look at the code, I zipped the project at:
ftp://209.148.86.214
The important part is in the Init.c file ( I think the problem is there) so I include it's code here:
Code: | void Init(void)
{
ANSEL = 0; // Configure all A/D inputs as Digital
ADCON0 = 0; // Disable the ADC
VRCON = 0; // Disable voltage reference
Init_UART_9600();
CMCON0=0x00; // Disable the comparators
}
void Init_UART_9600(void)
{
RCSTA=0x90;
BAUDCTL=0x08;
SPBRG=0x08;
SPBRGH=0x02;
TXSTA=0x06;
} |
The clock is at 20 MHz. Anyway, my problem is not a bad baud rate but no output data at all.
Thank you,
Simon Martineau |
In your original post, you said that the "signal for the byte I send is also different from what I expect". Now you are seeing nothing?.
Assuming the original code was sending something, what were you seeing?. When you said the 'idle level is 1.2v', was this before you accessed the UART (you might see this is the pin was floating, and the UART hadn't been selected yet)?.
Best Wishes |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Tue Jun 29, 2004 9:09 am |
|
|
Quote: | In your original post, you said that the "signal for the byte I send is also different from what I expect". Now you are seeing nothing?.
Assuming the original code was sending something, what were you seeing?. When you said the 'idle level is 1.2v', was this before you accessed the UART (you might see this is the pin was floating, and the UART hadn't been selected yet)?.
Best Wishes |
What I was seeing on the pin in my original post was with the RX and TX pins inverted. The software UART was working and the data was sent on the RX pin (I had inverted the pins, my mistake). What I was seeing on the TX pin was noise from the other pin that was transmitting. Nothing more.
For the 1.2V, I had it all the time with the old code. No logic ones or zeros, only that !*"/*"%$?* of 1.2V. Now with the new code (accessing directly the registers) I have 3.5 V on the pin, which is not better. I also don't have a signal nor any glitch on the pin since I put back TX on RC4 to use hardware UART (what I always wanted to do).
If you looked at the code I posted you can also see that I forgot to put the TRIS for port C. I corrected that mistake by adding the following:
Code: | TRISC = 0xEF; // Set RC4 as output |
in the Init function but I still have the same problem.
Thank you,
Simon Martineau |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Tue Jun 29, 2004 10:03 am |
|
|
I changed my TRISC value for 0xFF since I read on the datasheet that it has to be 1 for both TX and RX. Anyway, it changes absolutely nothing, it still don't work. |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Wed Jun 30, 2004 8:46 am |
|
|
No one has ever used the hardware UART on a PIC16F688 ???
If anyone has done it, I would really like to have a code example. I've tried different things in the last three days but nothing worked.
Simon Martineau |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 01, 2004 12:50 pm |
|
|
Your USART init code is not correct. It should be like this for a 20 MHz
crystal with the 16F688:
Code: | void Init_UART_9600(void)
{
RCSTA = 0x90;
BAUDCTL = 0x40;
SPBRG = 0x81;
SPBRGH = 0x00;
TXSTA = 0x26;
} |
------------------------------
Also, your method of disabling the comparators is not correct.
The data sheet says that you should write 0x07 to CMCON0
to disable the comparators. Example:
CMCON0 = 0x07;
-------------------------------
You will also need to create your own putc() and getc() functions.
I will show you how to make the putc() function:
#byte PIR1 = 0x0c
#bit TXIF = PIR1.1
#byte TXREG = 0x15
void my_putc(char c)
{
while(~TXIF);
TXREG = c;
} |
|
|
Simon Martineau
Joined: 28 Jun 2004 Posts: 10 Location: Montreal
|
|
Posted: Mon Jul 05, 2004 7:22 am |
|
|
It works !
Thank you very mutch.
Simon Martineau |
|
|
|