Back to the trough (Google has failed me)

Stuck with a problem in your code? Seek help here.

Re: Back to the trough (Google has failed me)

Postby TravisCucore » February 17th, 2015, 9:35 am

Thanks for the comments. If you don't mind I'd like to ask a few question relative to the notes you left for me.

In this code you dynamically calculate the SerialMask each time the loop is
executed. This is not wrong, but if you think about it, the SerialMask variable only
has 16 possible values and these 16 values are always the same each time SendPacket() is called.


Is it safe to assume that this train of thought is based in the idea that clock cycles are more important that memory usage, or am I mistaken in thinking that using a dynamic variable dependent on the value of i is saving me memory. I've never taken a programming class so I've got little wisdom to go on. I'm hoping to take Objective C and Assembly next semester as well as a Microcontroller class. I feel like those classes would close some gaping holes in my knowledge base lol. This code is part of a game I'm making for my brother. The helper chip draws the display, the rest is going to be event driven using interrupts.

This means that you only need to test
if (SerialMask & SerialBuffer)


If I understand you correctly, the if() provides a Boolean true or false by default unless I provide some additional arguments/math operators?

Last question I promise :). I've been developing this using Atmel's AVR Studio, and I've recently figured out how to use it's simulator. This is my new favorite thing because I don't have to constantly be programming chips and testing logic levels. Looking at what happens if the statement evaluates true, Serial_High does indeed change the relevant bit in the PORTB register and that in turn sets the PINB flag high. The next line however, does not. Now, I tried setting both bits using something like this "PORTB |= 0x09 which does indeed set the 0th and 3rd bits high, but for some reason the PINB register does not flag the 3rd but as it does the 0th. I switched the order these lines were executed to see what would happen, and the 3rd bit set but did not get flagged while the 0th bit did not set. I assume this is due to the 2 clock cycle propigation time required to set a bit so I assume inserting a _delayus() for the time equal to 2 or three clock cycles would solve the problem. However, the simulator does not like that. Do you have any ideas why the 3rd bit might not be getting flagged in the PINB register? Should I assume a short delay (allowing empty clock cycles) will solve my problem with the second line not executing? Is this the point where I just need to program a chip and look at the pins physically.

