[Atmel studio] won't accept !== - Page 1
Forum section: electronics / general

author: Simon
author_rank: Unknown
timestamp: July 10, 2018, 01:30:56 pm
content: if (ADC_array_position !== ADC_array_end)The compiler fails saying that there is nothing before "="

author: ljwinkler
author_rank: Unknown
timestamp: July 10, 2018, 01:32:54 pm
content: You can't have !==. It's either != (not equal to) or == (equal to)

author: Fred27
author_rank: Unknown
timestamp: July 10, 2018, 01:33:54 pm
content: You would happen to be a JavaScript developer, would you?

author: Simon
author_rank: Unknown
timestamp: July 10, 2018, 02:20:04 pm
content: Quote from: Fred27 on July 10, 2018, 01:33:54 pmNope, apart from a short foray into BASIC it has been C all the way.== is to test one thing is equal to another, I was just making the assumption that I could invert it. presumably != is a dedicated "not equal to" operator.

author: wraper
author_rank: Unknown
timestamp: July 10, 2018, 02:22:52 pm
content: Quote from: Simon on July 10, 2018, 02:20:04 pmI find it very strange that you never used != before.

author: ljwinkler
author_rank: Unknown
timestamp: July 10, 2018, 02:24:01 pm
content: In this case you could use: if!(ADC_array_position == ADC_array_end)

author: mbless
author_rank: Regular Contributor
timestamp: July 10, 2018, 02:42:43 pm
content: It's just like >= and <=. There's only 2 symbols.

author: JPortici
author_rank: Super Contributor
timestamp: July 10, 2018, 03:47:20 pm
content: Quote from: wraper on July 10, 2018, 02:22:52 pmsame. i can see you logic, but in C logical operators are either one or two symbols.if i may.. i suppose "ADC_array_end" is something like "size of array".. like if you declare array then ADC_array_end=15.if this is the case i suggest you use eitherCode:#define ADC_ARRAY_SIZE 16int ADC_array;...if (ADC_array_position >= ADC_ARRAY_SIZE) {..}..orCode:int ADC_Array;int ADC_Array_End = 15;..if (ADC_array_position > ADC_Array_End) {..}..for the unlikely (but very possible) situation where you increment ADC_array_position , but for some reason it skips ADC_array_end

author: Sal Ammoniac
author_rank: Super Contributor
timestamp: July 10, 2018, 04:05:59 pm
content: Quote from: ljwinkler on July 10, 2018, 02:24:01 pmThat won't work either. This does: if (!(ADC_array_position == ADC_array_end))

author: james_s
author_rank: Super Contributor
timestamp: July 10, 2018, 04:15:22 pm
content: != is a very common operator used in quite a few languages to indicate inequality. It surprises me to hear someone got past the "Hello World" stage without encountering it. I have yet to encounter a 3-character operator.

author: ejeffrey
author_rank: Super Contributor
timestamp: July 10, 2018, 04:23:20 pm
content: Quote from: Simon on July 10, 2018, 02:20:04 pmIn C (and most languages) you don't ever invert operators, only values. You can't do something like !< or !>= either. != is a separate operator from == that has the opposite value, but one isn't defined in terms of the other.Someone asked about javascript because javascript has two "equality" comparison operators, == and ===. So those two operators have inverses that are != and !==. Again these are not syntactically created by applying inverse to the == and === operators, they are operators in their own right.

author: Simon
author_rank: Unknown
timestamp: July 10, 2018, 05:25:14 pm
content: Nope, i have never had to use != before. I am now using ">" anyway, the problem was the last one in many i was looking at in writing some new code to run a project in a very different way to how I normally do it.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 10, 2018, 06:13:41 pm
content: Quote from: james_s on July 10, 2018, 04:15:22 pmIn C/C++ there are the bitshift and assign operators:<<= and >>=Code:a <<= 2is the same asCode:a = a << 2

author: westfw
author_rank: Super Contributor
timestamp: July 10, 2018, 09:09:00 pm
content: QuoteFortran has logical operators up to 6 characters (heh. 2nd time I've had cause to mention fortran in the last couple of days.)".and.", ".not.", ".nequv." (there are ".le." equivalents for the standard comparison operators as well)Cobol apparently has "IF WS-NUM1 IS GREATER THAN OR EQUAL TO WS-NUM2 THEN", but since this is an EE forum, we can be excused for not ever having seen any cobol... (and I don't think even cobol coders actually WRITE code like that.)

author: glarsson
author_rank: Frequent Contributor
timestamp: July 10, 2018, 09:27:58 pm
content: In Forth one of the standard "operators" is */MOD that performs a multiply and division (with intermediate results as double precision) returning both the result of the division but also the remainder.

