|
|
View previous topic :: View next topic |
Author |
Message |
osmelfb86
Joined: 21 Aug 2021 Posts: 5
|
I am looking for help to use a pic 16f628a to turn on LEDs |
Posted: Sat Aug 21, 2021 11:37 am |
|
|
Hello friend, at the moment I am trying to carry out a project to turn things on or led using an IR remote control, for example with a remote control of the most used with the Nec protol, will anyone have an idea of how this project can be carried out? |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1941 Location: Norman, OK
|
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 482 Location: Montenegro
|
|
Posted: Sat Aug 21, 2021 8:40 pm |
|
|
Maybe this'll help, maybe not. So you have an IR detector. NEC protocol has a 9ms starting burst. This is the longest time without any transition. I'd go with detecting this 9ms start condition with CCP module. You never specified a particular key, so address and data payload isn't an issue. If I read the picture of the protocol correctly, even restarts have those 9ms windows to help with a remote turned somewhere random if you just hold any key down.
Last edited by PrinceNai on Sat Aug 21, 2021 8:58 pm; edited 1 time in total |
|
|
osmelfb86
Joined: 21 Aug 2021 Posts: 5
|
|
Posted: Sat Aug 21, 2021 8:58 pm |
|
|
Thank you very much friend, I really am a newbie in this topic, I wrote to see if someone on the forum already had an example to be able to turn things on, using a remote control, infrared. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 482 Location: Montenegro
|
|
Posted: Sat Aug 21, 2021 9:16 pm |
|
|
Whatever digital sensor you use, no matter Ir or PIR, button or a switch, they just give you impulses (forget debounce time for now for a mechanical switch). Some last for a predefined time, some change the state for the duration . If you follow this way, it is just a matter of detecting a change and measuring the time until the next change happens. For a start I'd go with 9ms +-10% and see what happens. You could also also use interrupt on change firing on both edges. Enable any timer interrupt (for which you know the exact time of overflow based on your clock) when any change happens, increment a variable in there and wait for the next change fom IR transmitter and with that next entry to the IOC interrupt . Then compare the value of your variable with something. If it is within a range, set a flag for your main to turn the LED on or off. If not, reset the variable. In main, when you get a positive, reset the flag and disable those two interrupts for a second or two, so the led stays on or off. Reset the flag and enable interrupt on change. |
|
|
osmelfb86
Joined: 21 Aug 2021 Posts: 5
|
|
Posted: Sat Aug 21, 2021 10:14 pm |
|
|
I understand you quite a friend, excuse me if I ask you, please, if you can share an example of how to apply what you indicate, it is that I am a newbie in these topics, programming in the pic. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 482 Location: Montenegro
|
|
Posted: Sat Aug 21, 2021 10:36 pm |
|
|
Well, I have stolen my fair bit of code and knowledge from this forum. Bear in mind there on this forum are at least 100 persons that can give you a better, faster and more logical answer. This is only my take on it, no guarantees. You happened to ask on a weekend, almost no replies are in that time. What PIC do you use? Frequency? This one is crucial for any code using timers to work. Timings. All my development stuff is on 18f46k22. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 482 Location: Montenegro
|
|
Posted: Sat Aug 21, 2021 10:51 pm |
|
|
Please understand. You should at least indicate how you intended to tackle the problem. Right, wrong, it doesn't matter. What have you tried so far? Before you post a question, there should be some work or at least some thought processing before. Ready made solutions are bought. Please code something out from the thin air is not the right kind of a question. Post what you did so far. |
|
|
osmelfb86
Joined: 21 Aug 2021 Posts: 5
|
|
Posted: Sat Aug 21, 2021 11:33 pm |
|
|
Thanks friend, I will try to do something in the source code. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 482 Location: Montenegro
|
|
Posted: Sun Aug 22, 2021 8:14 pm |
|
|
Couldn't sleep, way too hot.
This may or may not work, maybe someone will look into it and comment.
Code: |
#include <main.h>
#define LED PIN_B1 // where your LED is
#define CCP_INPUT PIN_C2
unsigned int8 Overflow_Count = 0; // counts Timer1 overflows
unsigned int16 StartTime; // holds the value of CCP_1 when the L-to-H interrupt occurs
unsigned int16 EndTime; // holds the value of CCP_1 when the H-to-L interrupt occurs
unsigned int32 Ticks = 0; // number of 4us ticks between end and start
int1 GotCapture = 0; // signal to main we have a H-to-L capture
// ++++++++++++++++++++++++++++ INTERRUPTS ++++++++++++++++++++++++++++++++++++
#INT_TIMER0
void TIMER0_isr(void) {
}
// 262 ms overflow
#INT_TIMER1
void TIMER1_isr(void) {
Overflow_Count++; // increment overflow counter
}
// this interrupt is triggered when a change is detected on PIN_C2
// the impulse is like this:
// 1 ---------
// l l
// l l
// l l
// 0 --------- ----------
// 9ms
#INT_CCP1
void CCP1_isr(void) {
// L-to-H change, start of capture. We are looking for a transition from low to high on PIN_C2
if(input_state(CCP_INPUT)){ // we have L-to-H transition, rising edge was detected. This is the start transition.
Overflow_Count = 0; // clear overflow counter
StartTime = CCP_1; // get start time
setup_ccp1(CCP_CAPTURE_FE); // capture on PIN_C2, change for a falling edge to get end time
}
// H-to-L change, end of capture. We have a transition from high to low on PIN_C2
else{
EndTime = CCP_1;
setup_ccp1(CCP_CAPTURE_RE); // capture on PIN_C2, initialized for a rising edge
GotCapture = 1; // indicate to main we had a L_to_H_to_L
}
}
// ++++++++++++++++++++++++++ END OF INTERRUPTS +++++++++++++++++++++++++++++++
void main(){
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32); //1.0 s overflow
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8); //used for capture, 262 ms overflow, 1 tick = 4us, 16 bit timer, input pin C2, CCP1 register
setup_ccp1(CCP_CAPTURE_RE); // capture on PIN_C2, initialized for a rising edge
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_CCP1);
enable_interrupts(GLOBAL);
// ----------------------------------------------------------------------------
while(TRUE){
if(GotCapture){ // so we captured something
GotCapture = 0;
Ticks = (Overflow_Count*65535 + EndTime) - StartTime; // take Timer1 overflow into account
// we are looking for a 9ms pulse, give or take 1ms. Let's say anything between 8 and 10 is ok, but it is to be tested.
// 8ms equals 2.000 ticks, 10ms equals 2.500 ticks at 4us per tick
if( 2000 < Ticks < 2500){ // check if we are in the zone
output_toggle(LED); // if yes, turn led on or off
disable_interrupts(INT_CCP1); // disable interrupts for a while to have a change of LED for at least 2s
delay_ms(2000);
enable_interrupts(INT_CCP1); // and return looking for a rising edge
} // if Ticks
} // if (got capture)
} // while true
} // main
|
.h file
Code: |
#include <18F46K22.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#device ICD=TRUE
#use delay(internal=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,errors)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)
|
|
|
|
|
|
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
|