/* Name: JJYSimulator_40kHz.c * Author: Toshi Nagata * Copyright: 2009-2018 (c) Toshi Nagata * License: Modified BSD License * * Clock: External 8MHz * Pin 5 (PB0): from ESP8266 (RF on/off) * Pin 6 (PB1): antenna out */ #include #include #include #include #define F_CPU 8000000.0 inline void pwm_on(void) { TCCR0A |= 0b00110000; // connect OC0B to PWM } inline void pwm_off(void) { TCCR0A &= 0b11001111; // disconnect OC0B from PWM PORTB |= 0b00000010; // output 1 to OC0B(=PB1) } /* Pin change interrupt handler */ ISR(PCINT0_vect) { if ((PINB & 0b00000001) != 0) { pwm_on(); } else { pwm_off(); } } int main(void) { cli(); // Disable interrupt DDRB = 0b00000010; // PB1 for output PORTB = 0b00000001; // PB0 enable pull-up // timer0: Highspeed PWM for 40kHz duty 1/2 Output (to OC0B) // external 8MHz, prescale 1/1: 1 timer clock = 0.125 us // 40kHz 1 cycle = 25 us = 200 clocks, duty 1/2: 25:25 TCCR0A = 0b00110011; // Set OC0B at Compare Match (=OCR0B) and clear at TOP TCCR0B = 0b00001001; // Prescale 1/1, Fast PWM with TOP = OCR0A TCNT0 = 0; OCR0A = 200; // PWM interval 25 us OCR0B = 100; // Duty 1/2 GIMSK = 0b00100000; // PCINT enabled PCMSK = 0b00000001; // PCINT0 (pin 5) enabled set_sleep_mode(SLEEP_MODE_IDLE); // Idle mode (PWM will keep running) pwm_off(); sei(); // Enable interrupt while (1) { sleep_enable(); sleep_cpu(); // Sleep until interrupt sleep_disable(); _delay_loop_1(100); // Wait for some time and go to sleep again } return 0; // Not reached }