just a general question on embedded c

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

hi to all the fellows .....

i want to ask certain things regarding embedded c programing 

i am new to embedded c programming and i started with AVR microcontroller

now i want to do all arduino based projects using embedded c code instead of C++ language used in arduino ide .

so i started doing random projects in atmega 328 using embedded c like i started doing projects by starting basics 

GPIO pins, bit shifting , serial communication and like that 

 

how should i learn / start / get to know embedded c so that i can interface any module/sensor with microcontroller like in previous posts i am asking question regarding serial communication ,ADC etc

even though before posting this i have read about its registers, its basic code but still i realised i dont know many things like bit banging , how to use sprintf ,print ... ( basicly AVR libc library)

so any suggestions how should i pursue with things.

 

also when i read the code of the librarys i get into thoughts what should i do or pursue things so that i can also wite such codes..

 

<p>Kunal Gupta</p>

Last Edited: Tue. Jan 7, 2020 - 11:23 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If it's just a general question on embedded c, it should be in the General Programming forum!

 

EDIT: It's now been moved.

 

Kunalgupta wrote:
i am new to embedded c programming

The language itself is no different from 'C' in any other context.

 

Are you familiar with 'C' programming in any other context(s) ?

 

Are you familiar with embedded systems in general?

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 7, 2020 - 11:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try lots of things. Make mistakes. Become expert.
A degree in electronic and software engineering wouldn’t go astray.

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

awneil wrote:

 

 

Are you familiar with 'C' programming in any other context(s) ?

 

Are you familiar with embedded systems in general?

 

yes i am familiar with C, i have done C++ and done have made many projects related to embedded system using arduino but in C++ language 

projects with RFID module, GSM module,HC-05 ...etc .

 

<p>Kunal Gupta</p>

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

Why would you not want to use C++?

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

The only thing u need is this...

https://www.avrfreaks.net/forum/newbie-start-here

A Beam of Light out of the War

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

Kunalgupta wrote:
interface any module/sensor with microcontroller like in previous posts i am asking question regarding serial communication ,ADC etc

 

None of that has anything to do with 'C' - the microcontroller neither knows nor cares what language you write your source code in!

 

So these are all about understanding how the hardware works; what the registers do; etc.

 

For this, you need to study the chip Datasheet. Also spend time browsing the manufacturer's website for Application Notes, examples, etc

 

In the case of Atmel/Microchip parts, the place to start is the Product Page; eg,

 

https://www.microchip.com/wwwproducts/en/ATmega328 - look on the 'Documents' tab.

 

Other manufacturers have similar arrangements.

 

Basics of interfacing anything to anything elsehttp://www.8052mcu.com/forum/read/160143

 

 

 i don't know many things like bit banging

Bit-banging is just about setting pins high & low, with timing - again, nothing specific to 'C'

 

 

 how to use sprintf ,print ...

Those are standard 'C' functions - they work exactly as elsewhere; not specific to embedded.

 

 

AVR libc library

Documentation is here:

 

https://www.nongnu.org/avr-libc/

 

https://www.microchip.com/webdoc/AVRLibcReferenceManual/index.html

 

 

any suggestions how should i pursue with things.

learning standard 'C' is best done on a PC - away from the added complications of cross-compilation and embedded targets.

 

Here are some 'C' learning & reference materials:

 

http://blog.antronics.co.uk/2011...

 

 

As with learning any new skill, the important thing is to with the basics & key concepts to lay good foundations and then build up step-by-step.

 

See Tip #6 (you know where to find it!)

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

