#include void GPIO_INIT()
typedef unsigned int uint;
typedef unsigned char uchar;
#define KEY0_DOWN() (RB4==0)//define key pressed
__CONFIG(0X1014);//internal clock
uchar KEY0_FLAG=0;
/*****************************/
//OSC SET; INTER 4MHZ PERIOD 1US
/*****************************/
void OSC_INIT()
{
OSCCON=0X67;//set internal clock 4MHZ
}
/*****************************/
//DELAY FUNCTION();
/*****************************/
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*************************/
//GPIO SET;
/*****************************/
{
TRISC5=0;//RC5 SET PWM OUT;
TRISD2=0;//RD2 SET PWM OUT;
RC5=RD2=0;
TRISB4=1;
IOCB4=1;//Enable RB4 level change interrupt
WPUB=0X10;//Enable pull-up
RBIE=1;
RBIF=0;
PEIE=1;
GIE=1;
}
/*************************/
//PWM MODEL INIT;
/*************************/
void PWM_INIT()
{
CCP1CON=0B00001100;//PWM mode
CCP2CON=0B00001100;//PWM mode
T2CKPS1=1;
PR2=155;//PWM period = [PR2+1]*4*TOSC*T2 pre-division value, so PWM frequency = 1/period = 400HZ
TMR2ON=1;
CCPR1L=0;//Duty cycle = CCPR1L:CCP1CON<5:4>/4(PR2+1);
CCPR2L=0;//Duty cycle = CCPR2L:CCP2CON<5:4>/4(PR2+1);
}
void main()
{
GPIO_INIT();
PWM_INIT();
while(1);//Initialization is complete and waiting for interrupt response is OK
}
//Interrupt response entry
void interrupt ALL_ISR()//Output 10%-100% duty cycle PWM waveform
{
if(RBIF)
{
delay(5);
if(KEY0_DOWN())
{
KEY0_FLAG++;
if(KEY0_FLAG==11)KEY0_FLAG=0;
}
switch(KEY0_FLAG)
{
case 0:CCPR1L=CCPR2L=0;break;
case 1:CCPR1L=CCPR2L=16;break;
case break; case 10:CCPR1L=CCPR2L=156 ;
break
; default : break ; } RBIF =0 ; } }
Previous article:MCP2510 CAN communication program (pic microcontroller)
Next article:Introduction to PIC32 SPI (Master/Slave Mode)
- 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.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Use of 2.4G wireless communication module NRF24L01 on Cortex M
- Last day! Answer the questions and you will have a chance to win a 50-yuan Jingdong card! A powerful tool to simplify complex sensor systems
- How to select low-power MCU
- MSP430F5529 clock multiplier setting is effective
- C language basics, I think it's a good book
- Thousands of websites operate black mobile phone platforms, and their operation methods are quite covert
- Musk asked engineers to write the code silently: tear it up after review and recreate it on the computer
- DIY an eco-bottle
- How many layers does TCP/IP have? What is the function of each layer?
- MSP430 Flash information area