on off with one button

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
on off with one button
November 24, 2020 - 3:42am

Hi all. ı use N76E003. ı need on off with one button. Can u help me please? ı use keil. 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

I want the output to be active when I press it, and to be passive when I press it again.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

The basic structure should look like this (it isnt the code, just sketch):

 

Of course, you can do it without interrupt - like polling input pin in main(), but you will waiste machine cycles.

Set rising edge trigering in interrupt register.

I choosed P0.5 as output and P3.0 as input.

A good idea is to use a little capacity on your button pin as a harware filtering, in profesional design a software debouncing also.

 

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

In Keil, your interrupt rutine can be made:

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

The last comment is all program, i didn`t compile it, you must try.

The simplified schematic:

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

The program and the schematic uses falling edge triggering (i changed it from original plan - 1st post) , so P3.0 is normally high via 2k2, low when button pressed

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"
 
 
void Delay_ms(unsigned int time);
int i;
 
void EXT_INT0(void)
interrupt 0
{
static unsigned char count=0;
if(count){
P12=1;
count=0;}
else {
P12=0;
count=1;
}
}
void main (void) 
{  
P12_PushPull_Mode;
P30_Input_Mode;
set_P3S_0;
  set_IT0;
set_EX0;
set_EA;
 
while(1)
  {
 
}}
 
 
void Delay_ms(unsigned int time1)
{
 
while (time1--)
{
for(i=0;i<200;i++);
}
}
merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

Elimi tuttuğumda led sönüyor. Ben bunu istemiyorum Basıp çektikten sonra ledin aktif olmasını istiyorum.

bastığımda led yanıp sönüyor ama çektiğimde led yanıp sönmüyor. Beni anlıyor musun? 

 

 
merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
Elimi tuttuğumda led sönüyor. Bunu istemiyorum. LED'in bastıktan sonra aktif olmasını istiyorum.

Bastığımda led yanıp sönüyor ama çektiğimde led yanıp sönmüyor. Beni anlıyor musun?
merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
When I hold my hand the led goes out. I do not want this. I want the LED to be active after I press it.

When I press it the led blinks but when I pull it the led does not blink. Do you understand me?
Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

not changing dear. ı just want, when ı press and pull it led is enable. 

its working like, when ı pull it led isnt enable

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Ok, a few questions:

How do you connect the button? Is it like in schematic I post?

How do you connect LED? Anode to P1.2 , catode to ground?

Do you use breadboard or some development kit?

 

I never use Keil, maybe there is some code optimization which destroy desired functionality.

Maybe it needs to clear ext.int0 flag after jump to subrutine. You can try put clr_IE0;  into begining of interrupt function.

I don`t know this uC, maybe it jumps to ext.int0 subrutine on falling and rising edge both.

Is this the only part of program, or do you extend it by some yours?

 

Sourav Gupta's picture

Offline

Joined: Feb 12, 2018

Replies: 585

Use a global veriable for example that value will be inverted from its retained value. Glow the led by checking the stored value.

For example make a global variable named 'status' and initialize with 0.

When you press the button the status will be 1 until you press the button once again.

Now the led state = status;

Thus the same button can be used to change the state of the led when you will press it.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

ı connected like your schmatic. ı use Nutiny-SDK-N76E003 V2.1

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

yes ı did like your idea. but no . really ı cant belive. ı try to read button status. when ı will push it, ı see 0, when ı pull it ı see 1. is it normal? 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

yes ı did like your idea. but no . really ı cant belive. ı try to read button status. when ı will push it, ı see 0, when ı pull it ı see 0. is it normal? sorry my mistakes. ı always see 0. is it normal? 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
int count;
int a=1;
void main (void) 
{  
InitialUART0_Timer1(9600);
P12_PushPull_Mode;
P30_Input_Mode;
 
 
 
while(1)
  {
// P12=0;
count=P30;
printf("adc : %d\r\n",count);
// Delay_ms(100);
if(count==0){//Delay_ms(50);
if(count==1)
a++;
}
if(a%2== 0){P12=0;}
if(a%2== 1){P12=1;}
}
}
Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

The LED on this board is connected in sink mode (catode to P12), so you have to clear P12 to LED start glowing.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

I would try to turn off all code optimization in Keil setings.

Choose this settings:

Optimization level -O0

-O0 disables all optimizations. This optimization level is the default. Using -O0 results in a faster compilation and build time, but produces slower code than the other optimization levels. Code size and stack usage are significantly higher at -O0 than at other optimization levels. The generated code closely correlates to the source code, but significantly more code is generated, including dead code.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Try this code:

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Or this:

 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

ı tryed but not changed dear. when pull my hand led is not enable. ı am trying to read button now.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

ı need button counter. 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Try to remove button, and make "button press" with wire.

Does you nitice any changes on LED status?

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"

void Delay_ms(unsigned int time);
int i;
unsigned char a= 1;
unsigned char b= 0;
void main (void) 
{  
  InitialUART0_Timer1(9600);
 P12_PushPull_Mode;
 P30_Input_Mode;
  P02_Input_Mode;

 
 while(1)
  {
    a = a+b;
    if(P30==1)
    {
      Delay_ms(50); 
      if(P30==0)
      {
        a++;
        printf("adc : %d\r\n",a);
        if(a%2== 0) 
        {
          clr_P12;
        }
        b=a;
        a=1;
      }
    }
    
    if(P02==1)
    {
      Delay_ms(50); 
      if(P02==0)
      {
        a++;
       printf("adc : %d\r\n",a);
       if(a%2== 0) 
        {
          set_P12;
        }
       b=a;
        a=0;
      }
    }
  }
}