author: Monkeh
author_rank: Super Contributor
timestamp: July 10, 2018, 10:19:57 pm
content: Quote from: Simon on July 10, 2018, 05:25:14 pmHave you considered actually reading a book on C? Obtaining some form of formal programming education? ... reading basically any code in the wild?

author: james_s
author_rank: Super Contributor
timestamp: July 10, 2018, 11:01:06 pm
content: Heck != is so ingrained that I see it used in casual conversation, usually by people who have some exposure to programming but not necessarily professional developers.

author: andersm
author_rank: Super Contributor
timestamp: July 10, 2018, 11:15:30 pm
content: Quote from: exit_failure on July 10, 2018, 06:13:41 pmC++20 will also add the three-way comparison "spaceship" operator <=>.

author: Koen
author_rank: Frequent Contributor
timestamp: July 10, 2018, 11:21:58 pm
content: !ErrorHasOccured() ??!??! HandleError();

author: sleemanj
author_rank: Super Contributor
timestamp: July 10, 2018, 11:29:28 pm
content: Quote from: james_s on July 10, 2018, 04:15:22 pmWeakly typed languages (for example, PHP and Javascript) often have both "==" and "===", for example1 == true1 === truegive different results.

author: Cerebus
author_rank: Super Contributor
timestamp: July 11, 2018, 12:17:51 am
content: Quote from: james_s on July 10, 2018, 04:15:22 pm"/:=", "divide and becomes", an assigning division operator in Algol 68 which assigns ENTIER(left/right) to left and returns the remainder of left/right as its result. So:Code:REF INT remainder;REF INT left := -7;INT right = 3;remainder := left /:= right;leaves:remainder = 2left = -3right unchangedAnd no, I never found a use for it in real life. I suspect that the motivation for the inclusion of the operation was the fact that a lot of processor architectures at the time implemented division so that the dividend and remainder ended up in two registers and they wanted an efficient way to exploit this when compilers weren't smart enough to optimise away a seperate pair of division and remainder operations into one 'divide with remainder' opcode.

author: helius
author_rank: Super Contributor
timestamp: July 11, 2018, 12:35:43 am
content: Quote from: Koen on July 10, 2018, 11:21:58 pmTrigraphs are great! A lot of the IOCCC entries make heavy use of them:http://www.ioccc.org/years.html

author: Leiothrix
author_rank: Regular Contributor
timestamp: July 11, 2018, 02:51:34 am
content: Quote from: james_s on July 10, 2018, 04:15:22 pmPowershell. It doesn't use a symbol like == for equality, it uses -eq. Powershell also has operators like -contains, -notmatch, and a pile of others. Typical Microsoft thinking at work.

