Newbie: PWM Example Won't Work on EasyPIC2

General discussion on mikroElektronika development boards.
Post Reply
Author
Message
Dale Stewart
Posts: 39
Joined: 05 Jan 2005 04:49
Location: Wollongong, Australia

Newbie: PWM Example Won't Work on EasyPIC2

#1 Post by Dale Stewart » 12 Apr 2005 05:24

Hi

I am very new to PICs and just compiled ( MikroC ) and loaded my first program to the EasyPIC2 board, the example for PWM that comes with MikroC ( PWM_Test.c ). for the 16F877A

According to the program notes, when the push buttons on RA0 or RA1 are pushed, the output on RC2/CCP should change.

So I assume that the LED on RC2 should change when these buttons are pushed (HIGH).

However, ALL the LEDs are on for Port C.

Settings:

Code Protection: OFF
LVP: OFF
WDT: OFF
HS Oscillator: ON
BODEN: ON


:?: From the code example (below), should only the individual bits for I/O be set? It looks like ALL bits for Port A are set as input, and ALL bits on port B are set for output; and ALL bits on Port C are set as output - is this the case?
void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate portb pins as output

ADCON1 = 6; // all ADC pins to digital I/O
PORTA = 255;
TRISA = 255; // PORTA is input

PORTC = 0xFF; // set PORTC to $FF
TRISC = 0; // designate PORTC pins as output
PWM_Init(5000); // initialize PWM module
}//~
Here is the whole code example:
/*
* Project name:
PWM_Test_01 (PWM library Demonstration)
* Done by:
Vladimir Petrovic, mikroElektronika, Feb.2005
* Description:
This is a simple demonstration of PWM library, which is being used for
control of the PIC's CCP module. The module is initialized and started,
after which the PWM Duty Ratio can be adjusted by means of two buttons
connected to pins RA0 and RA1. The changes can be monitored on the CCP
output pin (RC2).
* Test configuration:
MCU: P16F877A
Dev.Board: EasyPIC2
Oscillator: HS, 08.0000 MHz
Modules: x
SW: mikroC v1.01
*/

unsigned short j, oj;

void InitMain() {
PORTB = 0; // set PORTB to 0
TRISB = 0; // designate portb pins as output

ADCON1 = 6; // all ADC pins to digital I/O
PORTA = 255;
TRISA = 255; // PORTA is input

PORTC = 0xFF; // set PORTC to $FF
TRISC = 0; // designate PORTC pins as output
PWM_Init(5000); // initialize PWM module
}//~

void main() {
initMain();

j = 80; // initial value for j
oj = 0; // oj will keep the 'old j' value
PWM_Start(); // start PWM

while (1) { // endless loop
if (Button(&PORTA, 0,1,1)) // button on RA0 pressed
j++ ; // increment j
if (Button(&PORTA, 1,1,1)) // button on RA1 pressed
j-- ; // decrement j

if (oj != j) { // if change in duty cycle requested
PWM_Change_Duty(j); // set new duty ratio,
oj = j; // memorize it
PORTB = oj; // and display on PORTB
}
Delay_ms(200); // slow down change pace a little
}
}//~!
Any ideas, please?

Cheers

Dale
Last edited by Dale Stewart on 13 Apr 2005 04:48, edited 1 time in total.

Dale Stewart
Posts: 39
Joined: 05 Jan 2005 04:49
Location: Wollongong, Australia

Bug in Example Code

#2 Post by Dale Stewart » 12 Apr 2005 11:10

Hi

I changed the code- guess what? - it worked!

It looks like the example code that comes with MikroC has a bug as described in my previous post.

Here is the new code block that works.

Code: Select all

void InitMain() {
  PORTB = 0;              // set PORTB to 0
  TRISB = 0;              // designate portb pins as output

  ADCON1 = 6;             // all ADC pins to digital I/O
  PORTA = 255;
  TRISA = 0b00000011;            // BitS RA0 & RA1 are inputs

  PORTC = 0b00000100;           // Set bit RC2/CCP
  TRISC = 0;              // designate PORTC pins as output
  PWM_Init(5000);         // initialize PWM module
}//~
Hope this helps beginners like myself.

Cheers

Dale

ps I plan to post beginner-level code on my website, as I believe simple examples are useful. Please visit and bookmark my website and expect to see a section to download code examples. :wink:

I will post later when it is up, and will post an email address for beginners who want to send me their simple examples.

Another idea is for code for robotics later on.

http://www.users.tpg.com.au/daleste/index.htm

mahmoud1230
Posts: 3
Joined: 15 Mar 2013 21:48

Re: Newbie: PWM Example Won't Work on EasyPIC2

#3 Post by mahmoud1230 » 17 Mar 2013 20:34

It does not work with this code

Code: Select all

unsigned short current_duty, old_duty;   // Define variables
                                         // current_duty and old_duty

void initMain() {
    ANSEL = 0;                           // All I/O pins are configured as digital
    ANSELH = 0;
    PORTA = 255;                         // Port A initial state
    TRISA = 255;                         // All port A pins are configured as inputs
    PORTB = 0;                           // Initial state of port B
    TRISB = 0;                           // All port B pins are configured as outputs
    PORTC = 0;                           // Port C initial state
    TRISC = 0;                           // All port C pins are configured as outputs
    PWM1_Init(5000);                     // PWM module initialization (5KHz)
}

void main() {
    initMain();
    current_duty = 16;                   // Initial value of variable current_duty
    old_duty = 0;                        // Reset variable old_duty
    PWM1_Start();                        // Start PWM1 module
    
    while (1) {                          // Endless loop
        if (Button(&PORTA, 0,1,1))       // If the button connected to RA0 is pressed
            current_duty++ ;             // increment variable current_duty
        
        if (Button(&PORTA, 1,1,1))       // If the pressed button is connected to RA1
            current_duty-- ;             // decrement value current_duty
        
        if (old_duty != current_duty) {  // If current_duty and old_duty are not
            PWM1_Set_Duty(current_duty); // equal set PWM to a new value,            
            old_duty = current_duty;     // save the new value
            PORTB = old_duty;            // and show it on port B
        }
        
        Delay_ms(200);                   // 200mS delay
    }
}

Thanks

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Newbie: PWM Example Won't Work on EasyPIC2

#4 Post by filip » 18 Mar 2013 15:56

Hi,

Which MCU, development system, compiler are you using ?

Regards,
Filip.

Post Reply

Return to “Development Boards”