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);;
}
}
Problem with Buttons! Please help!
Re: Problem with Buttons! Please help!
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.
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.
Re: Problem with Buttons! Please help!
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.
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.
Re: Problem with Buttons! Please help!
Hi,
Which MCU are you using ?
Regards,
Filip.
Which MCU are you using ?
Regards,
Filip.
Re: Problem with Buttons! Please help!
Hi,
I believe we have the same conversation on the other topic, so we will continue there.
Regards,
Filip.
I believe we have the same conversation on the other topic, so we will continue there.
Regards,
Filip.