Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 sbit LED3 at Rb4_bit; sbit LED2 at Rb2_bit; sbit LED at Rb0_bit; sbit LED1 at Rb1_bit; //sbit Switch at RC1_bit; // #define Switch_Pin 1 //#define Switch_Port PORTC //#define Debounce_Time 20 unsigned short oldstate; unsigned short last_duty, current_duty; void main() { CMCON |= 7; // Main; TRISB = 0; // Set PORTB direction to be output; PORTB = 0; // Turn OFF LEDs on PORTA; trisb3_bit = 0; TRISA6_bit = 1; // Set PORT RB1 input for button; oldstate = 0; // Define value of oldstate; current_duty = 0; // Initial value of variable current_duty; last_duty = 0; // Reset variable last_duty; pwm1_init(5000); // PWM module initialization (5KHz); pwm1_start(); // Start PWM1 module; pwm1_set_duty(last_duty); do { // 200mS delay if (Button(&PORTA,0,1,1)) oldstate = 1; Delay_ms(100); //delay for prevent crash if (oldstate &&Button(&PORTA,0,1,0)) { led = ~led; //invert state of PORTB oldstate = 0; } if (Button(&PORTA,1,1,1)) oldstate = 1; Delay_ms(100); //delay for prevent crash if (oldstate &&Button(&PORTA,1,1,0)) { led1 = ~led1; //invert state of PORTB oldstate = 0; } if (Button(&PORTA,2,1,1)) oldstate = 1; Delay_ms(100); //delay for prevent crash if (oldstate &&Button(&PORTA,2,1,0)) { led2 = ~led2; //invert state of PORTB oldstate = 0; } if (Button(&PORTA,3,1,1)) oldstate = 1; Delay_ms(100); //delay for prevent crash if (oldstate &&Button(&PORTA,3,1,0)) { led3 = ~led3; //invert state of PORTB oldstate = 0; } } while(1); //Infinite loop
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 while(last_duty>0<255){ if (Button(&PORTA, 6,1,0)) last_duty = last_duty +2; // If the button connected to // RA0 is pressed Pwm1_Set_Duty(last_duty); delay_ms(100); if (Button(&PORTA, 7,1,0)) last_duty = last_duty -2; // If the pressed button is // connected to RA1 Pwm1_Set_Duty(last_duty); delay_ms(100); } delay_ms(200);
Code C - [expand] 1 while((SW_PORT & 0xF7) != 0xF7);
Code C - [expand] 1 while(SW_PORT != 0xF7);
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);
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?