Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Problem with UAR1_WRITE_TEXT command;

Status
Not open for further replies.

Naseer Ahmed

Newbie level 5
Joined
Feb 19, 2015
Messages
9
Helped
1
Reputation
2
Reaction score
1
Trophy points
3
Location
Islamabad, Pakistan, Pakistan
Activity points
113
Hello! dear friends....

May you all live happy and healthy life....

I am using MikroC for programing of PIC 16F877A.

A problem occurs in the execution of the program in Proteus when the string UART1_WRITE_TEXT ("shot No.") is written more than ten times in the code. The code shows no error while compiling in MikroC but in Proteus it doesn't work by the define sequence of the code. It run randomly in the code (Some sort of error).

Does any one of you know about this problem?
 

hello,


Maybe you have not enough RAM ?
=> overflow ..


try to use this




Code:
UART1_WRITE_CTEXT ("shot No.") ;



void UART1_Write_CText(const char *txt1)
 {
   while (*txt1)
      UART1_Write(*txt1++);
}
 

Here is my complete code dear Susan: Unfortunately, I didn't use comments along with the code. So sorry for that...This is for PIC16F877A.



Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#define END SENSE PORTA.F5
#define STARTSENSE PORTA.F4
#define CW PORTD.F0
#define ACW PORTD.F1
 
 
 
 char Delay1, WD;
 int x,y,a,d1,d2,d3,d4,d5,d6,i,x1,x2,x3,x4,t0,t1,t2,t3,t4,t5;
 int Array[] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
 int D_Array [10] = {0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09};
 unsigned int Delay_Array[10];
 
 
 
void LF ()
{
  UART1_Write(10);
  UART1_Write(13);
}
void load()
    {
 
      if (x1 == 0)
      d1 = i;
 
      else if (x1 == 1)
      d2 = i;
 
      else if (x1 == 2)
      d3 = i;
 
      else if (x1 == 3)
      d4 = i;
      else if (x1 == 4)
      d5 = i;
      else
      d6 = i;
    }
 
  void Value()                                                                  //Enter Delay Values
  {
        while (UART1_READ () == 0x0D){}
       x1 = 0;
           while (uart1_read() != 0x0D && x1 != 5)
       {
         delay_ms(100);
         if (UART1_DATA_READY() == 1)
        {
           delay1 = Uart1_read ();
 
           if (delay1 == 0x30)
           {i = 0;
             uart1_write (array[i]);
             load();
             x1++;
           }
           else if (delay1 == 0x31)
           {i = 1;
              uart1_write (array[i]);
              load();
              ++x1;
           }
           else if (delay1 == 0x32)
           {i = 2;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x33)
           {i = 3;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x34)
           {i = 4;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x35)
           {i = 5;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x36)
           {i = 6;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x37)
           {i = 7;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x38)
           {i = 8;
              uart1_write (array[i]);
              load();
              x1++;
           }
           else if (delay1 == 0x39)
           {i = 9;
               uart1_write (array[i]);
               load();
               x1++;
           }
           else {}
 
          }
        }
  }
 
 
   void gendelay()
 {
 
   if (x1 == 1)
     t1 =  d1;
 
   else if (x1 == 2)
     t1 = d1*10 + d2;
 
   else if (x1 == 3)
   t1 = (d1*100 + d2*10 + d3);
 
   else if (x1 == 4)
   t1 = (d1*1000 + d2*100 + d3*10 + d4);
 }
 
 
void CLOCKW ()
{
 
 UART1_WRITE_TEXT("Move to Pos 0");
   while (STARTSENSE == 0)
   {
        ACW = 1;
   }
}
 
    void wait()
    {
      delay_ms(1000);
    }
 
 
