Skip to content

Instantly share code, notes, and snippets.

@Rumidom
Created October 4, 2024 01:39
Show Gist options
  • Select an option

  • Save Rumidom/28d41663bd0606bb29ad7aaa9aef530d to your computer and use it in GitHub Desktop.

Select an option

Save Rumidom/28d41663bd0606bb29ad7aaa9aef530d to your computer and use it in GitHub Desktop.
NEC Protocol example.py
from machine import Pin,PWM
from time import ticks_diff, ticks_us
import time
LED_Sig = Pin(2, Pin.OUT)
LED_Neg = Pin(0, Pin.OUT)
LED_Pos = Pin(3, Pin.OUT)
LED_Neg.off()
LED_Pos.on()
pwm = PWM(LED_Sig, freq=38000)
pwm.duty_u16(1310)
REC_Sig = Pin(20, Pin.IN, Pin.PULL_UP)
Last_Interrupt_t = time.ticks_us()
def rec_interrupt(pin):
global Last_Interrupt_t
print(time.ticks_us()-Last_Interrupt_t)
Last_Interrupt_t = time.ticks_us()
def send(bit):
pwm.duty_u16(1310)
time.sleep_us(560)
pwm.duty_u16(0)
if bit == 0:
time.sleep_us(565)
else:
time.sleep_us(1685)
def send_frame():
pwm.duty_u16(1310)
time.sleep_us(9000)
pwm.duty_u16(0)
time.sleep_us(4500)
def end_frame():
pwm.duty_u16(1310)
time.sleep_us(560)
pwm.duty_u16(0)
REC_Sig.irq(trigger=Pin.IRQ_FALLING, handler=rec_interrupt)
send_frame()
for bit in [0,1,1,1,0,0,1,1]:
send(bit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment