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 support@ccsinfo.com

[need help today]P16f877A interrupt Rb0 (press&hold SW)

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



Joined: 08 May 2016
Posts: 4

View user's profile Send private message

[need help today]P16f877A interrupt Rb0 (press&hold SW)
PostPosted: Sun May 08, 2016 12:21 am     Reply with quote

Request: (only use interrupt rb0)
1. Press SW (not hold), count and display number of times pressed (if up to 19 begin down, example 0,1,...18,19,18,17,...1,0,1,2....continue)--->solved
2. Press and hold SW, count will up/down and display continuous--->i don't know how to solve this problem, and where (in void ngat_RB0() or in void main() ?)

Here is my C file (PICC) and simulation (Proteus 8.4)
http://www.mediafire.com/download/3oxamwfmxaa9wzr/code_test_rb0_SW.rar

/sorry for my poor english


Here is my code:
Code:

#include <16F877A.h>
#fuses xt
#use delay(clock=4000000)
int8 ma7seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int8 count,i,n;

#int_ext
void ngat_RB0()
{

i=i+1;

}


void display()
{

if(i==19)
  {i=0;n=n+1;}

if(n%2==0)
  {count=i;}
else
  count=19-i;

output_d(ma7seg[count/10]);
output_low(pin_e0);
delay_ms(25);
output_high(pin_e0);

output_d(ma7seg[count%10]);
output_low(pin_e1);
delay_ms(25);
output_high(pin_e1);
}


void main()
{
i=0;
n=0;
count=0;

set_tris_e(0x00);
set_tris_b(0x01);
set_tris_d(0x00);
output_e(0xff);
output_d(0x00);

enable_interrupts(global);
enable_interrupts(int_ext);
ext_int_edge(H_to_L);

while(1)
  {
   display();
  }

}


Last edited by langkhachpfiev on Sun May 08, 2016 12:34 am; edited 4 times in total
langkhachpfiev



Joined: 08 May 2016
Posts: 4

View user's profile Send private message

PostPosted: Sun May 08, 2016 12:29 am     Reply with quote

If you know, please help. Thank you.
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Sun May 08, 2016 3:09 am     Reply with quote

The answer is 'rethink'.

Don't use int_ext.

Instead use a timer. In this timer, test if the button is pressed. If it is increment the counter.

This way the counter will increment at the timer interval, for as long as the button is pressed.
langkhachpfiev



Joined: 08 May 2016
Posts: 4

View user's profile Send private message

PostPosted: Sun May 08, 2016 3:21 am     Reply with quote

Ttelmah wrote:
The answer is 'rethink'.

Don't use int_ext.

Instead use a timer. In this timer, test if the button is pressed. If it is increment the counter.

This way the counter will increment at the timer interval, for as long as the button is pressed.

must be use interrupt rb0 because that project is require "only use interrupt rb0", i tried don't use interrupt and use "if clause" to check event have press button, it runs right but only displays when pressed, but finally, the target is use interrupt rb0.
//thanks for your reply
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun May 08, 2016 6:18 am     Reply with quote

Be sure to try after you begin using an actual circuit.

With connection to real things like a crystal, pullup to MCLR - decouple capacitors and power etc....
incomplete circuit -cant work AT ALL except in pretend ISIS sand box....

Simulations mean precious little and nobody here want to waste time on them.
Proteus /Isis sims are not the way to go.
Check the links here for help

http://www.ccsinfo.com/forum/viewtopic.php?t=47549
Ttelmah



Joined: 11 Mar 2010
Posts: 19436

View user's profile Send private message

PostPosted: Sun May 08, 2016 10:20 am     Reply with quote

Talk about doing it the most complex way.....
If this is a 'lesson', then tell your teacher to go back to school!.
Are you sure he/she does not want you to actually point out that this is a daft way of doing things.

You would either have to change the hardware, or do a relatively complex 'cheat'.

What you would have to do is change the interrupt edge, and record where the button actually 'is'.
So you start the code with the interrupt set to trigger on the falling edge from the button. Then have a global flag 'button_down', starting set to 'FALSE'.
In the interrupt, if the input is low, then set 'button_down' to TRUE, and reprogram the interrupt to trigger on the rising edge. If the input is high, set 'button_down' to FALSE and set the interrupt back to detecting the falling edge.

Then in the main, have a downcounter variable. When button_down is FALSE set this to zero.

Then test if (button_down && downcounter--==0) increment your display count and set downcounter = value.

Chose 'value' to give you the speed of repeat you want.

This way, so long as the button stays 'down', every 'downcounter' loops, your display count will be updated.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sun May 08, 2016 7:57 pm     Reply with quote

