PWM code works in simulator (ISIS Proteus), but not on my breadboard.

Thread Starter

Bjornk

Joined Jan 28, 2013
12
Hi.
When I use this PWM code below to Nerdegutta (thanks by the way) I get it to work in ISIS Proteus,
but not when I try it on a breadboard with a pic 16f628A and a led. I can provide the Proteus schema if necessary.

I connected the led to RB3. I also tryed to use an transistor if the output current from the pic was to low to drive the led.

My question: Is there something I need to activate or do on the microcontroller to get PWM to work?

Any other tips or thoughts regarding this problem appreciated.


Thanks



Bjørn


C-Code:

Rich (BB code):
      /* 
 
Program:        main.c 
Description:    PWM Test 
PIC:            PIC 16 F 628 
IDE:            MPLAB 
Compiler:        Hi Tech C 
Date:            Mars 2011 
Author:            Jens Christoffersen 
WEB:            www.nerdegutta.org 
*/ 
 
/* Include & definitions */ 
 
#define _XTAL_FREQ 4000000 
 
/* Configuration */ 
__CONFIG    (WDTDIS & 
PWRTEN & 
MCLREN & 
BOREN & 
LVPDIS & 
DATUNPROT & 
UNPROTECT & 
INTIO); 
 
/* Prototyping function */ 
 
/* Gobal variables */ 
static unsigned char retning; 
 
static void Intr(void) interrupt 0 
{ 
 if(T0IF)    // Har vi fått en timer interupt? 
 { 
 T0IF=0; 
 
 if(retning)    // teller oppover 
 { 
 CCPR1L++; 
 if(CCPR1L == 255) 
 retning=0; 
 } 
 else 
 { 
 CCPR1L--; 
 if(CCPR1L == 0) 
 retning=1; 
 } 
 } 
} // end static void Intr(void) 
 
/* Functions */ 
 
/* Main Program */ 
 
void main() 
{ 
retning=0; 
 
TRISA = 0b11111111; 
TRISB = 0b00000000; 
 
PORTA = 0b00000000; 
PORTB = 0b00000000; 
 
PR2 = 255; // Setter PWM periode 
CCPR1L = 1; // Setter PWM duty cycle 
CCP1CON = 4|8; // Setter PWM mode 
CCP1X = 1;  
 
T2CON = 0x00; 
T2CKPS0 = 1; // Set timer 2 prescaler to 1:16 
T2CKPS1 = 1; // These bits are in T2CON 
TMR2ON = 1; // Enable timer 2 
 
// Set up timer0 interrupt 
T0CS=0;    // Internal clock source 
PSA=0;    // Assign prescaler to timer0 
PS2=0;    PS1=1;    PS0=0; 
INTCON=0; 
GIE=1; 
T0IE=1; 
TMR0=0; 
 
while (1); // Fortsetter inn i evigheten, og forbi 
 
 
}
 

tshuck

Joined Oct 18, 2012
3,534
You have a resistor from MCLR to Vdd, right? You have MCLR enabled in your configuration. If you don't have one, either add a 10k resistor, or disable MCLR.


If this doesn't solve your problem, take a screenshot of your schematic, and upload it here...
 

Thread Starter

Bjornk

Joined Jan 28, 2013
12
Hi, and thanks for the reply.

I tryed to turn off the MCLR, but I still can't get it to work.
By the way I use MPLabX and comp. Hi-tech C.
And I can't get any volt readings on my multimeter on port RB3.

It's so frustrating not getting this to work.

Any thoughts regrading this problem is apprisiated.

I'll added my very simple schematics.


Rich (BB code):
    /*

Program:        main.c
Description:    PWM Test
PIC:            PIC 16 F 628
IDE:            MPLAB
Compiler:        Hi Tech C
Date:            Mars 2011
Author:            Jens Christoffersen
WEB:            www.nerdegutta.org
*/

/* Include & definitions */

#define _XTAL_FREQ 4000000
#include <pic.h>
#include <htc.h>
#include <pic16f628a.h>

//* Configuration */

__CONFIG(FOSC_INTOSCCLK & 
        WDTE_OFF & 
        PWRTE_OFF & 
        MCLRE_OFF & 
        BOREN_ON & 
        LVP_OFF & 
        CPD_OFF & 
        CP_OFF);


