MIKROC PWM WITH LED, STM32F103C8T6, MIKROC PRO FOR ARM
As the title recommend this investigation tests Microchips PIC18F2550 Microcontroller and its PWM Peripheral. The Code introduced here turns on drove associated at pin 13 (RC2/CCP1) of the PIC18F4550 with 0% brilliance and afterward steadily builds the splendor up to 100% with the change in the obligation cycle by calling PWM1_Set_Duty (obligation) work by expanding obligation variable after each 10-mile sec.
Schematics:
PWM Library Functions Description:
There are the following 4 capacities gave by MikroC Compiler which lets you control the PIC microcontroller's PWM Channel.
1. PWM1_Init (const long freq)
This standard should be called before utilizing different capacities from PWM Library.
Takes "freq" boundary as recurrence in hertz, and introduces the PIC microcontroller's PWM channel.
All PWM modules use Timer2 for its activity, so you can not set various frequencies for various PWM modules.
2. void PWM1_Set_Duty(unsigned short duty_ratio)
Sets PWM obligation proportion. Boundary obligation takes esteems from 0 to 255, where 0 is 0%, 127 is half, and 255 is 100% obligation proportion. Other explicit qualities for obligation proportion can be determined as (Percent*255)/100.
3. void PWM1_Start(void)
The capacity returns nothing and takes no information boundary. This capacity begins with PWM.
4. void PWM1_Stop(void)
The capacity returns nothing and takes no information boundary. This capacity stops PWM.
The source code introduced here sudden spikes in demand for the inner 8.000Mhz underlying oscillator.
Design:
Source Code:
void main()
{
unsigned char duty = 0;
OSCCON = 0x70; // configures oscillator divider for 8MHz int. oscillator
PORTC = 0; // sets port c to all 0
TRISC = 0; //configures port c as output port
PWM1_Init (5000); // sets pwm frequency to 5000Hz
PWM1_Start(); // starts pwm1 peripheral
while (1)
{
PWM1_Set_Duty(duty); // sets duty cycle
duty++; // increments duty cycle by 1
Delay_ms(10); // adds 10 msec delay
}
}
PWM WITH LED, STM32F103C8T6, MIKROC PRO FOR ARM
unsigned int pwm_period1;
void main() {
GPIO_Digital_Output(&GPIOA_BASE,_GPIO_PINMASK_8 | _GPIO_PINMASK_9 | _GPIO_PINMASK_10 | _GPIO_PINMASK_11);
GPIOA_ODR = 0b0000111100000000;
Vdelay_ms(50);
GPIOA_ODR = 0b0000000000000000;
Vdelay_ms(50);
pwm_period1=PWM_TIM1_Init(200);//200Hz, maximun frecuency of STM32
while(1){
PWM_TIM1_Set_Duty(pwm_period1*0, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.10, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.20, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.30, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.40, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.50, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.60, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.70, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.80, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.90, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.90, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.80, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.70, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.60, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.50, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.40, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.30, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.20, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
PWM_TIM1_Set_Duty(pwm_period1*0.10, _PWM_NON_INVERTED, _PWM_CHANNEL1);
PWM_TIM1_Start(_PWM_CHANNEL1, &_GPIO_MODULE_TIM1_CH1_PA8);
delay_ms(50);
}
}
How to Program the CCP1 Module in PIC16F877A (10-Bit PWM Mode)
The accompanying code exhibit how to utilize the CCP1 module in PWM mode with a 10-cycle of goal. On statement PWM obligation cycle is set to zero, when the "Obligation INCREASE" button is squeezed beat width begins expanding and by squeezing the "Obligation DECREASE" button, beat width begins diminishing. The code is written in "mikroC PRO for PIC v.6.6.3" IDE and reenactment is finished with Proteus 8.0 SP0.
Code in mikroC
// define crystal frequency
#define _XTAL_FREQ 20000000
// timer-2 prescalar
#define TMR2_PRESCALE 4
sbit DUTY_INC_Dir at TRISB.B0;
sbit DUTY_DEC_Dir at TRISB.B1;
sbit DUTY_INC at PORTB.B0;
sbit DUTY_DEC at PORTB.B1;
long pwm_freq = 5000;
PWM1_Initialize(void);
PWM1_Duty(unsigned int duty);
void main(void)
{
unsigned int count = 0;
// make as input
DUTY_INC_Dir = 1;
DUTY_DEC_Dir = 1;
// init pwm1
PWM1_Initialize();
PWM1_Duty(0);
while(1)
{
if(DUTY_INC == 1)
{
PWM1_Duty(count);
if(count>=1023)
count = 0;
else
count++;
Delay_ms(20);
}
if(DUTY_DEC == 1)
{
PWM1_Duty(count);
if(count<= 0)
count = 1023;
else
count--;
Delay_ms(20);
}
}
}
PWM1_Initialize(void)
{
// set pwm1 frequency
PR2 = (_XTAL_FREQ/(pwm_freq*4*TMR2_PRESCALE)) - 1;
// Configure the CCP1 module in pwm mode
CCP1CON.CCP1M3 = 1;
CCP1CON.CCP1M2 = 1;
CCP1CON.CCP1M1 = 0;
CCP1CON.CCP1M0 = 0;
// set timer-2 prescalar is 4
T2CON.T2CKPS1 = 0;
T2CON.T2CKPS0 = 1;
// turn timer-2 ON
T2CON.TMR2ON = 1;
// make RC2/CCP1 as output
TRISC.B2 = 0;
}
PWM1_Duty(unsigned int duty)
{
if(duty<1023)
{
duty = ((float)duty/1023)*(_XTAL_FREQ/(pwm_freq*TMR2_PRESCALE));
// These bits are the two LSbs of the PWM duty cycle
// store bit-0
CCP1CON.CCP1X = duty & 1;
// store bit-1
CCP1CON.CCP1Y = duty & 2;
// store bit-9 to bit-2, by shifting two bits right
CCPR1L = duty>>2;
}
}
0 Comments