Problem with Buttons! Please help!

Discussion on projects that are created by users and posted on mikroElektronika website.
Post Reply
Author
Message
piclv
Posts: 8
Joined: 20 Feb 2012 19:25

Problem with Buttons! Please help!

#1 Post by piclv » 20 Feb 2012 19:42

Hello,

I am trying to control the PWM duty cycle through 2 buttons on the EasyPic v7 Board.
However, once I press once on the increase PWM button the program hangs.
On the other hand, if i press the decrease PWM button, the Duty Cycle will go down a little and then hang...

Please help!
Here is the code:

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections

char txt1[] = "PWM DC = ";
char txt2[7];
char i; // Loop variable

void Move_Delay() // Function used for text moving
{
Delay_ms(500); // You can change the moving speed here
}


void main() {
unsigned short DC = 127;
TRISD = 0xFF; //Configure PORTD as Input
TRISC = 0x00; //Configure PORTC as Output

Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row



PWM1_Init(25000); //PWM frequency = 25KHz
PWM1_Set_Duty(DC); //PWM Duty Cycle = 50%
PWM1_Start(); //Start PWM on CCP1 = RC2 pin


while (1)
{
if (Button(&PORTD, 0, 1, 1)) // Detect logical one for Pull Down Resistor
{ if (DC != 255)
{
DC++;
PWM1_Set_Duty(DC);}
}
delay_ms(5);

if (Button(&PORTD, 1, 1, 1)) // Detect logical one for Pull Down Resistor
{ if (DC != 0)
{
DC--;
PWM1_Set_Duty(DC);}
}
delay_ms(5);
Lcd_Out(1,1,txt1);
IntToStr(DC,txt2);
Lcd_Out(2,1,txt2);;
}

}

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

Re: Problem with Buttons! Please help!

#2 Post by filip » 21 Feb 2012 10:31

Hi,

Did you put the PORTD pins in the pull-down mode using appropriate switches and put Button Press Level jumper J17 in the VCC position ?

Regards,
Filip.

piclv
Posts: 8
Joined: 20 Feb 2012 19:25

Re: Problem with Buttons! Please help!

#3 Post by piclv » 22 Feb 2012 07:20

Dear Filip,

PORTD pins RD0_bit and RD1_bit are in resistor pull down mode.
Jumper J17 is on VCC with protection enabled (resistor is not shorted).

I am really confused!
Please help.

Thank you.

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

Re: Problem with Buttons! Please help!

#4 Post by filip » 22 Feb 2012 09:52

Hi,

Which MCU are you using ?

Regards,
Filip.

piclv
Posts: 8
Joined: 20 Feb 2012 19:25

Re: Problem with Buttons! Please help!

#5 Post by piclv » 22 Feb 2012 17:23

PIC16F877a

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

Re: Problem with Buttons! Please help!

#6 Post by filip » 24 Feb 2012 12:20

Hi,

I believe we have the same conversation on the other topic, so we will continue there.

Regards,
Filip.

Post Reply

Return to “User Projects”