Thread: Help needed in C to control an HD44780 with an ATMEL microcontroller

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    Help needed in C to control an HD44780 with an ATMEL microcontroller

    Hello guys, am using an AT91M55800A microcontroller on an AT91EB55 board, what i want to try and do is to read an 8 bit binary data from a text file and display it in ASCII characters obviously..

    so far i managed to control the lcd defined all the pins and it's workin fine i can display anythin on it now

    Code:
    #include "AT91M55800A.h"
    #include "lib_AT91M55800A.h"
    #include "eb55.h"
    #include "stdio.h"
    #include "lcd.c"
    
    
    extern void LCD_init (void );
    extern void LCD_WriteChar (unsigned int) ;
    extern void LCD_command (unsigned int);
    extern void LCD_normal (void);
    extern void LCD_blink_position (unsigned int, unsigned int);
    extern void LCD_display(unsigned int, unsigned int, const char *);
    extern int  LCD_busy (void);
    extern void delay_20ms(void);
    
    int main( void )
    
    {
          AT91PS_AIC     pAic;
            // Load System pAic Base address 
            pAic = AT91C_BASE_AIC; 
            
           //* Init
           // First, enable the clock of the PIOB
           AT91F_APMC_EnablePeriphClock ( AT91C_BASE_APMC, 1<<AT91C_ID_PIOB ) ;
    
           //LCD init
           LCD_init();
           //write char, one by one
            while(LCD_busy());
            LCD_WriteChar(0x1C);
            LCD_WriteChar(0x3C);
                      
            //blink postion 1,1
            LCD_blink_position(1,1);
                    
    }
    ***as u can see from the code that to display any character in ASCII i have to write its value in hex***

    **All the pins initialization and the funtion in the lcd.c

    the next step i wana try and read a text file with 8 binary numbers and display it on the lcd. i've written a code to change from binary to hex ... but not sure and i havent got a clear idea in my mind what to do.. any help would be appreciated, thank you


    the code to change bin2hex:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int bin2dec(char *bin);
    
    int main()
    {
            char bin[80] = "";
            char *p;
            int  dec;
    
            while(strcmp(bin,"0"))
            {
                    printf("\n Enter a binary number : ");
                    fgets(bin, sizeof(bin), stdin);
                    // check for and remove trailing \n
                    if ((p = strchr(bin,'\n')) != NULL)
                    {
                            *p = '\0';
                    }
                    dec = bin2dec(bin);
                    if (dec) printf("\n Hexadecimal = 0x%02X  \n",dec);
            }
    
            getchar();  
            return 0;
    }
    
    
    int bin2dec(char *bin)
    {
            int  b, k, m, n;
            int  len, sum = 0;
    
            len = strlen(bin) - 1;
            for(k = 0; k <= len; k++)
            {
                    n = (bin[k] - '0'); // char to numeric value
                    if ((n > 1) || (n < 0))
                    {
                            puts("\n\n  only 1 and 0!\n");
                            return (0);
                    }
                    for(b = 1, m = len; m > k; m--)
                    {
                            
                            b *= 2;
                    }
                    // sum it up
                    sum = sum + n * b;
                    
            }
            return(sum);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > LCD_WriteChar(0x1C);
    > LCD_WriteChar(0x3C);
    So what do you see when you do this?

    > fgets(bin, sizeof(bin), stdin);
    Since I doubt you have a file system, I'd say you need to read out of memory
    Code:
    char *lines[] = {
      "00001111",
      "11110000",
      "11001100",
    };
    Apply your function to each of those, and LCD_WriteChar the result.

    Also, look up the << operator, you can make your bin2dec() function a lot more compact.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    > LCD_WriteChar(0x1C);
    > LCD_WriteChar(0x3C);

    that was just an example like each one of them represnts an ASCII character from the table of the hd44780 lcd for example like
    > LCD_WriteChar(0x41); will display the character A on the screen

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So would
    LCD_WriteChar('A');

    This whole binary thing seems overly complicated to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 02-18-2009, 06:10 AM
  2. help needed with edit control & encryption
    By willc0de4food in forum Windows Programming
    Replies: 2
    Last Post: 03-16-2006, 08:21 PM
  3. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM