PWM problem pic16f1825

Status
Not open for further replies.

alexandru321

Newbie level 2
Joined
Nov 29, 2017
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
38
hello guys i have some problem with pwm ,i want to control a servo motor with adc ,i cant make 20 ms period....i tried but max is 16 ms...i don t know wath i do wrog ,pls help.

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000
#define TMR2PRESCALE 4
#include"pic16f1825.h"
#include <xc.h>
 
long PWM_freq = 5000;
long PWM_period;
PWM_Initialize()
{
      
    PR2 =(_XTAL_FREQ/(PWM_freq*4*TMR2PRESCALE)) - 1; //Setting the PR2 formulae using Datasheet // Makes the PWM work in 5KHZ
    CCP4M3 = 1; 
    CCP4M2 = 1;
    T2CKPS0= 1;
    T2CKPS1= 1;
    TMR2ON= 1; //Configure the Timer module
    TRISC1=0; 
 
}
 
PWM_Duty(unsigned int duty)
{
      if(duty<1023)
  {
 
    duty = ((float)duty/1023)*(_XTAL_FREQ/(PWM_freq*TMR2PRESCALE)); 
    DC4B1 = duty & 1;
    DC4B0 = duty & 2; 
    CCPR1L = duty>>2;
    
  }
}
 
void ADC_Initialize()
{
  ADCON0 = 0b0000001; //ADC ON and Fosc/16 is selected
  ADCON1 = 0b11010000;
}
unsigned int ADC_Read(unsigned char channel)
{
  ADCON0 &= 0x11000101; //Clearing the Channel Selection Bits
  ADCON0 |= channel<<3; //Setting the required Bits
  __delay_ms(2); 
  GO_nDONE = 1; //Initializes A/D Conversion
  while(GO_nDONE); //Wait for A/D Conversion to complete
  return ((ADRESH<<8)+ADRESL); //Returns Result
}
 
void main()
{
   int adc_value;
   int pot;
  TRISC = 0x00; //PORTC as output
  TRISA = 0xFF; //PORTA as input
  //TRISD = 0x00;
  ADC_Initialize(); //Initializes ADC Module
  PWM_Initialize();  //This sets the PWM frequency of PWM1
 
  do
  {
    adc_value =0.01825*ADC_Read(0)+7;
     pot =170 -adc_value ;
    __delay_ms(5);  //Reading Analog Channel 0 
    PWM_Duty(adc_value);
      __delay_ms(50); 
      
  }while(1); //Infinite Loop
  
}



 
Last edited by a moderator:

Hi,

you say 20ms, this is 50Hz,
but in your code I see 5000Hz, this is 200us

What do you want now?
 

hi,i set 50 hz but the period is 10 ms ,i dont know were is the problem,may be from prescaler or timer?
 

Hi,

Please confirm that the chip runs with 20MHz and there is no clock divider involved.

Klaus
 

hello,


at FOSC 20MHz
you can't reach as low value of 20mS


at 4MHz , i can get a maximum of 16mS, using MikroC PWM library, only if i modify "Pre" and "Post" divider of PR2 register
with a PIC12F1840..

you better have to use 2 timers , one for 20mS cycle , other one for the commande from 1 to 2 mS.
 

You have set the oscillator to HS but there is no crystal in your schematic. Therefore it is probably some artifact of the simulator that is providing you with a primary clock source and that could be any frequency.
You need to actively set the oscillator correctly. The use of the <xc.h> include makes me think you are using the XC8 compiler - just in case you misunderstand this (many do when starting out with the XC-series compilers) the _XTAL_FREQ define is not used by anything except the compiler-supplied delay macros and whatever you put into your code.
Susan
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…