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.

[SOLVED] button problem mikroc and 16F877

Status
Not open for further replies.

krtodoroff

Newbie level 5
Joined
Jan 10, 2011
Messages
10
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,418
Have problem, Buttons on PORTA not working .LCD work OK bu buttons not answar. Please help.
Thak You

Here CODE:

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
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RD7_bit;
sbit LCD_D5 at RD6_bit;
sbit LCD_D6 at RD5_bit;
sbit LCD_D7 at RD4_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISD7_bit;
sbit LCD_D5_Direction at TRISD6_bit;
sbit LCD_D6_Direction at TRISD5_bit;
sbit LCD_D7_Direction at TRISD4_bit;
 
 
void main() {
 
 
  CMCON = 0x07;
  TRISA = 0x1F;
 
 
  Lcd_Init();
 
  Delay_mS(400);
 
 
       Lcd_Cmd(_LCD_CLEAR);
       Lcd_Cmd(_LCD_CURSOR_OFF);
       Lcd_Out(1, 1,"some text");
        Delay_mS(1000);
 
 while (1)
 {
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2, 1, "pres button");
         Delay_mS(400);
 
  if (Button(&PORTA,0,1,1))
   {
 
 
        Lcd_Cmd(_LCD_CLEAR);
        Lcd_Out(2,1, "+");
        Delay_mS(400);
 
   }
 
 }
       
       
       
}

 
Last edited by a moderator:

As i see there is no CMCON reg on 877 it should be ADCON and set 011x to make all porta as digital.
CMCON availablaon 877A but related to comparator.
Hope helps
 
I am SET:

ADCON1 |= 0x0F
CMCON |= 7;

and result is same
 

I solve the problem with ASM, but I did not understand what's wrong

asm{
MOVLW 0x06 //ADCON1 |= 0x0F;
MOVWF ADCON1
MOVLW 0xFF
MOVWF TRISA
}
 

You could just as easily used C in setting the required registers.

Assembly language is not necessary.

BigDog

---------- Post added at 07:10 ---------- Previous post was at 06:56 ----------

This:

BANKSEL PORTA
CLRF PORTA

BANKSEL ADCON1
MOVLW 0x06
MOVWF ADCON1

BANKSEL TRISA
MOVLW 0xCF
MOVWF TRISA

Equates To:

PORTA = 0x00;

ADCON1 = 0x06

TRISA = 0xCF

In your case you should use TRISA = 0xFF since all of PORTA are set as inputs in your previous ASM block.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
i try this many times, but my inputs not answer.
 

Are you using a PIC16F877 or a PIC16F877A?

I'm not sure it makes a difference, but I check the datasheets.

Which pin on PORTA is the button actually attached?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
I use PIC16F877A, and my buttons are A0-A4.
Now this working, but i have problem whit EEProm Writing. For first time I use PIC16F877A, and something I do not understand why things have worked, now not.
 

Both the Assembly and C Code snippets above generate the same machine code. So they better both work or we are all in trouble.

What type of problem are you having with the EEPROM?

What other types of problems are you having?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
this is void:
void WriteRoml(unsigned int tempint){
int adr = 0;

ch[0] = (tempint/1000)%10 + 48;
ch[1] = (tempint/100)%10 + 48;
ch[2] = (tempint/10)%10 + 48;
ch[3] = tempint%10 + 48;
Lcd_Out(1, 6, ch);
for(adr=0;adr<4;adr++){
EEPROM_Write(0x00+adr, ch[adr] );
delay_ms(25);
}
}

but when i call void, program stop here

soundl = 4321;
WriteRoml(soundl);

write data but not continu.
 

Here all code until now i write. The problem is that i wont write some integer wolues into EEPROM end then to read it.

CODE:

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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RD7_bit;
sbit LCD_D5 at RD6_bit;
sbit LCD_D6 at RD5_bit;
sbit LCD_D7 at RD4_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISD7_bit;
sbit LCD_D5_Direction at TRISD6_bit;
sbit LCD_D6_Direction at TRISD5_bit;
sbit LCD_D7_Direction at TRISD4_bit;
 
unsigned char hours;
unsigned char mins;
unsigned char secs;
unsigned char secs_char;
unsigned char a_min;
unsigned char a_sec;
unsigned char al_min;
unsigned char al_sec;
 
unsigned char debounce;
unsigned char debounce_menu;
unsigned char menu_state;
 
unsigned char new_second;
unsigned long period;
 
char *dispoutl = "    ",*dispouth = "     " , *ch; //*ch = "     "
unsigned soundl,soundh;
 
#define XTALHz 10000000;
 
 
 