I can't help but comment on this: there is so much wrong with the circuit as drawn beyond the missing parts.

its not engineered. it's cookie cutter-ed.

With a 5v supply , the pic to PNP drive I-limit resistors would force 15 or ma of base current when active- which for a transistor with an Hfe as low of 15 assumes an LED load of 225ma...... if the transistor is a 2n3906 with a min hfe of 150 - and we assume 25ma load then then it becomes obvious that 24k is the correct drive resistor value.
but that's screwed too
since the pnp's are switches not current sources as designed -
so without current limit series resistors the current in the LED's might get awfully high too. The basic circuitry is totally f%#ked from top to bottom.
The code is just as wacked.

I think would be PIC programmers or their close support associates need better circuit design skill than i EVER see in these pleas for help.

While these questions focus on CCS code -
it is hard to watch circuit mal-design with ISIS
be so consistently, shockingly negligent.


Last edited by asmboy on Mon May 09, 2016 6:52 am; edited 1 time in total
langkhachpfiev



Joined: 08 May 2016
Posts: 4

View user's profile Send private message

PostPosted: Sun May 08, 2016 9:49 pm     Reply with quote

ok thanks you guys for your comment
this project is one of three part (interrupt rbo-interrupt timer1-adc to change pwm) of my "big home exercise "

Here My full Code and Simulate File of 3 part,sent to teacher yesterday: (CCS PicC and Protues 8.4)
http://www.mediafire.com/download/olljw8ss4xmsfp2/big+home+exercise+p16f877a.rar

@asmboy&ttelmah: yes,this circuit on protues i design simple to test code ,not a design of real circuit (removed all resistor connect to Led pins).Microcontroller is one of my subject at school,and i am starter to learn it.thanks your advises

And this is my try best code yesterday ,of this part,a little blink but it run right as request (sorry for code note in Vietnamese).The request is only silmutate right on protues,so i am not sure it run right or not in reality.

Code:
//******************************************************************************
//author: nguyen thanh hai                                             
//project:interrupt rb0                                             
//hardware:p16f877a                                                     
//date:08.05.2016                                                       
//******************************************************************************
#include <16f877a.h>                     //them thu vien pic16f877a
#fuses  xt                               //khai bao su dung tan so thap
#use delay(clock=4000000)                //khai bao tan so thach anh osc=4mhz
int8 ma7seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};      //khai bao mang tu 0-9 theo he 16 cua led 7seg anot chung
int8 dem,count,i,n;                      //khai bao cac bien dem


#int_ext
void ngat_RB0()
{
  clear_interrupt(int_ext);              //xoa co bao ngat ngoai
  enable_interrupts(global);             //cho phep ngat toan cuc
}

void display()                           //ham hien thi quet led
{
   if(input(pin_b0)==0)                  //kiem tra nut nhan
      {   
     i=i+1;                              //cho bien dem i tang
     delay_ms(125);
       if(i==99)
          {i=0;n=n+1;}                   //neu i dem toi 99 thi set i =0 va cho n tang 1 don vi moi lan i dem toi 99
       if(n%2==0)                        //so lan dem toi 99 la n chan,thi bien count = bien dem i
          {count=i;}
       else                              //so lan dem toi 99 la n le,thi bien count =99-( bien dem i)
           count=99-i;
      }
   
     output_d(ma7seg[count/10]);         //xuat gia tri hang chuc ra port d
     output_low(pin_e0);                 //bat led hang chuc
     delay_ms(50);
     output_high(pin_e0);                //tat led hang chuc

     output_d(ma7seg[count%10]);         //xuat gia tri hang don vi ra port d
     output_low(pin_e1);                 //bat led hang don vi
     delay_ms(50);
     output_high(pin_e1);                //tat led hang don vi

}

 
 
void main()                              //chuong trinh chinh
{
i=0;
n=0;
count=0;
dem=0;                                   //set cac bien dem ban dau =0
set_tris_b(0x01);                        //set port rb0 la input
set_tris_e(0x00);                        //set port e la output
set_tris_d(0x00);                        //set port d la output
output_e(0xff);                          //tat 2 led 7seg bang cach ngat transitor pnp
output_d(0x00);                          //set port d ve 0

enable_interrupts(global);               //cho phep ngat toan cuc
enable_interrupts(int_ext);              //cho phep ngat ngoai
ext_int_edge(H_to_L);                    //ngat ngoai phat hien xung canh xuong khi co su kien nhan button


while(true)                              //vong lap vo tan
{
 display();                              //goi ham quet led
}
}
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