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] Question about programming trick

Status
Not open for further replies.
if it takes a few seconds then the input is probably read a few thousand time before the wrong data happens so I wonder why at that particular moment, do you have anything else in your code that could interfere in the reading process, any interrupt or some other peripheral?
 

Hi guys, :)



I Have question :) in this scenario I have 8 parallel inputs w/ 8 switches and it is being pulled up by 8 resistors, so the normal values of my input read by the PIC when no switches pressed is 255.(all 8bit input are high/1)

the switch used is a push button switch.. NORmally open

when I press one switch located at PORTA.RA0 the input value will now be 255 -1 = 254..

the switch is momentary when I dont push it it will go back and the input value again will be 255.

so now the problem is I want to hold that value until the next activity of the switch. (the value 254)

I want to store that value inside a variable without losing it even if the switch is not press anymore

I tried something like this but after sometime inside a while 1 loop the value 254 disappear..

this is my code:

PHP:
if(input != 255) //when any of the switch is pressed
store = input 

return store;

The above bold letters indicate that you are using 8 pins for switches...so I thought to use Intr on Change.

Now, I am confused...what is it exactly you are doing :) :) ;-)
Can you share schematic of hardware design?
 
if it takes a few seconds then the input is probably read a few thousand time before the wrong data happens so I wonder why at that particular moment, do you have anything else in your code that could interfere in the reading process, any interrupt or some other peripheral?


I already disable all peripheral I am using..
I just only input shift registers..


okay.. I will update this thread with complete information.

I will upload here the simulation and the source code I am workin on..

---------- Post added at 10:25 ---------- Previous post was at 10:23 ----------

The above bold letters indicate that you are using 8 pins for switches...so I thought to use Intr on Change.

Now, I am confused...what is it exactly you are doing :) :) ;-)
Can you share schematic of hardware design?

the schematic posted by alexan_e is what exactly I am using..

whatever input received from that shift register will be copied to another port.. that is only I want to do..

I will upload here my work..

Thanks
 

you can try to validate the data before using them by comparing that the last x number of results are the same, you read the input very fast anyway so event a momentary button press will give a few hundred times the same result.
As an additional advantage you can probably use it to debounce your buttons in the real circuit


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
int get_inputDATA()
{
    int data = 0,i;
    static int prev_data, count;  
 
    SPI_InLatch = 1; //load data
    delayus(1); 
    SPI_InLatch = 0;
 
    SPI_PLOAD = 0;
    delayus(1); 
    SPI_PLOAD = 1;
    
    
    for (i=7; i>=0; i--)
    {
        if(SPI_SI)
        data |= (1 << i); //store data received
        toogle_clock();
    }
 
    //return data;
    
    if (data==prev_data) count++;  // increment while getting the same data
    else {prev_data=data; count=0;} // if not same as previous data keep new data and reset counter
    
    if (count==10)  // if data was the same for 10 times or you can try more times
    {      
        count=0;
        if(data != 255)
        store = data;
        
    }
    
    return store;
 
}



Alex
 
Hi Alexan_e..

Still the same value is not retaining.. Im attaching the proteus simulation and the source code..
 

Attachments

  • button.7z
    40.1 KB · Views: 52

in proteus simulation I get a watchdog expired warning every 2.3sec , this is probably the reason that the value is lost, a total reset
 
what do you mean? in my simulation?

---------- Post added at 12:22 ---------- Previous post was at 12:21 ----------

okay.. I see it now.. I will try to hardcode the configuration bits and disable the watchdog timer.
 

yes, check the simulation log while running the simulation
 

I see it now.. it's weird I can't disable the WDT... it's weird . WDT is not running in simulation in some PIC simulation..
 

I have no idea, maybe it is a bug of proteus model, if the watchdog is not working in reality then the result will be correct

---------- Post added at 13:41 ---------- Previous post was at 13:40 ----------

if the watchdog is not running in your ide simulator then it is probably a proteus model bug
 

yes.... WE wasted time because of this.. now I lear again from you.. next time I will always visit the protues log... Thanks so much

I spent almost 24hours for this problem...
 

hi,, the problem was already spotted.. the watchdog timer keeps reseting the MCU.. it's weird ..

---------- Post added at 14:05 ---------- Previous post was at 13:45 ----------

Problem solved!!!

for future reference to other members. if you are working with PIC18f452 in proteus YOU NEED TO DISABLE THE WATCHDOG TIMER...

special thanks to Alexan_e..

thanks so much..
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top