CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Read characters from uart using RS232
Goto page Previous  1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu May 12, 2016 12:08 am     Reply with quote

Honestly though, 'step back'.

You have no proof, that your chip is actually running, or running at the right rate.
This is why the _first_ program anybody should try is very simple:
Code:

//Have your processor include here
//Have your fuses and oscillator setup here

//Then connect an LED to one pin of the PIC (with a suitable current
//limiting resistor, and define this here
#define LED PIN_xx //whatever pin you are using

void main(void)
{
     while (TRUE)
     {
        output_high(LED);
        delay_ms(500);
        output_low(LED);
        delay_ms(500);
    }
}


It is not worth trying to do anything else, till this works, and the LED _is_ flashing at 1/sec intervals. Nothing else can work, unless the chip is working, and working at the right rate.
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Thu May 12, 2016 12:44 am     Reply with quote

But in the last example I've posted there isn't any getc or get_string, I am only using puts() function and continues without displaying nothing in its url. So I guess that the problem is not the "gets" functions, and also the module is blinking its blue led each second as if each second is sending the string I am sending in the puts() function, si I guess that there must be another issue I am not seeing, and I don't know what could be the problem
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Thu May 12, 2016 12:54 am     Reply with quote

Sorry Ttelmah, I didn't see your post. I will try that first, you're right. I have the same configuration in this pic than in the others pics I have, but this one is new and is the first time I'm using it, so I will try the blinking led example to see if the config is correct.

In the meantime, suppose that this afternoon, when I've tried that, the answer is that it has the correct configuration and is working in a correct frequency. What should be the next step? Because if the blink led example works good, there must not be a problem of the baud rate right?
I should say that the wifi module is configured now at 9600 baud, so the baud rate I've put in the #use rs232() line is correctly. I know that the module works at that baud rate because when I ask AT+BAUD it answers 9600.
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu May 12, 2016 1:26 pm     Reply with quote

Hi,

100% of my PIC / CCS 'C' projects utilize an digital I/O controlled 'Power' LED, and a diagnostic/debugging serial port connection. The first code written for all of my projects is the classic 'blinking' LED program, followed soon after by a 'Hello World' serial data transmission test. If you aren't employing a similar 'startup' protocol with your projects then you are wasting your time!! It's shocking how many people post here about this problem or that without first making sure that their PIC is actually running!!

Seriously, this is really basic, basic hardware/software development!!
_________________
John

If it's worth doing, it's worth doing in real hardware!
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Thu May 12, 2016 2:08 pm     Reply with quote

Maybe this is a stupid question, but when you say that you try first the blink led program, and after the Hello world to check the serial data transmision, you mean that for the second example, you use the serial port RX and TX right?

If you use that, that means that you are connecting your pic with the computer via Serial. I need to make you a question, as it is my first time using the Serial Tx and RX communication, how you do this example? How you connect your pic with the computer?

I've tried one time to build the USB connection between my pic (18F4550) which is able to connect to the USB using the example program that CCS provides, but without success, because of some problems with the USB drivers or something like that.

There is some other way to try this Serial example without using the USB? I've read that I must use a TTL converter or a MAX232 to do it. I have the MAX232 but how can I connect it with the PC without using the USB? Maybe using the PICKIT3 is possible to do it?

