Displaying using LCD

This is a discussion on Displaying using LCD within the C Programming forums, part of the General Programming Boards category; Code: #include <p18f4520.h> void Init_LCD(void); void W_ctr_8bit(char); void W_data_8bit(char); void Delay_1kcyc(void); #define LCD_DATA PORTD #define LCD_RS PORTBbits.RB1 #define LCD_E PORTBbits.RB2 ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    26

    Displaying using LCD

    Code:
    #include <p18f4520.h>
    void Init_LCD(void);
    void W_ctr_8bit(char);
    void W_data_8bit(char);
    void Delay_1kcyc(void);
    
    #define LCD_DATA   PORTD
    #define LCD_RS       PORTBbits.RB1
    #define LCD_E         PORTBbits.RB2
    
    unsigned char LCD_TEMP, i,j,k,m;
    char MESS[8] = "WELCOME";
    
    void main ()   {
            
               ADCON1=0x0F;
               TRISB = 0b11111001;
               TRISD = 0;
    
               Init_LCD();
               for (i=0; i<7; i++)
                          W_data_8bit(MESS(i));
               while(1);
    }
    
    void Init_LCD () {
               W_ctr_8bit(0b00111000);
               W_ctr_8bit(0b00001100);
               W_ctr_8bit(0b00000110);
               W_ctr_8bit(0b00000001);
               W_ctr_8bit(0x02);
    }
    
    void W_ctr_8bit(char x) {
               LCD_RS           = 0;
               LCD_E             = 1;
               LCD_DATA       = x;
               LCD_E             = 0;
               Delay_1kcyc();
    }
    
    void W_data_8bit(char x) {
               LCD_RS           = 1;
               LCD_E             = 1;
               LCD_DATA       = x;
               LCD_E             = 0;
               Delay_1kcyc();
    }
    
    void Delay_1kcyc() {
              unsigned int j;
                        for(j=0, j<32; j++);
    }
    Hi guys, the above code is to display the LCD a HELLO word, on PIC18f4520 using the MPLab IDE, Is it possible if I add on a ADC Conversion part to display the result the value in the same way?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Sure it is, you just need to convert the reading from the ADC into some kind of string, and you're there.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    hmmm, before I go into the ADC. I want to display 2 lines of text, which part of the program should i edit??

    so i first i need to activate the ADC right?.

    Code:
    #include <adc.h>
    
    void ADC_Init(void)
    {
              TRISAbits.TRISA0 = 1;
    ADCON0 = 0x01;
    ADCON1 = 0x0E;
    }

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    > I want to display 2 lines of text, which part of the program should i edit??
    Anything which refers to 'MESS' is worthy of further examination.

    > so i first i need to activate the ADC right?
    Seems like a good idea 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    Need to store the ADC reading into somewhere first right? Than from the somewhere I call it out to display at the LCD?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Yes.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    Code:
    #include <p18f4520.h>
    #include <adc.h>
    #include <delays.h>
    
    void ADC_Init(void);
    void LCD_Init(void);
    void W_ctr_8bit(char);
    void W_data_8bit(char);
    void Delay_1kcyc(void);
    
    #define LCD_DATA     PORTD
    #define LCD_RS       PORTBbits.RB1
    #define LCD_E        PORTBbits.RB2
    
    unsigned char LCD_TEMP, i,j,k,m;
    char MESS[50]
    
    void main ()   {
            
               ADCON1=0x0F;
               TRISB = 0b11111001;
               TRISD = 0;
    
    		   scanf("%s",&MESS);
    		   sprintf(I don't know what to put here
    
               LCD_Init();
               for (i=0; i<50; i++)
                          W_data_8bit(MESS(i));
               while(1);
    }
    
    void ADC_Init (void) {
    		 TRISAbits.TRISA0 = 1;
    
    	ADCON0 = 0x01;
    	ADCON1 - 0x0F;
    }
    void LCD_Init (void) {
               W_ctr_8bit(0b00111000);
               W_ctr_8bit(0b00001100);
               W_ctr_8bit(0b00000110);
               W_ctr_8bit(0b00000001);
               W_ctr_8bit(0x02);
    }
    
    void W_ctr_8bit(char x) {
               LCD_RS           = 0;
               LCD_E           	= 1;
               LCD_DATA       	= x;
               LCD_E            = 0;
               Delay_1kcyc();
    }
    
    void W_data_8bit(char x) {
               LCD_RS     		= 1;
               LCD_E            = 1;
               LCD_DATA       	= x;
               LCD_E            = 0;
               Delay_1kcyc();
    }
    
    void Delay_1kcyc() {
              unsigned int j;
                        for(j=0; j<32; j++);
    }
    I want to get the input from the ADC, than display it on a LCD. May it be hex numbers or some values. How can i just make it display?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    - init the lcd
    - init the adc
    - display your message

    Then in a loop
    - read an adc value
    - use sprintf to format that value as a string
    - display your string
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    Code:
    #include <p18f4520.h>
    #include <adc.h>
    #include <delays.h>
    
    void ADC_Init(void);
    void LCD_Init(void);
    void W_ctr_8bit(char);
    void W_data_8bit(char);
    void Delay_1kcyc(void);
    
    #define LCD_DATA     PORTD
    #define LCD_RS       PORTBbits.RB1
    #define LCD_E        PORTBbits.RB2
    
    unsigned char LCD_TEMP, i,j,k,m;
    
    unsigned int read_adc(unsigned char adc_input){
      ADMUX=adc_input|ADC_VREF_TYPE;
      ADCSRA=( (1<<ADEN) | (1<<ADSC) | (1<<ADIE) | (1<<ADPS1) | (1<<ADPS2) 
    );
      while(!(ADCSRA & 0x10)); 
      ADCSRA|=0x10;
      return ADCW;
    }
    
    unsigned char *temperature(unsigned char channel,unsigned int *maxt){
      unsigned int temp,temp_0,temp_1;
      double mytemp;
      char buf[10],tmp[10];
      mytemp=read_adc(channel);
      mytemp*=2.56/1.024;
      temp=mytemp;
      if(temp>*maxt) *maxt=temp;
      temp_0=temp/10;
      sprintf(buf,"%d",temp_0);
      temp_1=(temp%100)%10;
      sprintf(tmp,"%d",temp_1);
      strcat(buf,"."); strcat(buf,tmp);
      return buf;
    }
    
    void main ()   {
            
               TRISB = 0b11111001;
               TRISD = 0;
    			
    		   ADC_Init();
               LCD_Init();
               for (i=0; i<50; i++)
                          W_data_8bit(MESS(i));
               while(1);
    }
    
    void ADC_Init (void) {
    		 TRISAbits.TRISA0 = 1;
    
    	ADCON0 = 0x01;
    	ADCON1 - 0x0F;
    }
    void LCD_Init (void) {
               W_ctr_8bit(0b00111000);
               W_ctr_8bit(0b00001100);
               W_ctr_8bit(0b00000110);
               W_ctr_8bit(0b00000001);
               W_ctr_8bit(0x02);
    }
    
    void W_ctr_8bit(char x) {
               LCD_RS           = 0;
               LCD_E           	= 1;
               LCD_DATA       	= x;
               LCD_E            = 0;
               Delay_1kcyc();
    }
    
    void W_data_8bit(char x) {
               LCD_RS     		= 1;
               LCD_E            = 1;
               LCD_DATA       	= x;
               LCD_E            = 0;
               Delay_1kcyc();
    }
    
    void Delay_1kcyc() {
              unsigned int j;
                        for(j=0; j<32; j++);
    }
    This is what I have so far, any comments?? I followed your thingy. But there's some parts I'm not too sure about for eg.

    Code:
    void W_data_8bit(char x) {
               LCD_RS     		= 1;
               LCD_E            = 1;
               LCD_DATA       	= x;
               LCD_E            = 0;
               Delay_1kcyc();
    }
    What does this part of the code do??. I tried researching on the net, but to no avail.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    unsigned char *temperature(unsigned char channel,unsigned int *maxt)
    Yes, call this inside your empty while(1); loop.

    But first, you need to fix this
    return buf;
    You can't return a pointer to a local array.

    > What does this part of the code do?
    I thought that part was given to you by your book / tutor / tutorial, and I would assume it would have explained it.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    Omg, I think I'm almost done.

    um, the tutorial was given me to 2years ago. So I don't really remember what it does.

    I've been trying alot on my own, because some parts don't work

    Code:
    unsigned int read_adc(unsigned char adc_input){
      ADMUX=adc_input|ADC_VREF_TYPE;
      ADCSRA=( (1<<ADEN) | (1<<ADSC) | (1<<ADIE) | (1<<ADPS1) | (1<<ADPS2) 
    );
      while(!(ADCSRA & 0x10)); 
      ADCSRA|=0x10;
      return ADCW;
    }
    Hmmm, what could be the problem?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    It would be good if you describe what goes wrong.

    You are waiting for bit 4 (0x10) to get set, then you set the same bit in the register - is that what you want to do (by no means am I saying you shouldn't do this - depends on the way the register is defined - it's not impossible that the register is one where you have to write different things than you read).

    --
    Mats

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Code:
    >void ADC_Init (void) {
    >		 TRISAbits.TRISA0 = 1;
    >
    >	ADCON0 = 0x01;
    >	ADCON1 - 0x0F;
    >}
    Are you sure you copied this right? The line in red doesn't do anything. And you haven't called this function in main() either.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,235
    This code shouldn't even compile. This line is wrong:

    Code:
    char MESS[8] = "WELCOME";
    ...
    ...
    ...
    W_data_8bit(MESS(i));

  15. #15
    Registered User
    Join Date
    Jul 2007
    Posts
    26
    matsp,
    the compiler say there is an error, but a friend of mine told me, it's wrong register for the PIC Controller, that is he said it's register for AVR compiler, but i'm using the MP LAB ID2 with a PIC18f4520 microchip, i'm checking the datasheets to change the routine. if possible.

    swoopy,
    i think i typed wrongly. should be ADCON1 = 0x0F; right?

    brewbuck,
    that part is the older code i think, the newest one i've change the mess into the
    reading from the ADC

    Code:
    void main ()   {
            
               TRISB = 0b11111001;
               TRISD = 0;
               
               ADC_Init();
               LCD_Init();
                   for (i=0; i<50; i++)
                          unsigned char *temperature(unsigned char channel,unsigned int *maxt)
               while(1);
    }
    am I'm suppose to define the TRISB part at the top
    Code:
    #define TRISB_VALUE      0b11111001;
    or i should keep the TRISB at the same place?.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. lcd tv as monitor?
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-25-2008, 03:35 AM
  2. change LCD port for microcontroller
    By iedchan in forum C Programming
    Replies: 2
    Last Post: 05-11-2007, 06:19 AM
  3. LCD vs CRT
    By Brain Cell in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 01-26-2005, 12:03 PM
  4. LCD for gaming, any suggestion?
    By alphaoide in forum Tech Board
    Replies: 0
    Last Post: 05-02-2004, 05:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21