The OP
Published on 2018-11-5 20:20
Only look at the author
This post is from MCU
Latest reply
I use the PIC16F677 microcontroller and
use the PWM output of the TMR2 timer. The functional requirement is that the positive duty cycle is 100uS, and the frequency can be adjusted from 1k to 3k. Then I use timer 1 for 10ms timing, and process it in the interrupt service function. Directly change the value of PR2 to achieve frequency adjustment. In the main function, operate PSTRCONbits.STRB = 1; T2CONbits.TMR2ON = 1; to turn on the PWM output, and use PSTRCONbits.STRB = 0; T2CONbits.TMR2ON = 0; to turn off the PWM output.
Then, when the PWM waveform is output, there will be a continuous low level of 1.3ms without output, as shown in the oscilloscope picture.
///
...
In timer 1 operation, enter the interrupt service function once every 10ms
unsigned char pr2num;
unsigned char setp_num = 1;
unsigned char PWM_FREQL = 249; //1Khz low frequency assignment
unsigned char PWM_FREQH = 60; //3.5kHZ 3521hz high frequency assignment
void TMR1_Initialize(void)
{
T1CON = 0x00; //T1 clock is not divided by fosc/4 formula 65536-(X/4)
TMR1H = 0x00; //1S timing initial value 0
TMR1L = 0x00;
PIE1bits.TMR1IE = 1;
PIR1bits.TMR1IF = 0;
INTCONbits.PEIE = 0; //Peripheral interrupt disabled
}
void TMR1_ISR (void) //Not used
{
TMR1H = 0xF6;//10ms
TMR1L = 0x3C;
flag_tick_count++;
if(PWMOUT_on == 1) //If PWMOUT_on = 1 in the main function, the PWM output will be turned on and the change from 1k-3.3k will be PR2-decrease+
{
if(PWMOUT_up_dowm == 0)
{
pr2num -= setp_num;
if (pr2num <= PWM_FREQH)
{ PWMOUT_up_dowm
= 1;
}
}
else
{
pr2num += setp_num;
if(pr2num >= PWM_FREQL)
{
PWMOUT_up_dowm = 0;
}
}
PR2 = pr2num;
}
}
/// ...
if(pwm_start == 1)// PWM is on pwm_start =1, off =0
{
if(PWMOUT_on == 0)
{
PWMOUT_on = 1;
TMR2 = 0 ;
pr2num= PWM_FREQL;
PSTRCONbits.STRB = 1;
T2CONbits.TMR2ON = 1;
}
}
else
{
if(PWMOUT_on == 1)
{
PWMOUT_on = 0;
TMR2 = 0;
pr2num= PWM_FREQL; Here, PR2 is assigned an initial value to stop output, and
PSTRCONbits.STRB = 0;
T2CONbits.TMR2ON = 0;
}
}
Details
Published on 2020-4-2 16:51
| ||
|
||
This post is from MCU
| ||
|
||
|
3
Published on 2018-11-5 23:56
Only look at the author
| |
Personal signature刻苦学习,共同进步
|
|
|
4
Published on 2018-11-6 08:04
Only look at the author
| |
|
|
|
5
Published on 2018-11-6 08:52
Only look at the author
This post is from MCU
| ||||||||||
|
||||||||||
|
dingzy_2002
Currently offline
|
6
Published on 2018-11-6 11:27
Only look at the author
This post is from MCU
Comments | |
|
||
|
dingzy_2002
Currently offline
|
7
Published on 2018-11-6 11:32
Only look at the author
This post is from MCU
| |
|
||
|
dingzy_2002
Currently offline
|
8
Published on 2018-11-6 11:36
Only look at the author
This post is from MCU
| |
|
||
|
This post is from MCU
| ||
|
||
|
This post is from MCU
| ||
|
||
|
This post is from MCU
| ||
|
||
|
dingzy_2002
Currently offline
|
This post is from MCU
| |
|
||
|
13
Published on 2020-4-1 10:54
Only look at the author
This post is from MCU
Comments
1. Generally, PR2 is used to set the PWM period/frequency. Under normal circumstances, the frequency range is determined according to the application requirements. Do not make any changes. 2. If PR2 is modified alone, the PWM period will change greatly and the resolution will be insufficient.
Details
Published on 2020-4-2 12:50
| ||
|
||
|
dingzy_2002
Currently offline
|
14
Published on 2020-4-2 12:50
Only look at the author
This post is from MCU
| |
|
||
|
15
Published on 2020-4-2 16:51
Only look at the author
This post is from MCU
| ||
|
||
|
EEWorld Datasheet Technical Support
EEWorld
subscription
account
EEWorld
service
account
Automotive
development
circle
About Us Customer Service Contact Information Datasheet Sitemap LatestNews
Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190