There are at least a couple of aspects to this. One is learning a programming language and one is learning about micros. The learning of the language is probably best done away from the micros. It's far easier to debug and see instant results if you learn a language for PC programs. If you don't wholly know a language at this stage I would concentrate on C++ not C. (it came later and learned from C's mistakes) - like I say, learnt the basic language structure and concepts away from a micro.

 

To learn the use of micros specifically then just take the typical peripherals in order of increasing complexity and write examples that make them do stuff.

 

A bit like learning Piano - you can only build a knowledge base by starting with the small/simple stuff, working with that until it becomes second nature, then use that knowledge to go on and learn the more complex stuff.

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

Everyone Else wrote:
Why would you not want to use C++?

I focussed on 'C' in my replies because that's what you asked, and what I know best - but this is a very good question!

 

As you're clearly not heavily invested or burdened with a load of legacy 'C' why, in this 3rd decade of the 21st Century, would you not start with C++ ?

 

You shouldn't have any trouble understanding existing 'C' code when you understand C++, and it is perfectly possible to have some 'C' code in an otherwise C++ Project.

 

take a look at this current thread:  https://www.avrfreaks.net/forum/going-embedded-c-c

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Jan 7, 2020 - 12:15 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Embedded C adds address spaces and fixed-point math.

GCC 5.5 :

Using the GNU Compiler Collection (GCC): Named Address Spaces

6.16 Named Address Spaces

As an extension, GNU C supports named address spaces as defined in the N1275 draft of ISO/IEC DTR 18037. ...

http://www.open-std.org/JTC1/SC22/WG14/www/projects#18037

TR 18037: Embedded C

...

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Tue. Jan 7, 2020 - 12:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kunalgupta wrote:
GPIO pins, bit shifting , serial communication and like that

You learn about these things in the datasheet, appnotes and online articles, they pertain to the MCU or even general electronics, not the language.

 

Basically, the first thing you need to learn is how to access memory mapped SFRs from the language.

In C/C++ you do it with this kind of construct:

 

#define PORTB (*(volatile uint8_t *)(0x24))

Now you can use the PORTD macro as if it was a variable.

 

In C++ you can also do it in other ways like:

volatile uint8_t & portb = (*(volatile uint8_t *)(0x24));

but I digress...

 

 

Basically, the manufacturer already prepared files with all these macros for you to use, so in the case of AVRs, you include

#include <avr/io.h>

and then it's just a matter of reading the datasheet to see how to use the MCU peripherals. Navigating through these include files is also a great source of information when the datasheet is iffy.

Another very important asset you need to learn are the facilities available from avr-libc.

 

This should be enough to get you started.

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

gchapman wrote:

Embedded C...

 

I wonder if the OP is talking about "Embedded C" or using C for embedded systems?

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

Hopefully Kunal will state the later as the former has limited implementation.

The compiler can have address spaces as a feature with fixed-point math supplied by the MCU manufacturer.

"Dare to be naïve." - Buckminster Fuller

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

thanks to everyone for giving suggestionssmiley.

some links provided are very helpfull.

Now i am going through links and getting many new things and in much guided way

<p>Kunal Gupta</p>

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

I have done some Arduino code for small projects, but really like writing my own C-code for the various interfaces.

 

My primary job is developing software for Automotive Engine Controllers.

C is the language of choice because C++ has too much overhead due to functional timing constraints.

That said, I just gravitate towards C when programming my small projects on the Arduino boards.

 

I also teach an Microprocessor/Microcontroller class at the local college here in Detroit area.

So, the approach I would take is to develop your own libraries for each of the individual microcontroller modules.

 

Start with general I/O by interfacing to an HD44780 LCD using GPIO.

It is a great exercise for introducing the student to Discrete I/O concept.

 

After this, move on to the ADC.

Hook up a variable resistor to an analog input channel, perform a conversion on the channel and then print it out to the LCD.

 

All this, of course, is going to require that you read the data sheet for the microcontroller and any other peripheral interfaces (such as the LCD module).

 

Good luck with the bit-banging!

 

I have some libraries for the LCD if you get stumped on what to do.

Hint: LCD can run in both 4-bit and 8-bit mode. I prefer using MEGA2650 so I have enough I/O for 8-bit mode.

 

 

 

 

 

  

Last Edited: Thu. Jan 9, 2020 - 12:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jensenjoe wrote:
Start with general I/O by interfacing to an HD44780 LCD using GPIO.

There's quite a few foundations need to be laid before you're ready to do an HD44780 LCD, and I would say that a UART is far more useful (and simpler) before you get to the LCD:

 

https://www.avrfreaks.net/comment/1138166#comment-1138166

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jensenjoe wrote:
C is the language of choice because C++ has too much overhead due to functional timing constraints.
Intriguing - I also work in automotive where we use C++ almost exclusively. I would love to know what you mean by "functional timing constraints" and how this is supposedly different for C++ compared to C. Say you just stick to the C compatible basic subset of C++ how is the code that is then generated any different to plain C? Are you talking about indirect calling through vtables or something as being a time/space waster ??

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

I would usually avoid C++ because the language is enormous and impossible to keep up with. Do you know what the "<=>" operator does? You need to, because it got added! So did printf-like formatting stuff because all the cout/cerr stuff is an unusable disaster. What's "auto" do today? We don't know! How about those 5 different pointer qualifiers?

 

C++ is also much more vulnerable to "things that you can't see are changing the behavior of the code". I don't think it offers much benefit for the sorts of things we do in small embedded systems.

 

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

the_real_seebs wrote:
Do you know what the "<=>" operator does? You need to, because it got added!

 

You don't have to use a feature just because it's there...

 

I could ask it the other way around. Does C++ prevents you from doing something you could do in C? So far all I've found is named address spaces.

 

edit: actually I think there is something else, but it must be so minor I don't even remember what it is.

edit#2: Oh, I remembered, it was designated initializers. But it seems it's also added to C++20, so...

Last Edited: Thu. Jan 9, 2020 - 04:36 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your first post!

jensenjoe wrote:
My primary job is developing software for Automotive Engine Controllers.

C is the language of choice because C++ has too much overhead due to functional timing constraints.

Functional safety is another constraint.

A best practice is a safety standard.

On 7-Jan'20, the ones at Microchip released the functional safety variants of the MPLAB XC toolchain and one C++ toolchain; C++ is in MPLAB XC32++, not enabled for MPLAB XC16, and no mention for MPLAB XC8.

 

ISO - Keeping safe on the roads: series of standards for vehicle electronics functional safety just updated

via ISO - ISO 26262-1:2018 - Road vehicles — Functional safety — Part 1: Vocabulary

due to

MPLAB® XC8 C Compiler Version 2.19 Release Notes for AVR® MCU

MPLAB® XC8 C Compiler Version 2.19 Release Notes for PIC® MCU

Release Notes for MPLAB XC16 C Compiler v1.49

MPLAB XC32 Compiler v2.39, Release Notes

 

P.S.

re ISO 26262 2018 :

Changes to the newly revised versions include the addition of important specific requirements for semi-conductors as well as new requirements for motorcycles.

IIRC, BMW is adding ESC to motorcycles (ABS plus anti-skid)

A common issue for ones on two wheels is low-siding due to loss of traction of the front wheel.

IIRC, BMW bikes will torque the steering tube while simultaneously ABS'ing the wheels such that the wheels' bite will be restored and the bike righted without over-correction.

Bike riders are educated, trained, mentored, and practiced in low-siding.

RSD

 


Firmware Update v18.03 | Barr Group

...

 

In this issue:

...

  • The State of Embedded Systems Safety

...

 

edit :

Functional Safety | Microchip Technology

 

"Dare to be naïve." - Buckminster Fuller

Last Edited: Fri. Jan 10, 2020 - 12:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

The problem with existing C++ books is that the language is difficult for beginners and so are the books...We believe that C++ has crossed the single book complexity barrier...

C++ The Core Language, Satir and Brown

 

 

#1 This forum helps those that help themselves

#2 All grounds are not created equal

#3 How have you proved that your chip is running at xxMHz?

#4 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand." - Heater's ex-boss

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

El Tangas wrote:
So far all I've found is named address spaces.
The C++ part of the Embedded C standard states that

  • address spaces would be a C++ extension
  • fixed-point math by C++ classes
  • IO hardware by C++ templates

n1169.pdf (ISO/IEC TR 18037) (Embedded C)

[page 104]

 

Another way is via the BSP portion of an RTOS or framework; these usually also supply C and C++ toolchains (the same toolchain for building the RTOS or framework and the applications)

The BSP adds to C++.

Extending C++ is more likely a toolchain modification though practice is as mentioned in post #9 (inter-operability between computer languages)

 


ISO - ISO/IEC TR 18015:2006 - Information technology — Programming languages, their environments and system software interfaces — Technical Report on C++ Performance

 

"Dare to be naïve." - Buckminster Fuller

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

jensenjoe wrote:

 

So, the approach I would take is to develop your own libraries for each of the individual microcontroller modules. 

  

 

I also thought and plannned by doing this way . like i planned that i will interface all the modules (which can be interfaced with arduino ) with the avr microcontroller ( specifically atmega 328pb and 2560 using) in embedded c code .

doing so will develop my embedded c coding skills and embedded system also.

But doing so takes lots of time because i have to read datasheet of every module ( like i now days working on dht11,IR receiver...). And as i am in second year of my college.

So is my approach for developing skills in embedded system correct or not?

I basically wants to pursue my carrer in embedded systems and avionics ( communication , navigation ,guidance) . So from the net and seeing the linkden i found that embedded c coding is basic and must in embedded system.

And  in avionics field i found main things are navigation , communication and guidance  so for this what i thought that i will make projects with Lora module , Zigbee , GPS ..using such modules with embedded c code only .

 

these are approach which i thought. Any other suggestion or correction ?????

i basically want guidance on what skills and how should i do to get into field of embedded systems and avionics 

 

 

 

 

jensenjoe wrote:

Good luck with the bit-banging!

 

if you have done bit -banging can u provide some online resources for it for bit banging in UART. mostly links which are given are for bit bangng in SPI or I2C.

 

 

 

 

 

 

 

  

<p>Kunal Gupta</p>

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

Kunalgupta wrote:
But doing so takes lots of time because i have to read datasheet of every module

 

Modern chips have 1000+ pages of datasheet. At some point you need to read it. My recent little project (got the parts and pcbs on friday!) used a STM32 processor which I'd not done much with before. Even though I used Arduino, there is problems running multiple i2c ports, so i had to write my own i2c master to solve the problem. Lots of reading to do. My pcb didn't work first up - guess what, i had to do some reading! Luckily it only needed a wire added. 

 

Things are getting more complex, so there's a greater learning curve. 

 

 

As for bit-banging uart - I was doing this when I was 10 as the micro I was using didn't have a uart - then again, most of them back then didn't have one. You should be able to work it out from first principles. 

Last Edited: Sun. Jan 12, 2020 - 05:23 AM