Thanks and sorry if the question is stupid =(


edit: After try if the blink led example works fine blinking the led each second, I can discard that the problem is the frequency of the pic, because I have the same configuration using the blink led example and the UART program. So now, I don't know where could be the problem. I am going to try to post an image of how I am connecting everything to check if the problem maybe could the the connections. I need to mention that the wifi module works perfectly because it work fine with my arduino board.


Last edited by lozi_dani on Thu May 12, 2016 2:29 pm; edited 2 times in total
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu May 12, 2016 2:28 pm     Reply with quote

Hi,

No, your question is not stupid at all! Nearly all of my projects utilize the hardware UART to communicate with external hardware (GSM modems, GPS modules, LCD displays, etc.), so these connections aren't generally available for all purpose debugging. Instead, I designate an extra I/O pin as a diagnostic serial output, and the compiler creates the necessary code ('software serial port') to make it work. Whether hardware (the UART) or software, seeing 'Hello World' printed on my terminal gives me confidence that my PIC is (1) operating, and (2) operating at the correct speed!

The PIC hardware generates logic level serial signals which are not compatible with a PC serial port, which uses 'RS232' signal levels. To connect the PIC to the computer you need a RS232 interface IC like the MAX232 device. This device is bidirectional and does the required level translation in both directions (PIC --> PC, and PC --> PIC). There are countless examples of MAX232 circuits available with a quick Google search.
_________________
John

If it's worth doing, it's worth doing in real hardware!
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Thu May 12, 2016 2:32 pm     Reply with quote

Sorry I edited my post too late hehe.

I have a MAX232 to do it, but I dont know how to connect it with my PC. This is the piece I have:

http://electricaltechnology.org/wp-content/uploads/2014/10/What-is-MAX232.png

But I guess that I will need some interface to connect it to my PC right?
It could be so good for me now if there is some way to adapt the PICKIT3 to connect the pic with my PC to be able to send things through Serial


edit: I am seeing that CCS C Compiler has a tool called Serial Port Monitor. I guess that with this tool, and with my pic connected to my computer via the PICKIT3, I will be able to use the UART without using the USB interface, am I right?

Also, I am looking for information of the physical connections of the MAX232 between my PIC and my PC. I've seen this picture:

https://www.cpp.edu/~pbsiegel/picprogs/circuit_232.jpg

I think this connection are good and I can run my UART with the Serial monitor using this connections, but I have not that PC DB9 female. I should change it for my PICKIT3, maybe you know how can I wire that example of connections with my PICKIT3?

Meanwhile, I will be looking for more information and if I find something I will write it here.

I am sure that I will achieve to make work my project, and when I achieve it I will post everything here for others that could use it. Your help is being very important to me. Many many thanks!
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Thu May 12, 2016 3:32 pm     Reply with quote

Ok after look for some information about how to connect the rs232 to the PC, I've seen the solution I think I am going to implement:
I am going to try to connect the rs232 port to the MAX232 driver. the MAX232 driver to a RS232 to TTL adapter like this:

http://img.dxcdn.com/productimages/sku_148963_1.jpg

and this Rs232 adapter to a Serial /TTL to Usb adapter like this:

https://wiki.openwrt.org/_media/doc/hardware/usbtors232.ttl.jpeg

I've not found other simplest solution... =(

I will go tomorrow to buy everything, because as I saw, I need this component and some capacitors that I have not now to try it.

Anyway, if someone has another solution please tell me, because if my solution doesn't works I need to try something different..
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Fri May 13, 2016 8:31 am     Reply with quote

After buy the RS232 TTL/Serial to USB adapter, I am trying to see if something is displayed through the Serial Monitor that CCS C Compiler provides, but I am not able to see nothing.

This is the code I am using:
Code:

#include <UART.h>
#include <input.c> //contains get_string
#include <string.h> //needed for the string functions.
#include <stdlib.h>
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)


int8 dummy;
#define CLEAR_UART() while(kbhit()) dummy=getc()
//#define BUFFER_MAX_SIZE 16
//char datos[BUFFER_MAX_SIZE];


void main()
{   
   while(true){
      puts("enviando datos a web");
      delay_ms(1000);
      CLEAR_UART(); //throw reply away
   }

}


Maybe I'm doing something wrong?

Also I am connecting the serial adapter like this:

PIC Serial to USB adapter
RX --> RX
TX --> TX

I've tried also this:

RX --> TX
TX --> RX

The VCC and GND of the Serial adapter are connected to the same VCC and GND of the pic, and this is the serial adapter I am using:

http://hobbycomponents.com/1444-thickbox_default/ch340-usb-to-ttl-serial-adaptor.jpg

Exactly the same, with the pins in the same order.

Can I be doing something wrong?

Thanks for your help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri May 13, 2016 9:18 am     Reply with quote

Here is the actual webpage for the device. You only posted a photo.
http://hobbycomponents.com/home/586-ch340-usb-to-ttl-serial-adaptor

The website says you have to install drivers.
Quote:

Once you have installed the drivers the module will appear as a
normal serial COM port on your computer making it compatible with most
terminal programs and development environments such as the Arduino IDE.