void poll_timer(void)
{
  //-----------------------------------------------------
  // this function must be called often, don't allow more
  // than 131 mS between calls.
  // 8Mhz; TMR1 1:4 prescale overflows every 7.6Hz (131mS)
  // (if 20Mhz TMR1 1:4 prescale overflows every 19Hz (53mS))
  //
  // TMR1 is at 1:4 prescaler, so 1 second = xtal Mhz / 16.
  //-----------------------------------------------------
  // if TMR1 overflowed, check if 1 second has passed
  if(PIR1.TMR1IF)
  {
    // clear overflow flag
    PIR1.TMR1IF = 0;
    // add another overflow period to total
        period += 65536;
    // test if reached 1 second yet...
    if(period >= 625000) //(XTALHz/16)
    {
      period -= 625000;
      new_second = 1;
    }
  }
}
 
//-----------------------------------------------------------------------------
 
void update_clock(void)
{
 
  if(secs >= 60)
  {
    secs = 0;
    mins++;
  }
  if(mins >= 60)
  {
    mins = 0;
    hours++;
  }
 
  if(hours > 24)    // for 00:00:00 - (24 hour) clock
  {
    hours = 0;
  }
 
 
  if(al_sec > 60)
//  {
//    if(a_sec == 0)
    {
      al_sec = 59;
      al_min--;
    }
//  }
  if(al_sec <= 0)
  {
   a_sec = 0;
  }
  if(al_min > 100)
  {
    al_min = 5;
    a_sec = 1;
 
  }
}
 
 
 
//-----------------------------------------------------------------------------
 
void display_clock_menu(void)
{
  //-----------------------------------------------------
  // display the clock values to the LCD
 
  // move to home position
//  Lcd_Move(0,0);    // position on 2x16 display
 
  // show hours
  Lcd_Chr(1, 12, '0' + (hours / 10));
  Lcd_Chr(1, 13, '0' + (hours % 10));
 // Lcd_Chr(2, 3,':');
//  poll_timer();
 
  // show mins
  Lcd_Chr(1, 15, '0' + (mins / 10));
  Lcd_Chr(1, 16, '0' + (mins % 10));
//  Lcd_Chr(2, 6, ':');
//  poll_timer();
  if (secs_char==1)
   {
    Lcd_Chr(1, 14, ' ');
    secs_char++;
   }
   else
   {
     if (secs_char==3)
      {
        Lcd_Chr(1, 14, ':');
        secs_char=0;
       }
   }
 
 
}
 
//-----------------------------------------------------------------------------
 
