#define button PORTA.F4
#define LEDs PORTA.F0
#define Buzzer PORTA.F0
#define STEP1 0b00010010
#define STEP2 0b00000010
#define STEP3 0b00000110
#define STEP4 0b00000100
#define STEP5 0b00001100
#define STEP6 0b00001000
#define STEP7 0b00011000
#define STEP8 0b00010000
void Delay(unsigned int ms)
{
switch(ms)
{
case 500:
Delay_ms(500);
break;
case 200:
Delay_ms(200);
break;
default:
Delay_ms(1000);
}
}
void main(void)
{ unsigned int i;
TRISA =0b00110000;
TRISB = 0; //Port B as output (LCD is OUTPUT)
TRISC=0*00;
PORTC=0;
Lcd_Init(&PORTB);
Lcd_Config(&PORTB,3,1,2,7,6,5,4);
Lcd_Cmd(LCD_CURSOR_OFF);
while(1){
if(button==1)
{ //Button is not pressed
Lcd_Cmd(LCD_CLEAR); //Clear LCD display (Blank the LCD screen)
Lcd_Out(1, 4, "HAVE A SAFE"); //Print text on LCD at line 1, column 4
Lcd_Out(2, 6, "JOURNEY"); //Print text on LCD at line 2, column 5
Delay_ms(1000);
LEDs=0;
PORTC=STEP8;
Delay(200);
PORTC=STEP7;
Delay(200);
PORTC=STEP6;
Delay(200);
PORTD=STEP5;
Delay(200);
PORTC=STEP4;
Delay(200);
PORTC=STEP3;
Delay(200);
PORTC=STEP2;
Delay(200);
PORTC=STEP1;
Delay(200);
break;
}
if(button==0)//Button is pressed
{
Lcd_Cmd(LCD_CLEAR); //Clear LCD display (Blank the LCD screen)
Lcd_Out(1,5,"CAUTION!!!");
Lcd_Out(2,5,"CAUTION!!!");
Delay_ms(500);
PORTC=STEP1;
Delay(500);
PORTC=STEP2;
Delay(500);
PORTC=STEP3;
Delay(500);
PORTC=STEP4;
Delay(500);
PORTC=STEP5;
Delay(500);
PORTC=STEP6;
Delay(500);
PORTC=STEP7;
Delay(500);
PORTC=STEP8;
Delay(500);
while(1){
Buzzer=1;
Delay_ms(2000);
LEDs=1;
Delay_ms(2000);
LEDs=0;
Delay_ms(500); }
break;
}
}
}
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 #define SW PORTA.F4 #define LED PORTA.F0 #define BUZZER PORTA.F1 char steps[] = {0b00010010, 0b00000010, 0b00000110, 0b00000100, 0b00001100, 0b00001000, 0b00011000, 0b00010000}; char doOnce1 = 0, doOnce2 = 0, mode = 0, i = 0, count = 0, state = 0, lock = 0; //Timer1 //Prescaler 1:4; TMR1 Preload = 15536; Actual Interrupt Time : 200 ms //Place/Copy this part in declaration section void InitTimer1_200ms() { T1CON = 0x21; INTCON.TMR1IF = 0; TMR1H = 0x3C; TMR1L = 0xB0; INTCON.TMR1IE = 1; INTCON = 0xC0; } //Timer1 //Prescaler 1:8; TMR1 Preload = 3036; Actual Interrupt Time : 500 ms //Place/Copy this part in declaration section void InitTimer1_500ms() { T1CON = 0x31; INTCON.TMR1IF = 0; TMR1H = 0x0B; TMR1L = 0xDC; INTCON.TMR1IE = 1; INTCON = 0xC0; } void Interrupt() { if(INTCON.TMR1IF) { INTCON.TMR1IF = 0; if(state == 0) { if(mode == 0) { PORTC = steps[7 - i]; TMR1H = 0x3C; TMR1L = 0xB0; } else if(mode == 1) { PORTC = steps[i]; TMR1H = 0x0B; TMR1L = 0xDC; } i++; } if(i == 8) { i = 0; if(mode == 1)state = 1; else { T1CON.TMR1ON = 0; lock = 0; } } if(state) { if(state == 1) { BUZZER = 1; count = 0; } if(count == 4) { LED = 1; } else if(count == 8) { LED = 0; } else if(count == 9) { state = 0; count = 0; T1CON.TMR1ON = 0; doOnce1 = 0; doOnce2 = 0; lock = 0; } state++; count++; } } } void main(void) { CMCON = 0x07; ADCON1 = 0x87; TRISA = 0x30; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; Lcd_Init(&PORTB); Lcd_Config(&PORTB,3,1,2,7,6,5,4); Lcd_Cmd(LCD_CURSOR_OFF); Lcd_Cmd(LCD_CLEAR); while(1){ if(lock == 0) { if(SW) { Delay_ms(50); if(SW) { if(!doOnce1) { Lcd_Cmd(LCD_CLEAR); Lcd_Out(1,4,"Have a safe"); Lcd_Out(2,6,"Journey"); Delay_ms(800); LED = 0; InitTimer1_200ms(); mode = 0; i = 0; doOnce1 = 1; lock = 1; } } } else if(!SW) { Delay_ms(50); if(!SW) { if(!doOnce2) { Lcd_Cmd(LCD_CLEAR); Lcd_Out(1,5,"Caution!!!"); Lcd_Out(2,5,"Caution!!!"); Delay_ms(500); LED = 0; InitTimer1_500ms(); mode = 1; i = 0; doOnce2 = 1; lock = 1; } } } } } }
while(1){
Buzzer=1;
Delay_ms(2000);
LEDs=1;
Delay_ms(2000);
LEDs=0;
Delay_ms(500); }
break;
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 #define button PORTA.F4 #define LED PORTA.F0 #define Buzzer PORTA.F1 #define STEP1 0b00010010 #define STEP2 0b00000010 #define STEP3 0b00000110 #define STEP4 0b00000100 #define STEP5 0b00001100 #define STEP6 0b00001000 #define STEP7 0b00011000 #define STEP8 0b00010000 char doOnce1 = 0, doOnce2 = 0; void Delay(unsigned int ms) { switch(ms) { case 500: Delay_ms(500); break; case 200: Delay_ms(200); break; default: Delay_ms(1000); } } void main(void) { TRISA = 0b00110000; TRISB = 0; TRISC = 0; PORTC = 0; Lcd_Init(&PORTB); Lcd_Config(&PORTB,3,1,2,7,6,5,4); Lcd_Cmd(LCD_CURSOR_OFF); Lcd_Cmd(LCD_CLEAR); while(1){ if(button == 1) //Button is not pressed { Delay_ms(50); if(button == 1) //Button is not pressed { if(doOnce1 == 0) { Lcd_Cmd(LCD_CLEAR); Lcd_Out(1,4,"HAVE A SAFE"); Lcd_Out(2,6,"JOURNEY"); doOnce1 = 1; } Delay_ms(1000); LED = 0; PORTC = STEP8; Delay(200); PORTC = STEP7; Delay(200); PORTC = STEP6; Delay(200); PORTD = STEP5; Delay(200); PORTC = STEP4; Delay(200); PORTC = STEP3; Delay(200); PORTC = STEP2; Delay(200); PORTC = STEP1; Delay(200); } } else if(button == 0) //Button is pressed { Delay_ms(50); if(button == 0) { if(doOnce2 == 0) { Lcd_Cmd(LCD_CLEAR); //Clear LCD display (Blank the LCD screen) Lcd_Out(1,5,"CAUTION!!!"); Lcd_Out(2,5,"CAUTION!!!"); doOnce2 = 1; } Delay_ms(500); PORTC = STEP1; Delay(500); PORTC = STEP2; Delay(500); PORTC = STEP3; Delay(500); PORTC = STEP4; Delay(500); PORTC = STEP5; Delay(500); PORTC = STEP6; Delay(500); PORTC = STEP7; Delay(500); PORTC = STEP8; Delay(500); while(1) { Buzzer = 1; Delay_ms(2000); LEDs=1; Delay_ms(2000); LEDs=0; Delay_ms(500); doOnce1 = 0; doOnce2 = 0; break; } } } } }
Code C - [expand] 1 2 3 4 uint16 delay. // Somewhere in your code: if((delay == 200) }} (delay == 500)) Delay_ms(delay); else Delay_ms(1000);
Code C - [expand] 1 2 3 4 for(i = 0 ; i < 8 ; ++i) { MOTOR_PORT = step[i]; delay(200); }
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?