CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

cursor position

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

cursor position
PostPosted: Thu Feb 14, 2008 5:07 pm     Reply with quote

Hi All,
I am stuck with writing the code for choosing a number on a LCD.

Pic18f4525
This is not the main()
All the include's and defines are OK
The keypad,LCD and cursor works fine.


Here is where I’m stuck:
Code:

lcd_putc("\St    0123456789\n");   ///write this on first line of LCD
lcd_gotoxy(9,2); /////move the next write position(cursor) to 9 on row 2
lcd_putc("00.000uA");      ////write this in the location above
lcd_gotoxy(7,1);   /////move cursor position to 7 on line 1
while (TRUE) {
      k=kbd_getc();         ////call keypad
      if(k!=0)              //// carry on
       if(k=='4'){          ////if button 4 on keypad pressed
    lcd_gotoxy(8,1); ////////move cursor to 8 on line 1

Just in the context above I have 3 buttons
“>”, move the cursor right - This is the button 4 in **(k=='4')**
“<”, move the cursor left - This will be button 6 in **(k=='6')**
“Enter”, confirm choice and store - This will be button 2 in **(k=='2')**

All the code will only ever refer to 4,6 and 2 where each will perform the associated task.
I don’t have a problem in getting this to work – AS in displaying or moving the cursor.

But what I need to do is adapt it so that every time **(k=='4')** then the position of the cursor is moved to the right – this means that the (8,1); increments every time **(k=='4')** .
This also needs to be “else if “ with the opposite scenario, where **(k=='6')** position of the cursor is moved to the left – this means that the (8,1); decrements every time **(k=='6')** .
And then this needs to be “else” if enter is pressed, where **(k=='2')**.

If you’re a genius and can do it, then the icing on the cake would be to set limits for maximum position of left and right, where looking at
("\St 0123456789\n");

Maximum left would be position (7,1);
Maximum Right would be position (16,1);

I hope you understand what I’m trying to do. Sort of like an onboard keypad where the cursor chooses a value.
So in simple terms, the user can just keep pressing “>” and “<” until he moves to the desired number. This should be continuous only until enter is pressed.

Thanks for any help
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

cursor
PostPosted: Fri Feb 15, 2008 7:27 am     Reply with quote

I have done this so far, but where I am stuck is when I get lcd_get(i++,1), and then want to store this number and display it in the first digit on the second line. I dont know how what to do once I've written get lcd_get(i++,1).



Code:
#use delay(clock=4000000)
void steady_init(){
char k;
char i;
i=8;
   port_b_pullups(TRUE);
   lcd_init();
   kbd_init();
loop2:
    lcd_putc("\St    0123456789\n"); 
    lcd_gotoxy(9,2);           
    lcd_putc("00.000uA");   
   lcd_gotoxy(7,1);       
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(i++,1); }        
    else if(k=='2'){
           lcd_getc(i++,1);
          lcd_gotoxy(9,2);


Please help


This is where I have got to now but it still doesn't work. I think the problem is still with the lcd_getc(i++,1) in the code

Code:
#use delay(clock=4000000)
void steady_init(){
char k;
char i;
i=7;
   port_b_pullups(TRUE);
   lcd_init();
   kbd_init();
loop2:
    lcd_putc("\St    0123456789\n"); 
    lcd_gotoxy(9,2);           
    lcd_putc("00.000uA");   
   lcd_gotoxy(7,1);       
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(i++,1); }        
    else if(k=='2'){
    switch (lcd_getc(i++,1))        
{
case '0': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");
          lcd_gotoxy(9,2);           
          lcd_putc("00.000uA");
        break;   
case '1': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");   
          lcd_gotoxy(9,2);           
          lcd_putc("10.000uA");
        break;   
case '2': lcd_gotoxy (1,1);
        lcd_putc("\St    0123456789\n"); 
          lcd_gotoxy(9,2);           
          lcd_putc("20.000uA");
        break;   
case '3': lcd_gotoxy (1,1);
        lcd_putc("\St    0123456789\n"); 
          lcd_gotoxy(9,2);           
          lcd_putc("30.000uA");
        break;             
case '4': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");
          lcd_gotoxy(9,2);           
          lcd_putc("40.000uA");
        break;   
case '5': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");
          lcd_gotoxy(9,2);           
          lcd_putc("50.000uA");
        break;   
case '6': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");
          lcd_gotoxy(9,2);           
          lcd_putc("60.000uA");
        break;   
case '7': lcd_gotoxy (1,1);
          lcd_putc("\St    0123456789\n");   
          lcd_gotoxy(9,2);           
          lcd_putc("70.000uA");
        break;   
case '8': lcd_gotoxy (1,1);
        lcd_putc("\St    0123456789\n"); 
          lcd_gotoxy(9,2);           
          lcd_putc("80.000uA");
        break;   
case '9': lcd_gotoxy (1,1);
        lcd_putc("\St    0123456789\n"); 
          lcd_gotoxy(9,2);           
          lcd_putc("90.000uA");
        break;   


Please help
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

Cursor
PostPosted: Sat Feb 16, 2008 3:25 pm     Reply with quote

Completed and works great. In short, this was for an on screen keypad.
The problem was with the code yet again. I had to change the delays in the LCD.C to read the value where the cursor was present. Here is the complete code.

Program code:

Code:
#use delay(clock=4000000)
void steady_init(){
char k;
char d;
char p;
p=7;
    port_b_pullups(TRUE);
    lcd_init();
    kbd_init();
    lcd_putc("\St     0123456789\n"); 
    lcd_gotoxy(9,2);           
    lcd_putc("00.000uA");   
   lcd_gotoxy(7,1);       
while (TRUE) {
      k=kbd_getc(); 
      if(k!=0)     
       if(k=='6'){ 
         lcd_gotoxy(++p,1); }        
    else if(k=='4'){
          lcd_gotoxy(--p,1); }
    else if(k=='2'){
         d=lcd_getc(p,1);
     switch  (d)
{
case '0': lcd_gotoxy(9,2);           
          lcd_putc("00.000uA");
        break;         
case '1': lcd_gotoxy(9,2);           
          lcd_putc("10.000uA");
        break;             
case '2': lcd_gotoxy(9,2);           
          lcd_putc("20.000uA");
        break;           
case '3': lcd_gotoxy(9,2);           
          lcd_putc("30.000uA");
        break;             
case '4': lcd_gotoxy(9,2);           
          lcd_putc("40.000uA");
        break;             
case '5': lcd_gotoxy(9,2);           
          lcd_putc("50.000uA");
        break;             
case '6': lcd_gotoxy(9,2);           
          lcd_putc("60.000uA");
        break;           
case '7': lcd_gotoxy(9,2);           
          lcd_putc("70.000uA");
        break;           
case '8': lcd_gotoxy(9,2);           
          lcd_putc("80.000uA");
        break;           
case '9': lcd_gotoxy(9,2);           
          lcd_putc("90.000uA");
        break;   
       
}
}
}
}


Changes that are needed in LCD.C

code:

Code:
#use delay (clock=4000000)
// This structure is overlayed onto the data ports so that you may use
// whatever ports you desire
struct lcd_pin_map

  boolean dummy;      // PinA0 is not used
  boolean enable;     // PinA1
  boolean rw;         // PinA2
  boolean rs;         // PinA3
  int unusedA  : 4;   // The rest of portA
  int unusedB;        // portB is not used
  int unusedC;        // portC is not used
  int     data : 4;   // lower nibble of portD is used for data lines
  int unusedD  : 4;   // The rest of portD
} lcd;
#if defined(__PCH__)
  #locate lcd = 0xF80
#else
  #locate lcd = 5
#endif

struct lcd_tris_map
{
  boolean dummy;      // PinA0 is not used
  int control  : 3;
  int unusedA  : 4;   // The rest of portA
  int unusedB;        // portB is not used
  int unusedC;        // portC is not used
  int data     : 4;   // lower nibble of portD is used for data lines
  int unusedD  : 4;   // The rest of portD
} lcdtris;

#if defined(__PCH__)
  #locate lcdtris = 0xF92
#else
  #locate lcdtris = 0x85
#endif

#define set_tris_lcd(x) lcdtris.data = (x); lcdtris.control = 0;



#define lcd_type 2           // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40    // LCD RAM address for the second line


byte CONST LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xf, 1, 6};
                             // These bytes need to be sent to the LCD
                             // to start it up.


                             // The following are used for setting
                             // the I/O port direction register.

#define LCD_WRITE    0       // For write mode all pins are out
#define LCD_READ     15      // For read mode data pins are in


//--------------- Start of code ---------------------------------





byte lcd_read_byte() {
      byte low,high;

      set_tris_lcd(LCD_READ);
      lcd.rw = 1;
      delay_cycles(10);
      lcd.enable = 1;
      delay_cycles(10);
      high = lcd.data;
      lcd.enable = 0;
      delay_cycles(10);
      lcd.enable = 1;
      delay_us(5);
      low = lcd.data;
      lcd.enable = 0;
      set_tris_lcd(LCD_WRITE);
      return( (high<<4) | low);
}


void lcd_send_nibble( byte n ) {
      lcd.data = n;
      delay_cycles(1);
      lcd.enable = 1;
      delay_us(10);
      lcd.enable = 0;
}


void lcd_send_byte( byte address, byte n ) {

      lcd.rs = 0;
      while ( bit_test(lcd_read_byte(),7) ) ;
      //delay_ms(10);
     
      lcd.rs = address;
      delay_cycles(1);
      lcd.rw = 0;
      delay_cycles(1);
      lcd.enable = 0;
      lcd_send_nibble(n >> 4);
      lcd_send_nibble(n & 0xf);
}


void lcd_init() {
    byte i;

    set_tris_lcd(LCD_WRITE);
    lcd.rs = 0;
    lcd.rw = 0;
    lcd.enable = 0;
    delay_ms(100);
    for(i=1;i<=3;++i) {
       lcd_send_nibble(3);
       delay_ms(5);
    }
    lcd_send_nibble(2);
    for(i=0;i<=3;++i)
       lcd_send_byte(0,LCD_INIT_STRING[i]);
}


void lcd_gotoxy( byte x, byte y) {
   byte address;

   if(y!=1)
     address=lcd_line_two;
   else
     address=0;
   address+=x-1;
   lcd_send_byte(0,0x80|address);
}

void lcd_putc( char c) {
   switch (c) {
     case '\f'   : lcd_send_byte(0,1); break;
     case '\n'   : lcd_gotoxy(1,2);        break;
     case '\b'   : lcd_send_byte(0,0x10);  break;
     case '\t'   : lcd_send_byte(0,0x14);  break;
     default     : lcd_send_byte(1,c);     break;
   }
}

char lcd_getc( byte x, byte y) {
   char value;
    lcd_gotoxy(x,y);
    lcd.rs=1;
    value = lcd_read_byte();
    lcd.rs=0;
    return(value);
}




I am very happy again after 2 days of working this out.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group