[PIC] Button function does'nt work for PWM

Status
Not open for further replies.

Aseng

Newbie level 4
Joined
Dec 27, 2015
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
47
Everyone,
First of all, I am here is a newbie in this forum.

I am using a MikroC for compiler from Mikroelektronika, I want to build a PWM based on IC UC PIC16F877A, this code I copied paste from other in this forum, I try for few times to compile the project but his doesn't work, when I do simulate in a proteusView attachment pwm pic16f877a.7z.

So I need some help to correction what is going on this code problem.

This project is PWM, RA0 - RA7 is an input and the an output is RC0 -RC5, what I want is when I press RA0 then RC0 will up slowly and also for the rest.

So could you please help me to solved this matter.

On below is attached for the project.
Code:
sbit LED1 at RC0_bit;
sbit LED2 at RC1_bit;
sbit LED3 at RC2_bit;
sbit LED6 at RC3_bit;
sbit LED4 at RC4_bit;
sbit LED5 at RC5_bit;


#define SW_PORT PORTA
#define SW_TEST_VAL 0xD7
#define PWM_FREQ 5000

unsigned short gPwmDuty = 255;
    void testButtonRelease() {
     while((SW_PORT & 0xF7) != 0xF7);
}
void main() {

     CMCON = 7;
     TRISA = 0xFF;
     TRISC = 0x00;
     PORTA = 0x00;
     PORTC = 0x00;

     PWM1_Init(PWM_FREQ);
     PWM1_Set_Duty(gPwmDuty);
     PWM1_Start();

     while(1) {

            if((SW_PORT & SW_TEST_VAL) == 0xD6) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED1 = ~LED1;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xD5) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED2 = ~LED2;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xD3) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED3 = ~LED3;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0xC7) {
                   Delay_ms(80);
                   testButtonRelease();
                   LED4 = ~LED4;
            }
            else if((SW_PORT & SW_TEST_VAL) == 0x97) {
                   Delay_ms(80);
                   testButtonRelease();
                   if((gPwmDuty >= 0) && (gPwmDuty <= 254)) {
                            gPwmDuty++;
                            PWM1_Set_Duty(gPwmDuty);
                   }
            }
            else if((SW_PORT & SW_TEST_VAL) == 0x57) {
                   Delay_ms(80);
                   testButtonRelease();
                   if((gPwmDuty <= 255) && (gPwmDuty > 0)) {
                            gPwmDuty--;
                            PWM1_Set_Duty(gPwmDuty);
                   }
            }
     }
}
View attachment pwm pic16f877a.7z

Many Thanks.
 

Add

Code:
ADCON1 = 0x87;

You need to disable ADC on PORTA to make the button work.
 
Reactions: Aseng

    Aseng

    Points: 2
    Helpful Answer Positive Rating
Add

Code:
ADCON1 = 0x87;

You need to disable ADC on PORTA to make the button work.

pic.programmer.
I try to add ADCON1 = 0x87; in line below CMCON but his still not changes.
I am not sure is this code (#define SW_TEST_VAL 0xD7) is suitable for PIC16F877A.
Please help.
 

pic.programmer.
I try to add ADCON1 = 0x87; in line below CMCON but his still not changes.
I am not sure is this code (#define SW_TEST_VAL 0xD7) is suitable for PIC16F877A.
Please help.

The code has been working, ......thanks for helping PIC Programmer.:thumbsup::thumbsup:
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…