/* Prototyping function */

/* Gobal variables */
static unsigned char retning;

#define MOTOR_A     RB3     // Waterpump

#define MOTOR_A_TRIS TRISB3

static void Intr(void) interrupt 0
{
 if(T0IF)    // Har vi fått en timer interupt?
 {
 T0IF=0;

 if(retning)    // teller oppover
 {
 CCPR1L++;
 if(CCPR1L == 255)
 retning=0;
 }
 else
 {
 CCPR1L--;
 if(CCPR1L == 0)
 retning=1;
 }
 }
} // end static void Intr(void)

/* Functions */

/* Main Program */


void main()
{
        MOTOR_A_TRIS = 0;
retning=0;


PR2 = 255; // Setter PWM periode
CCPR1L = 1; // Setter PWM duty cycle
CCP1CON = 4|8; // Setter PWM mode
CCP1X = 1;

T2CON = 0x00;
T2CKPS0 = 1; // Set timer 2 prescaler to 1:16
T2CKPS1 = 1; // These bits are in T2CON
TMR2ON = 1; // Enable timer 2

// Set up timer0 interrupt
T0CS=0;    // Internal clock source
PSA=0;    // Assign prescaler to timer0
PS2=0;    PS1=1;    PS0=0;
INTCON=0;
GIE=1;
T0IE=1;
TMR0=0;

while (1); // Fortsetter inn i evigheten, og forbi


}
 

Attachments

tshuck

Joined Oct 18, 2012
3,534
First, it looks like your LED is the wrong direction, flat side should be toward ground.

Second, you need a resistor to limit the current to the LED, a 1k is a typical value.

Third, your LED is on pin 18, not 9. This is RA1.
 

tshuck

Joined Oct 18, 2012
3,534
You should also be aware they no PIC will supply enough current to drive a motor, you'll need to fix that when moving to an actual motor...
 

Thread Starter

Bjornk

Joined Jan 28, 2013
12
Thanks tshuck, it's working now!

Alot of rookie mistakes there... But I have learned my lesson and to use more time constructing the circuit.

Thanks again!
 

Attachments

tshuck

Joined Oct 18, 2012
3,534
That's great! Mistakes are how you learn what to look for, do consider yourself on the road for great debugging skills! ;)
 

jayanath

Joined Dec 11, 2010
17
I have following code. but all leds are fading and getting on together. no running action. how can i make chaser action with fading effect. i mean comet tail. pls help me?



#include <pic.h>
#define FADE_RATE 12
#define INITIAL_WIDTH 128
void fade()
{
unsigned char rate = FADE_RATE;
unsigned char pulse_width = INITIAL_WIDTH;
unsigned char count;
while(pulse_width){
PORTB= 0b11111111;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
if(!rate--){
pulse_width--;
rate = FADE_RATE;
}
}
}
int main(void) {
char b;
TRISB = 0b00000000;
while(1){
for(b=0x02; b; b<<=1){
PORTB = b;
fade(b>>1);
}
for(b=0x40; b; b>>=1){
PORTB = b;
fade(b<<1);
}
}
}
 

tshuck

Joined Oct 18, 2012
3,534
I have following code. but all leds are fading and getting on together. no running action. how can i make chaser action with fading effect. i mean comet tail. pls help me?



#include <pic.h>
#define FADE_RATE 12
#define INITIAL_WIDTH 128
void fade()
{
unsigned char rate = FADE_RATE;
unsigned char pulse_width = INITIAL_WIDTH;
unsigned char count;
while(pulse_width){
PORTB= 0b11111111;
for(count=0; count<pulse_width; count++){
asm("NOP");
}
PORTB=0b00000000;
for(; count; count++){
asm("NOP");
}
if(!rate--){
pulse_width--;
rate = FADE_RATE;
}
}
}
int main(void) {
char b;
TRISB = 0b00000000;
while(1){
for(b=0x02; b; b<<=1){
PORTB = b;
fade(b>>1);
}
for(b=0x40; b; b>>=1){
PORTB = b;
fade(b<<1);
}
}
}
How about you start your own thread instead of stealing someone else's? ;)

Your implementation is not the same as your function definition - fade() is not the same as fade(char).
 
Top