They have drivers on this page, but apparently you have to be logged in
to their forum to be able to download drivers.
http://forum.hobbycomponents.com/viewtopic.php?f=79&t=1782&hilit=hcmodu0076
Quote:
Attachment:
HCMODU0076_CH340SER_Windows_Drivers.zip

But a little bit of searching shows they are also on Github:
https://github.com/HobbyComponents/CH340-Drivers/tree/master/CH341SER
DRVSETUP64 appears to be the one to get.

There is also a 3.3v / 5v switch on the board. I'm not totally sure what
that does because they don't give a schematic. I think it may just
provide an auxiliary voltage on the Vc pin of the 6-pin header.
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Fri May 13, 2016 9:39 am     Reply with quote

PCM Programmer thanks for your help! But the drivers are not the problem. When I connect this adapted with my PC, it install automatically the drivers and I can see in which port has been installed. So I think that maybe the problem is the way I have connected it or maybe a problem in the code, but I don't know that could be. Do you have some other suggestions??

Thanks a lot!
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri May 13, 2016 10:50 am     Reply with quote

OK a quick test to confirm the TTL<>USB module is working is to...
connect the TX and RX pins of it together. This 'loopback' will allow every PC kBD press to be displayed on the PC screen using a terninal program.
Once you confirm this happens you 1) KNOW the PC is OK, 2 the terminal program is OK and 3 the TTL<>USB module is OK.

Taken 1 step at a time, you have to eliminate possible 'problems'.

Now, after this all works, connect to PIC and see what happens. Rx>Tx; Tx>Rx.

I usually just have the PIC send a 'J' every 500 ms to the PC to confirm the PIC CAN transmit to the PC.

Then I code a simple 'echo' program. The PIC receives a character from the PC and then sends it back.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Fri May 13, 2016 12:01 pm     Reply with quote

You still would not know that it produces a Voh that is high enough for the Vih of the PIC UART.
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Mon May 16, 2016 8:09 am     Reply with quote

Ok! thanks for your replies!

After trying a while what you both are advising me, I achieve to receive something through the TTS<>USB adapter.

I have connected the PIC and the TTL<>USB adapter like this:

RX-TX
TX-RX
Vcc (TTL<>USB) - Vcc (PIC)
GND (TTL<>USB) - GND (PIC)

I am using PUTTY and I am able to see something in the COM port. This is my code now:

Code:

#include <UART.h>
#include <input.c> //contains get_string
#include <string.h> //needed for the string functions.
#include <stdlib.h>
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)

int8 dummy;
#define CLEAR_UART() while(kbhit()) dummy=getc()

void main()
{
   while(true){
      puts("enviando datos a web\n");
      delay_ms(1000);
      CLEAR_UART(); //throw reply away
   }

}


I am trying to get in putty only what is in the puts() function each 1000ms. And this is what I get:

x▒x▒x▒x▒▒xx▒x▒x▒x▒▒xx▒x▒x▒x▒xx▒▒▒xxxxxx▒x

I think could be something about the baud rate, but I am using a baud rate of 9600 in the PIC and in PUTTY, so I am not sure that this could be the reason.

Before this I have tried what you told me, trying a simple blink led to see if the clock is working as it must, and my led blinks each second as i have programmed it.

Maybe is something wrong in the code that could give me this result?

I am excited because I think I am a little bit near of my solution.. at least I am understanding how UART works to understand how to adapt it to my project hehe!


edit: I have also tried just now this:

Code:

#include <UART.h>
#include <input.c> //contains get_string
#include <string.h> //needed for the string functions.
#include <stdlib.h>
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)

#define CLEAR_UART() while(kbhit()) dummy=getc()

void main()
{
     while(true){
         putc(1);
   }
}


And only receive:

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx....

Any idea?
temtronic



Joined: 01 Jul 2010
Posts: 9174
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon May 16, 2016 8:50 am     Reply with quote

replace this..
putc(1);
with this...

putc('J');

J is a printable character while 1 is actually a not printable ASCII code for 'Start Of Header' ( SOH). Find an ASCII Lookup Table or Chart, print it, tape to PC sidewall for quick reference.

As for the 'communication parameters', be sure PIC and PUTYY are the same . 8 data bits, 1 stop, 9066, No parity. Also be sure 'local echo' is turned off in PUTTY.

You're close to sucess... !
Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4  Next
Page 2 of 4

 
Jump to:  
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