Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PWM from PIC16F877a....

Status
Not open for further replies.

koolguy

Active Member
Hi,

I am want PWM for speed controlling of DC motor for this i am using PIC16F877a...
here is the code written according to the hind in Data shet...

Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

main(){
TRISC=0x00;// out put for PWM
CCP1CON=0b11111000; //pwm mode
PR2=0xff;//for 19.9khz freq
CCPR1L=0x0f;/duty cycle random
T2CON=0b01100000;//pescallaer to 1:4 don't know why?
CCP1IE=0;//disable interput
 
Again... There is a PWM motor controller code in my tutorials...

The reason for the Timer prescale is well documented in the datasheet..

The PWM period is specified by writing to the PR2
register. The PWM period can be calculated using the
following formula:

PWM Period = [(PR2) + 1] • 4 • TOSC• (TMR2 Prescale Value)

PWM frequency is defined as 1/[PWM period].
When TMR2 is equal to PR2, the following three events
occur on the next increment cycle:

You can see its used to calculate the period....
 
I want to ask few thing what is the application of capture/compare in uC??
and why are you using | here?
ch |= 0x0C; // Dont need capture
void speed(char motor, int speed)
{
if(speed > 128)
reverse(motor); // over 128 reverse motor
else
forward(motor);
speed &= 0x7F;

why >128 is used meaning of this?
 
The capture compare registers are used for PWM... Setting the bits, 2 and 3, of the CCPxCON register turn PWM mode on..

In the example we turn the motor clockwise or anticlockwise... There are 256 resolutions available... from 0 to 127 forwards... 128 to 255 backwards..
 
I can't help but think about 98% of Ritesh's questions can be answered by either READING THE DATASHEET, searching on GOOGLE, or READING EXISTING TUTORIALS. Just do a little reading Ritesh, it's not that hard :p;)
 
DS Thanks for your vote of confidence.... However, This is an open forum... Many people cannot read the datasheets ( I don't know if they are translated in to other languages )..

When I read "some" datasheets, I'm left a tad confused.. And I can read them!!!

There are two choices you are faced with when reading a post, " Do I help " or " Do I not help ".... The choice is up to you.... The only thing I ask my helpee's is they try...

I choose to help Ritesh.... If you look back on all his posts, he finds it difficult to grasp the basic concept of programming... He just needs a little nudge..

I try to steer him towards datasheets... Identifying excerpts from the datasheets so he can see where things are mentioned..

I always go back to when I was learning.... There was no Tinterweb and I struggled.... If I can help... I will!!!


Once again... I know some people think that giving newbie's hand outs is not teaching, but not helping is also not teaching...
 
Once again... I know some people think that giving newbie's hand outs is not teaching, but not helping is also not teaching...

If the "handout" demonstrates one particular point and explains it in a practical way, then that is extremely good teaching, but if you hand out a complete solution as "copy paste", then that does not teach anything.
 
Last edited:
DS Thanks for your vote of confidence.... However, This is an open forum... Many people cannot read the datasheets ( I don't know if they are translated in to other languages )..

When I read "some" datasheets, I'm left a tad confused.. And I can read them!!!

There are two choices you are faced with when reading a post, " Do I help " or " Do I not help ".... The choice is up to you.... The only thing I ask my helpee's is they try...

I choose to help Ritesh.... If you look back on all his posts, he finds it difficult to grasp the basic concept of programming... He just needs a little nudge..

I try to steer him towards datasheets... Identifying excerpts from the datasheets so he can see where things are mentioned..

I always go back to when I was learning.... There was no Tinterweb and I struggled.... If I can help... I will!!!


Once again... I know some people think that giving newbie's hand outs is not teaching, but not helping is also not teaching...

Understood Ian. Best of luck to both of you!

If the "handout" demonstrates one particular point and explains it in a practical way, then that is extremely good teaching, but if you hand out a complete solution as "copy paste", then that does not teach anything.

First of all misterT, Ian wrote that, not me ;)

Second, you're correct. If I "hand out" answers, I generally do my best to explain what I did, so that it can become a teaching experience. I try to work through it, take it piece by piece, and show each part as its done. I feel that's often the best way to go.

Matt
 
First of all misterT, Ian wrote that, not me ;)

I know.. First I wrote something and then I edited what I wrote and weird things happened.. I did not bother to fix it. Sorry about that. In my original post I gave Ian credit for helping people. He has great patience etc. I get frustrated when people seem to be lazy.. when the first thing they do is to come here and ask questions like "please help.. this is urgent.. please provide code and ciruit". I don't think Ritesh is lazy.. he is just too eager to do fancy stuff that he skips the basics. Slow down and study!
 
I know.. First I wrote something and then I edited what I wrote and weird things happened.. I did not bother to fix it. Sorry about that. In my original post I gave Ian credit for helping people. He has great patience etc. I get frustrated when people seem to be lazy.. when the first thing they do is to come here and ask questions like "please help.. this is urgent.. please provide code and ciruit". I don't think Ritesh is lazy.. he is just too eager to do fancy stuff that he skips the basics. Slow down and study!

Ah, ok. No worries then :)
 
I am designing PCB for Motor driver using L298...so, i want to know for controlling speed of single motor in both direction wireless ...
the pin Enable and input 1 and 2 should be connected where??
I mean to say enable to Vcc or PWM ??? or uC PWM output to Input 1 and 2??
 
Hi again,

I was testing code in oshon but port c output waveform remain at zero, why??
Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000

main(){

TRISC=0x00;         // out put for PWM
CCP1CON=12;         //pwm mode
PR2=55;				//for freq
while(1){
CCPR1L=0x02;			//duty cycle random
T2CON=0b01100000;		//pescallaer to 1:1
CCP1IE=0;		//disable interput

}
}
 
Hi Ritesh... I'm struggling to keep up with ETO at the moment... Very busy at work..

I'll try and stick this on the sim later and see if I can get it going.... It looks ok, other than the three statements in the while loop don't need to be there.
 
Hi again,

This code is working fine for me now i want to add the ADC result to CCPR1L register!!
Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


void main()
        {
        unsigned char   dc ;

        TRISC = 0 ;                     // set PORTC as output
        PORTC = 0 ;                     // clear PORTC

        /*
         * configure CCP module as 4000 Hz PWM output
         */
        PR2 = 0b11111001 ;
        T2CON = 0b00000100 ;
        CCP1CON = 0b00111100;

     while(1)                       // forever
                {
                /*
                 * PWM resolution is 10 bits
                 * don't use last 2 less significant bits CCPxCON,
                 * so only CCPRxL have to be touched to change duty cycle
                 */
                for(dc = 0 ; dc < 255 ; dc++)
                        {
                        CCPR1L = dc ;
                       
                        __delay_ms(100) ;
                        }
                
                }
        }


but this is working do you find any error actually i want to control speed of PMDC motr at 20Khz freq...
Code:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000


void main()
        {
	TRISB=0X00;
TRISA = 0xff ;
ADCON1=0b00000000;
ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
	ADIF=0;
	ADIE=1;
	PEIE=1;
 TRISC = 0 ;
        PR2 = 0b11111001 ;
   T2CON = 0b00000100 ;   
 CCP1CON = 0b00111100;                  
        PORTC = 0 ;  
while(1){



     
                          
    

 __delay_us(10);
GO_DONE=1;
	__delay_us(10);                    
   CCPR1L = ADRESH ;
                       
                      
                
                
      }}
 
I tested this code

C:
#include <htc.h>
__CONFIG(LVP_OFF & BOREN_OFF & PWRTE_ON & WDTE_OFF & FOSC_HS);
#define _XTAL_FREQ 20000000
 
 
void main()
	{
	TRISB=0X00;
	TRISA = 0xff ;
	ADCON1=0b00000000;
	ADCON0=0b10000001;//000 = channel 0, (RA0/AN0)
	ADIF=0;
	ADIE=1;
	PEIE=1;
 	TRISC = 0 ;
	PR2 = 0b11111001 ;
	T2CON = 0b00000100 ;   
	CCP1CON = 0b00111100;                  
	PORTC = 0 ;  
	while(1)
		{
 		__delay_ms(10);
		GO_DONE=1;
		__delay_ms(10);                    
   		while(GO_DONE);
   		CCPR1L = ADRESH ;
		}
	}

Period = 22.7 khz

I had to increase the delays so the ADC was more stable...
 
Period = 22.7 khz

I was using the formula as in data sheet but i am confused with unit Tosc is 20x10^6 MHz and etc so, result is coming very large!!

OK, i am working on wireless speed control of DC motor so, i need to send this data to other uC via TX and Rx so, UART will be fine for this ??

will this will work or not??
1. **broken link removed**
2.**broken link removed**
3. **broken link removed**
 
TOSC... Not FOSC.... If you are using a 20Mhz xtal the speed will be 20Mhz / 4 = 5Mhz.. The TOSC = 1/5Mhz = 0.2μS

Period = 1 / (TOSC * 4 * ( PR2 + 1) * (T2CON Pre scaler) )

Period = 1/ ( 0.2μS * 4 * 56 * 1 ) = 22.32Khz

( I must have made a mistake on the one above )

Either 2 or 3... I use Xbee and get on quite well.. But 3 looks quite good...

I use a lower cost Xbee replacement **broken link removed**
 
HI again,

I had buy that rx and tx low cost module!!
So, can't we use this for comm between to pic uC to send the ADC result to other uC via tx pair???
 
Please note that this device will not support direct UART communication when connected to PC or microcontrollers as there is a lot of noise always available on these frequencies. If you are looking for Serial Communication see RF 2.4Ghz Serial Link instead of this product. We will not be providing any support for serial communications after sales. For remote control applications please use Encoder and Decoder ICs.

To use it for serial communication you will need a low end serial encoder... Manchester coding is favorite... Read this..

**broken link removed**

The other two have direct serial interfaces ( virtual wire )
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top