Need help to solve the program

Discussion in 'Programmer's Corner' started by Suneeth Nair, Jun 14, 2014.

  1. Suneeth Nair

    Thread Starter New Member

    Jun 14, 2014
    1
    0
    Hello friends,

    I am a starter in PIC and I have done a program in pic c complier for the following problem
    _____________________________________________________________
    When PIC A0 receives an input of more than 4 v, it is converted into digital output and that is fed to a counter. When the count reaches 5 the memory is incremented by 1 and is multiplied with 3 and displayed as cost as well as the counter is reset.

    These are displayed in a LCD 16*2 screen. whenever the cost goes above 20, a message is sent via sms using a gsm 900 module to a pre described number. and finally the cost is reset to 0.

    I have attached a word file of the program. The problems i face is the LCD display is not working as well as the message is not sent over the gsm.

    _________________________________________________________________

    Now the problem is program works fine with proteus model but in real time it dosen't work. Even the LCD 1682 display is not diplaying the result as well as there is no message being sent over the gsm.

    Can anyone help me with this program and tell me what are the errors i have done..

    Thanks in advance.. Hoping to receive some help.

    This the program which i did for PIC 16f877a:

    Code ( (Unknown Language)):
    1.  
    2. #include"C:\Users\vijay\Desktop\project\Program for\main.h"
    3. #bit lcdrs=0X09.2
    4. #bit lcde=0X09.0
    5. #bit lcdrw=0X09.1
    6. #byte lcddata = 0x8
    7. void lcdinit();
    8. void lcdclear();
    9. void lcdclock();
    10. void lcddly();
    11. void line1();
    12. void line2();
    13. void lcdputc(char c);
    14. //unsigned char disp[16],discou,dl;
    15. void lcddly()
    16. {
    17. delay_ms(1);
    18. }
    19. void lcdxy(char r,char c)
    20. {    
    21. if(r==1)
    22. {
    23. lcdrs=0;
    24. lcddata=0x80+c;
    25. lcdclock();
    26. lcdrs=1;
    27. }
    28. else if(r==2)
    29. {
    30. lcdrs=0;
    31. lcddata=0xC0+c;
    32. lcdclock();
    33. lcdrs=1;
    34. }
    35. }
    36. void lcdinit()
    37. {
    38. lcde=0;
    39. lcddata=0x38;
    40. lcdrs=0;
    41. lcdclock();
    42. lcddata=0x14;
    43. lcdclock();
    44. lcddata=0x0c;
    45. lcdclock();
    46. lcddata=0x06;
    47. lcdclock();
    48. lcddata=0x01;
    49. lcdclock();
    50. lcdrs=0;
    51. lcddata=0x80;
    52. lcdclock();
    53. }
    54. void lcdclock()
    55. {
    56. lcde=1;
    57. lcddly();
    58. lcde=0;
    59. lcddly();
    60. }
    61. void lcdclear()
    62. {
    63. lcdrs=0;
    64. lcddata=0x01;
    65. lcdclock();
    66. lcddata=0x0c;
    67. lcdrs=0;
    68. lcdclock();
    69. }
    70. void lcdputc(char c)
    71. {
    72. switch(c)
    73. {
    74. case '\f':
    75. {
    76. lcdrs=0;
    77. lcddata=0x01;
    78. lcdclock();
    79. lcddata=0x0c;
    80. lcdrs=0;
    81. lcdclock();
    82. lcdrs=0;
    83. lcddata=0x80;
    84. lcdclock();
    85. lcdrs=1;
    86. }break;
    87. case '\n':
    88. {
    89. lcdrs=0;
    90. lcddata=0xc0;
    91. lcdclock();
    92. lcdrs=1;
    93. }break;
    94. default:
    95. lcddata=c;
    96. lcdclock();
    97. }
    98. }
    99.  
    100. int value;
    101. int8 c=0,cost=0,d,u=0,a;
    102. void main()
    103. {
    104. start:
    105. setup_adc_ports(AN0);
    106. setup_adc(ADC_CLOCK_INTERNAL);
    107. setup_psp(PSP_DISABLED);
    108. setup_spi(SPI_SS_DISABLED);
    109. setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
    110. setup_timer_1(T1_DISABLED);
    111. setup_timer_2(T2_DIV_BY_16,127,1);
    112. setup_comparator(NC_NC_NC_NC);
    113. setup_vref(FALSE);
    114. set_adc_channel(0);
    115. delay_us(20);
    116. set_tris_b(0xFF);
    117. set_tris_d(0X00);
    118. set_tris_e(0X00);
    119. set_tris_a(0xF0);
    120. set_tris_c(0x0F);
    121. lcdinit();
    122. lcdclear();
    123. lcdxy(1,0);
    124. lcdclear();
    125. // for(qw=0;qw<=2;qw++)
    126. while(1)
    127. {
    128. delay_ms(1);
    129. value=read_adc();
    130. lcdinit();
    131. lcdclear();
    132. lcdxy(1,0);
    133. lcdclear();
    134. lcdxy(1,0);
    135. printf(lcdputc, "count:%d",d);
    136. if(value>=31)
    137. {
    138. c++;
    139. d=c;
    140. lcdinit();
    141. lcdclear();
    142. lcdxy(1,0);
    143. lcdclear();
    144. lcdxy(1,0);
    145. //delay_ms(100);
    146. printf(lcdputc, "count:%d",d);
    147. }
    148. if(d>=5)
    149. {
    150. u++;
    151. a=u;
    152. c=0;
    153. cost=(u*3);
    154. lcdinit();
    155. lcdclear();
    156. lcdxy(1,0);
    157. lcdclear();
    158. lcdxy(1,0);
    159. //delay_ms(100);
    160. printf(lcdputc,"Units:%d",a);
    161. printf(lcdputc, " Cost:%d", cost);
    162. goto start;
    163. }
    164. if (cost>=20)
    165. {
    166. //serial_init(9600);
    167. printf("AT\n");
    168. delay_ms(20); // 2 sec delay
    169. printf("AT+CMGF=1\n");
    170. delay_ms(20); // 2 sec delay
    171. printf("AT+CMGS=\"09825858509\"\n");
    172. delay_ms(20); // 2 sec delay
    173. printf ("Cost=%d ", cost); // sends ADC value as SMS
    174. putchar(26); // Ctrl-Z indicates end of SMS and transmit
    175. delay_ms(20); // 2 sec delay
    176. cost=0;
    177. }
    178. else
    179. { goto start;
    180. }
    181.  
     
    Last edited by a moderator: Jun 14, 2014
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You should break the problem down into parts.

    A simulator doesn't capture all the nuances of real hardware, so a working system in a simulator does not mean working hardware...
     
  3. absf

    Senior Member

    Dec 29, 2010
    1,492
    372
    I assume you're simulating on the proteus. What is the clock speed you used in the simulator and what is the crystal frequency you used in the real HW?

    As Tshuck has said, break you problems into parts and tackle them one at a time. The crystal frquency you used is important because the LCD is a slow device and needed a lot of time delays in sending commands and data to it.

    Try to make the LCD work first before tackling other problems.

    Allen
     
Loading...