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

Reset the microcontroller on the WDT
Goto page Previous  1, 2, 3  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jeremiah



Joined: 20 Jul 2010
Posts: 1314

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 6:14 am     Reply with quote

qwwe wrote:
Thank you for this code
In your opinion, these lines:
Code:

int t;
t=getc();

The output of the getc() function is of type character and (t) of type int
Does not this make a problem?

It's not really a problem. On your platform they are the same size and on other platforms int is bigger, so it will all work out. It's definitely better form to use char for t, but I'm sure it was just a quick typo is all.

qwwe wrote:

But another question
In the following code when it returns.
Returns to the beginning of the routine interrupt.
Code:

if (t==0xA)
 return;  //Throw LF



It exits the ISR and skips the rest of the code in it. Basically it says if t is 0x0A, stop what you are doing and leave the interrupt.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 7:04 am     Reply with quote

jeremiah wrote:

It's definitely better form to use char for t, but I'm sure it was just a quick typo is all.


It is, I believe because in the example its a temporary variable used in two contexts. In the first half of the routine its used to hold the character from the serial port, i.e. a char, in the second half it's used as a (small) integer to hold the buffer index. Int works fine for both. C is nowhere near as fussy about data typing as many later languages. That was a deliberate language design decision and enables C to be code-efficient and flexible, especially for the systems programming for which it was originally developed. That efficiency is arguably what has made it so suited for embedded programming, and therefore what keep C alive today, long after most other of its language contemporaries have long fallen by the wayside. It also allow programmers to get themselves into deep trouble...

I'd probably have had two variables: they are locals after all and would have had little or no impact on overall memory use, but then without a data stack, as is the case on most PICs, locals in interrupt routines have to be allocated in RAM so that they are always available. On most other architectures, i.e. with a stack, they'd simply be put on the stack.
newguy



Joined: 24 Jun 2004
Posts: 1899

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 7:17 am     Reply with quote

RF_Developer wrote:
C is nowhere near as fussy about datatyping as many later languages.


I worked with a software developer years back who did all our custom PC software and a lot of our Linux stuff. He'd constantly ride me about putting strings into an array of int8[]. I finally asked him where those characters are actually stored? What's the difference between that data and a number? When he finally realized that the PIC is just moving bits around, and it doesn't care what those bits actually are, he left me alone.

The one thing I couldn't get him to realize is that, in an embedded system (no OS), globals aren't a bad thing. He rewrote a family of projects to be compliant with modern sw design theory (objects, get() & set() types of data interaction), and in so doing he broke the stack, which grew immensely due to his style of coding.

...Go back to your tightly typed languages and let me worry about moving those bits around properly.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 7:57 am     Reply with quote

The thing with all these SW concepts such as strong typing, encapsulation, object orientation and so on is that they are responses to the problems of managing complexity in code. They have little or nothing to do with computers, instead they are tools to make life more manageable for us humans. They are there to limit and direct us to get round our human failings: to make our jobs easier and more "productive". A computer does not "know" or "care" what a bit, or a collection of bits represents: its all ones and zeros, nothing more.

Many of these modern techniques are indeed "wasteful" and produce relatively "bloated" code, but they are there to enable us to produce the complexity of code that's required for many of today's big software projects. They allow code to be rationally broken up into manageable sections to allow teams of coders to work together. They allow relatively clean and "easy" to define interfaces between code sections, they protect code written by one group or individual from being too adversely affected by code written by others. Its' all about humans and the way we code and work together, it's not about computers.

One reason I like embedded programming, is that is is close to early computing. There's nothing, such as an OS, to insulate me from the hardware. I have to do everything from the ground up, just as they did sixty years ago, and in the case of PICs, often with little more resources. I like that the memory I use is measured in kilobytes rather gigabytes and that the only backing store is one I provide. For that, C is right in the sweet spot, global variables be damned!

However, all this history and philosophy isn't getting the original posters problem sorted :-)
qwwe



Joined: 17 Sep 2017
Posts: 59

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 9:46 am     Reply with quote

One question:
When we use the delay, the program is also active and the serial interruption is active.
Whether there is a delay in the function and information. The program departs from the inside and receives information or waits for a delay.
How to wait for the full information to be received from the serial port.

Mr Ttelmah said that use a 1-bit variable to change the value from 0 to 1 when it receives information.

But I want to delay until the answer is completely received from the modem.
If, in the method of Mr. Ttelmah, he begins to receive the program information to send the next command to the modem.


Last edited by qwwe on Thu Oct 12, 2017 10:47 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 10:33 am     Reply with quote

On the int versus char, definitely better to use char.
I used the program originally typed as the starting point.... Evil or Very Mad

I actually always try to avoid int, preferring to be explicit at all times, so char, int8, int16 etc..

