void change_intensity()
{
static unsigned int motor_flagA;
static unsigned int inten_flagA;
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((ldr1 <= 500) && (motor_flagA < 1))
{
turn_A(ARIGHT,23);
motor_flagA++;
}
#asm("cli")
if((PORTB.0 == 1) && (ldr1 <= 500) && (motor_flagA == 1))
{
if(x > 1)
{
x = x - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.0 == 1) && (ldr1 >= 550))
{
if(x < 280)
{
x = x + 1;
inten_flagA++;
}
}
#asm("sei")
if((ldr1 >= 550) && (inten_flagA == 1) && (motor_flagA == 1))
{
turn_A(ALEFT,23);
motor_flagA = 0;
inten_flagA = 0;
}
[COLOR="#FF0000"]// But here will be the second motor, can I disable it again[/COLOR]
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((PORTB.1 == 1) && (ldr2 <= 500))
{
if(y > 1)
{
y = y - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.1 == 1) && (ldr2 >= 550))
{
if(y < 280)
{
y = y + 1;
}
}
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((PORTB.2 == 1) && (ldr3 <= 500))
{
if(z > 1)
{
z = z - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.2 == 1) && (ldr3 >= 550))
{
if(z < 280)
{
z = z + 1;
}
}
}
// But here will be the second motor, can I disable it again
void change_intensity()
{
static unsigned int motor_flagA = 0;
static unsigned int motor_flagB = 0;
static unsigned int motor_flagC = 0;
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((ldr1 <= 300) && (motor_flagA < 1))
{
turn_A(ARIGHT,23);
motor_flagA++;
}
#asm("cli")
if((PORTB.0 == 1) && (ldr1 <= 300) && (motor_flagA == 1))
{
if(x > 1)
{
x = x - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.0 == 1) && (ldr1 >= 350))
{
if(x < 280)
{
x = x + 1;
}
}
#asm("sei")
if((ldr1 >= 350) && (x == 280) && (motor_flagA == 1))
{
turn_A(ALEFT,23);
motor_flagA = 0;
}
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((ldr2 <= 300) && (motor_flagB < 1))
{
turn_B(BRIGHT,23);
motor_flagB++;
}
#asm("cli")
if((PORTB.1 == 1) && (ldr2 <= 300) && (motor_flagB == 1))
{
if(y > 1)
{
y = y - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.1 == 1) && (ldr2 >= 350))
{
if(y < 280)
{
y = y + 1;
}
}
#asm("sei")
if((ldr2 >= 350) && (y == 280) && (motor_flagB == 1))
{
turn_B(BLEFT,23);
motor_flagB = 0;
}
// If light intensity below the setpoint, open blinds, and shorten the firing delay
if((ldr3 <= 300) && (motor_flagC < 1))
{
turn_C(CRIGHT,23);
motor_flagC++;
}
#asm("cli")
if((PORTB.2 == 1) && (ldr3 <= 300) && (motor_flagC == 1))
{
if(z > 1)
{
z = z - 1;
}
}
// If light intensity greater than the setpoint, increase the firing delay, and close blinds
if((PORTB.2 == 1) && (ldr3 >= 350))
{
if(z < 280)
{
z = z + 1;
}
}
#asm("sei")
if((ldr3 >= 350) && (z == 280) && (motor_flagC == 1))
{
turn_C(CLEFT,23);
motor_flagC = 0;
}
}
unsigned int motor_stepA[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000}; [COLOR="#FF0000"]// This and the others and also below[/COLOR]
void turn_A(unsigned char directionA, int stepA)
{
static unsigned char i = 0;
unsigned int j = 0;
while(j <= stepA)
{
j++;
if(directionA)
{
i++;
}
else
{
i--;
}
if(i == 255)
{
i = 3;
}
else
if(i == 4)
{
i = 0;
}
PORTB = motor_stepA[i]; // [COLOR="#FF0000"]I connected the dimmers to PORTB.0 1 and 2. May this part affect those pulses?[/COLOR]
delay_ms(50);
}
}
PORTB = motor_stepA; [
PORTB = (PORTB & 0b10000111) | motor_stepA[i];
First, during the execution, the external interrupt signal is suddenly changing from 5 Volt into around 2 Volt and stay at 2 Volt as shown by the picture below:
is it the same when you zoom in?Second, when I tried to zoom out the oscilloscope, I got this:
I have no idea what this graph represents
is it the same when you zoom in?
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 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 #include <mega8535.h> #include <stdio.h> #include <delay.h> #include <stdint.h> #define ALEFT 0 #define ARIGHT 1 #define BLEFT 0 #define BRIGHT 1 #define CLEFT 0 #define CRIGHT 1 unsigned char buf[10]; volatile unsigned int ldr1; volatile unsigned int ldr2; volatile unsigned int ldr3; unsigned int count = 0; volatile unsigned int x = 280; volatile unsigned int y = 280; volatile unsigned int z = 280; unsigned int motor_stepA[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000}; unsigned int motor_stepB[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000}; unsigned int motor_stepC[4] = {0b00101000, 0b00110000, 0b01010000, 0b01001000}; // Alphanumeric LCD functions #include <lcd.h> #asm .equ __lcd_port=0x15 ;PORTC #endasm // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) { // Start Timer 0 TCCR0 = 0x02; } // Timer 0 overflow interrupt service routine interrupt [TIM0_OVF] void timer0_ovf_isr(void) { // Local variables initialization static unsigned int pulse_flag = 0; // Reinitialize Timer 0 value TCNT0=0xD3; // Increment count every 50 us count++; // Front lamp if(count == x) { PORTB.0 = 0; } else if(count == x + 1) { PORTB.0 = 1; pulse_flag++; } // Middle lamp if(count == y) { PORTB.1 = 0; } else if(count == y + 1) { PORTB.1 = 1; pulse_flag++; } // Back lamp if(count == z) { PORTB.2 = 0; } else if(count == z + 1) { PORTB.2 = 1; pulse_flag++; } // Stop timer when all pulses are completed if(pulse_flag >= 3) { // Clear flag pulse_flag = 0; // Clear count count = 0; // Stop the timer TCCR0 = 0; // Reset the timer TCNT0 = 0; // Check ADC } } #define ADC_VREF_TYPE 0x40 // Read the AD conversion result unsigned int read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } void turn_A(unsigned char directionA, int stepA) { static unsigned char i = 0; unsigned int j = 0; while(j <= stepA) { j++; if(directionA) { i++; } else { i--; } if(i == 255) { i = 3; } else if(i == 4) { i = 0; } PORTB = (PORTB & 0b10000111) | motor_stepA[i]; // PORTB.0 1 2 and 7 as output delay_ms(50); } } void turn_B(unsigned char directionB, int stepB) { static unsigned char k = 0; unsigned int l = 0; while(l <= stepB) { l++; if(directionB) { k++; } else { k--; } if(k == 255) { k = 3; } else if(k == 4) { k = 0; } PORTD = (PORTD & 0b10000111) | motor_stepB[k]; // PORTD.0 1 2 as input delay_ms(50); } } void turn_C(unsigned char directionC, int stepC) { static unsigned char m = 0; unsigned int n = 0; while(n <= stepC) { n++; if(directionC) { m++; } else { m--; } if(m == 255) { m = 3; } else if(m == 4) { m = 0; } PORTA = (PORTA & 0b10000111) | motor_stepC[m]; // PORTA.0 1 2 as input delay_ms(50); } } void display_adc() { ldr1=read_adc(0); lcd_gotoxy(0,0); sprintf(buf,"L1:%d%d%d%d",ldr1/1000,(ldr1%1000)/100,(ldr1%1000%100)/10,ldr1%1000%100%10); lcd_puts(buf); ldr2=read_adc(1); lcd_gotoxy(9,0); sprintf(buf,"L2:%d%d%d%d",ldr2/1000,(ldr2%1000)/100,(ldr2%1000%100)/10,ldr2%1000%100%10); lcd_puts(buf); ldr3=read_adc(2); lcd_gotoxy(0,1); sprintf(buf,"L3:%d%d%d%d", ldr3/1000,( ldr3%1000)/100,( ldr3%1000%100)/10, ldr3%1000%100%10); lcd_puts(buf); } void change_intensity() { static unsigned int motor_flagA = 0; static unsigned int motor_flagB = 0; static unsigned int motor_flagC = 0; // If light intensity below the setpoint, open blinds, and shorten the firing delay if((ldr1 <= 300) && (motor_flagA < 1)) { turn_A(ARIGHT,23); motor_flagA++; } #asm("cli") if((PORTB.0 == 1) && (ldr1 <= 300) && (motor_flagA == 1)) { if(x > 1) { x = x - 1; } } // If light intensity greater than the setpoint, increase the firing delay, and close blinds if((PORTB.0 == 1) && (ldr1 >= 350)) { if(x < 280) { x = x + 1; } } #asm("sei") if((ldr1 >= 350) && (x == 280) && (motor_flagA == 1)) { turn_A(ALEFT,23); motor_flagA = 0; } // If light intensity below the setpoint, open blinds, and shorten the firing delay if((ldr2 <= 300) && (motor_flagB < 1)) { turn_B(BRIGHT,23); motor_flagB++; } #asm("cli") if((PORTB.1 == 1) && (ldr2 <= 300) && (motor_flagB == 1)) { if(y > 1) { y = y - 1; } } // If light intensity greater than the setpoint, increase the firing delay, and close blinds if((PORTB.1 == 1) && (ldr2 >= 350)) { if(y < 280) { y = y + 1; } } #asm("sei") if((ldr2 >= 350) && (y == 280) && (motor_flagB == 1)) { turn_B(BLEFT,23); motor_flagB = 0; } // If light intensity below the setpoint, open blinds, and shorten the firing delay if((ldr3 <= 300) && (motor_flagC < 1)) { turn_C(CRIGHT,23); motor_flagC++; } #asm("cli") if((PORTB.2 == 1) && (ldr3 <= 300) && (motor_flagC == 1)) { if(z > 1) { z = z - 1; } } // If light intensity greater than the setpoint, increase the firing delay, and close blinds if((PORTB.2 == 1) && (ldr3 >= 350)) { if(z < 280) { z = z + 1; } } #asm("sei") if((ldr3 >= 350) && (z == 280) && (motor_flagC == 1)) { turn_C(CLEFT,23); motor_flagC = 0; } } void main(void) { // Input/Output Ports initialization PORTA=0x00; DDRA=0xF8; PORTB=0x07; DDRB=0xFF; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0xF8; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: 1500.000 kHz // Mode: Normal top=0xFF // OC0 output: Disconnected TCCR0 = 0x02; TCNT0 = 0xD3; OCR0 = 0x00; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Falling Edge GICR|=0x40; MCUCR=0x02; MCUCSR=0x00; GIFR=0x40; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x01; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x84; SFIOR&=0xEF; // Alphanumeric LCD initialization // Characters/line: 16 lcd_init(16); // Global enable interrupts #asm("sei") while (1) { display_adc(); change_intensity(); } }
PORTD = (PORTD & [COLOR="#FF0000"]0b00000000[/COLOR]) | motor_stepB[k]; // PORTD.0 1 2 as input
PORTA = (PORTA & [COLOR="#FF0000"]0b00000000[/COLOR]) | motor_stepC[m]; // PORTA.0 1 2 as input
Uhhhmmmm.....
AND operator is used to see if a bit is set or clear
Then OR operator is used to set bit 0 in first condition and then store the result back into it.
PORTB = (PORTB & 0b10000111) | motor_stepA[i]; // if PORTB is cleared then the first condition gives zero
PORTB = 0 | motor_stepA[i]; // OR operator will always true whenever one or both of the condition contain 1
NoAm I right?
PORTB = (PORTB & 0b10000111) | motor_stepA[i]
Not yet ;-)Hope I'am right now?
It is exactly the opposite, you want to clear the motor pin bits 3 4 5 6 and preserve the existing value to the other bits 0 1 2 and 7I connected the motors to PORTB. 3 4 5 6, which means I need to clear the bits that I don't need (0 1 2 and 7).
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?