What does one learn about the hardware when coding via C language?

Status
Not open for further replies.
With microcontrollers: I think it is important to understand your hardware as thoroughly as possible, no matter what language you use.
With personal computers: I think it is important that the programming language is as portable and easy to maintain as possible, no matter what the underlying hardware is.

Actually C does not hide the hardware specific things from a microcontroller programmer. It's the libraries that do the hiding. I bet most hobby C-programmers wouldn't know how to set up an interrupt handler without the library macros. And I bet most assembly coders wouldn't know how to do that either (in C).. But when you learn how to do that, in C or in asm, you must learn about the hardware.
 
Last edited:
Can you see any difference between,
Code:
    movlw    0x07
    movwf    CMCON
and
Code:
    CMCON=0x07;
You still need to know the hardware at the same level to write either.

Here is typical code to initialize a pic,
Code:
    OSCCON=0x70;            //8MHz please
    OSCTUNEbits.PLLEN=1;    //*4 = 32MHz
    ADCON1=0x07;            //no ADC
    CMCON=0x07;
    INTCON2bits.NOT_RBPU=0; //WPU on
    T1CON=0;                //init timer 1
    T2CON=0b01001110;       //pre=16,post=10
    PR2=250;                //200 times per second
    PIE1bits.TMR2IE=1;
    PIE1bits.RCIE=1;
    INTCONbits.GIE=1;       //Global interrupts enabled
    INTCONbits.PEIE=1;       //Peripheral interrupts enabled
Looks to me like you need the same level of hardware knowledge. Unless, of course, you use all the libraries but I find it's quicker to learn the chip than the libraries and far more flexible.

Mike.
 
my own persoanl experiance limited as it is leads me to think in many cases the libs are harder to use, for example the adc lib has different versions for different chips and there are a couple of errors in them, when i used ADC i found it easier to use the registers directly from the datasheet. so in this case i agree with mike.
whateve language you use at some point your going to have to read the datasheet and get involved with the hardware
 

The register definitions are usually library macros too (dereferenced pointers to the memory locations). I agree that some library functions are quite useless.. Why would I call "ADC_read(1);" function when I can just read the register containing the conversion result. And I really hate what Arduino IDE has done to microcontroller programming.

With AVRs, if you want to write 0x55 to register PORTA, you would write (in C):
PORTA = 0x55;

The preprocessor will expand this to:
(*(volatile unsigned char *)(0x1B)) = 0x55;

After compiling the final opcode becomes:
Code:
66:       	PORTA = 0x55;
+00000047:   E585        LDI       R24,0x55       Load immediate
+00000048:   BB8B        OUT       0x1B,R24       Out to I/O location

Some macros can have unexpected results, if the programmer doesn't know what the macro really does. So you really need to know your hardware and your tools. Whatever they are.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…