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 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: 19368

View user's profile Send private message

PostPosted: Tue May 10, 2016 1:37 pm     Reply with quote

Good start.
Comments inline:
Code:

#include <UART.h>
#device PASS_STRINGS=IN_RAM
//This may need to be inside UART.h. Depends what this contains
//This is needed to allow the PIC to handle strings in ROM like strings
//in RAM
#use delay(clock=20000000)
//We don't know that this is right, depends on your chip, and do
//you have a 20MHz crystal attached?.
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)
//When using the hardware UART, always use ERRORS. Otherwise if
//a couple of characters get missed, the UART [u]will[/u] hang.

//Always do the delay, and UART setup's before including anything
//other than processor setup stuff
#include <input.h> //contains get_string
#include <stdlib.h>
#include <string.h> //needed for the string functions.

#define BUFFER_MAX_SIZE 16
char datos[BUFFER_MAX_SIZE];
int8 dummy;

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

void main(void)
{
   delay_ms(500);
   //PIC's wake up very quickly. May be too fast for your modem
   //better to leave a little delay.
   //configuracion del wifi
   puts("AT+CWMODE=3\r\n");
   //At this point the modem _will_ send a reply. If you don't read this
   //without ERRORS this will hang the UART.
   delay_ms(1000); //CCS allows you to delay in mSec or uSec
   CLEAR_UART(); //throw reply away
   puts("AT+CIPMUX=1\r\n");
   delay_ms(1000); //as above
   CLEAR_UART();
   puts("AT+CIPSERVER=1,80\r\n");
   //You now want to get the reply, delaying will miss this....
       
   while(true){
      get_string(datos, BUFFER_MAX_SIZE);
      if (strstr(datos,"P1")!=NULL)
      {
           //here "P1" was in the string

      }
      //if P1 is in datos, then motor1 = high
      //else if P2 is in datos, then motor2 = high
      //else if P3 is in datos, then motor1 = low and motor2 = low
   }
}


This assumes your control strings do end with a line feed (get_string requires this)
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Tue May 10, 2016 1:51 pm     Reply with quote

Many many thanks Ttelmah!

I am going to try it now! But let me ask you a little stupid thing, The puts() function is right in my code? I mean, that function, in the same way I have it in my code, is working good?

Now, while i am trying to understand how get_string() and puts() works, I have tried to do this:

Code:

#include <18F4550.h>
#include <stdlib.h>
#use delay(clock=20000000)
#use RS232(BAUD=9600, XMIT=PIN_C6, RCV=PIN_C7)

#define BUFFER_MAX_SIZE 16

char datos[BUFFER_MAX_SIZE];

void main()
{

   //configuracion del wifi
   puts("AT+CWMODE=3\r\n");
   delay_ms(1000);
   puts("AT+CIPMUX=1\r\n");
   delay_ms(1000);
   puts("AT+CIPSERVER=1,80\r\n");
   delay_ms(1000);
   
   while(true){
      //get_string(datos, BUFFER_MAX_SIZE);
      puts("sending data to web");
      delay_ms(1000);
   }

}


Only i am trying to see in the IP address of my wiFi module each 1 second the string "sending data to web". This should works?

About the clock, I have a 20MHz crystal and I've configured the fuses as Divided by 5 to have the 20MHz of the external clock hehe, I forget to specify that.

This part
Code:

 if (strstr(datos,"P1")!=NULL)


Is very important to me. As I can understand, if it is != NULL, that means that there is a match. I don't care where is placed in the string and nothing like that, only to know if there is a match, and with this now I understand how I can get it!
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Tue May 10, 2016 1:57 pm     Reply with quote

If you have it divided by 5, then your clock rate will be 4MHz.

Tell us what your chip actually is, and show your fuses.
Clock needs to match what is actually being used inside the chip, or nothing serial will work.

You can use puts, or printf. Printf allows you to include values that change (variables). Hence it is much more common to use this. Both will work.
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Tue May 10, 2016 2:04 pm     Reply with quote

