#11 Standard integers (stdint.h) and mixing integer types

  Рет қаралды 66,260

Quantum Leaps, LLC

Quantum Leaps, LLC

Күн бұрын

Пікірлер: 82
@classyjohn1923
@classyjohn1923 2 жыл бұрын
quick comment: 24:13 it would be better to just typecast to the lvalue datatype since if u16c > 32,767, you will get the incorrect value regardless in the implementation you showed.
@StateMachineCOM
@StateMachineCOM 2 жыл бұрын
Yes, there is a problem at 24:13. The correct version of the code is "s32 = 10 - (int32_t)u16c;" I have added an info card to the video at this time stamp and the code has also been corrected in the code downloads for this lesson ( www.state-machine.com/quickstart ) and on GitHub ( github.com/QuantumLeaps/modern-embedded-programming-course ). Thank you for pointing this out! --MMS
@sandpaper-egg-2020
@sandpaper-egg-2020 Жыл бұрын
I recently got hired by a company for a mixed programming position. I had NO experience in embedded systems programming whatsoever and was struggling to catch up. This course single-handedly brought me up to speed with quality and clarity unrivaled by any other KZbin training course (and I have watched quite a few). If you have just found this course, keep going. You have come to the right place. The fact that Miro Samek has posted this course for free is the height of scholastic generosity. I have learned more from this course than any computer-science college class.
@birnodin
@birnodin 7 жыл бұрын
This video should be viewed from every C-Programmer once a day!
@RandallLowetx
@RandallLowetx 2 жыл бұрын
Once someone has learned from any of the other tutorials out there how to build something basic in C or C++, this series is where everyone should go to learn what's actually happening. So freaking clear and valuable.
@leohuang990
@leohuang990 6 ай бұрын
Great to know that in C, small integers in computation are converted to the platform's "int" instead of the left-hand side's datatype, and a signed integer used with unsigned ones is converted to unsigned int. Did not notice that stdint was in the C99 standard and ARM & RISV-V can support both endianness. A very informative lecture.
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
Karel, Here is the explanation for the promotion (from MISRA-C:2012, Appendix C): "An integer promotion conversion to signed int or unsigned int takes place whenever a bit-filed or object with (un)signed char, (un)signed short, or enum type is used arithmetically as described in C90 Section 6.2.1.1, C99 Section 6.3.1.1..." So, in summary, the integer promotion happens before the expression is evaluated (before the ~ operand in this case) and also before the result of the expression is balanced to the right-hand side of the equals operand. --MMS
@ismaeelahmed
@ismaeelahmed 10 жыл бұрын
Hello Dr. Samek, Thanks for these wonderfully taught lessons .. however still waiting for lessons on interrupt sections ..... Please add them as well... thanks
@StateMachineCOM
@StateMachineCOM 4 жыл бұрын
@@ismaeelahmed Interrupts are explained in Lessons-16,17,18 (kzbin.info/www/bejne/oIGUe6yjoa2IqsU ). --MMS
@mx4evaNZ
@mx4evaNZ 10 жыл бұрын
Just plain excellent. Great tutorials on both C and Arm. Thanks a lot
@clemolk
@clemolk 5 жыл бұрын
This course is simply great because it does not focus only on basic and standard topics but it instead explains very well several practical problems you can encounter in embedded programming. Thank you Dr. Samek.
@KarelHeyse
@KarelHeyse 10 жыл бұрын
Can you elaborate on why u8a in "~u8a == 0x00u" (26:40 in the video) is promoted to (unsigned) int before inversion is applied, and not afterwards. I understand that in the case of "u8a == 0x00u" the left hand side is promoted to unsigned int to match the type of the right hand side.
@miguelorrrr
@miguelorrrr 3 жыл бұрын
Seven years ago but jus in case you still have the question. 22:19 When you have an unsigned and signed operands, both are promoted to unsigned int and the result is unsigned int: 10 - 100 = -90 = FFA6 (unsigned and 16 bits of length)
@MEKTOR900
@MEKTOR900 10 жыл бұрын
I'm really glad about this kind of courses help out to undesrtand thing that sometimes seems to be imposible when you are just reading a book, this lessons had been so useful, thank you very much for giving your time and dedication to others.
@rubenspatterson
@rubenspatterson Жыл бұрын
The type of the result will be equal to the type of the operand that allows a larger maximum value, so the other operand will first be converted to a value of the appropriate type. It should be noted that in the C programming language, not a single calculation operation is performed on data smaller than int. (on MSP430 not smaller than 16 bit int)
@theemelex
@theemelex 10 жыл бұрын
Dear Dr. MS, I really have learned a lot from these lessons. I am a so-so embedded C programmer, not good but not bad either. I have been trying to learn C++ and OO concepts have me stumped. I'd like this same course in C++ for ARM. It would really help the learning curve a lot. Besides, I don't think anyone else's doing anything embedded related in C++. I would really appreciate something in C++. Thanks a lot for these lessons. theemelex
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
There are still many things to learn about C in this course, so this will remain the main focus. However, I do plan to talk about object-oriented programming and I will show how to achieve encapsulation, single inheritance, and polymorphism (virtual functions) in C. The way I will present these concepts is exactly how they are implemented internally in C++. In fact, I will compare machine code generated by the C++ compiler with the code generated by the C compiler. Again, my main guidance for this course is that when students see how the concepts are ultimately implemented, they will understand the concepts better and will be able to use them more efficiently and with greater confidence.
@keithevans6450
@keithevans6450 7 жыл бұрын
Great job Miro! You have probably saved someone hours or days of head scratching with this video. Also wanted to mention that I think your videos could easily be used in a high-school level course with appropriately motivated or mentored students. Even the BBC micro:bit can be programmed in C (among other things) and uses an ARM Cortex-M0. Fry's Electronics has been promoting it over the summer.
@StateMachineCOM
@StateMachineCOM 7 жыл бұрын
Yes, I think that interested high schoolers should be able to follow along and learn from this course. I'd also love to use something like the BBC micro:bit, but it does not seem to have a built-in debugger interface that would allow easy stepping through the code like I so frequently do in these videos. In this respect, BBC micro:bit resembles Arduino. You can easily download the compiled code, but you can't step through. If I were to select a board for this course today, I would probably choose one of the STM32 NUCLEO boards (perhaps a Cortex-M0+ part). NUCLEOs allow easy code upload (they show up as USB drives), but at the same time they allow single-step debugging. --MMS
@devkhan1
@devkhan1 10 жыл бұрын
Very Informative , Desperately waiting for next in series lectures !!!
@FRED82627
@FRED82627 10 жыл бұрын
Excellent course. Million thanks and expect the next.
@김남규-k3k
@김남규-k3k 3 жыл бұрын
MISRA C standard guides some rules about implicit & explicit casting in integral expression. I've compling with that rules, however didn't fully understand what it requires. Thanks to this brilliant lecture, i can finally figure out purpose of the guidelines and differences of integral promotion in various machine.
@basemanq
@basemanq 10 жыл бұрын
When will you post the next tutorial? This course is just get started. There are still many other important topics to cover, interrupts, hardware timers, uart, i2c etc. This is a great tutorial. I have been using Keil uVision quite some time. I am always interested in IAR embedded workbench. The simulation tools are great assets in terms of debugging. If you can make some more advanced tutorials (hardware timeing, i2c bus) on using those simulation tools, that would be really helpful.
@ElectronicsGate
@ElectronicsGate 10 жыл бұрын
This was an important lesson. Thanks. Please keep doing this great series.
@04mfjoyce
@04mfjoyce 3 жыл бұрын
Thank you for the detailed explanations! This is a fantastic series.
@vivektodmal1
@vivektodmal1 4 жыл бұрын
This is hell lot of information in one video...very nice man keep it up
@dongolahmed
@dongolahmed 10 жыл бұрын
First, I really do appreciate the effort and time you put sir into these awesome and god-send lessons. Second, I wonder if you sir shorten the period between the lessons ... 'cause with this pace, it would take years to reach more advanced topics such as RTOS. Thanks sincerely,
@lemon6521
@lemon6521 2 жыл бұрын
It did take years but we got there eventually!
@MohammedNoureldin
@MohammedNoureldin 10 жыл бұрын
Waiting for the next lesson Dr. Semek, could it be soon released? I'm just excited to go on with it
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
The next lesson has just been posted on KZbin. Thanks for the encouragement to keep making these lessons :-) --MMS
@MohammedNoureldin
@MohammedNoureldin 10 жыл бұрын
Thank's a lot doctor! :-)
@Steve-gt5nb
@Steve-gt5nb 10 жыл бұрын
Thanks so much...This is excellent course...Keep up the good work!!!!
@GalinaMalakhova
@GalinaMalakhova 4 жыл бұрын
:D Thanks so much for making this series! Very educational.
@bhavaysen
@bhavaysen Ай бұрын
Hi @Quantum Leaps, LLC, Dr. Samik, In the example, where you demonstrated the comparison, there will be an issue. if ( (uint32_t) u32e > -1 ){ u8a = 1U; } else { u8a = 0U; } what will be the result when u32e = 0xFFFFFFFF ? This becomes a bug in the code, or there is some solution to this? please share and thanks for the lectures, these are too good and helpful.
@MuhammadtahirRana-nn1cb
@MuhammadtahirRana-nn1cb Жыл бұрын
God bless you sir.
@devkhan1
@devkhan1 10 жыл бұрын
Eagerly waiting for Embedded Programming Lesson12 video
@jaimef.sanchez7462
@jaimef.sanchez7462 2 жыл бұрын
Hi Dr. Are you taliking about Floating variables at any specific session?
@dongolahmed
@dongolahmed 10 жыл бұрын
Sir, when will you make lessons on serial communications (UART, SPI, I2C) ?
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
I plan to talk about communications, but this course is not there yet. People still need to learn about interrupts, for example. But to learn about interrupts, I need to explain the interrupt vector table. This, in turn, leads to explaining pointers to functions and the startup code. So, as you can see we have quite a few subjects to cover before we will be ready for communications. --Miro
@ahmedwahdan672
@ahmedwahdan672 7 жыл бұрын
I saw this series about 10 times, and I get back to it to refresh my knowledge, Thanks a lot. Unfortunately, I get warning while downloading the code using Cortex Core m0 and the assembly code not changed either, it still uses the M4 instructions? where is the problem?
@VuNguyen-ws6nt
@VuNguyen-ws6nt 10 жыл бұрын
Is this the last tutorial in the series? Well, I would expect some more tutorials :D
@shashankhebbar1921
@shashankhebbar1921 5 жыл бұрын
great video and in depth explanation. can you show us how to interface a sensor with microcontroller using i2c protocol.
@hakuhakuji
@hakuhakuji 3 жыл бұрын
Why did we put a header definition in delay.c? Isn't it redundant since it is linked during compiling i.e. gcc -o -c main.c delay.c?
@StateMachineCOM
@StateMachineCOM 3 жыл бұрын
There are two files: delay.h header file contains the *declaration* of the function delay() and delay.c source file contains the *definition* of the function delay(). The declaration is just the function prototype and is needed for the compiler, not the linker. For example, the header file delay.h is also included in the main.c source file. This inclusion allows the code inside mani.c to call the delay() function, because the compiler needs to see the function prototype (declaration) in order to property call a function with all its parameters. On the other hand, the linker is not concerned with generating code for calling functions or any other code. Linker is only concerned with placing code and data into various memories (ROM, RAM) and with fixing the addresses. You might also want to watch lesson #14, where you will see explanation of the build process and the roles of the compiler and linker . --MMS
@saurabhsoni5047
@saurabhsoni5047 3 жыл бұрын
Many thanks to you Sir for this excellent series on Programming in Embedded Systems ! This will always be referenced by the one interested in Embedded Programming. I have been experimenting on the mixing types you explained above. I ran into a doubt - was hoping if you could provide some hints. Many Thanks ! #include #include int main() { uint8_t __VAR = 0xFF; // Printing some values to give the reader an idea about the underlying system printf("%u ", sizeof(uint8_t)); // 1 printf("%u ", sizeof(uint32_t)); // 4 printf("%08x ", (uint32_t) __VAR); // 000000FF printf("%08x ", (uint32_t) -1); // FFFFFFFF // Doubt is the below code snippet if(__VAR > -1) printf("OOPS"); else printf("CHECK"); return 0; } This code finally prints OOPS and I wonder why. Since, 000000FF is less than FFFFFFFF, I was expecting it to print CHECK. Please note this is a similar code which you used in the video, just that I have used the datatype uint8_t and you have used uint32_t. Thank You Again for this amazing series of video lectures !
@navinreddy88
@navinreddy88 10 жыл бұрын
Sir, can you please explain the concept of storage of global const variables and local const variables.
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
The correct use of the 'const' keyword and the issue of 'const-correctness' of programs is a big subject, actually. I would have to devote at least one whole lesson to it. I will keep it in mind as a subject for the future. --MMS
@kevinchu9601
@kevinchu9601 3 жыл бұрын
27:23 u8a will be promoted to int, do you mean unsigned int ? i'm a little confused about this. Thanks
@StateMachineCOM
@StateMachineCOM 3 жыл бұрын
All small integer types, no matter signedness, get implicitly converted to (signed) int when used in most expressions. But yes, the implicit integer promotion rules in C are murky and it's difficult to explain them simply. One good discussion of the issues is found on StackOverflow: stackoverflow.com/questions/46073295/implicit-type-promotion-rules
@kevinchu9601
@kevinchu9601 3 жыл бұрын
@@StateMachineCOM Thanks very much. So what is the type of 0x00U? In my opinion, the type of 0x00U should be unsigned int, then u8a should be promoted to unsigned int. Am i wrong?
@Nikita-wh2vc
@Nikita-wh2vc 9 күн бұрын
26:04 It should be noted that such explicit cast (unsigned integer to signed integer) is also dangerous, because it works incorrectly for unsigned integers greater than or equal to 0x80000000U. Obviously, the unsigned integer 0x80000000U is greater than -1 in common sense, but after explicit cast it becomes negative number -2,147,483,648. Maybe the better way is to make an explicit cast to int64_t
@StateMachineCOM
@StateMachineCOM 9 күн бұрын
Mixing signed and unsigned representations is tricky, and its correctness depends on the context. Therefore, explicit casting is often necessary. Using a bigger integer (like your suggested int64_t) only kicks the can down the road, because precisely the same argument can be made at the end of the dynamic range of the bigger integer (0x8000000000000000). --MMS
@ahsanalirafaq971
@ahsanalirafaq971 4 жыл бұрын
can you tell me at 13:30 , how can you assign a 16 bit variable which can hold only 16 bit data a 20 bit data which is beyond its capacity ????
@StateMachineCOM
@StateMachineCOM 4 жыл бұрын
The values assigned to the u16c and u16d variables at 13:30 are within range of 16-bis. Please note that these are decimal constants: 40000==0x9C40 and 30000==0x7530. The problem is only later, when the two variables are added together, because the result (70000==0x11170) indeed exceeds the 16-bit boundary. --MMS
@ahsanalirafaq971
@ahsanalirafaq971 4 жыл бұрын
@@StateMachineCOM thank you very much
@peteys2255
@peteys2255 5 жыл бұрын
Thank you for the interesting content on your channel! It helps a lot both beginners and advanced persons in Embedded Programming. I guess I misunderstood the following Code at 27:50 : Why will the comparison (~u8a == 0x00U) never become true? Without casting u8a will be promoted to int, but 0xFFU=255, and as signed int -1 respectively 1111 1111 binary (signed, 2er complement) or am I wrong? Therefore the operation ~u8a results in 0x00? I would look forward to a feedback :)
@jongxina3595
@jongxina3595 5 жыл бұрын
u8a (which is 0xff) gets promoted to 0x000000ff (8 to 32 bit promotion). therefore ~u8a will be 0xffffff00 which is not 0
@StateMachineCOM
@StateMachineCOM 5 жыл бұрын
The only thing that I'd like to add to Ben's explanation is that by C Standard, an integer must be at least 16-bit wide, so even in a 8-bit CPU, such as AVR, the promotion will result in 0x00FF. This means that ~u8a==0x00U is always false for any CPU type.
@mingwong9233
@mingwong9233 9 жыл бұрын
I'm a little confused here.. why is it that the 16-bit machine be able to perform a 32-bit calculation? I don't understand how the 16-bit machine can produce 70000 when it is bigger than 2^16 -1
@StateMachineCOM
@StateMachineCOM 9 жыл бұрын
Ming Wong 32-bit computations require more instructions on 16-bit CPU (or 8-bit CPU). In fact, you can see in the video that 32-bit addition requires two 16-bit additions plus taking into account any carry-over from the lower 16-bits.
@bareminimum-mrtz
@bareminimum-mrtz 5 жыл бұрын
Appreciate the effort 😀
@amandeepbedi1280
@amandeepbedi1280 10 жыл бұрын
Really Helpful..:)
@veraj5494
@veraj5494 7 жыл бұрын
Very helpful. Thanks
@yashesvi-raina
@yashesvi-raina 3 жыл бұрын
This was challenging.
@liuliu-vo2tf
@liuliu-vo2tf Жыл бұрын
厉害
@kapkunal
@kapkunal 10 жыл бұрын
Thank you
@jp_engineer
@jp_engineer 6 жыл бұрын
It's the first time I attend this class. Thank you a lot for teaching. I'm using ARM_GCC_541 compiler and I have tested a code. Could someone help me? code: unsigned char a=9, b=10; int c = 0; case 1 -> c = (unsigned char)(a - b); //( this gives 255) case 2 -> c = (unsigned char)a - (unsigned char)b; //( this gives -1) case 3 -> c = (unsigned char)a - b;//( this gives -1) shouldn't all the case give 255 since I casted all of them?
@StateMachineCOM
@StateMachineCOM 6 жыл бұрын
In all cases you have the same promotion to `int`, regardless of the types of the operands. But only in the first case, you cast the *result* on (unsigned char). Therefore, only the first case produces 255. Because the type of the operands does not matter, you will get the same result when you declare int a=9,b=10. --MMS
@muhammadmaruf1896
@muhammadmaruf1896 6 жыл бұрын
Can anyone please explain, how does 0x70000 in 32 bit converted to 0x4464 in 16 bit?? (17:20 in video).....
@StateMachineCOM
@StateMachineCOM 6 жыл бұрын
First, this is NOT 0x70000 but 70000 decimal. Please check with the calculator that this 70000 is 0x11170, which truncated to 16 bits is 0x1170, which is 4464 decimal. --MMS
@jamesmaxwell381
@jamesmaxwell381 6 жыл бұрын
Thank you Dr. Samek. About MISRA-C, there are discussion about whether it is actually helpful. Some people say that it is just a corporate standard for large organizations to force devs. adhere to a writing style and it is "The Way of Big Companies" doing things which may not be the best way of solving problems. They even point to a study done by TU Delft which suggests adhering to MISRA-C actually causes more faults (NOTE: I haven't read that paper myself though). What is your though on this? link to that paper: ieeexplore.ieee.org/document/4658076/
@StateMachineCOM
@StateMachineCOM 6 жыл бұрын
CMSIS is actually a set of several standards and components, including CMSIS-Core, CMSIS-RTOSv1, CMSIS-RTOSv2, CMSIS-Driver, CMSIS-DSP, CMSIS-Pack, CMSIS-Zone, etc. This video course uses only the most basic part (CMSIS-Core) for the CPU core and built-in peripherals. In my opinion, this part of CMSIS is not too bad and is actually helpful. The other parts of the CMSIS might not be as useful. --MMS
@jamesmaxwell381
@jamesmaxwell381 6 жыл бұрын
I guess you've mistaken MISRA-C with CMSIS. I was asking about MISRA-C. (But I also had a similar question about CMSIS but didn't ask and I'm happy you answered that too :D)
@wassimdhokar731
@wassimdhokar731 3 жыл бұрын
Check course online to learn embedded C programming on arm cortex-m processor: www.udemy.com/course/embedded-arm-cortex-m3-programming-guide/
@batuhanmete775
@batuhanmete775 3 жыл бұрын
everything is just perfect except the feeling that your breath in my ear at about minute 16. Just kidding thanks :)
@joelinacio
@joelinacio 10 жыл бұрын
Only your videos don't play audio in my Chrome...in Firefox is ok.
@StateMachineCOM
@StateMachineCOM 10 жыл бұрын
What can I do about it? Do you have any constructive ideas? --MMS
@joelinacio
@joelinacio 10 жыл бұрын
just to inform ... maybe someone has the same problem ... I'm trying to figure out what's going on with Chrome on my computer.
@bluehornet6752
@bluehornet6752 10 жыл бұрын
Joel Inacio Works just fine for me here in Chrome. I'm watching it and posting here now from Chrome. No problems here.
@matthewszklany101
@matthewszklany101 Ай бұрын
Has anyone else gotten the error "Error 65: access violation at 0x400FE070 : no 'write' permission" when trying to run the code in the debugger? It seems to have something to do with line 587 in system_TM4C123.c which is: SYSCTL->RCC2 = 0x078027810 I don't think I observed this when I had the board connected, rather I think it may have something to do with the simulator?
@StateMachineCOM
@StateMachineCOM Ай бұрын
Projects that involve real peripherals, such as the GPIO, generally don't run in the simulator. Perhaps the simulator could be configured to accept reads/writes to the address range starting at 0x40000000, but I haven't investigated it. For these lessons, please just use the real board. --MMS
#16 Interrupts Part-1: What are interrupts, and how they work
15:58
Quantum Leaps, LLC
Рет қаралды 80 М.
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 16 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 179 МЛН
The C stdint.h Header File
21:52
John's Basement
Рет қаралды 3,5 М.
#10 Stack Overflow and Other Pitfalls of Functions
27:41
Quantum Leaps, LLC
Рет қаралды 75 М.
x86 Assembly: Hello World!
14:33
John Hammond
Рет қаралды 1,4 МЛН
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 184 М.
Beginners Should Think Differently When Writing Golang
11:35
Anthony GG
Рет қаралды 123 М.
What is size_t in C?
6:09
CodeVault
Рет қаралды 35 М.
#9 Modules, Recursion, ARM Application Procedure Call Standard (AAPCS)
19:07
#20 Race Conditions: What are they and how to avoid them?
16:57
Quantum Leaps, LLC
Рет қаралды 42 М.
Object-Oriented Programming is Embarrassing: 4 Short Examples
28:03
Brian Will
Рет қаралды 2,1 МЛН
How to Fight a Gross Man 😡
00:19
Alan Chikin Chow
Рет қаралды 16 МЛН