PIC18F4550 - Buttons and LCD

Discussion in 'Embedded Systems and Microcontrollers' started by Bellon, Nov 1, 2015.

  1. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    Hello!
    I need to schedule four buttons with the following functions "back" , " OK" , " up " and " down " . I have this code here but found it difficult to compile in mplab and also program along with the messages that I want to appear on my LCD (16x2) . I was trying to use vectors to display the phrases in the LCD , but it's getting so big that I'm losing myself easily. So I decided to delete that part of the code and only left the "skeleton" of it.
    I appreciate if you could help me on this, the question of the buttons and how to put a menu to appear on the LCD with this code . I'm kind stuck and what I found on the Internet did not help much .

    I'm using a pic18f4550.

    Back -> RB1
    OK -> RB0
    UP-> RB2
    Down-> RB3

    Code (Text):
    1. #include <xc.h>
    2. #include <stdio.h>
    3. #include "lcd.h"
    4. #ifndef _XTAL_FREQ
    5. #define _XTAL_FREQ 16000000
    6. #endif
    7.  
    8. //filter keyboard
    9. #define MAX_FILTER_CNT     250
    10.  
    11. unsigned char Key1Cnt = 0;
    12. unsigned char Key2Cnt = 0;
    13. unsigned char Key3Cnt = 0;
    14. unsigned char Key4Cnt = 0;
    15.  
    16. unsigned char Key1 = 0;
    17. unsigned char Key2 = 0;
    18. unsigned char Key3 = 0;
    19. unsigned char Key4 = 0;
    20.  
    21. if (Key1  != RB0)
    22. {
    23. Key1Cnt++;
    24.  
    25. if(Key1Cnt > MAX_FILTER_CNT )
    26. {
    27.     Key1Cnt  = 0;
    28.     Key1  = RB0;
    29. }
    30. }
    31.  
    32. else
    33. {
    34.     Key1Cnt  = 0;
    35. }
    36.  
    37. if (Key2  != RB1)
    38. {
    39. Key2Cnt++;
    40.  
    41. if(Key2Cnt > MAX_FILTER_CNT )
    42. {
    43.     Key2Cnt  = 0;
    44.     Key2  = RB1;
    45. }
    46. }
    47.  
    48. else
    49. {
    50.     Key2Cnt  = 0;
    51. }
    52.  
    53. if (Key3  != RB2)
    54. {
    55. Key3Cnt++;
    56.  
    57. if(Key3Cnt > MAX_FILTER_CNT )
    58. {
    59.     Key3Cnt  = 0;
    60.     Key3  = RB2;
    61. }
    62. }
    63.  
    64. else
    65. {
    66.     Key3Cnt  = 0;
    67. }
    68.  
    69. if (Key4  != RB3)
    70. {
    71. Key4Cnt++;
    72.  
    73. if(Key4Cnt > MAX_FILTER_CNT )
    74. {
    75.     Key4Cnt  = 0;
    76.     Key4  = RB3;
    77. }
    78. }
    79.  
    80. else
    81. {
    82.     Key4Cnt  = 0;
    83. }
    84.  
    85. void inicio(){
    86.     lcd_clear();
    87.     printf("Control system");
    88.     printf("greenhouse");
    89. }
    90.  
    91. void insistema(){  
    92.     lcd_clear();
    93.     printf("1 \n");    
    94.     printf("2 \n");
    95.                                
    96. }
    97.  
    98. void informacoes(){         //shows a list of options
    99.                            
    100.     var=1;
    101. }
    102.  
    103. //options
    104. void cult0(){
    105. lcd_clear();
    106.     printf("op1");
    107. }
    108. void cult1(){
    109. lcd_clear();
    110.     printf("op2");
    111. }
    112. void cult2(){
    113. lcd_clear();
    114.     printf("op3 ");
    115. }
    116. void cult3(){
    117. lcd_clear();
    118.     printf("op4");
    119. }
    120. void cult4(){
    121. lcd_clear();
    122.     printf("op5");
    123. }
    124. void cult5(){
    125. lcd_clear();
    126.     printf("op6");
    127. }
    128. void cult6(){
    129. lcd_clear();
    130.     printf("op7");
    131. }
    132. void cult7(){
    133. lcd_clear();
    134.     printf("op8");
    135. }
    136.  
    137. void cult8(){
    138. lcd_clear();
    139.     printf("op9");
    140. }
    141.  
    142. void cult9(){
    143. lcd_clear();
    144.     printf("op10");
    145. }
    146.  
    147. void cult10(){
    148. lcd_clear();
    149.     printf("op11");
    150. }
    151.  
    152. void cult11(){
    153. lcd_clear();
    154.     printf("op12");
    155. }
    156.  
    157. //information about each option
    158. //(It needs more than four lines for each information option)
    159. void inf0(){
    160. lcd_clear();
    161.     printf("inf1 ....");
    162. }
    163. void inf1(){
    164. lcd_clear();
    165.     printf("inf2 .....");
    166. }
    167. void inf2(){
    168. lcd_clear();
    169.     printf("inf3 .....");
    170. }
    171. void inf3(){
    172. lcd_clear();
    173.     printf("inf4 ......");
    174. }
    175. void inf4(){
    176. lcd_clear();
    177.     printf("inf5  ......");
    178. }
    179. void inf5(){
    180. lcd_clear();
    181.     printf("inf6 ......");
    182. }
    183. void inf6(){
    184. lcd_clear();
    185.     printf("inf7 ......");
    186. }
    187. void inf7(){
    188. lcd_clear();
    189.     printf("inf8 .....");
    190. }
    191.  
    192. void inf8(){
    193. lcd_clear();
    194.     printf("inf9 .....");
    195. }
    196.  
    197. void inf9(){
    198. lcd_clear();
    199.     printf("inf10 ......");
    200. }
    201.  
    202. void inf10(){
    203. lcd_clear();
    204.     printf("inf11 .....");
    205. }
    206.  
    207. void inf11(){
    208. lcd_clear();
    209.     printf("inf12 ......");
    210. }
    211.  
    212.  
    213.  
    214. int main(void){
    215.    
    216.     TRISD=0x00;
    217.     TRISB=0x0F;
    218.     ADCON1=0XFF;
    219.     CMCON=0X07;
    220.     PORTD=0;
    221.    
    222.     for(aux=0; aux<200;aux++) __delay_ms(10);
    223.     lcd_init();
    224.    
    225.     printf("Control system");
    226.     printf("greenhouse");
    227.    
    228.     while(1)
    229.     {
    230.        
    231.     }
    232. }
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    Exactly what do you expect all of those printf statements to do?
     
  3. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    All variables that start with "cult" are the options. When they are selected via the "ok" button , will show the information, " inf ".
    I put everything in office , but probably the part of the options do not need.
     
  4. djsfantasi

    AAC Fanatic!

    Apr 11, 2010
    2,790
    827
    A read of the code looks like your braces are not matched the way you expect (could be wrong). What are you using as your IDE? Some of them have an auto-format option which indents your code and makes it easier to see where the code segments line up.

    Also, where are your functions "cultn" and "infn" are used?

    And some of your names, like "insistema" are not in English and hard for me to understand.
     
  5. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    I didn't use the functions yet. I was working with vectors, but it was with some problems, so I need your help, to see if it's necessary use character vector or something like that. "insistema" would be to start the system. I'll put some sensors, and measurements will start after I select for the system start .
     
  6. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    More about the program:
    User will have four options buttons: back, ok, up and down. Through these buttons, the user will be able to read the information on the LCD and select them. My idea was not to leave the message automatically appearing, but that the user had control when it was necessary to go up or down the screen to read the information.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988

    That isn't an answer to my question.
     
  8. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    But I said.
    Do you wonder what's written in the texts ? They are in five lines , nothing too important
     
  9. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    It is a simple question. What are you expecting the printf statements to do?

    No offense but if you can't understand simple statements and questions in English then there is no point in continuing unless you can get someone to help you with your posts.
     
  10. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    Sorry, I will try to explain better.
    The program begins with a simple menu with two options: Option (a) and option (b).
    If you press the "OK" button, the option(a) is selected, and then will open a screen with a two-line sentence.
    To select the option (b), you must use the "down" button and then "ok". Then opens a menu with eleven options. Each option will occupy two lines of LCD (16x2).
    So, for example, to select option two(2), you must press the "down" button and then "OK" . Each of these eleven options, when selected, will give information about five rows (five text lines, more or less).
    And when, for example, you want to return to the eleven options menu, just press the "back" button.
    And if he needs to read the options or information that he already passed with the "down" button, just hit "up" button.
     
  11. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    That is fine but it still does not answer the question.

    What are you expecting the printf statement to do? If you won't answer that then there is no point in continuing.

    I feel like I am corresponding to one of those automated systems. :)
     
  12. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    Unfortunately you are not speaking clearly, so I have no more to do. I'm trying to figure out what to do with this my problem a few days ago, and I'm lost and I'm taking a long time (I'm actually out of time). I just want to know what I need to use that program. I was using vectors for the start and functions for each line options, but the program was getting confused and too big. I just wanted a tip, something like, "you can use "x" to the functions and "y" for the lines. Not to take issue with the selection of buttons, you can make “z” way." Here's the code I had before. I didn’t posted before because it is confusing and I think that would make no sense.
     
  13. Bellon

    Thread Starter New Member

    Sep 19, 2015
    9
    0
    Code (Text):
    1.  
    2. #include <xc.h>
    3. #include <stdio.h>
    4. #include "lcd.h"
    5. #ifndef _XTAL_FREQ
    6.  // Unless specified elsewhere, 4MHz system frequency is assumed
    7.  #define _XTAL_FREQ 16000000
    8. #endif
    9.  
    10.  
    11. char linha0_1[16]="op";
    12. char linha0_2[16]="1";
    13.  
    14. char linha1_1[16]="op";
    15. char linha1_2[16]="2";
    16.  
    17.  
    18. int var=0;
    19.  
    20. void linha0(){
    21.         lcd_clear();
    22.         lcd_puts(linha0_1);
    23.         printf("\n");
    24.         lcd_puts(linha0_2);
    25. }
    26.  
    27. void linha1(){
    28.         lcd_clear();
    29.         lcd_puts(linha1_1);
    30.         printf("\n");
    31.         lcd_puts(linha1_2);
    32. }
    33.  
    34. void linha2(){
    35.         lcd_clear();
    36.         lcd_puts(linha2_1);
    37.         printf("\n");
    38.         lcd_puts(linha2_2);
    39. }
    40.  
    41. void linha3(){
    42.         lcd_clear();
    43.         lcd_puts(linha3_1);
    44.         printf("\n");
    45.         lcd_puts(linha3_2);
    46. }
    47.  
    48. void inicio(){
    49.     lcd_clear();
    50.     printf("Sist. de Cont.");
    51.     printf("p/ Estufas Dom.");
    52. }
    53.  
    54. void insistema(){
    55.     lcd_clear();
    56.     printf("1: \n");
    57.     printf("2: \n");
    58. }
    59.  
    60. void inf(){
    61.     var=1;
    62. }
    63.  
    64. void inf0(){
    65. lcd_clear();
    66.     printf("op1");
    67. }
    68. void inf1(){
    69. lcd_clear();
    70.     printf("op2");
    71. }
    72. void inf2(){
    73. lcd_clear();
    74.     printf("op3");
    75. }
    76. void inf3(){
    77. lcd_clear();
    78.     printf("op4");
    79. }
    80. void inf4(){
    81. lcd_clear();
    82.     printf("op5");
    83. }
    84.  
    85. void inf4(){
    86. lcd_clear();
    87.     printf("op6");
    88. }
    89.  
    90. void inf4(){
    91. lcd_clear();
    92.     printf("op7");
    93. }
    94.  
    95. void inf4(){
    96. lcd_clear();
    97.     printf("op8");
    98. }
    99.  
    100. void inf4(){
    101. lcd_clear();
    102.     printf("op9");
    103. }
    104.  
    105. void inf4(){
    106. lcd_clear();
    107.     printf("op10");
    108. }
    109.  
    110. void inf4(){
    111. lcd_clear();
    112.     printf("op11");
    113. }
    114.  
    115.  
    116. int main(void){
    117.     char temp=0, aux;
    118.  
    119.     int ant0,atual0,ant1,atual1,ant2,atual2,ant3,atual3,lin=0;
    120.  
    121.     TRISD=0x00;
    122.     TRISB=0x0F;
    123.     ADCON1=0XFF;
    124.     CMCON=0X07;
    125.     T1CON=0b11111001;
    126.     PORTD=0;
    127.     for(aux=0; aux<100;aux++) __delay_ms(10);
    128.     lcd_init();
    129.    
    130.     linha1();
    131.  
    132.    
    133.     while(1){
    134.  
    135.         if(TMR1IF){
    136.         TMR1IF=0;
    137.         TMR1L=0xB0;
    138.         TMR1H=0x3C;  
    139.  
    140.         /*inicio();
    141.         for(aux=0; aux<200;aux++) __delay_ms(10);
    142.         linha1();*/
    143.  
    144.         atual1=RB1;          
    145.             if(atual1==0&&ant1==1){
    146.                     lin++;
    147.                     if(lin>3)lin=3;
    148.                     if(lin==0){
    149.                         if(var==1)inf0();
    150.                         else linha0();
    151.                     }
    152.                     if(lin==1){
    153.                         if(var==1)inf1();
    154.                         else linha1();
    155.                     }
    156.                     if(lin==2){
    157.                         if(var==1)inf2();
    158.                         else linha2();
    159.                     }
    160.                    
    161.             }
    162.             ant1=atual1;
    163.        
    164.             atual0 = RB0;
    165.             if(atual0==0&&ant0==1){
    166.                 lin--;
    167.                 if(lin<0)lin=0;
    168.                 if(lin==0){
    169.                         if(var==1)inf0();
    170.                         else linha0();
    171.                     }
    172.                     if(lin==1){
    173.                         if(var==1)inf1();
    174.                         else linha1();
    175.                     }
    176.                     if(lin==2){
    177.                         if(var==1)inf2();
    178.                         else linha2();
    179.                     }
    180.                    
    181.                 }
    182.             ant0=atual0;
    183.  
    184.             atual2=RB2;          
    185.             if(atual2==0&&ant2==1){
    186.                
    187.                     switch(lin){
    188.                        
    189.                         case 1:
    190.                         if(var==0){
    191.                         insistema();
    192.                         for(aux=0; aux<200;aux++) __delay_ms(10);
    193.                         linha2();
    194.                         }
    195.                         if(var==1){
    196.                         lcd_clear();
    197.                         printf("info ");
    198.                         for(aux=0; aux<200;aux++) __delay_ms(10);
    199.                        
    200.  
    201.                         inf1();
    202.                             }
    203.  
    204.                         break;
    205.                        
    206.                         case 2:
    207.                         if(var==0){
    208.                         inf();
    209.                         inf0();
    210.                         }
    211.                        
    212.                         break;
    213.  
    214.                        
    215.                
    216.                 }
    217.                 }
    218.             ant2=atual2;
    219.  
    220.             atual3=RB3;          
    221.             if(atual3==0&&ant3==1){
    222.                 var=0;
    223.                 linha0();                      
    224.                
    225.                 }
    226.                
    227.             ant3=atual3;
    228.         }
    229. }
    230. }
    231.  
    232.  
     
  14. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,866
    988
    I am "speaking" perfectly clearly. It is you that cannot understand.

    What is the purpose of the printf statement sin your code? What are you expecting it to do? It's can't get any clearer than that. If you can't undertand a simple qustion such as that then no one is going to be able to help you.
     
    Jswale likes this.
  15. atferrari

    AAC Fanatic!

    Jan 6, 2004
    2,644
    759
    A pergunta dele, é clara demais meu irmao.
     
Loading...