Sorry, I am wrong, I wrote too fast. I am using external clock, so I am using HS fuse to use it. my pic is 18F4550.

I am trying this now:


Code:

#include <UART.h>
#include <input.h> //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];
 

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


void main()
{
   delay_ms(500);
   //configuracion del wifi
   puts("AT+CWMODE=3\r\n");
   delay_ms(1000);
   CLEAR_UART(); //throw reply away
   puts("AT+CIPMUX=1\r\n");
   delay_ms(1000);
   CLEAR_UART(); //throw reply away
   puts("AT+CIPSERVER=1,80\r\n");
   
   
   while(true){
      //get_string(datos, BUFFER_MAX_SIZE);
      puts("enviando datos a web");
      delay_ms(1000);
   }

}


But I get the error: input.h file can not be opened. I am looking for this library but I haven't in my drivers. Maybe I have to download it from somewhere?

edit: Sorry, I've notice that I have a file called input.c, and I've also seen that there are few examples where people use #include <input.c> instead of #include <input.h>

I am going to try when I arrive at home this afternoon because i am at work now, but when i've tried I will put the results.

In the meantime, you know Ttelmah if when you refer to input.h, you want to say input.c? I've been looking the code of input.c file and it has a get_string() function:

Code:

void get_string(char* s, unsigned int8 max) {
   unsigned int8 len;
   char c;

   --max;
   len=0;
   do {
     c=getc();
     if(c==8) {  // Backspace
        if(len>0) {
          len--;
          putc(c);
          putc(' ');
          putc(c);
        }
     } else if ((c>=' ')&&(c<='~'))
       if(len<=max) {
         s[len++]=c;
         putc(c);
       }
   } while(c!=13);
   s[len]=0;
}


Is this the function I need to be able to run my code? I guess it is, but only to confirm.
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

View user's profile Send private message

PostPosted: Wed May 11, 2016 12:54 am     Reply with quote

They call it input.c

It's a bit of an 'annoyance', that for some completely unknown reason, they call a lot of their include files .c, rather than .h.....
lozi_dani



Joined: 22 Jan 2016
Posts: 40

View user's profile Send private message

PostPosted: Wed May 11, 2016 4:30 pm     Reply with quote

Ok now I am trying this simple example only to see if my PIC send something to the url of the wifi module:

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()
{
   delay_ms(500);
   //configuracion del wifi
   puts("AT+CWMODE=3\r\n");
   delay_ms(1000);
   CLEAR_UART(); //throw reply away
   puts("AT+CIPMUX=1\r\n");
   delay_ms(1000);
   CLEAR_UART(); //throw reply away
   puts("AT+CIPSERVER=1,80\r\n");
   
   
   while(true){
      //get_string(datos, BUFFER_MAX_SIZE);
      puts("enviando datos a web");
      delay_ms(1000);
   }

}


But it isn't works. Maybe is something wrong in my code?

My wifi module, the ESP8266-01, has a little blue led which shows me if something is sending or receiving through the TX/RX pins. I can see how each second the blue led blinks as if something is sended or received, so I understand that there is some information going through the UART, but when I put in the browser the IP address, I am not able to see anything. The web says that it cannot open the page, as if nothing was connected there. I've done a ping using the CMD and that IP address sends me a response, so i guess that the module is active.

Maybe could I am doing something wrong in the code? Or maybe should I check other things to see why is not working?

Thanks a lot!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed May 11, 2016 5:18 pm     Reply with quote

this line
Code:

c=getc();

can hang the execution waiting for a character if none has arrived. it is a primary reason you should not use get_string() if you want a reliable program.
temtronic



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

View user's profile Send private message

PostPosted: Wed May 11, 2016 6:03 pm     Reply with quote

There is another 'workaround' for a PIC 'hanging' forever waiting for a string to come...
CCS has an example of a 'timed' input where either the string IS received OR a 'timeout' has occurred. This 'timeout' generally is 2X the time it takes for the PIC to normally receive the string.
I think this example is in the FAQ section of the manual...

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19368

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!
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 1, 2, 3, 4  Next
Page 1 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