on off with one button

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

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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.

 

 

  Joined November 06, 2020      64
Friday at 08:06 PM

In Keil, your interrupt rutine can be made:

  Joined November 06, 2020      64
Friday at 08:06 PM

  Joined November 06, 2020      64
Friday at 08:06 PM

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

The simplified schematic:

 

  Joined November 06, 2020      64
Friday at 08:06 PM

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

  Joined November 06, 2020      64
Friday at 08:06 PM

#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++);
}
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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? 

 

 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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?

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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?

  Joined November 24, 2020      25
Tuesday at 03:39 AM

  Joined November 06, 2020      64
Friday at 08:06 PM

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

its working like, when ı pull it led isnt enable

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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?

 

  Joined November 06, 2020      64
Friday at 08:06 PM

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.

  Joined February 12, 2018      696
Monday at 02:11 PM

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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? 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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? 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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;}
}
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

  Joined November 06, 2020      64
Friday at 08:06 PM

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.

  Joined November 06, 2020      64
Friday at 08:06 PM

Try this code:

 

  Joined November 06, 2020      64
Friday at 08:06 PM

Or this:

 

  Joined November 06, 2020      64
Friday at 08:06 PM

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

ı need button counter. 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

Does you nitice any changes on LED status?

 

  Joined November 06, 2020      64
Friday at 08:06 PM

  Joined November 06, 2020      64
Friday at 08:06 PM

#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++);
}
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

#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++);
}
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

look these are my codes. 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

look these are my codes. 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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?

  Joined November 06, 2020      64
Friday at 08:06 PM

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.

 

 

  Joined November 06, 2020      64
Friday at 08:06 PM

  Joined November 06, 2020      64
Friday at 08:06 PM

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

  Joined November 06, 2020      64
Friday at 08:06 PM

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

can ı set just falling edge or rising egde? 

  Joined November 24, 2020      25
Tuesday at 03:39 AM

If it is youtube, just type name here.

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

  Joined November 06, 2020      64
Friday at 08:06 PM

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

For active low switch is this:

 

  Joined November 06, 2020      64
Friday at 08:06 PM

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

For active low switch is this:

  Joined November 06, 2020      64
Friday at 08:06 PM

  Joined November 06, 2020      64
Friday at 08:06 PM

#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++);}
 
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

#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++);}
 
}

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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.

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

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

  Joined November 06, 2020      64
Friday at 08:06 PM

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.

 

  Joined November 06, 2020      64
Friday at 08:06 PM

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.

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

mervedamar@hotmail.com

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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

  Joined November 24, 2020      25
Tuesday at 03:39 AM

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.

  Joined November 26, 2020      2
Thursday at 04:34 PM