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.

[PIC] problem with pwm generation using pic16f876

Status
Not open for further replies.

Teja.p

Member level 2
Joined
May 29, 2016
Messages
44
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,727

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void main(void)
{
PORTB=0x00;
TRISB=0x00;
TRISC=0x00;
 PORTC=0x00;
 
 
while(1)
{
PR2=500;
CCPR1L=0;
CCP1CON=0x30;
INTCON=0xc0;
TRISC=0x00;
PORTC=0X00;
TMR2IF_bit = 0;          // clear TMR2IF
  TMR2IE_bit = 1;
T2CON=0x03;
T2CON=0x04;
TMR2=0x00;
 CCP1CON=0x3c;
 }
 
  }
 
void interrupt()
 {
  if (TMR2IF_bit)
   {
    TMR2IF_bit = 0;        // clear TMR2IF
    }
}

 
Last edited by a moderator:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
while(1)
{
PR2=500;
CCPR1L=0;
CCP1CON=0x30;
INTCON=0xc0;
TRISC=0x00;
PORTC=0X00;
TMR2IF_bit = 0;          // clear TMR2IF
  TMR2IE_bit = 1;
T2CON=0x03;
T2CON=0x04;
TMR2=0x00;
 CCP1CON=0x3c;
 }
 
  }


1st code in while loop is not right Nothing should be in there.
2nd PR2 is 8 bit register its value should be 0-255
3rd CCPR1L should have value 0-255 for PWM duty cycle which may be define in while loop if you want to update duty cycle
4th you are defining same register values multiple times


Which programming tool you are using?
 
Dear sir can u please correct the code and post that one... please sir
with 10khz frequency....
 

You did not mention about programming tool
I Think it MikroC pro for PIC

Code:
void main()
{
TRISC=0x00;  // PORTC is defined as ouput 
PORTC=0x00; // Clear PORTC
PWM1_Init(10000);  // Initialize PWM1 with 10KHz
PWM1_Set_Duty(127);  // 127 is 50% Duty Cycle 255 is 100% and 0 is )%
PWM1_Start();    // start PWM1
while(1);
}


Quite good Explanation over here
https://www.youtube.com/watch?v=-7D_qIXYTog&ab_channel=Bandar
 
thanks for giving the reply sir
i am using mikroc
i need the program without using library functions, please sir
 

The library functions do almost nothing except load those values into the PWM registers. If you look at the data sheet it explains how to configure the PWM registers (its very easy).

Brian.
 

LETS say You are Using 4MHz internal Crystal

Code:
void main()
{
TRISC=0x00;  // PORTC is defined as ouput 
PORTC=0x00; // Clear PORTC
CCP1CON = 0x0C;   // PWM Mode
T2CON = 0x04;   // Timer2 on
PR2 = 100;         // 10 KHz Frequency
CCPR1L = 125;    // 50% DUTY Cycle as mentioned above

while(1);
}


Check if it works?
 
thanks u sir
i have question
why u not use timer over flow interrupt.
 

i have question
why u not use timer over flow interrupt.

Once Timer is configured as PWM, the CCP output generates an endless astable pulse with a determined duty-cycle. The only need of using a timer overflow is if you wanted to generate specific waverforms, which was not what you asked:

with 10khz frequency....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top