void Display_alarm(void)
  {
 
  Lcd_Chr(1, 1, '0' + (al_min / 10));
  Lcd_Chr(1, 2, '0' + (al_min % 10));
  Lcd_Chr(1, 3, ':');
  poll_timer();
  Lcd_Chr(1, 4, '0' + (al_sec / 10));
  Lcd_Chr(1, 5, '0' + (al_sec % 10));
  poll_timer();
/*
 
*/
}
 
 
void Prg1(void)
  {
   Sound_Init(&PORTD, 2);
    Sound_Play( 2500, 500);
    Sound_Play( 2500, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
//  Sound_Play( soundl, 500);
 /*
    Sound_Play( 10000, 500);
    Sound_Play( soundl+100, 500);
    Sound_Play( soundl+200, 500);
    Sound_Play( soundl+300, 500);
    Sound_Play( soundl+400, 500);
    Sound_Play( soundl+500, 500);
    Sound_Play( soundl+600, 500);
    Sound_Play( soundl+700, 500);
    Sound_Play( soundl+800, 500);
    Sound_Play( soundl+900, 500);
 */
    secs = secs + 5;
    al_sec--;
  }
 
//------------------  EEPROM read 0x00 low ----------------------------------
 
void RomReadl(char *chx){
 int adr = 0;
// i = 0;
 chx[0]= EEPROM_Read(0x00);
 adr = chx[0];
 soundl = (adr-48)*1000;
 
 chx[1]= EEPROM_Read(0x01);
  adr = chx[1];
 soundl = soundl + (adr-48)*100; // pow(10,2)
 
 chx[2]= EEPROM_Read(0x02);
  adr = chx[2];
 soundl = soundl + (adr-48)*10;
 chx[3]= EEPROM_Read(0x03);
  adr = chx[3];
 soundl = soundl + (adr-48);
// WordToStr(i,dispout);
// Lcd_Out(1, 6, dispout);
// Lcd_Out(1, 6, chx);
// Delay_ms(i);
}
 
//------------------  EEPROM read 0x00 high ----------------------------------
 
void RomReadh(char *chx){
 int adr = 0;
// i = 0;
 chx[0]= EEPROM_Read(0x10);
 adr = chx[0];
 soundh = (adr-48)*10000;
 
 chx[1]= EEPROM_Read(0x11);
  adr = chx[1];
 soundh = soundh + (adr-48)*1000; // pow(10,2)
 
 chx[2]= EEPROM_Read(0x12);
  adr = chx[2];
 soundh = soundh + (adr-48)*100;
 chx[3]= EEPROM_Read(0x13);
  adr = chx[3];
 soundh = soundh + (adr-48)*10;
 chx[4]= EEPROM_Read(0x14);
  adr = chx[4];
 soundh = soundh + (adr-48);
// WordToStr(i,dispout);
// Lcd_Out(1, 6, dispout);
// Lcd_Out(1, 6, chx);
// Delay_ms(i);
}
 
//------------------  EEPROM write 0x00 low ----------------------------------
 
void WriteRoml(unsigned int tempint){
int adr = 0;
 
 ch[0] = (tempint/1000)%10 + 48;
 ch[1] = (tempint/100)%10 + 48;
 ch[2] = (tempint/10)%10 + 48;
 ch[3] = tempint%10 + 48;
 Lcd_Out(1, 6, ch);
   for(adr=0;adr<4;adr++){
      EEPROM_Write(0x00+adr, ch[adr] );
      delay_ms(25);
     }
}
 
//------------------  EEPROM write 0x10 high ----------------------------------
 
void WriteRomh(unsigned int tempint){
int adr = 0;
 
 ch[0] = (tempint/10000)%10 + 48;
 ch[1] = (tempint/1000)%10 + 48;
 ch[2] = (tempint/100)%10 + 48;
 ch[3] = (tempint/10)%10 + 48;
 ch[4] = tempint%10 + 48;
 Lcd_Out(1, 6, ch);
   for(adr=0;adr<5;adr++){
      EEPROM_Write(0x10+adr, ch[adr] );
     }
}
 
//------------------------------ MAIN ----------------------------------------
 
void main() {
 
  CMCON  |= 7;
 
  porta = 0;
 
 TRISA = 0b00011111;         // TRISA = 0b76543210
  T1CON = 0b00100001 ;
 TRISA0_bit = 1;
 TRISA1_bit = 1;
 TRISA2_bit = 1;
 TRISA3_bit = 1;
 TRISA4_bit = 1;
 TRISA5_bit = 1;
   asm{
    MOVLW 0x06             //ADCON1 |= 0x0F;
    MOVWF ADCON1
    MOVLW 0xFF
    MOVWF TRISA
    }
 
 
  period = 0;
  new_second = 0;
  debounce = 0;
  debounce_menu = 0;
  menu_state = 1;
  hours = 12;
  mins = 0;
  secs = 0;
  al_min = 0;
  al_sec = 10;
  a_sec = 0;
  
  
  soundl = 4321;
  soundh = 54321;
 
  WriteRoml(soundl);
//  WriteRomh(soundh);
 
 
  Lcd_Init();
 
Delay_mS(400);
 
 
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1,"some text");
 
 
 Sound_Init(&PORTD, 2);
//  Sound_Play(1000, 1000);
 
 
Lcd_Cmd(_LCD_CLEAR);
  while(1) {
 
//    if(EEPROM_Read(0x00) ==  11111111)
//     {
//     WriteRoml(soundl);
//     }
    if(EEPROM_Read(0x10)==  0xFF)
     {
//     WriteRomh(soundh);
     }
 
  
  poll_timer();
  
      if (new_second)
    {
        new_second = 0;
        secs++;
        secs_char++;
        al_sec--;
        update_clock();
       if(menu_state == 1 )     //&& new_second
         {
          display_clock_menu();    // display to LCD
          Display_alarm();
          }
      } //----- end timer -----
 
 
 
           //---- "Menu button" state 1 -----
    if (Button(&PORTA, 2, 1, 1) && menu_state == 1)
     {
       debounce_menu++;
       if(debounce_menu > 45)
        {
        menu_state = 2;
        Lcd_Cmd(_LCD_CLEAR);
        RomReadl(dispoutl);
        Lcd_Out(1, 6, dispoutl);
        debounce_menu = 0;
        }
      } // end if  --- Menu 1 --- sound low
      
          //---- "Menu button" state 2 -----
    if (Button(&PORTA, 2, 1, 1) && menu_state == 2)
     {
       debounce_menu++;
       if(debounce_menu > 45)
        {
        menu_state = 3;
        Lcd_Cmd(_LCD_CLEAR);
        RomReadl(dispouth);
        Lcd_Out(1, 6, dispouth);
        debounce_menu = 0;
        }
      } // end if  --- Menu 2 --- sound high
      
          //---- "Menu button" state X -----
    if (Button(&PORTA, 2, 1, 1) && menu_state == 3)
     {
       debounce_menu++;
       if(debounce_menu > 45)
        {
        menu_state = 1;
        Lcd_Cmd(_LCD_CLEAR);
        debounce_menu = 0;
        }
      } // end if  --- Menu 2 --- Exit to MAIN
 
    if (Button(&PORTA, 0, 1, 1))       //---- "- button" -----
     {
       Lcd_Out(2,2, "-");
       Delay_mS(1000);
       secs++;
       Lcd_Cmd(_LCD_CLEAR);
 
      }
 
 
   if(Button(&PORTA, 4, 1, 1))           //---- "+ button" -----
    {
      Lcd_Out(2,1, "+");
      Delay_mS(1000);
      secs++ ;
      Lcd_Cmd(_LCD_CLEAR);
     }
 
 
     if (al_sec == 0 && al_min == 0)
   {
    Prg1();
   }
 
  }// Endless loop
}



the voids are worked pefect on 16F628A but there is not enough program memory.
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top