author: Bassman59
author_rank: Super Contributor
timestamp: July 11, 2018, 03:21:37 am
content: Quote from: Simon on July 10, 2018, 01:30:56 pmThat’s because you’re writing C, not Verilog.This is one of the reason why I think people who say, “If you know C, you’ll be able to pick up Verilog in no time!” Yeah, sure, despite the fact that the two languages serve different problem domains.Here’s a fun bug that baffled me for a little bit. I was doing a project that had an 8051 talking to an FPGA. And for the micro code, I wrote something like:Code:if (foo /= bar) {And wondered why it compiled, linked, but didn’t work.

author: Monkeh
author_rank: Super Contributor
timestamp: July 11, 2018, 03:26:28 am
content: Quote from: Leiothrix on July 11, 2018, 02:51:34 amTypical Microsoft thinking?-eq is an integer comparison operator which probably goes back to the early 1970s. ie. pre-Microsoft. You may find shells are a more varied world than you expect.

author: gnif
author_rank: Unknown
timestamp: July 11, 2018, 04:37:35 am
content: Quote from: james_s on July 10, 2018, 04:15:22 pmPHP uses the extra character to indicate a strict type check, for exampleCode:1 == 1 = True1 == "1" = True1 === "1" = False1 === 1 = True

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 06:55:09 am
content: Quote from: Monkeh on July 10, 2018, 10:19:57 pmI have two books, "Programming in C" which is very long winded and constantly makes reference to examples pages away and has a lot of stuff that is PC related and not really helpful on a µC. I also have "The C pragramming Language" by the language authors themselves, this is so concise that it misses essential bits out expecting me to make assumptions.So the latter is useless much of the time as it lacks a complete description of the practical implementations and does not even give comprehensive syntax explanation and the former means that by the time I have read the bits I don't have any interest in as not µC related I loose the plot for the bits I may need if i can remember all of the garbage I just read. Using the book as a reference is hard as it's so wordy and diluted nothing can be found with ease.I could do with a different book i guess that is more embedded orientated. As for formal education I aw doing a pointless HNC in electrical engineering because my boss still remembers the good old days when he went to uni and actually learnt something.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 07:40:54 am
content: Quote from: Simon on July 11, 2018, 06:55:09 amµC C still is C. They usually jsut add functionality through libraries. Be aware that there are functions that are offered by your computer's operating system that won't work on a µC. Some books/tutorial indeed don't distinguish between those two very well. Nevertheless knowing how to code in C will help you almost every step of the way.I myself always had good experiences with the O'Reilly books although I have to admit that I never owned one about C. This one looks interesting:http://shop.oreilly.com/product/9781565923065.doAlso don't be discouraged, if the first few chapters of the book/tutorial you chose don't really seem to apply to µC. Most of them just try to help newcomers to get their machine set up and be able to compile their code locally (ideally while also teaching them what they are doing and not just how to do it). The part about actually writing Code should be just as applicable to writing µC programs as it is to unix programming.Also, if you want, feel free to send me some of your programs maybe we can look at them together. While I haven't written too many pieces of µC code yet I am somewhat proficient in C itself. Maybe we can both learn something from it.

author: hans
author_rank: Super Contributor
timestamp: July 11, 2018, 07:41:38 am
content: Quote from: gnif on July 11, 2018, 04:37:35 amYes and it needs it:http://php.net/manual/en/types.comparisons.phpNote that 0 == "php" evaluates to true, or in fact any string.Integer value 1 does not.I guess it's similar for JS, that because these languages have such abrokenloose type system, is that you need these extra operators to fix things up in particular situations.In terms of learning the ins and outs of C.. if you ignore or avoid the heavy standard library function usage, then any book will do. You can learn the ins and outs of pointers and the C language itself quite well from it. Just remember that if you're on an embedded microcontroller, that a printf() will have far more consequences than on a regular computer.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 10:05:17 am
content: I really need to get my head around pointers but yea every time I try to read up how to use them tangents are taken and half the book brought into the mix. The problem with most books is that the examples are not embedded orientated so I just loose interest. It's all about characters in and out and doing stuff with text.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 10:55:58 am
content: i really don't want to be rude but from your comments I would guess that you still need to learn about more basic topics than microcontroller specific stuff and to a certain extend even more basic things than pointers. Those really are things that you can learn from most books or tutorials. I think your trying to put a roof on your house without having built all four walls.As I said before: I'd be happy took look at your code, make suggestions and maybe even tutor you to the extend of my knowledge.

author: hamster_nz
author_rank: Super Contributor
timestamp: July 11, 2018, 11:09:32 am
content: I have felt Simon's pain. Languages with weird logical opperators are everywhere.For 'not equal' C uses !=, Some languages use <>, one even uses /=, and you have languages that have type-specific comparisons like '-ne' to make numeric comparisons.The one I hate is '<=' in VHDL. it means different thing in different places and I find it mentally jaring when it means 'less than or equal' due to context.... And then there is (was?) Perl....

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 11:47:29 am
content: Quote from: exit_failure on July 11, 2018, 10:55:58 amYes you are right. but most books give PC type programs in terms of problems and examples. They focus a lot on doing something with user inputted data and then putting the result on a screen.Time is a limited resource and I find i remember more by doing. What I have done so far is read analogue inputs and do something with the readings and produce a PWM output. I have numerous things on my list of to do's, there is a combination of learning more about pure C as you say, the ins and outs of the various AVR peripherals, the ins and outs of the protocols the AVR can talk and methods of solving varios problems.At the moment I am writing a program that uses an array and interrupts for the first time.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 12:03:07 pm
content: Do you have any code I could look at? Maybe I can give you some hints from there.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 11, 2018, 12:34:19 pm
content: Quote from: exit_failure on July 11, 2018, 12:03:07 pmthe OP problem is only with the not equal comparison operator. you probably cant see much in his code except application specific logic. as he mentioned he usually did just ADC to PWM conversion, in this app probably he want to compare between 2 array elements and act the IO appropriately. btw, we will find workaround based on operators provided by the language specification, not try to invent a new operator...https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 02:26:12 pm
content: What I am doing is writing a more complicated program where I look at various ADC inputs and alter more outputs. In time I'd like to look at the serial port so that I can get diagnostics, but at the moment I'll have minimum functionality.These are my notes so far:1. The ECU section of the PCB has at its heart an ATmega644P microcontroller (µC). The µC will run on its internal RC clock of 8MHz.2. Interrupt routines. The program flow will rely on interrupts to time the triggering of code blocks at known time intervals. The “main” function will setup the µC and enable the required interrupts.2.1 The ADC will run on a 125KHz clock derived from the system 8MHz clock divided by 64. This will take 13 cycles to carry out a conversion on the selected channel in 104µs. The ADC over samples to increase resolution and reduce noise.As each conversion ends the end of conversion interrupt will fire and the value in the ADC result register will be put into the current location in an array. For as long as the array location used is not the last location the ADC will be restarted, and the array location incremented before exiting the routine. Once the array is full the ADC will no longer be started.The ADC must be first started externally with the array location reset to “0” and the desired channel selected. To check if the array is full and ready to read a new set of values from, test the current array location in use.2.2 The core program functions will be carried out in a timer interrupt routine. Timer 1 is 16 bits and allows for significant time lapses between calls that will allow sufficient time for the entire routine to be serviced before it is time to be called again. A prescaler of 8 will be used giving a resolution of 1µs. The top count will be 8000 giving an interval of 8ms.3. Tasks and features of the main schedule.3.1 tasks_calls: this is a 32 bit variable that will increment every time the main tasks are called by a counter overflow interrupt. It will be a measure in units of 8ms of the time since starting the program.

author: Monkeh
author_rank: Super Contributor
timestamp: July 11, 2018, 02:28:10 pm
content: Quote from: Simon on July 11, 2018, 06:55:09 amPages 16, 41, and 207. You're meant to read the book, not skim it. Also, there's an index.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 02:36:54 pm
content: Yes as I said given that I was having several problems getting my skeleton stuff together that was the last of my priorities and I asked at the end of the working day. Had I left it to the next day I would have questioned myself and got the book out.My problem with that book is the more complex stuff. The switch case explanation does not fully describe the syntax and where semi colons go.

author: Monkeh
author_rank: Super Contributor
timestamp: July 11, 2018, 02:41:23 pm
content: Quote from: Simon on July 11, 2018, 02:36:54 pmSemi-colons end any statement - that's on page 10..

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 02:51:18 pm
content: a small reminder and clear syntax examples just make life a little easier. one character wrong and all sorts of errors come up in compilation. I am not a machine reading the book I am a human, one that likely has ADHD and coding scares the shit out of me.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 02:58:21 pm
content: So I have a variable that needs to be used in two different files, I can't keep the compiler happy. Where should I put this variable, neither of the files is main.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 03:07:05 pm
content: quick google sorted it. Not the sort of thing to find very fast in a book.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 03:11:53 pm
content: Quote from: Simon on July 11, 2018, 02:26:12 pmSo, apart from the setup stuff you mentioned, your main only contains an empty while loop?Quote from: Simon on July 11, 2018, 02:26:12 pmIs this something you programmed it that way or is this a library function, you are using?Quote from: Simon on July 11, 2018, 02:26:12 pmWhat's the reason behind this?Limiting memory Usage? Only having a limited amount of data per cycle (cycle here meaning the time between your timer interrupts)? Averaging of a fixed number of data point? Something different?Quote from: Simon on July 11, 2018, 02:26:12 pmSounds perfectly alright to me.Quote from: Simon on July 11, 2018, 02:26:12 pmIs this simply for measuring how long your uC has been running for or do you need this for something else too?Except for the part with the arrays which, depending on why it is implemented in that way, might be done in another fashion, the structure of your program seems very reasonable.Quote from: Simon on July 11, 2018, 02:58:21 pmIn my experience, the way to get nice, clean code is passing those variable/values. This is the territory where you most likely will encounter pointers and the difference between call-by-value(passing an actual value itself) and call-by-reference(passing a pointer to said value). Please let me know if you need more explanations about this.Quote from: Simon on July 11, 2018, 02:51:18 pmI can sympathise with that. For me it's hardware that I'm for some reason really afraid of. As soon as it involves hardware, it's a real struggle for me to even start a project.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 05:30:46 pm
content: All the code is mine that way i know how it works and I'm writing my notes to help me later.The ADC stuff seems to be the leanest way of doing it. I have written it all to be able to over-sample up to 16 bits which is probably pointless but will at least get around noise if I ever need it. this requires 8192 bytes of RAM per channel. I could create an array for each channel but it would require way too much RAM with 8 channels. I typically use 13 bits which requires 64 samples in which case i could. I can't use the data until I compute the average anyway but this is not something I want to do inside the end of conversion routine. I guess I could also have a sufficiently large variable to just keep adding variables to it until a separate variable counts up to the top and bit shifting is a fairly quick operation and have it cycle through each channel.I also don't want to end up in a situation where the ADC interrupt takes over but I guess the actual computations will take less than tho 100-200µs between conversions allowing one of the few devices with a lower priority to get a look in.

author: Sal Ammoniac
author_rank: Super Contributor
timestamp: July 11, 2018, 05:57:02 pm
content: Quote from: Simon on July 11, 2018, 02:51:18 pm“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler

author: james_s
author_rank: Super Contributor
timestamp: July 11, 2018, 08:12:13 pm
content: Quote from: exit_failure on July 11, 2018, 03:11:53 pmThis is something I don't really understand. Hardware, software, it's all the same in a sense, you learn the rules, you learn what the instructions do, you learn what the components do, and you put something together following those rules. These days especially software and hardware are pretty tightly inter-twined.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 08:20:05 pm
content: Quote from: Simon on July 11, 2018, 05:30:46 pmMy knowledge about uCs is somewhat limited. Does the oversampling itself already need those 8kB of memory or do you mean the storage of the values in your array? How is the oversampling done exactly?

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 08:34:09 pm
content: The oversampling is done in software, you just sample over and over and average. An array could keep more of a rolling average if you have the ram. My current algorithm just adds many samples to a temporary variable and then shifts to divide.

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 08:35:08 pm
content: Bigger micros can oversample in hardware.

author: tggzzz
author_rank: Super Contributor
timestamp: July 11, 2018, 08:56:33 pm
content: Quote from: james_s on July 11, 2018, 08:12:13 pmVery true; I don't know why that isn't obvious to everybodyI first realised that when I was considering how I would implement a processor: I triumphantly reinvented the concept of microprogramming - a mere 20 years after it had first been implemented by Maurice Wilkes in the EDSAC 1.5!Those of us that have been around a while have seen functions move from hardware to software and back again, and vice versa.

author: tggzzz
author_rank: Super Contributor
timestamp: July 11, 2018, 09:01:12 pm
content: Quote from: Simon on July 11, 2018, 08:34:09 pmOversampling relies on the noise having a random distribution. Are you sure that is the case in your system?Alternatively you canensureit is random: add known pseudorandom noise at the input, digitise, and subtract the known noise digitally. That's the basis of spread spectrum systems.Aphorism: If you know how to optimise, then do it. If not, then randomise".

author: Simon
author_rank: Unknown
timestamp: July 11, 2018, 09:22:38 pm
content: Well it's always a trade-off between speed, silicon size and flexibility.

author: exit_failure
author_rank: Regular Contributor
timestamp: July 11, 2018, 09:25:09 pm
content: Quote from: Simon on July 11, 2018, 08:34:09 pmIf you have a rolling average, you don't really need that much memory, takes eight samples by summing them in one variable, rightshift 3, store that in another varieable, take another 8 samples , rightshift 3, take the average of that and the previous average and store to the second variablea mockup code might look like thisCode:unint16_t eightSamples = 0;unint16_t rollingAverage = 0;uint8_t colletedSampleCount = 0;//the interrupt routine would be something like:if(colletedSampleCount < 8) {eightSamples += sampleFromADC;colletedSampleCount++;}else {uint16_t average = eightSamples >> 3;if(rollingAverage == 0) {rollingAverage = average;eightSamples = 0;colletedSampleCount = 0;}else {rollingAverage = (rollingAverage + average) >> 1eightSamples = 0;colletedSampleCount = 0;}}Of course you'd have to have all of those variables eight times (exccept colletedSampleCount maybe) and, as you said, maybe in an array. But it would need a lot less memory than 8kB per channel or am I overlooking something?

author: tggzzz
author_rank: Super Contributor
timestamp: July 11, 2018, 09:38:34 pm
content: Quote from: Simon on July 11, 2018, 09:22:38 pmI'm not sure what you are referring to there. If it is my point about randomness, then no. If anything, it would be a trade-off between valid and not valid.

author: sleemanj
author_rank: Super Contributor
timestamp: July 11, 2018, 10:55:21 pm
content: Quote from: exit_failure on July 11, 2018, 09:25:09 pmI don't think that works.11111111111111111111111100What is the average of this sequence of 24 numbers, ( (8 + 8 + 7 + 100) / 24) = 5.125What is the average of this sequence using your adding mean-of-eight algorithmFirst 8, mean 1, set rolling average mean = 1Next 8, mean 1, set rolling average mean = (1 + 1) / 2 = 1Final 8, mean 13.375, set rolling average mean = (1 + 13.375) / 2 = 7.1875Don't worry, I made essentially the same mistake, and rs20 corrected me, just paying it forward...https://www.eevblog.com/forum/microcontrollers/absolute-value-of-1000-sample/msg1203405/#msg1203405

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 11, 2018, 11:26:23 pm
content: You'll need 3x 32bit variables.. adc_total, adc_count, adc_average. All init to zero except adc_count set to number of averaging required. After each adc reading.. adc_total = adc_total + adc - adc_average, adc_average = adc_total / adc_count... 12 bytes ram, 1 add 1 sub 1 div... further tweak possible...

author: Bassman59
author_rank: Super Contributor
timestamp: July 12, 2018, 04:14:27 am
content: Quote from: Simon on July 11, 2018, 06:55:09 amFirst, “orientated” is not a word. At least not in English. CPractical C Programmingis really pretty good. A lot of the stuff you need with micros is there, such as bit-twiddling and masking, and it’s not specific to any platform. It talks about pointers and how and when to use them. One thing to keep in mind is that you can’t use dynamic memory allocation (malloc()andfree(), say) on an 8-bit micro. You can and should use pointers when you need to.For embedded stuff, it always boils down to this. Peripherals live at particular memory locations. Reading from, say, the ADCVAL address returns the most recent conversion results. Writing to the UART_TX register loads the transmitter and starts the hardware shifting out. The rest is really just housekeeping. The micro vendor likely supplies a header file that defines all of the registers that are used to access the peripherals, and that header file should have#defined bit masks and constants that enable you to make sense of what you are reading and writing.The rest of what you’re doing, say averaging ADC samples, is the same on a micro as it is on a desktop computer.

author: Simon
author_rank: Unknown
timestamp: July 12, 2018, 07:10:21 am
content: 8K of RAM is 16 bit oversampling for a 10 bit ADC and hardly worth doing.What I currently do is have a 32 bit variable and use a for loop to run enough samples after each readitg I do:32_bit_var = 32_bit_var + ADC_resultAfter exiting the loop I bit shift.Now doing this takes a lot of time most of it waiting around for the ADC so i am trying to making it more "multithreaded". I can use an array being the fastest way to stick a value in a place to empty the ADC result register or I can do the same old addition and when a seperate variable that keeps count of the number of samples taken hits the top I bit shift the sum I already have and move on to the next channel.I do also wonder if i can do a rolling average by retaining my 32 bit variable taking a new reading, shifting that reading to be of equivalent magnitude, adding it to the 32 bit variable and the dividing by 2 (bit shift again).

author: exit_failure
author_rank: Regular Contributor
timestamp: July 12, 2018, 08:59:29 am
content: You could implement your array like a ring buffer, giving you a nice method of calculating your rolling average:Setup:Code://storage for the last 8 valuesuint16_t buffer = {0};//storage for the rolling sumsuint32_t currentSum = {0};//storage for the position in the bufferuint8_t head = {0};//initilise all with 0 or use global/static variables, otherwise there might be random crap in thereThe interrupt routine would be something like:Code:uint8_t interruptNo = 0;//store the oldest value in a temporary variableuint16_t oldHead = buffer[head];//get the value from your ADC's register overwriting the 'oldest' value in the arraybuffer[head] = getValuefromADC();//Calculate the new rolling averagecurrentSum += buffer[head];currentSum -= oldHead;uint16_t rollingAverage = currentSum >> 3;//do whatever you want with the average herehead++;if (head >= 8) {head = 0;}Memory usage single channel: (2*8 + 4 + 1 + 2 + 2) bytes= 25 bytes (+overhead, +padding if aligned)Memory usage eight channels: 25 bytes * 8 = 200 bytes (+overhead)Overall I woldn't think that this shouldn't use much more than 500 bytes of memory including overhead and padding.I still don't really understand how the oversampling uses 8kB of RAM. Could you maybe explain that to me or point me in the direction of some documentation?

author: Simon
author_rank: Unknown
timestamp: July 12, 2018, 10:20:22 am
content: http://ww1.microchip.com/downloads/en/AppNotes/doc8003.pdfIf you do the working out, if I was crazy enough to do 16 bit sampling with the 10 bit ADC I need to take 4096 readings that each require 2 bytes each.IF i want to have a constant rolling average on THAT channel I need a dedicated array to that channel that i cycle through. This is all worse case paranoia. If i am doing 13 bit sampling I only need 64 samples and 128 bytes of RAM. Or I just add each new value to an accumulator variable in which case for 16 bits I only need a 32 bit variable or 4 bytes of RAM. This is probably more sensible and does allow for a fully automated and free running ADC routine that will just update global variables as they become available.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 12, 2018, 10:49:12 am
content: Quote from: exit_failure on July 12, 2018, 08:59:29 amalso available inhttp://www.ti.com/lit/an/slaa694a/slaa694a.pdfpage 4... few bytes is all it takes to achieve that, except if we want to keep N number of over sampled data, then memory requirement will be 2 x N bytes + the few bytes...

author: donotdespisethesnake
author_rank: Super Contributor
timestamp: July 12, 2018, 07:56:08 pm
content: Quote from: Sal Ammoniac on July 11, 2018, 05:57:02 pmHmm, I've seen quite a few fools struggle with the former, and many self-claimed "good programmers" writing cryptic gibberish, so I would rephrase that“Any good programmer can write code that a computer can understand. Exceptional programmers write code that humans can understand.”Oh, and don't forget the +++ operator.Concatenating operators is genius though, I expect to put it into my next language. Then !&& is the NAND operator, !|| is NOR.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 12, 2018, 11:56:07 pm
content: Cryptic gibberish is required in resource limited system to get the most. Even the native machine code is cryptic gibberish.

author: hamster_nz
author_rank: Super Contributor
timestamp: July 13, 2018, 12:55:33 am
content: There is stylish code, there is cryptic gibberish, and then there is the somewhat avant-garde.For your wonderment, I submit mac.h, from V7 UNIX:Code:/**UNIX shell**S. R. Bourne*Bell Telephone Laboratories**/#define LOCALstatic#define PROCextern#define TYPEtypedef#define STRUCTTYPE struct#define UNIONTYPE union#define REGregister#define IFif(#define THEN){#define ELSE} else {#define ELIF} else if (#define FI;}#define BEGIN{#define END}#define SWITCHswitch(#define IN){#define ENDSW}#define FORfor(#define WHILEwhile(#define DO){#define OD;}#define REPdo{#define PER}while(#define DONE);#define LOOPfor(;;){#define POOL}#define SKIP;#define DIV/#define REM%#define NEQ^#define ANDF&&#define ORF||#define TRUE(-1)#define FALSE0#define LOBYTE0377#define STRIP0177#define QUOTE0200#define EOF0#define NL'\n'#define SP' '#define LQ'`'#define RQ'\''#define MINUS'-'#define COLON':'#define MAX(a,b)((a)>(b)?(a):(b))WHILE/DO/OD, LOOP/POOL and REP/PER/DONE have to be considered an outstanding efforts in trying to make a tool fit in with long-established thought patterns that have disappeared.I was thinking the other day about all the knowledge and skills that have been lost going from mostly tape based batch processing to online systems. The ability to process large volumes of nearly offline-data with minimal resources starts to sound quite a bit like "map-reduce" and other big data paradigms.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 13, 2018, 05:58:26 am
content: Quote from: hamster_nz on July 13, 2018, 12:55:33 amVB/Pascal guy want to make a better world?some prominent people should have thought to make pic and avr cross compatible... virtually not physically...Code:;/////////////////////////////////////////////////////////////////////////////////////////////////;// microchip compatible opcode;/////////////////////////////////////////////////////////////////////////////////////////////////.macro return ;clock = 4/5ret.endm.macro retlw;(val) [W = val] ;clock = 5/6ldi W, @0return.endm.macro retfie ;clock = 4/5reti.endm.macro goto;(addr) ;clock = 2rjmp @0.endm.macro decfsz;(reg) ;clock = 2 (not skip), 3 (skip)dec @0brbs SREG_Z, (PC+2).endm.macro incfsz;(reg) ;clock = 2 (not skip), 3 (skip)inc @0brbs SREG_Z, (PC+2).endmand then there are more user friendly less cryptic gibberish...Code:.macro clearBitW;(bit) ;clock = 1cbr W, (1 << @0).endm.macro clearBitF;(reg, bit) ;clock = 1cbr @0, (1 << @1).endm.macro clearBitR;(io, bit) ;clock = 2cbi @0, @1.endm;/////////////////////////////////////////////////////////////////////////////////////////////////;// status and arithmetic;/////////////////////////////////////////////////////////////////////////////////////////////////.macro setCarry ;clock = 1sec.endm.macro clearCarry ;clock = 1clc.endm.macro rotateLeftW ;[rotate left W through carry @ rlf] ;clock = 1rol W.endm.macro rotateLeftF;(reg) [rotate left reg through carry @ rlf] ;clock = 1rol @0.endm.macro rotateLeftR;(io) [rotate left io through carry @ rlf] ;clock = 3setValueWR @0rotateLeftWsetValueRW @0.endm.macro rotateRightW ;[rotate right W through carry @ rrf] ;clock = 1ror W.endm.macro rotateRightF;(reg) [rotate right reg through carry @ rrf] ;clock = 1ror @0.endm.macro rotateRightR;(io) [rotate right io through carry @ rrf] ;clock = 3setValueWR @0rotateRightWsetValueRW @0.endmjust a few snip taken from the miscellaneous "avr_gen_01_asm.inc"

author: donotdespisethesnake
author_rank: Super Contributor
timestamp: July 13, 2018, 06:13:28 am
content: Quote from: Mechatrommer on July 12, 2018, 11:56:07 pmYes, I've seen that lazy excuse used by poor programmers many times. It's not even true.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 13, 2018, 06:45:51 am
content: Quote from: donotdespisethesnake on July 13, 2018, 06:13:28 amyeah just because its not in your reality or you are C# programmer programming Structured Query Language all the time doesnt mean it doesnt or shouldnt exist.

author: Cerebus
author_rank: Super Contributor
timestamp: July 13, 2018, 07:54:15 am
content: Quote from: Mechatrommer on July 13, 2018, 06:45:51 amSpeaking as a man who cut his teeth on mainframes with less memory than many a typical System-On-a-Chip microcontroller, often including the disk arrays (8Mb disk packs anyone), and instruction cycle types measured in microseconds, yet which still managed to concurrently run compilers, database systems, application programs and a few on-line users, I find myself having to side with donotdespisethesnake. Cryptic crap is just that, there's no excuse, no matter how limited your systems resources for that type of coding style. It is lazy, and shows scant regard for the poor bastards who have to read or maintain your code after you get yourself killed by insisting that it's more 'efficient' and 'saves resources' to not look both ways before crossing the road.A 30Mb disk drive from the era I'm talking about:

author: tggzzz
author_rank: Super Contributor
timestamp: July 13, 2018, 08:13:23 am
content: Quote from: Cerebus on July 13, 2018, 07:54:15 amYou beat me to it in all respects, but then I don't bother correcting Mechatrommer.In my case the fastest instruction time was 576us (==0.5ms==2kIPS), and the backing storage is shown below. Yes, that is 35mm film, complete with sprocket holes (A significant portion of the movie industry was just up the road from the computer manufacturer!)orhttps://www.flickr.com/photos/sbell/3380330082

author: TomS_
author_rank: Frequent Contributor
timestamp: July 13, 2018, 08:39:02 am
content: Quote from: james_s on July 10, 2018, 04:15:22 pmYou havent tried JavaScript yet have you.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 13, 2018, 12:39:28 pm
content: Quote from: Cerebus on July 13, 2018, 07:54:15 amif you talking about lack of comment then i have to agree. but even given enough commenting, there are codes you cannot simply read without having the proper material in hand. if a programmer with graphics or data security background dead, there is no way (or will take months or years at least) for your young programmer with pure computing background to handle let alone improve the demised code. from "human readable" point of view of functioning of the program. even programmer with the same background sometimes.. try reading quake's code without reading the documentation and tell me how'd they do it this and this? maybe we are talking about different semantics here.

author: Simon
author_rank: Unknown
timestamp: July 13, 2018, 05:13:36 pm
content: I'm trying to use descriptive variable and definitions to make it more legible later.

author: sokoloff
author_rank: Super Contributor
timestamp: July 13, 2018, 05:24:20 pm
content: Quote from: Simon on July 11, 2018, 02:26:12 pmTip: declare the “location in use” variable as volatile.

author: hans
author_rank: Super Contributor
timestamp: July 13, 2018, 05:30:54 pm
content: Quote from: Mechatrommer on July 13, 2018, 12:39:28 pmI think you're confusing domain specific knowledge and crap code here. There isnoexcuse for crap code. If one can only make programs work with crap code, it highlights the incompetence. Further down the road you're guaranteed you won't be able to maintain it, let alone someone else.Some fields like cryptography, compression, machine learning, etc. are driven by theoretical computer science based fields, and it will take some time to get up to speed in that kind of code. We're all human after all. But crap code only hinders the adoption effort, and although a common occurrence, introduces the suggestion that a massive project needs a "rewrite". Then during a rewrite, people will write down the exact same formulas and matrix manipulations down, perhaps with a few twists here and there depending on the personal style of a programmer.

author: Mechatrommer
author_rank: Super Contributor
timestamp: July 13, 2018, 05:59:26 pm
content: yes crap code can be a different meaning to cryptic gibberish to my understanding. if you are talking about very ill thought codes from the beginning, that need constantly changed/fixed without proper refactoring, then yes its definitely an incompetency. i believe a competent (experienced) programmers should be able to make a very well thought out codes from beginning and know how and when to refactor properly. this is simply a distinguishing between a novice and experienced programmers, a natural thing that happened.

author: lukewren
author_rank: Contributor
timestamp: July 14, 2018, 11:05:35 pm
content: Quote from: Bassman59 on July 11, 2018, 03:21:37 amMy usual line is "it's like C, but all the lines happen at once".It gets the point across -- that lots of it is quite familiar (operators and operator precedences for example), but the mindset is completely alien