1. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Hi.
    Ran in to some problems with pins on my 16f1509
    need to move the datastream for my LCD from port B ( RB4-7)
    and over to Port C. ( RC4-7 ) and still need the RS & E on RC1-2
    is that possible ?
    Can i just change the line in LCD.C "#define LCD_DATA PORTB" to
    "#define LCD_DATA PORTC"
    and still use "#define LCD_RS RC2" "#define LCD_EN RC1"
    Scematic attached.
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,025
    You can in hardware but you'll have to look at the code that writes the 4 bit data to the port. Since PORTB is only 4 bits, you can just write the data to the port, ignoring the 4 LSbits. Moving the bus to PORTC would require you to change only the 4 data bits without affecting the other bits on the port. This can be done by AND and OR like so:

    Code ( (Unknown Language)):
    1. char temp;
    2.  temp = LATC & 0x0f;  read current port, clear bits 4-7, preserve 0-3
    3.  temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
    4.  LATC = temp;
    After writing to the bus, you can use bsf,bcf to strobe the other bits.
     
  3. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    think i know what you mean, but i tink the code is a bir wrong.
    1. read the port. and clear bit 4-7, ok, dont see how.
    2.
    Code ( (Unknown Language)):
    1.  temp |= New_LCD_Data ; assumes new LCD data is justified in temp as dddd0000
    meaning ??
    3. wat NeXT, u are use ASM code together with c code.



     
  4. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,025
    I didn't intend to write complete code, just give you the basic idea... We'll call it pseudocode.

    1). You are reading the current value of LATC to preserve bits 0-3 since when done, you can only write the full 8 bits. By reading into a temp variable then doing the bitwise AND on temp, you clear bits 4-7 (the old LCD data) and leave the other bits alone. This paves the way for the ORing of the new LCD data.

    2) You combine the new LCD data 4-7 with the old bits in temp 0-3 by ORing:
    0000bbbb (temp, bbbb is bits 0-3 that you want to preserve, 0000 is from the AND)
    dddd0000 (new LCD data dddd0000, bits 0-3 here are 0 to preserve bbbb in temp. OR'ed together you get:

    ddddbbbb the new data combined with the unchanged old bits. That's what gets written to the port.

    3)What's next is that you get the next 4 bit nibble of the LCD data, justify it to dddd0000 again (by shifting) then you have the 2ed half of the LCD data byte and you repeat the process. That part of the code should be the same as if you used PORTB for the databus.
     
    FroceMaster likes this.
  5. FroceMaster

    Thread Starter Member

    Jan 28, 2012
    400
    4
    Here is my "new" code, and it Works ;)

    Code ( (Unknown Language)):
    1.  
    2.  */
    3. #ifndef _XTAL_FREQ
    4.  // Unless specified elsewhere, 4MHz system frequency is assumed
    5.  #define _XTAL_FREQ 4000000
    6. #endif
    7.  
    8. #include <htc.h>
    9. #include "lcd.h"
    10. #define LCD_RS RB6
    11. #define LCD_EN RB7
    12. #define LCD_D4 RC4 // Data bits
    13. #define LCD_D5 RC5 // Data bits
    14. #define LCD_D6 RC6 // Data bits
    15. #define LCD_D7 RC7 // Data bits
    16. #define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
    17. /* write a byte to the LCD in 4 bit mode */
    18. void
    19. lcd_write(unsigned char c)
    20. {   static unsigned char temp;
    21.      temp=c;
    22.  __delay_us(40);
    23.  
    24.  if(c & 0x80) LCD_D7=1; else LCD_D7=0;
    25.  if(c & 0x40) LCD_D6=1; else LCD_D6=0;
    26.  if(c & 0x20) LCD_D5=1; else LCD_D5=0;
    27.  if(c & 0x10) LCD_D4=1; else LCD_D4=0;
    28.  LCD_STROBE();
    29.  if(c & 0x08) LCD_D7=1; else LCD_D7=0;
    30.  if(c & 0x04) LCD_D6=1; else LCD_D6=0;
    31.  if(c & 0x02) LCD_D5=1; else LCD_D5=0;
    32.  if(c & 0x01) LCD_D4=1; else LCD_D4=0;
    33.  LCD_STROBE();
    34. }
    35. /*
    36.  *  Clear and home the LCD
    37.  */
    38. void
    39. lcd_clear(void)
    40. {
    41.  LCD_RS = 0;
    42.  lcd_write(0x1);
    43.  __delay_ms(2);
    44. }
    45. /* write a string of chars to the LCD */
    46. void
    47. lcd_puts(const char * s)
    48. {
    49.  LCD_RS = 1; // write characters
    50.  while(*s)
    51.   lcd_write(*s++);
    52. }
    53. /* write one character to the LCD */
    54. void
    55. lcd_putch(char c)
    56. {
    57.  LCD_RS = 1; // write characters
    58.  lcd_write( c );
    59. }
    60.  
    61. /*
    62.  * Go to the specified position
    63.  */
    64. void
    65. lcd_goto(unsigned char pos)
    66. {
    67.  LCD_RS = 0;
    68.  lcd_write(0x80+pos);
    69. }
    70.  
    71. /* initialise the LCD - put into 4 bit mode */
    72. void
    73. lcd_init()
    74. {
    75.  
    76. //NY RUTINE  
    77.   LCD_RS = 0; // write control bytes
    78. __delay_ms(15);// power on delay
    79.  LCD_D4 = 1; // init!
    80.  LCD_D5 = 1; //
    81.  LCD_STROBE();
    82.  __delay_ms(5);
    83.  LCD_STROBE(); // init!
    84.  __delay_us(100);
    85.  LCD_STROBE(); // init!
    86.  __delay_us(5);
    87.  LCD_D4 = 0; // set 4 bit mode
    88.  LCD_STROBE();
    89.  __delay_us(40);
    90.  
    91.  lcd_write(0x28);// 4 bit mode, 1/16 duty, 5x8 font, 2lines
    92.  lcd_write(0x0C);// display on
    93.  lcd_write(0x06);// entry mode advance cursor
    94.  lcd_write(0x01);// clear display and reset cursor
    95. }
    96.  
     
Loading...