void main()
{
     TRISA = 0xFF;
     TRISB = 0;
     TRISC = 0x8F;
     TRISD = 0;
     TRISE = 0;
 
     PORTA = 0;
     PORTB = 0;
     PORTC = 0x8F;
     PORTE = 0;
     PORTD = 0;
 
     ADCON0 = 0x81;
     ADCON1 = 0x8B;
 
     UART1_Init(9600);
 
   for (;;)
   {
 
   UART1_Write_Text("Parameters:");                                //Voltage Values
   while (uart1_read() == 0x0D){}
   while (uart1_read() != 0x0D)
   {
     delay_ms(100);
    if (UART1_DATA_READY() == 1)
    {
       WD = Uart1_read ();
       uart1_write(WD);
     }
   }
   LF();
   UART1_Write_Text("Delay 1 = ");                                              //Delay1
   value();
   gendelay();
   Delay_Array[0] = t1/7;
   t1 = 0;
   LF();
 
   UART1_Write_Text("Delay 2 = ");                                              //Delay2
   value();
   gendelay();
   Delay_Array[1] = t1/7;
   t1 = 0;
   LF();
 
   UART1_Write_Text("Delay 3 = ");                                              //Delay3
   value();
   gendelay();
   Delay_Array[2] = t1/7;
   t1 = 0;
   LF();
 
   UART1_Write_Text("Delay 4 = ");                                              //Delay4
   value();
   gendelay();
   Delay_Array[3] = t1/7;
   t1 = 0;
   LF();
 
   UART1_Write_Text("Delay 5 = ");                                              //Delay5
   value();
   gendelay();
   Delay_Array[4] = t1/7;
   t1 = 0;
   LF();
 
   UART1_Write_Text("Delay 6 = ");                                              //Delay6
   value();
   gendelay();
   Delay_Array[5] = t1/7;
   t1 = 0;
   LF();
 
 
   LF();
       if(STARTSENSE == 0)
    {
       CLOCKW();
    }
       ACW = 0;
 
 
       LF();
       UART1_Write_Text("Enter Value");
 
       LF();
       UART1_Write_Text("B1 = ");
       Value();
       gendelay();
       t3 = t1;
       LF();
       UART1_Write_Text("B2 = ");
       Value();
       gendelay();
       t4 = t1;
       LF();
 
 
       UART1_Write_Text("press Enter to charg");
 
       while (uart1_read()  != 0x0D){}
       LF();
       x = 0;
      while (x <= t1)
      {
       CW = 1;
       PORTD.F2 = 1;
       PORTD.F3 = 1;
 
       x = 5*ADC_Read(0);
 
       if (x>=t3)
       {
        PORTD.F2 = 0;
       }
        else{}
       d4 = x/1000;
       d5 = D_Array[d4];
       UART1_WRITE(0x30 + d5);
 
       d3 = (x - d4*1000)/100;
       d5 = D_Array[d3];
       UART1_WRITE(0x30 + d5);
 
       d2 = (x - d4*1000 - d3*100)/10;
       d5 = D_Array[d2];
       UART1_WRITE(0x30 + d5);
 
       d1 = (x - d4*1000 - d3*100 - d2*10);
       d5 = D_Array[d1];
       UART1_WRITE(0x30 + d5);
       PIR1.ADIF = 0;
       delay_ms(1000);
 
 
      if (x<=t1)
       {
        t2 = 0;
        while(t2 != 4)
        {
         uart1_write(8);
         t2++;
        }
       }
       else
        {
           delay_ms(100);
           LF();
           LF();
 
        }
      }
 
      wait();
 
 
  }
 }

 
Last edited by a moderator:

i dont see UART1_Write_Text function...

- - - Updated - - -


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
void WriteString(char* string)
{
  while (*string != '\0')
    {
       while (U2STAbits.UTXBF);
        if(U2STAbits.TRMT)
        {
            U2TXREG = *string;
            string++;
            //IEC1bits.U2TXIE = 1;
        }
    }
}

 
Last edited by a moderator:

A couple of comments:
1) I cannot find the line that you mention in the first post in the code you have provided. While there are examples of "UART1_Write_Text" function calls, there is no instance of "shot No.". Therefore I have no idea where the problem might be.
2) Looking at the MikroC reference guide (https://download.mikroe.com/documents/compilers/mikroc/pic/mikroc-pic-manual-v101.pdf) the UART1_Read function is not blocking and will return whatever is in the UART buffer. The nature of the MCU you are using is such that the value returned immediately after the UART has received a character will be valid but after that it may or may not be the same character (this behaviors is undefined). As the reference guide says, you shoudl always see if there is a valid character by first calling UART1_Data_Ready.
Point 2) means that the other functions (that appear to read in a value from the terminal and convert it from a character string to a binary value) will probably be error prone. By the way, why not just use "UART1_Read_Text" (which IS blocking) to return the string and then use a function such as 'atoi' to convert it to an integer.
3) Having all of your variables as global shows that you are not really thinking about the proper structure of your program.
Susan
 

Dear Friends,

There are more than 10 uart1_write_text commands in the code that's why I just gave you an example of more than 10 commands like uart1_write_text ("Shot No. = ") strings. A code of 10kb size with less number such commands works properly but the code with the same size and more number of such commands produces errors on proteus but not while building.

Thanks for your previous replies.....

T
 

Unless I've missed something crucial I can't see how this could ever work.
Perhaps a flow chart will help -

chart_Page-1of2.jpg

If you need the bottom half let me know.
 

If it works until you add mode code/data then you could well be running out of memory.
Remember that you are dealing with a device that only has memory of 8K instructions, 368 bytes of RAM and a call stack depth of 8. Also the RAM has to take banking into account.
Check the linker map output before and after adding the additional code to see if it is exceeding some limit (and the MikroC linker is not complaining about it as it should).
Even given that, the comments above should be telling you that your code is very poor and that you should take a step back, think about what you are doing and then write more efficient and effective code. Generally that means starting with pencil and paper, clearly working through how the program should be working (top-down can be an effective approach) and then gradually adding in the detail and looking for functions that help you as you go along.
Susan
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top