void Delay_ms(unsigned int time1)
 {
 
 while (time1--)
{
for(i=0;i<200;i++);
}
}

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
#include "Delay.h"

void Delay_ms(unsigned int time);
int i;
unsigned char a= 1;
unsigned char b= 0;
void main (void) 
{  
  InitialUART0_Timer1(9600);
 P12_PushPull_Mode;
 P30_Input_Mode;
  P02_Input_Mode;

 
 while(1)
  {
    a = a+b;
    if(P30==1)
    {
      Delay_ms(50); 
      if(P30==0)
      {
        a++;
        printf("adc : %d\r\n",a);
        if(a%2== 0) 
        {
          clr_P12;
        }
        b=a;
        a=1;
      }
    }
    
    if(P02==1)
    {
      Delay_ms(50); 
      if(P02==0)
      {
        a++;
       printf("adc : %d\r\n",a);
       if(a%2== 0) 
        {
          set_P12;
        }
       b=a;
        a=0;
      }
    }
  }
}

void Delay_ms(unsigned int time1)
 {
 
 while (time1--)
{
for(i=0;i<200;i++);
}
}

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

look these are my codes. 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

look these are my codes. 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

It seems to me, now you are trying to use one button to set and another button to clear the output.

Am I correct?

Or something more? I see, you count some numbers and sending it via UART. Some counter?

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

However, this is very strange:

 

while(1)

  {

    a = a+b;

At some point the "a" variable will overflow. (reaching the maximum limit and then start increasing from 0) .

I just ask, maybe I didnt catch the idea.

 

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

This code switch the output after every button press (like you wanted) using polling bit in forever loop. It isnt bulletproof, but usable.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

I can not explain what I want. I want to add a video but I cannot.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

I can not explain what I want. I want to add a video but I cannot.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

can ı set just falling edge or rising egde? 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

If it is youtube, just type name here.

With little modification and clarification the rising, falling edge and level trigg. can be done.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

I am sorry, the last code I uploaded here is suitable for active high switch.

For active low switch is this:

 

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

I am sorry, the last code I uploaded here is suitable for active high switch.

For active low switch is this:

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
 
 
int i;
int counter=0;
void Timer0_ISR (void) ;
void Delay_ms(unsigned int time1);
 
void Timer0_ISR (void) interrupt 1  
{
    
    TL0 = (1536& 0x00FF);
    TH0 = ((1536 & 0xFF00) >> 8);
   counter++;
}
 
 
void main (void) 
{  
P15_Quasi_Mode; // Output
P30_Input_Mode; //Input
  TIMER0_MODE1_ENABLE;
set_T0M;
set_ET0;
set_EA;
 
P15=0;
while(1)
{
 
if(P30==1){
P15=1;
clr_TR0;
set_TR0; //Timer0 run
if(P30==0){
if(counter >=5){ // 
P15=1;
counter=0;
clr_TR0;
P15=1;
 
}
 
}}}
}
 
void Delay_ms(unsigned int time1)
{
 
while (time1--){for(i=0;i<200;i++);}
 
}
merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25
#include "N76E003.h"
#include "SFR_Macro.h"
#include "Function_define.h"
#include "Common.h"
 
 
int i;
int counter=0;
void Timer0_ISR (void) ;
void Delay_ms(unsigned int time1);
 
void Timer0_ISR (void) interrupt 1  
{
    
    TL0 = (1536& 0x00FF);
    TH0 = ((1536 & 0xFF00) >> 8);
   counter++;
}
 
 
void main (void) 
{  
P15_Quasi_Mode; // Output
P30_Input_Mode; //Input
  TIMER0_MODE1_ENABLE;
set_T0M;
set_ET0;
set_EA;
 
P15=0;
while(1)
{
 
if(P30==1){
P15=1;
clr_TR0;
set_TR0; //Timer0 run
if(P30==0){
if(counter >=5){ // 
P15=1;
counter=0;
clr_TR0;
P15=1;
 
}
 
}}}
}
 
void Delay_ms(unsigned int time1)
{
 
while (time1--){for(i=0;i<200;i++);}
 
}
merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

This code led is off first, it is on for a while when I press the button and pull it. It goes off after 3 seconds.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Is it working? The code is very "wild".

According your explanation, it is a monostable circuit - the state returns back after time interval.

Michal Podmanický's picture

Offline

Joined: Nov 06, 2020

Replies: 64

Btw, why dont you use a delay function from Keil library, it is written much better.

However, it isnt good to use delay func. very offen, it waist the performance of uC.

 

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

not istnt working. led light first, but later led not enable. oh this problem is very easy but ı cant understand that why dont work.

ı use timer, but this code should work, so led should be stable.

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

can u write my mail adress? ı will send you video? 

mervedamar@hotmail.com

merve damar's picture

Offline

Joined: Nov 24, 2020

Replies: 25

ı did. ı have wdt reset. so program being reset. ı have some settings.  ı changed and ok now :)

Milind Sandhansive's picture

Offline

Joined: Nov 26, 2020

Replies: 2

hi

can any one help button event programming with Nuvoton N76E003,

Single push button for multi task, such as short press and long press detection with a single push button.

Pages