Hi all. ı use N76E003. ı need on off with one button. Can u help me please? ı use keil.
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.
The last comment is all program, i didn`t compile it, you must try.
The simplified schematic:
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
Elimi tuttuğumda led sönüyor. Ben bunu istemiyorum Basıp çektikten sonra ledin aktif olmasını istiyorum.
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?
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?
not changing dear. ı just want, when ı press and pull it led is enable.
its working like, when ı pull it led isnt enable
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?
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.
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?
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?
The LED on this board is connected in sink mode (catode to P12), so you have to clear P12 to LED start glowing.
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.
ı tryed but not changed dear. when pull my hand led is not enable. ı am trying to read button now.
Try to remove button, and make "button press" with wire.
Does you nitice any changes on LED status?
#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++);
}
}
#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++);
}
}
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?
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.
This code switch the output after every button press (like you wanted) using polling bit in forever loop. It isnt bulletproof, but usable.
If it is youtube, just type name here.
With little modification and clarification the rising, falling edge and level trigg. can be done.
I am sorry, the last code I uploaded here is suitable for active high switch.
For active low switch is this:
I am sorry, the last code I uploaded here is suitable for active high switch.
For active low switch is this:
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.
Is it working? The code is very "wild".
According your explanation, it is a monostable circuit - the state returns back after time interval.
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.
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.
ı did. ı have wdt reset. so program being reset. ı have some settings. ı changed and ok now :)
merve damar
PermalinkI want the output to be active when I press it, and to be passive when I press it again.
- Log in or register to post comments
Joined November 24, 2020 25Tuesday at 03:39 AM