Thread: C for embedded systems

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    C for embedded systems

    Do you know where i could find some information about programming in c for embedded systems?

    A board where i could discuss with some who are involved...

    I am developing an application in Atmel's 89S8252 microcontroller and i am trying to connect a SEIKO 1216 graphic LCD to the controller via a 8255 programmable chip.

    If anyone could help me here, or there is another place i could find some help, please tell me.

    Thanks in advance

  2. #2
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    This comes up with some stuff that may be useful.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    There are some embedded software developers here at this board. Others can be found for example here: http://www.embedded.com.

    More about this microcontroller can be found here:
    http://www.atmel.com/atmel/acrobat/doc0401.pdf

    Is there a specific question you have?

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Sending logical 1-0 to ports

    Thanks for you help!

    Now i will ask you something more specific. I am trying to figure out but i still can't.

    My microcontroller has 4 ports.

    P0(0-->7), P1(...), P2, P3

    How can i say that the P2(1) = 1 for example?

    I have an example that says:
    ----------------------------------
    /*put this in initialize()*/
    sbit ena=0x81;
    sbit rs=0x82;
    sbit rd_wr=0x80;
    rd_wr=1; /*all writes to LCD*/

    void lcddatwr(uchar dbyte){
    rs=1;
    ena=1;
    P1=dbyte;
    ena=0;
    }

    ---------------------------------
    I can understand that
    0x81 = 81H = 8000 0001. (in schematic it is the 1st pin)

    What is this hex number. A place in memory? Is it a comand that when i say ena=1 to give the pin 5v voltage? And if yes, why is it 8000 0001 and not 7000 0001 or 9000 0001.

    So my general question is how can i seperate the 2nd bit from the 1st port from the 2nd bit from the 2nd or even the 3rd port?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If I remember my 8051 derivatives correctly, you do have bit addressable memory. The compiler documentation, along with the device datasheet, should be able to help with the specifics.

    For the example code you posted, sbit appears to be a language extension that allows the declaration of a bit-addressable data type. The "initialization" may actually be the association with a memory location.

    So my general question is how can i seperate the 2nd bit from the 1st port from the 2nd bit from the 2nd or even the 3rd port?
    From the documentation referred to by the link Shiro posted, P0 is at 0x80, P1 is at 0x90, and P2 is at 0xA0. So in a pinch, you might get away with something as quick and dirty as this.
    Code:
    *(unsigned char*)0x90 ^= 0x20; /* toggle state of P1.1 */
    But often the compiler has a header that has defines or special declarations for things such as ports. I'd check to see whether your compiler has provided such definitions/declarations for you already.

    You might access the bits in a different manner. What does your compiler documentation say regarding the sbit data? I would expect there might be some kind of access similar to the following.
    Code:
    P1.1 ^= 1; /* toggle state of P1.1 */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Okay, back at an undisclosed location I found some documentation for Keil's C51 compiler.
    sfr

    SFRs are declared in the same fashion as other C variables. The only difference is that the data type specified is sfr rather than char or int. For example:
    Code:
    sfr P0 = 0x80;     /*Port-0, address 80h */
    sfr P1 = 0x90;     /*Port-0, address 90h */
    sfr P2 = 0xA0;     /*Port-0, address A0h */
    sfr P3 = 0xB0;     /*Port-0, address B0h */
    P0, P1, P2, and P3 are the SFR name declarations. Names for sfr variables are defined just like other C variable declarations. Any symbolic name may be used in an sfr declaration.

    The address specification after the equal sign (=) must be a numeric constant. (Expressions with operators are not allowed.) This constant expression must lie in the SFR address range (0x80 to 0xFF).

    sbit

    With typical 8051 applications, it is often necessary to access individual bits with an SFR. The C51 compiler makes this possible with the sbit data type. The sbit data type allows you to access bit-addressable SFRs. For example:
    Code:
    sbit EA = 0xAF;
    This declaration defines EA to be the SFR bit at address 0xAF. On the 8051, this is the enable all bit in the interrupt enable register.

    NOTE
    Not all SFRs are bit-addressable. Only those SFRs whose address is evenly divisible by 8 are bit-addressable. The lower nibble of the SFR's address must be 0 or 8. For example, SFRs at 0xA8 and 0xD0 are bit-addressable, whereas SFRs at 0xC7 and 0xEB are not. To calculate an SFR bit address, add the bit position to the SFR byte address. So to access bit 6 in the SFR at 0xC8, the SFR bit address would be 0xCE (0xC8 + 6).


    Any symbolic name can be used in an sbit declaration. The expression to the right of the equal sign (=) specifies an absolute bit address for the symbolic name. There are three variants for specifying the address:

    • [...]
    Assuming much, I might make the following attempts to modify your example.
    Code:
    sfr P0 = 0x80;
    sfr P1 = 0x90;
    sfr P2 = 0xA0;
    sfr P3 = 0xB0;
    sbit P0rd  = 0x80;     /* variant 3 (int_constant) */
    sbit P0ena = P0 ^ 1;   /* variant 1 (sfr_name ^ int_constant) */
    sbit P0rs  = 0x80 ^ 2; /* variant 2 (int_constant ^ int_constant)  */
    sbit P1rd  = 0x90;
    sbit P1ena = 0x91;
    sbit P1rs  = 0x92;
    
    void main(void) /* not a hosted environment */
    {
       P0ena  = 1; /* set    P0.1 */
       P0rd   = 0; /* reset  P0.0 */
       P0ena ^= 1; /* toggle P0.1 */
       P1ena  = 0; /* reset  P1.0 */
       P1rs   = 1; /* set    P1.2 */
       P1rs  ^= 1; /* toggle P1.2 */
       P0  =  0x7; /* set P0.0, P0.1, and P0.2 */
       P0 &= ~0x2; /* reset P0.1 */
       P0 ^=  0x3; /* toggle P0.1 and  P0.2 */
    }
    This is completely untested, of course, because I don't have your target and compiler. But hopefully it is a close relative.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Thanks for the help!

    Now i have one more silly question

    I want the P0 to have this volts

    P07,P06,P05,P04,P03,P02,P01, P00
    1 , 0 , 1 , 0 , 1 , 1 , 0 , 0

    I could declare with patience

    P0^1 = 0
    P0^2 = 1
    ....

    But i think it would be easier if i could give a hex number because

    10101100 = 1010 1100 = 0xAC if i am not wrong

    So it would be great if i could say something like

    P0 = 0xAC and give these volts to the pins of the port 0.

    Is i possible and how?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    My first attempt would be just that. Test and see.
    Code:
    sfr  P0   = 0x80; /* declaration of P0 at location 0x80 */
    sbit P0_0 = 0x80; /* bit0 on P0 */
    void main(void)
    {
       P0 = 0xAC;     /* (byte access) assignment of values {1,0,1,0,1,1,0,0} to P0 */
       for( ;; )
       {
          /* the rest of the code */
          P0_0 ^= 1;  /* (bit access) toggle bit0 on P0 */
       }
    }
    What compiler are you using? From the documentation, is the sfr/sbit stuff similar? If so, then note that sfr is used to declare a byte and you have byte access; sbit is used to declare a bit and you have bit access. You can have either byte or bit access depending on the variable you choose to modify.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Thanks a lot!

    Thanks a lot Dave!

    PS. I am using Keil.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Systems Programming
    By shabbirhussain in forum C Programming
    Replies: 1
    Last Post: 10-05-2008, 07:22 AM
  2. APIs for windowing in other systems
    By sean in forum Tech Board
    Replies: 5
    Last Post: 08-23-2004, 11:45 AM
  3. book recommendation (operating systems)
    By iain in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-17-2002, 05:29 AM
  4. A tale of two systems.
    By sean in forum Linux Programming
    Replies: 8
    Last Post: 05-25-2002, 12:12 AM