Code: Select all
int SendPacket()
{
   cli();
   for (int i = 15; i >= 0; i--)
   {
      SerialMask |= (1<<i);
      if (SerialMask & SerialBuffer)
      {
         Serial_High
         Clock_High
TravisCucore
 
Posts: 46
Joined: May 14th, 2013, 1:02 am

Re: Back to the trough (Google has failed me)

Postby bandersnatch » February 20th, 2015, 3:32 pm

Hi,
Sorry for the delay in replying. I don't always have enough time for the forum.



Is it safe to assume that this train of thought is based in the idea that clock cycles are more important that memory usage, or am I mistaken in thinking that using a dynamic variable dependent on the value of i is saving me memory. I've never taken a programming class so I've got little wisdom to go on. I'm hoping to take Objective C and Assembly next semester as well as a Microcontroller class. I feel like those classes would close some gaping holes in my knowledge base lol. This code is part of a game I'm making for my brother. The helper chip draws the display, the rest is going to be event driven using interrupts.


Kool that you have thought about the reasons for my suggestion. This is the original reason of this technique but not the only reason.
In the bad old days of slow processors and limited memory it was essential to correctly juggle execution speed with memory usage.
In general (but not always) you can sacrifice memory for more execution speed or save memory via more complicated programming.
This is no longer as important as it once was but the principle of identifying and correctly using dynamic and static data
is an important cornerstone of all programming.
All programs consist of 2 fundamental elements:
- Definition of the data to be processed
- Definition of how the data is to be processed

The balance between these 2 elements depends on many factors:
- The nature of the original problem
- Some problems are more data-intensive and others are more processing-intensive
- The method (algorithm) used for solving the original problem
- E.g. bit extraction can be performed using bitmasks (data-based) or bit shifting (processing-based)
- Choosing the right algorithm can be very important.
There are number of well-known computing problems where different algorithms provide a solution in weeks, hours or seconds
They all provide the correct result but some are better than others
- The constraints of the target platform
- This is less important these days but a good balance between data & processing is still helpful when working with microcontrollers
- The programming language used
- The programming language you use has a subtle but concrete influence on the way you program, especially for beginners
Programming in "C" induces you to focus more on processes than data
Programming in more object-oriented languages such as "C++", "Java", "C#", "Smalltalk" induces you
to think a bit more about data. (A "Class" is an explicit combination of data to be stored ("Properties")
and the actions that can be performed ("Methods")
If you really want to turn your concept of "programming" upside down, there are also languages such as "Prolog"
where you program almost entirely by defining the data describing the problem to be solved..

- The personal experience / preferences of the programmer
- This is always a problem for beginners 'cos there are so many ways of attacking a problem
and the best solution is not always obvious.
You will develop your own style & preferences over time.
One useful rule of thumb is "Occams razor": The simplest answer is often correct.
Keep this in mind when programming. When things start to get difficult & you get stuck then
take a step back & consider a different approach, (e.g.) perhaps by using more data & less processing...

In short, the balance between data & processing is trivial & unimportant in your example but
a very important concept for programming in general

If I understand you correctly, the if() provides a Boolean true or false by default unless I provide some additional arguments/math operators?

NO!!!!
The point I wanted to make is that the C conditional test:
if (<SomeExpression>) {...} else {...};
Evaluates <SomeExpression> and then only decides whether the result is TRUE or FALSE.
Beginners always expect <SomeExpression> to be an explicit boolean comparison, e.g. a==b, a>B, a<b etc.
My point was that C can also evaluate numeric expressions as TRUE or FALSE, e.g. 1+1, a+b, a & b etc.
These numeric expressions have a numeric value but also a boolean value.
I made this point because testing numeric expressions as TRUE or FALSE is often used in C
programs & can be confusing to the uninitiated...

Last question I promise :). I've been developing this using Atmel's AVR Studio, and I've recently figured out how to use it's simulator. This is my new favorite thing because I don't have to constantly be programming chips and testing logic levels. Looking at what happens if the statement evaluates true, Serial_High does indeed change the relevant bit in the PORTB register and that in turn sets the PINB flag high. The next line however, does not. Now, I tried setting both bits using something like this "PORTB |= 0x09 which does indeed set the 0th and 3rd bits high, but for some reason the PINB register does not flag the 3rd but as it does the 0th. I switched the order these lines were executed to see what would happen, and the 3rd bit set but did not get flagged while the 0th bit did not set. I assume this is due to the 2 clock cycle propigation time required to set a bit so I assume inserting a _delayus() for the time equal to 2 or three clock cycles would solve the problem. However, the simulator does not like that. Do you have any ideas why the 3rd bit might not be getting flagged in the PINB register? Should I assume a short delay (allowing empty clock cycles) will solve my problem with the second line not executing? Is this the point where I just need to program a chip and look at the pins physically.

Sorry, but I don't have the necessary time to examine this problem in detail.
My intuition says that the problem is probably quite simple and you just need to take
a closer look at how you are supposed to use the PORTB register.
A few tips:
1/ The simulator is very good. If your code doesn't work in the simulator then it usually won't work in the hardware
2/ Embedded delays are almost NEVER the answer and always a bad idea
3/ Your code does not explicity set the initial value of the clock signal before clocking each bit
In general you should always explicitly set the initial states of all signals
4/ Look for sample code that writes to the PORTB register & modify this step by step for your code
- Work "success-oriented" in small steps. Get the sample code running & then make small changes
until it works in your application.

OK. I hope this has helped you.
Taking a programming course is a good idea. You can come a long way by teaching yourself and "learning by doing"
but formal education will save you a lot of unnecessary sweat & help you in ways that you never thought of.
Have fun & follow your curiosity ;^)))
bandersnatch
 
Posts: 150
Joined: September 17th, 2014, 12:06 pm

Re: Back to the trough (Google has failed me)

Postby TravisCucore » February 22nd, 2015, 7:25 pm

well, based on what I've learned here, I've decided to go back and re-conceptualize the structure of this program. You've made some very good points I need to address.

Thanks again for all your help.
TravisCucore
 
Posts: 46
Joined: May 14th, 2013, 1:02 am

Previous

Return to Help Me! Software

Who is online

Users browsing this forum: No registered users and 1 guest