Problem with variables

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys.

 

I'm using ATMEGA8,SIM800 and I'm working with AtmelStudio

Sometime I should send constant strings with SIM800. This is the definition of this strings for example:

const char SMS_MSG[2][10] PROGMEM =
{
	"Done!",
	"Cancel"
};

And this is my main while's code:

 

while (1)
{
    wdt_reset();
    _delay_ms(1000);	
    if (Msg_Received_f)
    {
        Msg_Received_f=0;
        Phone_Num_Detect();
    }
    if (P_MSG_f)
    {
        P_MSG_f=0;
        Process_SMS();
    }
    MSG_Buff_C=0;
    while (SMS_MSG[0][MSG_Buff_C])
    {
        wdt_reset();
        UART_TX(SMS_MSG[0][MSG_Buff_C]);
        MSG_Buff_C++;
        _delay_ms(1000);
    }
}

I defined "MSG_Buff_C" as "unsigned char" and it's a global variable. 

 

the problem is that MSG_Buff_C doesn't change...

please help me about this..

This topic has a solution.

...

Last Edited: Tue. Oct 17, 2017 - 11:54 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

sasan.riahi wrote:
the problem is that MSG_Buff_C doesn't change

How do you determine that?

 

As an aside, what is the name "MSG_Buff_C" supposed to convey?

The name suggests it might be a buffer - but you're using it as an index.

 

So why not call it Msg_Index ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As an aside, what is the name "MSG_Buff_C" supposed to convey?

Actually I ment "Message Buffer Counter" and it should count the parts of Message Buffer to the last part..

 

How do you determine that?

for example if MSG_Buff_C changes, in first time it should send character 'D' and in second time (I mean in while loop) it should send 'O' and so on... but it just sends 'D' 

...

Last Edited: Tue. Oct 17, 2017 - 11:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You have no SPM dereferencing in that?

 

If you are going to stick to PROGMEM then to read the characters in the array you must use pgm_read_byte().

 

As this looks like new code you are writing then unless your compiler is out of date or unless you are writing C++ rather than C I would highly recommend you replace PROGEM with __flash. (in which case the dereference will be implicit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have no SPM dereferencing in that?

I don't know what that is indecision 

 

yes I'm writing GCC with atmel studio.. but when i wrote __flash , atmel studio didn't recognise this expression!..

Will you tell me how should i use pgm_read_byte() ??

...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is watchdog period?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

http://www.atmel.com/webdoc/avrlibcreferencemanual/pgmspace_1pgmspace_data.html

explains how to read a value stored in flash using PROGMEM -consistent with Clawson's help).

What is your version of gcc + atmel studio?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sasan.riahi wrote:
when i wrote __flash , atmel studio didn't recognise this expression!

Either you made a mistake or you're running an old Atmel Studio.

 

sasan.riahi wrote:
Quote:
You have no SPM dereferencing in that?
 

I don't know what that is  

If you use PROGMEM to store constant data in flash memory then you need to access it in special ways. Here's a few pages from the documentation: 

http://www.nongnu.org/avr-libc/u...

http://www.nongnu.org/avr-libc/u...

 

There's a tutorial here at AVRfreaks: http://www.avrfreaks.net/forum/t...

 

sasan.riahi wrote:
I defined "MSG_Buff_C" as "unsigned char" and it's a global variable. 

Don't tell us what you did. Always show us what you did. Post a minimal but complete program that compiles and runs and demonstrates the problem. (Yes, it is your job to isolate the problem to such a small program that it is fit to post here.)

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Oct 17, 2017 - 11:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

What is watchdog period?

This is how i initialed watchdog timer, my clock source is 8MHz internal source

	// Watchdog Timer initialization
	// Watchdog Timer Prescaler: OSC/2048k
	WDTCR=(1<<WDCE) | (1<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);
	WDTCR=(0<<WDCE) | (1<<WDE) | (1<<WDP2) | (1<<WDP1) | (1<<WDP0);

 

...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

JohanEkdahl wrote:
Either you made a mistake or you're running a n old Atmel Studio.

Or he is using C++.

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
while (1)
{
    wdt_reset();               // why watchdog
    _delay_ms(1000);	       // then delay 1000ms - the watchdog may trigger !!?
    if (Msg_Received_f)
    {
        Msg_Received_f=0;
        Phone_Num_Detect();
    }
    if (P_MSG_f)
    {
        P_MSG_f=0;
        Process_SMS();
    }
    MSG_Buff_C=0;
    char c = pgm_read_byte(&SMS_MSG[0][MSG_Buff_C]);
    while (c)
    {
        wdt_reset();
        UART_TX(c);
        MSG_Buff_C++;
        _delay_ms(1000);  // again WHY delays when the watchdog is being used??
        c = pgm_read_byte(&SMS_MSG[0][MSG_Buff_C]);
    }
}

The point is that you cannot just access SMS_MSG[0][MSG_Buff_C] because it is in flash not RAM. So you need to use pgm_read_byte(&SMS_MSG[0][MSG_Buff_C]) to get a character from the flash based data.

 

I also hate the fact that you use upper case names for variables and functions. Most C programmers would only ever use upper case for one thing and that is the name of #define'd macros. So they will immediately see anything in uper case as being a macro (I do!)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sasan.riahi wrote:
but it just sends 'D' 

What is the delay between the 'D'-s? One second or two seconds or something different? I mean the actual delay, not 1 second you wrote in your code.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The point is that you cannot just access SMS_MSG[0][MSG_Buff_C] because it is in flash not RAM. So you need to use pgm_read_byte(&SMS_MSG[0][MSG_Buff_C]) to get a character from the flash based data.

Thank's all..

 

I used pgm_read_byte(&SMS_MSG[0][MSG_Buff_C]) and it works now..

 

I also hate the fact that you use upper case names for variables and functions.

Oklaugh I will edit my code , by the way thank you so much

...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's actually one second

...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you using the the watchdog AND delays? It's a VERY dangerous practice (and if you don't know why you clearly don't understand enough about the watchdog just yet).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Most C programmers would only ever use upper case for one thing and that is the name of #define'd macros.

 

a) Well, are defined macro s necessary except for giving array some dimensions,  and to make some kinds of functions (without allowing compiler checks?)

b) Were can one find reasonable  stylistic rules to write good/good looking  C, once algorithms are not too absurd  (I used pylint for python, and I was very surprised : by default, without configuration file,  oNlY UPPERCASE variables were tolerated in python by pylint).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why are you using the the watchdog AND delays? It's a VERY dangerous practice (and if you don't know why you clearly don't understand enough about the watchdog just yet).

 

I know I must not use delay in my code..

I'm just trying to make some sample codes for my future jobs and testing my project's features..

in final version i will use counters that are increased in timer interrupt routine and flags instead of delay in my functions...

...

Last Edited: Tue. Oct 17, 2017 - 12:27 PM