header from Scratch [SOLVED]

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
It would be interesting to know how the first C program was written for microcontroller. I am curious to know what would be the programmer approach when he is asked to write code for a new microcontroller by looking at the datasheet.

i think like this I would decide to have a header file and main.c file in my code
My real trouble would start when creating the header file for the microcontroller. I know MCU hardware header file will contain addresses of all hardware peripherals, registers, and names of all bits in all registers. All this information was given in the datasheet.

I have seen the header file of other microcontroller's hardware but that is beyond on my understanding. Everyone uses the preprocessor, but everyone's method is different. Overall we want the compiler to identify at which location we want to set the mode

There may be a different opinion here one may ask why you want to create header file when it is already available on internet. The answer is, I don't want to create a header file, I just want to understand how it is created. Why and how different processors are used

What would be your approach if someone ask you to write code including device header file for a new microcontroller by looking at the datasheet.
 

Yaakov

Joined Jan 27, 2019
3,565
The first program in C for an MCU evolved, it didn't look the way ti does today. The software tools for programming MCUs in high-level languages took many years to become what we have.

The first programs would have been assembler. The first C code would have relied on specific assembly language extensions.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
The first programs would have been assembler. The first C code would have relied on specific assembly language extensions.
@Yaakov Thanks!
What would be your approach if someone ask you to write code including device header file for a new microcontroller by looking at the datasheet.
 

Yaakov

Joined Jan 27, 2019
3,565
@Yaakov Thanks!
What would be your approach if someone ask you to write code including device header file for a new microcontroller by looking at the datasheet.
Not being a specialist in this area, I can't help much. But you might want to investigate the nature of the toolchain. The various layers and components are fundamental to understanding how everything works together.
 

Ian0

Joined Aug 7, 2020
3,459
To make sure that I wrote adequate documentation!
Most MCUs I have used have excellent hardware manuals, that say exactly how each peripheral works, what values have to be put in which registers to make it do what is required.
I wish I could say the same for the peripheral drivers that are provided in C. I usually write my own in assembler based on what is in the hardware manual.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
I think writing code for device header files is really a difficult task. I don't get any clues

Consider an something like : addresses of hardware peripherals, registers, PORT
P1 = 0x 01
P2 = 0x 02
P3 = 0x 03
P4 = 0x 04
INTCON = 0x05
OSCCON =0x 06
TMR =0x 07
TMRH =0x 08
TMRL = =0xD0
STATUS =0xFF

What preprocessor would you use for this in device header file
 

BobTPH

Joined Jun 5, 2013
3,765
think writing code for device header files is really a difficult task. I don't get any clues
No, it is not hard, it is tedious. I am developing a compiler targeting pic24 and pic33 processors. I spent weeks going through the family reference manual, coding up a class definition fir each hardware peripheral.

The hard oart if doing this project is determining exactly how the Microchip tools work so that i can be compatible with their tools.
Bob
 

BobTPH

Joined Jun 5, 2013
3,765
Look at the headers provided by any microcontroller tool set. That is how it is done. The prerequisites are knowing the language and understanding the datasheet.

Bob
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
hi P,
It appears I am always posting links for you, thats not too much of a problem.:)
But a quick web search gets the information you want, there are some well written documents and video's out there.
Thank you for helping me every time. I would like to apologize to you. But both of your links are not related to my issue.

your first link show how to make header file which is in c++ language. In your second link shown how to create header file.

I wanted to know how can we write code for header file by looking at the datasheet. What information is there in the datasheet that should have been in the hidden file? From the discussion it was found that the addresses of the memory, register, ports should be defined in the header file. I have seen that defined keywords are being used everywhere in the header file as well as some other macros being used. I was trying to understand why other micro is used.

I am glad that my question is getting answered well

Update I saw header for at89c51, Atmega328, and pic16f877
 

ericgibbs

Joined Jan 29, 2010
13,825
I wanted to know how can we write code for header file by looking at the datasheet.
hi P,
Until you know what a Header file is and what information it needs to contain, how can you possible learn what is needed in a Header file by reading a MCU datasheet.??:rolleyes:

You are, as they say, 'putting the cart before the horse'


E
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
294
hi P,
Until you know what a Header file is and what information it needs to contain, how can you possible learn what is needed in a Header file by reading a MCU datasheet.??
I have learned how to create header file in c language. For this I have also tested file by writing a program on my computer.
 

bug13

Joined Feb 13, 2012
1,995
What would be your approach if someone ask you to write code including device header file for a new microcontroller by looking at the datasheet.
Assuming you means just the header files (not including linker script and start up code). You just define all the register address.

For example:
C:
/* assuming it's a 8 bit system */
typedef volatile uint8_t register_t

/* 0x10000000 is whatever address of a register in a datasheet */
#define REG_A    (register_t*)(0x10000000)

/* then you can access the register like this */
REG_A = 0x01; /* set bit 0 */
Repeat all these for all the registers for the MCU you need to make the header file for.
 
Top