Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

A coupla questions on programming for a microprocessor for a C dufus

Hi! thanks so much if you can help, its just a few general questions


I am programming for the 68HC12 microprocessor (got the short straw).. I am using its pulse-width modulator, and have this demo code. Is this all I need? Do i need to define all those "PWCLK" variables (as in int=PWCLK) etc

[italic][color=Green]#include "hc12b32.h"
main()
{
/* Choose 8-bit mode */
PWCLK = PWCLK &

Comments

  • LundinLundin Member Posts: 3,711
    Moved post to Embedded C/C++.

    HC(S)12 happens to be the mcu I work with almost every day, so feel free to ask any questions about it on this board.

    ---

    Yes, you need to define all those variables as they are actually the registers of the processor. Most compilers (like Codewarrior for example) comes with a pre-defined register map. If you have a register map, you just need to include a h-file and nothing else. It seems likely that "hc12b32.h" is the register map. In that case you can ignore the rest of this post.

    Otherwise, you must define the registers manually. You'll have to look up the register address in the manual, then declare it like this:

    [code]#define REG_ABC (*(volatile unsigned char*)0x1234)[/code]

    Where 0x1234 is the register address and REG_ABC is the register name.

    To explain the above:

    You wish to access the address. For that you need a pointer. Registers are usually 8 bits, so unsigned char would be the right type to use. The first step is to make this:

    (unsigned char*)0x1234

    Next thing is to understand the volatile keyword. By using it you tell the compiler that the variable's value might change at any time, so the compiler won't do any weird optimizations on the variable. One must always use volatile for hardware registers.

    (volatile unsigned char*)0x1234

    Next: you wish to write a macro for this, so that you can use the register as if it was a common variable. So you take the contents of the variable's addess:

    *( (volatile unsigned char*)0x1234 )

    or simply

    *(volatile unsigned char*)0x1234 /* equivalent */

    Then, add extra parenthesises to make the macro safe no matter how it is called:

    (*(volatile unsigned char*)0x1234)
Sign In or Register to comment.