CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Software PWM 16F628A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
A2B



Joined: 22 Mar 2007
Posts: 2

View user's profile Send private message Visit poster's website

Software PWM 16F628A
PostPosted: Sat Apr 14, 2007 4:41 am     Reply with quote

Hello,

I'm trying to make a software PWM for the 16F628A. This is for the RGB led i'm trying to make fading. Does anyone have a example for me.

I'm using PCWH compiler version 4.023

Thanks
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

PostPosted: Sat Apr 14, 2007 8:25 am     Reply with quote

Use the forum search tool.

This is one of the topics I found helpful when I was faced with a similar project:

http://www.ccsinfo.com/forum/viewtopic.php?t=20050

John
mohikan



Joined: 15 Apr 2007
Posts: 1
Location: oradea, ro

View user's profile Send private message

PostPosted: Sun Apr 15, 2007 3:26 am     Reply with quote

I had a similar project, the LEDs were connected to port RA (RA2=LED Blue, RA3=LED Red, RA4=LED Green) each with its own resistor (RA4 needs pull-up), the software PWM is in timer2 ISR and it's from a Microchip MicroSolutions Newsletter.

Code:

-------------------------------------------------------------------------------------
#include "RGBTest.h"

#INT_TIMER2
void TIMER2_isr()
{
   #asm
      movf    pwm_counter,w
      subwf    pwm_duty_green,w
      rlf    output,f
      movf    pwm_counter,w
      subwf    pwm_duty_red,w
      rlf    output,f
      movf    pwm_counter,w
      subwf    pwm_duty_blue,w
      rlf    output,f
      
      rlf      output,f
      rlf      output,w
      movwf    PORTA
      
      incf    pwm_counter,f
      clrf   output
   #endasm
}

#inline
void set_pwm_duty(ColorType color,int duty)
{
   switch(color)
   {
      case RED:
      {
         pwm_duty_red=duty;
         break;
      }
      case GREEN:
      {
         pwm_duty_green=duty;
         break;
      }
      case BLUE:
      {
         pwm_duty_blue=duty;
         break;
      }
      default:break;
   }
}

void main()
{
   set_tris_a(0x21);//00100001
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
      setup_timer_1(T1_DISABLED);
      setup_timer_2(T2_DIV_BY_1,0x4E,1);
      setup_comparator(NC_NC_NC_NC);
      setup_vref(FALSE);
      setup_ccp1(CCP_OFF);
      enable_interrupts(INT_TIMER2);
      enable_interrupts(GLOBAL);
      
      printf("\n\rRGBTest v1.0 2006\n\r");
   
   while(TRUE)
   {
      switch(getc())
      {
         case '7':
         {
            ++pwm_duty_red;
            set_pwm_duty(RED,pwm_duty_red);
            break;
         }
         case '4':
         {
            --pwm_duty_red;
            set_pwm_duty(RED,pwm_duty_red);
            break;
         }
         case '8':
         {
            ++pwm_duty_green;
            set_pwm_duty(GREEN,pwm_duty_green);
            break;
         }
         case '5':
         {
            --pwm_duty_green;
            set_pwm_duty(GREEN,pwm_duty_green);
            break;
         }
         case '9':
         {
            ++pwm_duty_blue;
            set_pwm_duty(BLUE,pwm_duty_blue);
            break;
         }
         case '6':
         {
            --pwm_duty_blue;
            set_pwm_duty(BLUE,pwm_duty_blue);
            break;
         }
         default:break;
      }
      printf("> R:%03u G:%03u B:%03u\r",pwm_duty_red,pwm_duty_green,pwm_duty_blue);
   }
}

-------------------------------------------------------------------------------------
#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=19200,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8,stream=UART)
#use fast_io(a)

#define   PORTA   0x05

typedef enum
{
   RED,
   GREEN,
   BLUE
} ColorType;

unsigned int pwm_counter=0;
unsigned int pwm_duty_red=0;
unsigned int pwm_duty_green=0;
unsigned int pwm_duty_blue=0;
unsigned int output=0;
-------------------------------------------------------------------------------------
A2B



Joined: 22 Mar 2007
Posts: 2

View user's profile Send private message Visit poster's website

PostPosted: Mon Apr 16, 2007 3:18 am     Reply with quote

Thank you for the code. This is most helpfull
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group