Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PWM (Duty Cyc;e) PIC16F877A to MD22

Status
Not open for further replies.

isolde

Newbie level 3
Joined
Apr 26, 2012
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,302
Hi I was just wondering is ther anyway in C code I can control the duty cycle of this code:

If ((input(PIN_A2) == 0)&& (input(PIN_A3) == 0))
{
output_low(PIN_C0);
delay_us(2000);
output_high(PIN_C0);
delay_us(1500);
output_low(PIN_C0);
}
else if ((input(PIN_A2) == 0) && (input(PIN_A3) == 1))
{
output_low(PIN_C0);
delay_us(2000);
output_high(PIN_C0);
delay_us(1000);
output_low(PIN_C0);
}

This code acts as a PWM signal and sends the signals I need out. If anyone code give me a idea how to drop the duty cyle by 50% it would be appreciated the signals travels from 0-5V.
 

From your code, If pins A2 and A3 are 00, then C0 will be low for 2 ms and high for 1.5 ms. This gives a duty cycle of 1.5/(2+1.5) = 42.8%

If A2, A3 are 01, then you have C0 low for 2 ms and high for 1 ms giving you a duty cycle of 1/3 = 33.33%

If you want 50% duty cycle, just change the delay time to be equal for low and high.
 

Thanks this helps me alot :)
 

isolde,


Due PIC16F877 already have a built-in PWM hardware module, why don´t you use this ?
There are many advantages comparing with the above code.


+++
 

Yo seem to be using CSS. why not enter
setup_ccp1(CCP_PWM)
setup_timer_2(T2_DIV_1, PERIOD-1, 15);// prescaler by 1 period of 256 -- ccp freqency = XTAL / (4*PEIOID)
setu_pwm1_duty((long)x); // x = 0 - ((256*4)-1)

or you can say

#define PERIOD 256
#define SetVoltage(x) setup_pwm1_duty((long)(x*(PERIOD*4-1))/ 5)

void main(void){
set_tris_c(0);
setup_ccp1(CCP_PWM)
setup_timer_2(T2_DIV_1, PERIOD-1, 15);// prescaler by 1 period of 256 -- PWM freqency = XTAL / (4*PEIOID)
SetVoltage(2.5); // set for 2.5V
 

Thanks for all the help worked perfect.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top