// Cpu ATTiny13
// Frequency: Internal 9.6000Mhz without frequency division
// winavr gcc optimization level O0
#include #define nop() __asm__ __volatile__("nop") //Empty statement unsigned char PWM[]={0x20,0x40,0x60,0x80,0xA0,0xC0,0xE0,0xFF}; unsigned char PWMON; unsigned char PWMVal; //IO pin initialization void port_init(void) { PORTB = 0x08; //PB3 connected button is set to output pull-up DDRB = 0x37; //PB0 is set to 0 when the PWM output is powered on } //Timer0 initialization //Fast PWM mode frequency 37.500KHz void timer0_init(void) { OCR0A = 0x00; TCNT0 = 0x01; //Design number TCCR0A = 0x03; //Set Timer0 to fast PWM mode TCCR0B = 0x01; //Start Timer0 } //Delay N milliseconds void delay_ms(void) { unsigned char TempCycA; unsigned char TempCycB; for (TempCycA=0; TempCycA<10; TempCycA++) for (TempCycB=0; TempCycB<90; TempCycB++) nop(); } //Main function int main(void) { unsigned int tempval=0; unsigned int keytime=0; port_init(); timer0_init(); PWMON = 1; //Initially PWM is on PWMVal = 0; //First gear OCR0A = PWM[PWMVal]; //Change the duty cycle TCCR0A = 0x83; //Turn on PWM while(1) { if (!(PINB&0x08)) { keytime = 0; for (tempval=0; tempval<4000; tempval++) { if (!(PINB&0x08)) //Key sampling, anti-shake { keytime++; delay_ms(); } else break; } } if (PWMON) //When PWM is on { if ((keytime>100)&&(keytime<1000)) //Short press 100-1000ms to adjust { PWMVal++; if (PWMVal>7) //Loop PWMVal = 0; keytime = 0; OCR0A = PWM[PWMVal]; //Change the duty cycle } if (keytime>2999) //Long press for 3 seconds to turn off { PWMON = 0; keytime = 0; TCCR0A = 0x03; //Turn off PWM delay_ms(); //Delay } } else //When PWM is turned off { if (keytime>2999) //Long press for 3 seconds to turn on { PWMON = 1; keytime = 0; TCCR0A = 0x83; //Turn on PWM } } } }
Previous article:How to build a digital keypad security door lock using Arduino
Next article:Design of wireless sensor network nodes based on microcontroller and wireless communication module
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Guangzhou Auto Show: End-to-end competition accelerates, autonomous driving fully impacts luxury...
- Lotus launches ultra-900V hybrid technology "Luyao" to accelerate the "Win26" plan
- The new engine of the automotive industry: AI-driven predictive maintenance
- Himax Technologies releases third-generation LCD TDDI and high-end OLED touch IC and announces mass production to promote innovation in automotive display technology
- ON Semiconductor and Subaru Collaborate to Integrate Hyperlux Sensors into Subaru's Next-Generation EyeSight System
- Professor Tang Weiping of Shanghai Jiao Tong University: Research progress of two solid electrolyte materials
- Intelligent driving + intelligent cabin, how can domestically produced “smart cars” become more popular?
- What exactly is SLAM, which is often mentioned in autonomous driving?
- Shenzhen Institute of Advanced Technology and BYD will cooperate in the development of solid-state batteries
- Honda's all-solid-state battery demonstration production line is unveiled for the first time and will be put into use in January 2025
- Digitalization and technological progress make mobile multimedia a reality
- Answer the questions to win prizes! ADI Technology Express - Automotive Application Entertainment and Information Interaction Solutions
- [NXP Rapid IoT Review] Hello World project reads sensor values that are always 0
- Looking for TCP/IP communication tools
- Why can't the output of LM5117 reach 5V?
- TSD circuit
- A few questions for help
- [Repost] Introducing several clever and cheap current detection circuits
- Sharp develops analog circuit technology that automatically controls current consumption
- Introduction to 5G Small Cells