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 pic problem

Status
Not open for further replies.

neelam29

New Member
Dear all...
i want to design a ckt using pwm with 1HZ frequency and duty cycle 10% on pic 16 f690. i m posting this code please tell me wht changes to make for my requirement...
#include <pic.h>/* PIC Configuration Bit:
** INTIO - Using Internal RC No Clock
** WDTDIS - Wacthdog Timer Disable
** PWRTEN - Power Up Timer Enable
** MCLREN - Master Clear Enable
** UNPROTECT - Code Un-Protect
** UNPROTECT - Data EEPROM Read Un-Protect
** BORDIS - Borwn Out Detect Disable
** IESODIS - Internal External Switch Over Mode Disable
** FCMDIS - Monitor Clock Fail Safe Disable
*/
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS);// Using Internal Clock of 8 Mhz
#define FOSC 8000000L// Delay Function
#define _delay_us(x) { unsigned char us; \
us = (x)/(12000000/FOSC)|1; \
while(--us != 0) continue; }void _delay_ms(unsigned int ms)
{
unsigned char i;
do {
i = 4;
do {
_delay_us(164);
} while(--i);
} while(--ms);
}void main(void)
{
unsigned int ipwm;
unsigned char state; OSCCON=0x70; // Select 8 Mhz internal clock TRISC = 0x00; // Set All on PORTC as Output
ANSEL = 0x00; // Set PORT AN0 to AN7 digital I/O
ANSELH = 0x00; // Set PORT AN8 to AN11 as Digital I/O
PORTC = 0x00; // Turn Off all PORTC /* Init PWM for Single Output */
CCP1CON=0b00001100; // Single PWM mode; P1A, P1C active-low; P1B, P1D active-low
CCPR1L=0; // Start with zero Duty Cycle T2CON=0b00000101; // Postscaler: 1:1, Timer2=On, Prescaller = 1:4
PR2=0x65; // Frequency: 4.90 KHz
TMR2=0; // Start with zero Counter

PSTRCON=0b00000100; // Enable Pulse Steering on P1C (RC3)
state=0; // Start with state 1 for(;;) {
ipwm=0;
while (ipwm < 255) {
CCPR1L=++ipwm;
_delay_ms(5); // Delay 5 millisecond
}

ipwm=0xff;
while (ipwm > 0) {
CCPR1L=--ipwm;
_delay_ms(5); // Delay 5 millisecond
}

_delay_ms(100); // Delay 100 millisecond if (state == 0) {
state=1;
PSTRCON=0b00001000; // Enable Pulse Steering on P1D (RC2)
} else if (state == 1) {
state=2;
PSTRCON=0b00001100; // Enable Pulse Steering on P1C and P1D (RC3 and RC2)
} else {
state=0;
PSTRCON=0b00000100; // Enable Pulse Steering on P1C (RC3)
}
}
}

thanku ...my doubt is how can pr2 value be changed cause its only 8 bit reg..i want more....
 
Last edited:
Looking at the data sheet will tell you that slapping a 20Mhz Osc on will get the bit resolution you want. An alternate plan, is to use a device that that has a Power Control PWM module. So a device like a 18f1330, 18fXX31, and others, have up to 14 bits resolution at low clock rates and frequencies.
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top