PIC18F4520 + PWM

PIC18F4520 + PWM
Çeşme:
https://www.exploreembedded.com/wiki/Generating_PWM_with_PIC18F4520
https://www.exploreembedded.com/wiki/PIC16F877A_PWM


PIC C Library Web Site:
https://sourceforge.net/projects/picclib/
  1. PIC18F4520
// PIC18F4520 Configuration Bit Settings // 'C' source line config statements // CONFIG1H #pragma config OSC = XT // Oscillator Selection bits (XT oscillator) #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled) #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled) // CONFIG2L #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled)) #pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting) // CONFIG2H #pragma config WDT = ON // Watchdog Timer Enable bit (WDT enabled) #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768) // CONFIG3H #pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1) #pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset) #pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation) #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled) // CONFIG4L #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset) #pragma config LVP = ON // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled) #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode)) // CONFIG5L #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) not code-protected) #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) not code-protected) #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) not code-protected) #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) not code-protected) // CONFIG5H #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected) #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected) // CONFIG6L #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) not write-protected) #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) not write-protected) #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) not write-protected) #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) not write-protected) // CONFIG6H #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected) #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected) #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected) // CONFIG7L #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks) #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks) #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks) #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks) // CONFIG7H #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> #define _XTAL_FREQ 4000000 /*************************************************************************************************** ExploreEmbedded **************************************************************************************************** * File: timer.h * Version: 16.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: File contains the function declaration and the preprocessors for configuring and using the 8052 PWM. As 8051/52 doesn't have inbuilt PWM, timer2 is used to generate the max of 2PWM ouputs. These can be enabled/disabled using the constants PWM_0_ENABLE/PWM_1_ENABLE, setting them to 1/0 will enable/disable the respective PWMs The PWM output pin can be set using PWM0_PIN/PWM1_PIN. By default the dutycyle is set to 50%. The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the library works on any of development boards for respective controllers. However, ExploreEmbedded disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly. Files may be subject to change without prior notice. The revision history contains the information related to updates. GNU GENERAL PUBLIC LICENSE: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Errors and omissions should be reported to codelibraries@exploreembedded.com **************************************************************************************************/ #ifndef _PWM_H_ #define _PWM_H_ #include "inttypes.h" /************************************************************************************************* Function Prototypes *************************************************************************************************/ void PWM_Init(uint8_t channel); void PWM_SetDutyCycle(uint8_t channel,uint8_t dutyCycle); void PWM_Start(uint8_t channel); void PWM_Stop(uint8_t channel); /*************************************************************************************************/ #endif //------------------------------------------------ int main() { int dutyCycle; PWM_Init(0); // Initialize the PWM module and the Cycle time(Ton+Toff) is set to 100 PWM_Start(0); // Start PWM signal generation on Selected pin PWM_Init(1); PWM_Start(1); while(1) { for(dutyCycle=0;dutyCycle<100;dutyCycle++) // Increase the Brightness of the LED { PWM_SetDutyCycle(0,dutyCycle); PWM_SetDutyCycle(1,dutyCycle); __delay_ms(5); } for(dutyCycle=100;dutyCycle>0;dutyCycle--) // Decrease the Brightness of the LED { PWM_SetDutyCycle(0,dutyCycle); PWM_SetDutyCycle(1,dutyCycle); __delay_ms(5); } } } //------------------------------------------------ /*************************************************************************************************** ExploreEmbedded **************************************************************************************************** * File: pwm.c * Version: 16.0 * Author: ExploreEmbedded * Website: http://www.exploreembedded.com/wiki * Description: Contains the library function for PWM generation for Pic16f877a controllers The libraries have been tested on ExploreEmbedded development boards. We strongly believe that the library works on any of development boards for respective controllers. However, ExploreEmbedded disclaims any kind of hardware failure resulting out of usage of libraries, directly or indirectly. Files may be subject to change without prior notice. The revision history contains the information related to updates. GNU GENERAL PUBLIC LICENSE: This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Errors and omissions should be reported to codelibraries@exploreembedded.com **************************************************************************************************/ //#include "pwm.h" //#include <xc.h> #include "inttypes.h" #pragma warning push #pragma warning disable 752 // Suppress warnings related to size of variables(conversion to shorter data type). /*************************************************************************************************** void PWM_Init(void) **************************************************************************************************** * I/P Arguments : none * Return value : none * description :This function initializes the Timer2 module for generating the PWM using timers. ***************************************************************************************************/ void PWM_Init(uint8_t channel) { switch (channel) { case 0 : CCP1CON = 0x0F; // Select the PWM mode. PR2 = 100; // Set the Cycle time to 100 for varying the duty cycle from 0-100 CCPR1L = 50; // By default set the dutycycle to 50 //util_BitClear(TRISC,2); // Make the PWM pin(PC.2) Output TRISC2 = 0; break; case 1 : CCP2CON = 0x0F; // Select the PWM mode. PR2 = 100; // Set the Cycle time to 100 for varying the duty cycle from 0-100 CCPR2L = 50; // By default set the dutycycle to 50 //util_BitClear(TRISC,1); // Make the PWM pin(PC.1) Output TRISC1 = 0; break; } } /*************************************************************************************************** void PWM_SetDutyCycle( uint8_t channel, uint8_t dutyCycle ) **************************************************************************************************** * I/P Arguments: uint8_t: channel number at which PWM needs to be generated.(0/1). uint8_t : required dutyCycle at the pwm pin(0-100). * Return value : none * description :This function is used the set the duty cycle for the required channel. Ensure the PWM_Init() function is called before calling this function. This function only sets the duty cycle, the actual PWM generation satarts after calling PWM_Start(). Note: By default the dutycyle is set to 50%. ***************************************************************************************************/ void PWM_SetDutyCycle(uint8_t channel,uint8_t dutyCycle) { if(dutyCycle>100) dutyCycle = 100; switch (channel) { case 0 : CCPR1L = dutyCycle; break; case 1 : CCPR2L = dutyCycle; break; } } /*************************************************************************************************** void PWM_Start(uint8_t channel) **************************************************************************************************** * I/P Arguments: none. * Return value : none * description :This function is used to start the pwm generation for the required channel. ***************************************************************************************************/ void PWM_Start(uint8_t channel) { TMR2ON = 1; //Start the Timer for PWM generation } /*************************************************************************************************** void PWM_Start(uint8_t channel) **************************************************************************************************** * I/P Arguments: none. * Return value : none * description :This function is used to stop the pwm generation for the required channel. ***************************************************************************************************/ void PWM_Stop(uint8_t channel) { switch (channel) { case 0 : CCP1CON = 0x00; //Disable the CCP Module from generating PWM break; case 1 : CCP2CON = 0x00; //Disable the CCP Module from generating PWM break; } } #pragma warning pop