完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
嗨,所有,“我写了一个程序,利用PWM1-3,但我不确定如何设置PWM到特定输出引脚在PORTB(RB4-RB6)。在数据表中有关于PIN配置的下面,但是对于如何将RB4设置为PWM1输出还不十分清楚;所有PWM输出都与端口数据锁存器复用,因此引脚还必须通过清除相关端口tiS位来配置为输出。转换速率特性可以被配置为优化与PWM输出结合使用的速率。通过清除相关端口SLRCON位来实现高速输出切换。PWM输出可以被配置为开路漏输出,通过设置相关端口ODCON位“最好的问候”,
以上来自于百度翻译 以下为原文 Hi all, "I've written a program to utilise PWM1-3 but am unsure how to set the PWM to specific output pins on PORTB (RB4-RB6). in the datasheet there is the below regarding pin configuration but it's not quite clear on how to set RB4 to be PWM1 output; All PWM outputs are multiplexed with the PORT data latch, so the pins must also be configured as outputs by clearing the associated PORT TRIS bits. The slew rate feature may be configured to optimize the rate to be used in conjunction with the PWM outputs. High-speed output switching is attained by clearing the associated PORT SLRCON bits. The PWM outputs can be configured to be open-drain outputs by setting the associated PORT ODCON bits" Best Regards, |
|
相关推荐
4个回答
|
|
这个段落与如何分配输出到引脚无关。这是在PPS章节中所涉及的。
以上来自于百度翻译 以下为原文 That paragraph has nothing to do with how you assign outputs to pins. That is covered in the "PPS" chapter. |
|
|
|
谢谢Qyb,我有一些关于中断的问题,可以问一下,因为它是同一个项目吗?这是为了降低PWM的占空比,由于某些原因,当模拟器的端口输入改变时,它不会跳转到中断。请参见附加代码;编辑:由于某些原因,RC4和RC5在PORTCbits上显示高电平,我将其WPU比特设置为0,在下一次运行时得到。卡在中断例程中,看到IOCIF=1,边=0。
以上来自于百度翻译 以下为原文 Thanks Qyb, I'm having some issues with interrupt on change is it OK to question it here as it's same project? it's required for lowering changing the duty cycle of PWM. for some reason it doesn't jump to the interrupt when the port input is changed in the simulator. Please see attached code; /******************************************************************************/ /* CyclePlateV2.00 */ /* Microcontroller: PIC16F1578 */ /* PCB: Cycle Plate 2018 */ /* Author: btommo Date: 26/06/18 */ /******************************************************************************/ // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator; I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED Bit Can Be Cleared & Set Once) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include #include /******************************************************************************/ /*CONFIGURATION */ /******************************************************************************/ #define _XTAL_FREQ 32000000 //oscillator frequency for delay_ms() /******************************************************************************/ /* User Global Variable Declaration */ /******************************************************************************/ #define TURN PORTCbits.RC4 //positive trigger #define SIDE PORTCbits.RC5 //positive trigger #define RING PORTCbits.RC0 //pull up required #define CYCLEPWM PORTBbits.RB4 #define CRNRPWM PORTBbits.RB5 #define RINGPWM PORTBbits.RB6 uint8_t db_cnt; //debounce counter //uint8_t RLYs; //uint8_t MOMs; /******************************************************************************/ /* Main Program */ /******************************************************************************/ void initialise() { TRISB = 0; TRISC = 0b00110001; ANSELA = 0; ANSELB = 0; ANSELC = 0; //Oscillator control Register OSCCONbits.IRCF = 0b1110; //32MHz freguency instruction osc 8MHz OSCTUNEbits.TUN = 0; // Running at factory calibrated frequency //Option register OPTION_REGbits.nWPUEN = 0; //weak pull-ups enables by WPUX OPTION_REGbits.TMR0CS = 0; //TMR0 clock source is cycle clock OPTION_REGbits.PSA = 0; //Prescaler assigned to TMR0 OPTION_REGbits.PS = 0b111; //prescaler set to 1:256 (set after!!!) //INTCON INTCONbits.GIE = 1; //enable active interrupts INTCONbits.PEIE = 1; //enable active peripheral interrupts //INTCONbits.TMR0IE = 1; //enable TMR0 interrupt //INTCONbits.TMR0IF = 0; //reset TMR0 interrupt flag INTCONbits.IOCIE = 1; // enable interrupt on change INTCONbits.IOCIF = 0; // reset IOC flag IOCCPbits.IOCCP5 = 1; //rising and falling edge detection for SIDE IOCCNbits.IOCCN5 = 1; ADCON0bits.ADON = 0; // Disable ADC WPUCbits.WPUC0 = 1; //pull up on RING //PWM control PWM1CONbits.EN = 1; PWM1CONbits.POL = 0; PWM1CONbits.MODE = 0b00; PWM2CONbits.EN = 1; PWM2CONbits.POL = 0; PWM2CONbits.MODE = 0b00; PWM3CONbits.EN = 1; PWM3CONbits.POL = 0; PWM3CONbits.MODE = 0b00; //PWM interrupt on period /*PWM1INTEbits.PRIE = 1; PWM2INTEbits.PRIE = 1; PWM3INTEbits.PRIE = 1;*/ PWM1CLKCONbits.CS = 0b00; //FOSC PWM2CLKCONbits.CS = 0b00; PWM3CLKCONbits.CS = 0b00; PWM1CLKCONbits.PS = 0b000; //no prescaller period 8.192ms max PWM2CLKCONbits.PS = 0b000; //1/8x10^6 * 2^16 PWM3CLKCONbits.PS = 0b000; //for 5ms 2^16/8.192 * 5 = 40000 //load 2^16 - 40000 =25536 //Load 40000 into PRx //PWM load buffers at end of each period PWM1LDCONbits.LDA = 1; //load OFx PHx DCx and PRx at end of current period PWM1LDCONbits.LDT = 0; PWM2LDCONbits.LDA = 1; PWM2LDCONbits.LDT = 0; PWM3LDCONbits.LDA = 1; PWM3LDCONbits.LDT = 0; PWM1OFCONbits.OFM = 0b00; PWM2OFCONbits.OFM = 0b00; PWM3OFCONbits.OFM = 0b00; //set Period of PWM 40000 = 5ms PWM1PR = 40000; PWM2PR = 40000; PWM3PR = 40000; //set initial duty cycle to 99% //Duty cycle = (PWMxDC - PWMxPH)/PWMxPR+1 //PWMxDC = 0.99*(PWMxPR+1)+PWMxPH //=0.99*40001+0 PWM1DC = 39601; PWM2DC = 39601; PWM3DC = 39601; //for 20% duty cycle //PWMxDC = 0.2*40001+0 //=8000 //setting PWMxPH 0 PWM1PH = 0; PWM2PH = 0; PWM3PH = 0; //set pin for PWM RB4PPS = 0b0011; //PWM1out RB5PPS = 0b0100; //PWM2out RB6PPS = 0b0101; //PWM3out } void interrupt isr (void) { if (INTCONbits.IOCIF == 1) { INTCONbits.IOCIF = 0; if(SIDE == 0) { PWM1DC = 39601; PWM2DC = 39601; PWM3DC = 39601; } if(SIDE == 1) { PWM1DC = 8000; PWM2DC = 8000; PWM3DC = 8000; } } /*if (PWM1INTFbits.PRIF == 1) { PWM1INTFbits.PRIF = 0; } if (PWM2INTFbits.PRIF == 1) { PWM2INTFbits.PRIF = 0; } if (PWM3INTFbits.PRIF == 1) { PWM3INTFbits.PRIF = 0; }*/ } void main() { initialise(); //MAIN LOOP while(1) { } return; } Edit: for some reason RC4 and RC5 showed a high on the PORTCbits, i set their WPU bits to 0 and on the next run it got stuck in the interrupt routine seeing IOCIF == 1 and side == 0. |
|
|
|
我没有发现实际的问题,但是你的代码中有两个注释包括:PIC16F1578。h & gt;xc.h已经为你做了。你不能清除密码。IOCIF.数据表告诉你这是一个只读标志,你可以通过清除IOCAF、IOCBF、IOCF中的位来清除它。
以上来自于百度翻译 以下为原文 I don't spot the actual problem, but a couple of notes on your code Don't #include xc.h has already done that for you. You can't clear INTCONbits.IOCIF The datasheet tells you that is a read-only flag, you can only clear it by clearing the bits in IOCAF, IOCBF, IOCCF |
|
|
|
感谢我做了这些改变,我刚刚在硬件上测试了一个,而PWM周期似乎只有1.25ms,而不是用8MHz指令频率(FoCC=32兆赫)计算的5ms,即使RC5(旁侧)高的输入,它似乎对任何PWM的占空比没有影响。运行模拟与您提到的笔记和弱拉起的RC4和RC5禁用,程序停留在主回路,无论状态或RC5,并没有影响的价值存储在PWMXDC寄存器。编辑:发现我需要1:4预分频器的PWM时钟,因为它使用FoSC和非指令周期(FoCC/4)在改变“边”编辑时的上升沿或下降沿时仍不确定占空比:我刚刚在PCB上运行了下面的程序:当PCB最初被供电时,当PCB最初被供电时,占空比减小,而PCB则不占空。首先是电源,然后是高电平应用到“侧”,但我已经在初始化,只设置占空比为99%。当侧输入变为低时,占空比在20%处保持不变,而不是被改变为99%,但是在软件仿真中,PWMXDC改变为初始值为99%,该值是否需要加载到另一个位置?还是只有PWMXDC控制占空比?编辑:在模拟器上运行程序时,程序似乎不会被卡住,当定时器中断标志发生时,根据“侧”RC5的状态改变PWMXDC值(不确定它是否仅仅是IOC设置,所以我使用定时器1中断)编辑:我必须解开。E和重新启用PWMS在中断占空比改变。
以上来自于百度翻译 以下为原文 Thanks I have made those changes, I've just tested one on hardware and the PWMs period only seems to be 1.25mS as opposed to 5ms as calculated using 8MHz instruction frequency (fosc= 32MHz) and even with the input on RC5 (SIDE) High it seems to have no impact to the duty cycle of any PWM. Edit: I reran the simulation with the notes you mentioned and the weak pull up for RC4 and RC5 disabled and the program stays in the main loop no matter the state or RC5 and doesn't have an impact on the value stored in the PWMxDC registers. Edit: found out I needed 1:4 prescaler for the PWM clock as it uses fosc and not instruction cycle (fosc/4) still not sure on changing the duty cycle when it sees a rising edge or falling edge on "SIDE" Edit: I've just ran the below program on a PCB: /******************************************************************************/ /* CyclePlateV2.00 */ /* Microcontroller: PIC16F1578 */ /* PCB: Cycle Plate 2018 */ /* Author: btommo Date: 26/06/18 */ /******************************************************************************/ // CONFIG1 #pragma config FOSC = INTOSC // Oscillator Selection Bits (INTOSC oscillator; I/O function on CLKIN pin) #pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled) #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR) #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled) #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled) #pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin) // CONFIG2 #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off) #pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCKED Bit Can Be Cleared & Set Once) #pragma config PLLEN = ON // PLL Enable (4x PLL enabled) #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset) #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.) #pragma config LPBOREN = OFF // Low Power Brown-out Reset enable bit (LPBOR is disabled) #pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include #include /******************************************************************************/ /*CONFIGURATION */ /******************************************************************************/ #define _XTAL_FREQ 32000000 //oscillator frequency for delay_ms() /******************************************************************************/ /* User Global Variable Declaration */ /******************************************************************************/ #define TURN PORTCbits.RC4 //positive trigger #define SIDE PORTCbits.RC5 //positive trigger #define RING PORTCbits.RC0 //pull up required #define CYCLEPWM PORTBbits.RB4 #define CRNRPWM PORTBbits.RB5 #define RINGPWM PORTBbits.RB6 uint8_t db_cnt; //debounce counter //uint8_t RLYs; //uint8_t MOMs; /******************************************************************************/ /* Main Program */ /******************************************************************************/ void initialise() { TRISB = 0; TRISC = 0b00110001; TRISA = 0; ANSELA = 0; ANSELB = 0; ANSELC = 0; //Oscillator control Register OSCCONbits.IRCF = 0b1110; //32MHz freguency instruction osc 8MHz OSCTUNEbits.TUN = 0; // Running at factory calibrated frequency //Option register OPTION_REGbits.nWPUEN = 0; //weak pull-ups enables by WPUX OPTION_REGbits.TMR0CS = 0; //TMR0 clock source is cycle clock OPTION_REGbits.PSA = 0; //Prescaler assigned to TMR0 OPTION_REGbits.PS = 0b111; //prescaler set to 1:256 (set after!!!) //INTCON INTCONbits.GIE = 1; //enable active interrupts INTCONbits.PEIE = 1; //enable active peripheral interrupts //INTCONbits.TMR0IE = 1; //enable TMR0 interrupt //INTCONbits.TMR0IF = 0; //reset TMR0 interrupt flag INTCONbits.IOCIE = 1; // enable interrupt on change IOCAF = 0; IOCBF = 0; IOCCF = 0; IOCCPbits.IOCCP5 = 1; //rising and falling edge detection for SIDE IOCCNbits.IOCCN5 = 1; //Peripheral Interrupt enable PIR1bits.TMR1IF = 0; PIE1bits.TMR1IE = 1; //TMR1 T1CONbits.TMR1CS = 0b00; T1CONbits.T1CKPS = 0b00; T1CONbits.TMR1ON = 1; ADCON0bits.ADON = 0; // Disable ADC WPUCbits.WPUC0 = 1; //pull up on RING WPUCbits.WPUC4 = 0; WPUCbits.WPUC5 = 0; //PWM control PWM1CONbits.EN = 1; PWM1CONbits.POL = 0; PWM1CONbits.MODE = 0b00; PWM2CONbits.EN = 1; PWM2CONbits.POL = 0; PWM2CONbits.MODE = 0b00; PWM3CONbits.EN = 1; PWM3CONbits.POL = 0; PWM3CONbits.MODE = 0b00; //PWM interrupt on period /*PWM1INTEbits.PRIE = 1; PWM2INTEbits.PRIE = 1; PWM3INTEbits.PRIE = 1;*/ PWM1CLKCONbits.CS = 0b00; //FOSC PWM2CLKCONbits.CS = 0b00; PWM3CLKCONbits.CS = 0b00; PWM1CLKCONbits.PS = 0b010; //no prescaller period 8.192ms max PWM2CLKCONbits.PS = 0b010; //1/8x10^6 * 2^16 PWM3CLKCONbits.PS = 0b010; //for 5ms 2^16/8.192 * 5 = 40000 //load 2^16 - 40000 =25536 //Load 40000 into PRx //PWM load buffers at end of each period PWM1LDCONbits.LDA = 1; //load OFx PHx DCx and PRx at end of current period PWM1LDCONbits.LDT = 0; PWM2LDCONbits.LDA = 1; PWM2LDCONbits.LDT = 0; PWM3LDCONbits.LDA = 1; PWM3LDCONbits.LDT = 0; PWM1OFCONbits.OFM = 0b00; PWM2OFCONbits.OFM = 0b00; PWM3OFCONbits.OFM = 0b00; //set Period of PWM 40000 = 5ms PWM1PR = 40000; PWM2PR = 40000; PWM3PR = 40000; //set initial duty cycle to 99% //Duty cycle = (PWMxDC - PWMxPH)/PWMxPR+1 //PWMxDC = 0.99*(PWMxPR+1)+PWMxPH //=0.99*40001+0 PWM1DC = 39601; PWM2DC = 39601; PWM3DC = 39601; //for 20% duty cycle //PWMxDC = 0.2*40001+0 //=8000 //setting PWMxPH 0 PWM1PH = 0; PWM2PH = 0; PWM3PH = 0; //set pin for PWM RB4PPS = 0b0011; //PWM1out RB5PPS = 0b0100; //PWM2out RB6PPS = 0b0101; //PWM3out } void interrupt isr (void) { if (INTCONbits.IOCIF == 1) { IOCAF = 0; IOCBF = 0; IOCCF = 0; if(SIDE == 0) { PWM1DC = 39601; PWM2DC = 39601; PWM3DC = 39601; } if(SIDE == 1) { PWM1DC = 8000; PWM2DC = 8000; PWM3DC = 8000; } } if (PIR1bits.TMR1IF == 1) { PIR1bits.TMR1IF = 0; if(SIDE == 0) { PWM1DC = 39601; PWM2DC = 39601; PWM3DC = 39601; } if(SIDE == 1) { PWM1DC = 8000; PWM2DC = 8000; PWM3DC = 8000; } } /*if (PWM1INTFbits.PRIF == 1) { PWM1INTFbits.PRIF = 0; } if (PWM2INTFbits.PRIF == 1) { PWM2INTFbits.PRIF = 0; } if (PWM3INTFbits.PRIF == 1) { PWM3INTFbits.PRIF = 0; }*/ } void main() { initialise(); //MAIN LOOP while(1) { } return; } the Duty cycle is reduced when it sees a high on the "SIDE" input when the PCB is initially powered but not if the PCB is powered first and then a high is applied to "SIDE" but I've looked in initialisation which only sets the duty cycle to 99%. When the SIDE input is changed to a low the duty cycle stays the same at 20% as opposed to being changed back to 99% but in software simulation the PWMxDC changes back to the initial value for 99%, does the value need to be loaded into another location? or is it only PWMxDC that governs the duty cycle? edit: Running through the program on the simulator the program doesn't seem to get stuck and changes PWMxDC values based on the state of "SIDE" RC5 when the timer interrupt flag occurs (wasn't sure if it was just the IOC settings so I'm using timer 1 interrupt) edit: figured out that I had to disable and re-enable the PWMs in the interrupt for the duty cycle to change. |
|
|
|
只有小组成员才能发言,加入小组>>
5242 浏览 9 评论
2031 浏览 8 评论
1955 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3207 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2256 浏览 5 评论
778浏览 1评论
669浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
596浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
678浏览 0评论
577浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 22:12 , Processed in 1.431272 second(s), Total 82, Slave 66 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号