You can never wait till everything is complete, since this will vary. If the modem has an issue, it may well return 'OK' for the command, and then send you back a network error a few seconds later. You can also get unsolicited messages if you receive an SMS for example. This is why I'd always have a parser 'state machine', that your main code calls either whenever there is a character waiting, or when there is a line received, and this then checks the character/line against a table of responses, either as it arrives (so just exiting back until a complete reply is seen, or parsing the line and building a 'modem state' variable that is changed based upon what has been sent. You fundamentally can't rely on always getting a particular reply, and only this reply....
qwwe



Joined: 17 Sep 2017
Posts: 59

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 11:12 am     Reply with quote

thank you
I do not want to always get a specific answer from the modem.
I want to send a Command to the modem.
Then I'll give it a deadline.
To respond
Then send the next command.

I used the delay() function.
But you said this is a problem.
When I thought I came to the conclusion that this is probably my programming problem.
If the modem responds when I'm delayed.
The program does not interrupt the routine and the response is lost.
Is this the correct conclusion؟
Whether or not, when, for example, we are delayed, the routine interrupts and receives Or wait until the delay is complete.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 12:47 pm     Reply with quote

If at any point a delay is used in any interrupt handler then interrupts will be disabled in all delays.
However it also misses the possibility that you may get more responses than you expect. This is a big problem with waiting in a delay.

So have an interrupt 'tick' that decrements a counter. Say every tenth second.

Then you can just load the counter with a value like '300' (assuming int16), for 'thirty seconds', and loop with something like:
Code:


    tick_counter=300;
    send_command(); //whatever you use.
    while (!line_received && tick_counter)
        ;  //will wait here till a line is seen or the counter reaches 0.
    if (line_received)
    {
        //process the line
    }
    else
    {
        //probably consider resetting the modem, or re-initialising
    }


Then all you have to handle correctly is receiving messages you don't expect (you will get ones - for instance when the network status changes).
temtronic



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

View user's profile Send private message

PostPosted: Thu Oct 12, 2017 1:32 pm     Reply with quote

CCS does have an example in the FAQ section of the manual for a 'timed serial receive' where the program loops for data OR the timeout occurs.
It's obvious use is in receiving data from 'something' but you only want to wait xx seconds before 'aborting' the receive and go do something else.
qwwe



Joined: 17 Sep 2017
Posts: 59

View user's profile Send private message

PostPosted: Fri Oct 13, 2017 11:32 pm     Reply with quote

Hi, I wrote the code below for delay:
Code:

void delayr() //DELAY=0.9 SECOND
{
int8 [spam]=0;
int8 de=0;

for(de=0;de<=100;de++)
  {
   for([spam]=0;[spam]<=100;[spam]++)
     {
      delay_cycles(250);
      restart_wdt();
     }
  }
}

Because I've heard the delay_cycle function acts like the NOP commands.
As a result of delayed synchronization, the interrupt is also active.
Is it correct?
temtronic



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

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 4:53 am     Reply with quote

this..

delay_cycles(250);

IS a delay

and as Mr. T has pointed out the PIC will NOT do ANYTHING until the delay is completed.

If you look in the CCS manual, common answers.... section there are 2 methods for a 'timed' function NOT using delays. The first is an 'option' to the #use RS232(...) directive. The second, while called a 'timed button', can easily be modified for a 'timed RS232' function. Either of these should work fine for your application.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Oct 14, 2017 8:13 am     Reply with quote

Only 'delay_cycles(1)' codes as a NOP. Anything larger is a normal delay.
qwwe



Joined: 17 Sep 2017
Posts: 59

View user's profile Send private message

PostPosted: Mon Oct 16, 2017 2:31 am     Reply with quote

Mr.temtronic
Unfortunately I could not find these examples where you said, if it is possible to say the exact location.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Oct 16, 2017 2:50 am     Reply with quote

For the first, look at the manual entry for #USE RS232.
Only downside it you will get unexpected '\0' characters.

For the second, into the manual. Select 'search'. and just type 'button press'. The third line is the one Temtronic is talking about.

Be aware that provided you do not use a delay in your interrupts anywhere, there is then nothing to stop you using delays in the main code, except for the sloth in response this brings.
qwwe



Joined: 17 Sep 2017
Posts: 59

View user's profile Send private message

PostPosted: Mon Oct 16, 2017 7:44 am     Reply with quote

I used this, it also interrupts the interruption
Code:

void delayr()//delay == 1second
{
int8 no=0;
int8 [spam]=0;
int8 de=0;
for(de=0;de<=100;de++)
{
for([spam]=0;[spam]<=100;[spam]++)
{
for(no=0;no<=31;no++)
{
#asm
NOP
#endasm
}
}
}
}
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  Next
Page 2 of 3

 
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