View previous topic :: View next topic |
Author |
Message |
bassn1
Joined: 03 Aug 2011 Posts: 6
|
Incrementing Frequency by 50 Hz after X pulses |
Posted: Wed Aug 03, 2011 10:28 am |
|
|
CCS version 4.029
Hello, I was trying to implement the following code such that
I get a rough frequency ramp from 250-1000Hz at 50% DC.
Precision doesn't matter I'm just needing this to get do a rough ramp profile with a stepper.
Code: |
x=10000
while x<count
count++
if count is <50 pwm freq=approx 250
if count is <100 pwm freq=approx 300
if count is <750 pwm freq=approx 1000
if count=10000
pwm duty =0 ;shut off pwm module
|
I found this great lil nugget by PCM programmer but I am having a hard time adapting it into a function that i can call when I need it.
Just wanting to make a move, so I'm thinking a function called move?
I am an asm guy and I am amazed at what this compiler can do I am just beating my head against the wall trying to get this implemented I know its simple but could someone please enlighten me?
Code: |
#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#define NUM_PULSES 10000
//======================================
void main()
{
int16 count;
// Setup CCP1 for PWM at 250 Hz and 50% duty cycle.
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_16, 249, 1);
set_pwm1_duty(128);
count = 0;
// Do the specified number of pulses on CCP1 and then stop.
while(1)
{
if(interrupt_active(INT_TIMER2))
{
clear_interrupt(INT_TIMER2);
count++;
if(count == NUM_PULSES)
{
set_pwm1_duty(0); // Set PWM output = constant 0
break;
}
}
}
while(1);
}
|
I tried a case statement, for loop, and while but its not adding up because it either falls out or stays put and won't leave.
Thank you in advance. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 03, 2011 1:40 pm |
|
|
You should you try to do it and post your modified code. Maybe
put some "if, else if, else" statements in sequence with the existing
"count==" statement, and in each one, do a test for the count.
If it reaches your minimum count level for each step, then call
setup_timer_2() and set_pwm_duty() to select the new frequency
and to maintain a 50% duty cycle. Try it, and if it doesn't work,
then post the code and post the failure mode. Then I'll look at it. |
|
|
bassn1
Joined: 03 Aug 2011 Posts: 6
|
Thanks |
Posted: Wed Aug 03, 2011 2:59 pm |
|
|
Thanks for the reply PCM, I am not trying to get something for nothing I was just too embarrassed of my code!!!! I found a another potential solution.
Since I'm only working from 250-1000Hz,
If i use setup_timer_2(T2_DIV_BY_16, 249, 1);
to initialize and since Duty Cycle is a function of Prescale only and CCPR1L and CCP1con are constant.
Couldn't I just decrease period from 249-60 for every count? I should say for 189 counts anyway, and at that point I continue on until 10000 pulses are hit.
It looks good in excel from 249 to 60 it will increase from 250-1000
I shouldn't have to set duty cycle but once I believe.
PWM Period = [(PR2) + 1] • 4 • TOSC •
(TMR2 Prescale Value)
PWM Duty Cycle =(CCPR1L:CCP1CON<5:4>) •
TOSC • (TMR2 Prescale Value)
excel data
Code: |
16.00 60.00 1.00 1.000000000E-06 1.0E+3
16.00 61.00 1.00 1.000000000E-06 1.0E+3
16.00 62.00 1.00 1.000000000E-06 1.0E+3
16.00 63.00 1.00 1.000000000E-06 992.1E+0
16.00 64.00 1.00 1.000000000E-06 976.6E+0
16.00 65.00 1.00 1.000000000E-06 961.5E+0
16.00 66.00 1.00 1.000000000E-06 947.0E+0
16.00 67.00 1.00 1.000000000E-06 932.8E+0
16.00 68.00 1.00 1.000000000E-06 919.1E+0
16.00 69.00 1.00 1.000000000E-06 905.8E+0
16.00 70.00 1.00 1.000000000E-06 892.9E+0
16.00 71.00 1.00 1.000000000E-06 880.3E+0
16.00 72.00 1.00 1.000000000E-06 868.1E+0
16.00 73.00 1.00 1.000000000E-06 856.2E+0
16.00 74.00 1.00 1.000000000E-06 844.6E+0
16.00 75.00 1.00 1.000000000E-06 833.3E+0
16.00 76.00 1.00 1.000000000E-06 822.4E+0
16.00 77.00 1.00 1.000000000E-06 811.7E+0
16.00 78.00 1.00 1.000000000E-06 801.3E+0
16.00 79.00 1.00 1.000000000E-06 791.1E+0
16.00 80.00 1.00 1.000000000E-06 781.3E+0
16.00 81.00 1.00 1.000000000E-06 771.6E+0
16.00 82.00 1.00 1.000000000E-06 762.2E+0
16.00 83.00 1.00 1.000000000E-06 753.0E+0
16.00 84.00 1.00 1.000000000E-06 744.0E+0
16.00 85.00 1.00 1.000000000E-06 735.3E+0
16.00 86.00 1.00 1.000000000E-06 726.7E+0
16.00 87.00 1.00 1.000000000E-06 718.4E+0
16.00 88.00 1.00 1.000000000E-06 710.2E+0
16.00 89.00 1.00 1.000000000E-06 702.2E+0
16.00 90.00 1.00 1.000000000E-06 694.4E+0
16.00 91.00 1.00 1.000000000E-06 686.8E+0
16.00 92.00 1.00 1.000000000E-06 679.3E+0
16.00 93.00 1.00 1.000000000E-06 672.0E+0
16.00 94.00 1.00 1.000000000E-06 664.9E+0
16.00 95.00 1.00 1.000000000E-06 657.9E+0
16.00 96.00 1.00 1.000000000E-06 651.0E+0
16.00 97.00 1.00 1.000000000E-06 644.3E+0
16.00 98.00 1.00 1.000000000E-06 637.8E+0
16.00 99.00 1.00 1.000000000E-06 631.3E+0
16.00 100.00 1.00 1.000000000E-06 625.0E+0
16.00 101.00 1.00 1.000000000E-06 618.8E+0
16.00 102.00 1.00 1.000000000E-06 612.7E+0
16.00 103.00 1.00 1.000000000E-06 606.8E+0
16.00 104.00 1.00 1.000000000E-06 601.0E+0
16.00 105.00 1.00 1.000000000E-06 595.2E+0
16.00 106.00 1.00 1.000000000E-06 589.6E+0
16.00 107.00 1.00 1.000000000E-06 584.1E+0
16.00 108.00 1.00 1.000000000E-06 578.7E+0
16.00 109.00 1.00 1.000000000E-06 573.4E+0
16.00 110.00 1.00 1.000000000E-06 568.2E+0
16.00 111.00 1.00 1.000000000E-06 563.1E+0
16.00 112.00 1.00 1.000000000E-06 558.0E+0
16.00 113.00 1.00 1.000000000E-06 553.1E+0
16.00 114.00 1.00 1.000000000E-06 548.2E+0
16.00 115.00 1.00 1.000000000E-06 543.5E+0
16.00 116.00 1.00 1.000000000E-06 538.8E+0
16.00 117.00 1.00 1.000000000E-06 534.2E+0
16.00 118.00 1.00 1.000000000E-06 529.7E+0
16.00 119.00 1.00 1.000000000E-06 525.2E+0
16.00 120.00 1.00 1.000000000E-06 520.8E+0
16.00 121.00 1.00 1.000000000E-06 516.5E+0
16.00 122.00 1.00 1.000000000E-06 512.3E+0
16.00 123.00 1.00 1.000000000E-06 508.1E+0
16.00 124.00 1.00 1.000000000E-06 504.0E+0
16.00 125.00 1.00 1.000000000E-06 500.0E+0
16.00 126.00 1.00 1.000000000E-06 496.0E+0
16.00 127.00 1.00 1.000000000E-06 492.1E+0
16.00 128.00 1.00 1.000000000E-06 488.3E+0
16.00 129.00 1.00 1.000000000E-06 484.5E+0
16.00 130.00 1.00 1.000000000E-06 480.8E+0
16.00 131.00 1.00 1.000000000E-06 477.1E+0
16.00 132.00 1.00 1.000000000E-06 473.5E+0
16.00 133.00 1.00 1.000000000E-06 469.9E+0
16.00 134.00 1.00 1.000000000E-06 466.4E+0
16.00 135.00 1.00 1.000000000E-06 463.0E+0
16.00 136.00 1.00 1.000000000E-06 459.6E+0
16.00 137.00 1.00 1.000000000E-06 456.2E+0
16.00 138.00 1.00 1.000000000E-06 452.9E+0
16.00 139.00 1.00 1.000000000E-06 449.6E+0
16.00 140.00 1.00 1.000000000E-06 446.4E+0
16.00 141.00 1.00 1.000000000E-06 443.3E+0
16.00 142.00 1.00 1.000000000E-06 440.1E+0
16.00 143.00 1.00 1.000000000E-06 437.1E+0
16.00 144.00 1.00 1.000000000E-06 434.0E+0
16.00 145.00 1.00 1.000000000E-06 431.0E+0
16.00 146.00 1.00 1.000000000E-06 428.1E+0
16.00 147.00 1.00 1.000000000E-06 425.2E+0
16.00 148.00 1.00 1.000000000E-06 422.3E+0
16.00 149.00 1.00 1.000000000E-06 419.5E+0
16.00 150.00 1.00 1.000000000E-06 416.7E+0
16.00 151.00 1.00 1.000000000E-06 413.9E+0
16.00 152.00 1.00 1.000000000E-06 411.2E+0
16.00 153.00 1.00 1.000000000E-06 408.5E+0
16.00 154.00 1.00 1.000000000E-06 405.8E+0
16.00 155.00 1.00 1.000000000E-06 403.2E+0
16.00 156.00 1.00 1.000000000E-06 400.6E+0
16.00 157.00 1.00 1.000000000E-06 398.1E+0
16.00 158.00 1.00 1.000000000E-06 395.6E+0
16.00 159.00 1.00 1.000000000E-06 393.1E+0
16.00 160.00 1.00 1.000000000E-06 390.6E+0
16.00 161.00 1.00 1.000000000E-06 388.2E+0
16.00 162.00 1.00 1.000000000E-06 385.8E+0
16.00 163.00 1.00 1.000000000E-06 383.4E+0
16.00 164.00 1.00 1.000000000E-06 381.1E+0
16.00 165.00 1.00 1.000000000E-06 378.8E+0
16.00 166.00 1.00 1.000000000E-06 376.5E+0
16.00 167.00 1.00 1.000000000E-06 374.3E+0
16.00 168.00 1.00 1.000000000E-06 372.0E+0
16.00 169.00 1.00 1.000000000E-06 369.8E+0
16.00 170.00 1.00 1.000000000E-06 367.6E+0
16.00 171.00 1.00 1.000000000E-06 365.5E+0
16.00 172.00 1.00 1.000000000E-06 363.4E+0
16.00 173.00 1.00 1.000000000E-06 361.3E+0
16.00 174.00 1.00 1.000000000E-06 359.2E+0
16.00 175.00 1.00 1.000000000E-06 357.1E+0
16.00 176.00 1.00 1.000000000E-06 355.1E+0
16.00 177.00 1.00 1.000000000E-06 353.1E+0
16.00 178.00 1.00 1.000000000E-06 351.1E+0
16.00 179.00 1.00 1.000000000E-06 349.2E+0
16.00 180.00 1.00 1.000000000E-06 347.2E+0
16.00 181.00 1.00 1.000000000E-06 345.3E+0
16.00 182.00 1.00 1.000000000E-06 343.4E+0
16.00 183.00 1.00 1.000000000E-06 341.5E+0
16.00 184.00 1.00 1.000000000E-06 339.7E+0
16.00 185.00 1.00 1.000000000E-06 337.8E+0
16.00 186.00 1.00 1.000000000E-06 336.0E+0
16.00 187.00 1.00 1.000000000E-06 334.2E+0
16.00 188.00 1.00 1.000000000E-06 332.4E+0
16.00 189.00 1.00 1.000000000E-06 330.7E+0
16.00 190.00 1.00 1.000000000E-06 328.9E+0
16.00 191.00 1.00 1.000000000E-06 327.2E+0
16.00 192.00 1.00 1.000000000E-06 325.5E+0
16.00 193.00 1.00 1.000000000E-06 323.8E+0
16.00 194.00 1.00 1.000000000E-06 322.2E+0
16.00 195.00 1.00 1.000000000E-06 320.5E+0
16.00 196.00 1.00 1.000000000E-06 318.9E+0
16.00 197.00 1.00 1.000000000E-06 317.3E+0
16.00 198.00 1.00 1.000000000E-06 315.7E+0
16.00 199.00 1.00 1.000000000E-06 314.1E+0
16.00 200.00 1.00 1.000000000E-06 312.5E+0
16.00 201.00 1.00 1.000000000E-06 310.9E+0
16.00 202.00 1.00 1.000000000E-06 309.4E+0
16.00 203.00 1.00 1.000000000E-06 307.9E+0
16.00 204.00 1.00 1.000000000E-06 306.4E+0
16.00 205.00 1.00 1.000000000E-06 304.9E+0
16.00 206.00 1.00 1.000000000E-06 303.4E+0
16.00 207.00 1.00 1.000000000E-06 301.9E+0
16.00 208.00 1.00 1.000000000E-06 300.5E+0
16.00 209.00 1.00 1.000000000E-06 299.0E+0
16.00 210.00 1.00 1.000000000E-06 297.6E+0
16.00 211.00 1.00 1.000000000E-06 296.2E+0
16.00 212.00 1.00 1.000000000E-06 294.8E+0
16.00 213.00 1.00 1.000000000E-06 293.4E+0
16.00 214.00 1.00 1.000000000E-06 292.1E+0
16.00 215.00 1.00 1.000000000E-06 290.7E+0
16.00 216.00 1.00 1.000000000E-06 289.4E+0
16.00 217.00 1.00 1.000000000E-06 288.0E+0
16.00 218.00 1.00 1.000000000E-06 286.7E+0
16.00 219.00 1.00 1.000000000E-06 285.4E+0
16.00 220.00 1.00 1.000000000E-06 284.1E+0
16.00 221.00 1.00 1.000000000E-06 282.8E+0
16.00 222.00 1.00 1.000000000E-06 281.5E+0
16.00 223.00 1.00 1.000000000E-06 280.3E+0
16.00 224.00 1.00 1.000000000E-06 279.0E+0
16.00 225.00 1.00 1.000000000E-06 277.8E+0
16.00 226.00 1.00 1.000000000E-06 276.5E+0
16.00 227.00 1.00 1.000000000E-06 275.3E+0
16.00 228.00 1.00 1.000000000E-06 274.1E+0
16.00 229.00 1.00 1.000000000E-06 272.9E+0
16.00 230.00 1.00 1.000000000E-06 271.7E+0
16.00 231.00 1.00 1.000000000E-06 270.6E+0
16.00 232.00 1.00 1.000000000E-06 269.4E+0
16.00 233.00 1.00 1.000000000E-06 268.2E+0
16.00 234.00 1.00 1.000000000E-06 267.1E+0
16.00 235.00 1.00 1.000000000E-06 266.0E+0
16.00 236.00 1.00 1.000000000E-06 264.8E+0
16.00 237.00 1.00 1.000000000E-06 263.7E+0
16.00 238.00 1.00 1.000000000E-06 262.6E+0
16.00 239.00 1.00 1.000000000E-06 261.5E+0
16.00 240.00 1.00 1.000000000E-06 260.4E+0
16.00 241.00 1.00 1.000000000E-06 259.3E+0
16.00 242.00 1.00 1.000000000E-06 258.3E+0
16.00 243.00 1.00 1.000000000E-06 257.2E+0
16.00 244.00 1.00 1.000000000E-06 256.1E+0
16.00 245.00 1.00 1.000000000E-06 255.1E+0
16.00 246.00 1.00 1.000000000E-06 254.1E+0
16.00 247.00 1.00 1.000000000E-06 253.0E+0
16.00 248.00 1.00 1.000000000E-06 252.0E+0
16.00 249.00 1.00 1.000000000E-06 251.0E+0
16.00 250.00 1.00 1.000000000E-06 250.0E+0
|
any thoughts? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 03, 2011 3:59 pm |
|
|
Quote: | Couldn't I just decrease period from 249-60 for every count? |
The ratio of CCPR1L:CCP1CON<5:4> to PR2 is what controls the duty
cycle. If you change PR2 in order to change the PWM frequency, you
must also change CCPR1L:CCP1CON<5:4> if you want to maintain the
same duty cycle. |
|
|
bassn1
Joined: 03 Aug 2011 Posts: 6
|
|
Posted: Wed Aug 03, 2011 4:02 pm |
|
|
The duty cycle wont be constant in that range?
If i set it to 50, should it not stay at 50?
Note: im only changing period, not prescale.
Post and pre are constant |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 03, 2011 4:03 pm |
|
|
Do some experiments in hardware. |
|
|
bassn1
Joined: 03 Aug 2011 Posts: 6
|
asm |
Posted: Wed Aug 03, 2011 4:11 pm |
|
|
Code: |
#define NUM_PULSES 10000
//======================================
void main()
{
int16 count;
// Setup CCP1 for PWM at 244 Hz and 50% duty cycle.
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 249, 1);
set_pwm1_duty(128);
count = 0;
Ok I just want to decrement PR2 , but it tells me it must evaluate to a constant?
I looked in the .h file and I dont even see PR2>?
// Do the specified number of pulses on CCP1 and then stop.
while(1)
{
if (count<189)
{
if(interrupt_active(INT_TIMER2))
{
clear_interrupt(INT_TIMER2);
count++;
#asm
DECF,PR2,F
#endasm
}
else if(interrupt_active(INT_TIMER2))
{
clear_interrupt(INT_TIMER2);
count++;
}
if(count == NUM_PULSES)
{
set_pwm1_duty(0); // Set PWM output = constant 0
break;
}
}
while(1);
}
}
|
|
|
|
bassn1
Joined: 03 Aug 2011 Posts: 6
|
oops |
Posted: Wed Aug 03, 2011 4:13 pm |
|
|
oops,
see above code
Ok I just want to decrement PR2 , but it tells me it must evaluate to a constant?
I looked in the .h file and I dont even see PR2>? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 03, 2011 4:38 pm |
|
|
If you want to talk to a register directly, you don't use ASM. You declare
the register name and its address with a #byte statement. Then you
treat the register name as it's a variable in your code.
Look up #byte in the manual:
http://www.ccsinfo.com/downloads/ccs_c_manual.pdf
Just a tip: We don't like to see ASM in source code on this forum
unless it's absolutely necessary. |
|
|
|