0% found this document useful (0 votes)
123 views22 pages

3.3.2.3 Programming Code in C Language

The document contains C programming code for controlling a mobile robot and its functions. It includes code for initializing ports and pins, reading button input, and controlling motors to move the robot forward, backward, and turn based on the button pressed. It also contains code for functions like delay, reading sensor input, and turning motors at different speeds to allow for smooth or sharp turns.

Uploaded by

ashwin jackson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
123 views22 pages

3.3.2.3 Programming Code in C Language

The document contains C programming code for controlling a mobile robot and its functions. It includes code for initializing ports and pins, reading button input, and controlling motors to move the robot forward, backward, and turn based on the button pressed. It also contains code for functions like delay, reading sensor input, and turning motors at different speeds to allow for smooth or sharp turns.

Uploaded by

ashwin jackson
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 22

3.3.2.3 Programming code in c language ;MOBILE OPERATED ROBOT by jagan and aswin; #include <avr i/o.h> #include<macros.

h> Void delay (unsigned int i) { unsigned int j; While (i--) For(j=o;j<=1000;j++); } Void main () { unsigned int k,h; DDRA=0x00; DDRD=0XFF; DDRB=0XFF; while (1) { k =~PINA; h=k & 0x0F;

PORTB=H; switch (h) { case 0x0D: //Forward (KEY Pressed - 2) { PORTD=0x89; break; } case 0x07: //Backward (KEY Pressed - 8) { PORTD=0x86; break; } case 0x0B: //Sharp Left Turn (KEY Pressed - 4) { PORTD=0x85; break; }

case 0x09: //Sharp Right Turn (KEY Pressed - 6) { PORTD=0x8A; break; } case 0x0A: //Stop (KEY Pressed - 5) { PORTD=0x00; break; } case 0x0E: //Smooth Left turn Forward (KEY Pressed - 1) { PORTD=0x81; break; } case 0x0C: //Smooth Right turn Forward (KEY Pressed - 3) { PORTD=0x88;

break; } case 0x08: //Smooth Left turn Backward (KEY Pressed - 7) { PORTD=0x82; break; } case 0x06: //Smooth Left turn Backward (KEY Pressed - 9) { PORTD=0x88; break; } } } }

main()

{ P1=0x40; P3=0xff; high = 80; low = 30; flag = 0; time = 50; Start(); while(1) { P3|= 0x0f; Run(); } }

Start() { char exit,key;

exit =1; while(exit) { key = P1; if((key & 0x40)==0) exit=0; } }

Run() { char sensors; sensors = (P3 &=0x0f);

if((sensors & 0x01)==0) { TurnRight(); flag = 1; }

else if((sensors & 0x08)==0) { TurnLeft(); flag = 2; }

else if(sensors == 0x09) { Forward(high); flag = 0; }

else if(((sensors==0x0b)||(sensors==0x0d))&&(flag==0)) Forward(low);

Forward(char speed) { P1=0x64; speedright = speed+10; speedleft = speed;

delay(time); }

TurnRight() { P1=0x68; speedright = low+5; speedleft = low; delay(time); }

TurnLeft() { P1=0x54; speedright = low+5; speedleft = low; delay(time);

Reverse(char speed) { P1=0x58; speedright = speed; speedleft = speed+5; delay(time); }

time() // update clock every 1 second { sec++; if ( sec >= 60) { sec = 0;

min++; if ( min >= 60) { min = 0; hour++; if ( hour >= 24) hour = 0; } } }

testOnOff() { if ((flag1 & 0x20) == 0) { if(hour == onHour1 && min == onMin1) {

flag1 |= 0x10; // disable blink output_high(LAMP); // on triac } if(hour == offHour1 && min == offMin1) { output_low(LAMP); // off triac flag1 |= 0x20; // disable further test on off flag1 &= ~0x10; // reenable blink } } }

compareTimeOn_Off() { if((flag1 & 0x40) != 0) // allow entering only after 8:00 has been set { testOnOff();

if(hour == onHour2 && min == onMin2) { flag1 |= 0x10; // disable further blink output_high(LAMP); // turn lamp on } if(hour == offHour2 && min == offMin2) { output_low(LAMP); // turn lamp off flag1 &= ~0x10; // reenable blink } } }

setTime() { if ((flag1 & 0x01) != 0) //input(KEY)==0) {

hour = setHour; min = setMin; sec = 0; flag1 |= 0x40; // enable compare time flag1 &= ~0x20; // reenable test on off flag1 &= ~0x01; // clear key press bit rate = 5; } }

blink() // turn LED on 100 ms { output_low(LED); delay_ms(100); output_high(LED); }

fireLED() { if ((flag1 & 0x10) == 0) // blink only triac is not turned on { temp++; if ( temp == rate) { blink(); temp = 0; } } }

chkKEY() { if(input(KEY)==0)

{flag1 |= 0x01; // set bit 0 telling key been pressed flag1 |= 0x10; // disable firing LED output_high(LAMP); // turn on lamp when press button } }

main() {

setup_counters(RTCC_INTERNAL,RTCC_DIV_32); // [32768/4]/32 = 256Hz

output_low(LAMP); output_high(LED); flag1 = 0; rate = 1; temp = 0;

tmr0 = 0; hour = 18; min = 0; sec = 0;

while(1) { while( tmr0 != 0) // while waiting 1sec elapsed check button also chkKEY(); // the following tasks executed every 1 second time(); compareTimeOn_Off(); fireLED(); setTime();

} }

// reload preset time value from preset array for each key pressed

set_timer() { if((flag1&1) == 0) // enter only when keys have been released { if((P3&0x04) == 0) { flag1 |= 1; if(index1>=9) index1 =0; timer1= preset_timer1[index1++]; } if((P3&0x08) == 0) { flag1 |= 1; if(index2>=9) index2 =0;

timer2= preset_timer2[index2++]; }

if((P3&0x10) == 0) { flag1 |= 1; if(index3>=13) index3 =0; timer3= preset_timer3[index3++]; } if((P3&0x20) == 0) { flag1 |= 1; if(index4>=8) index4 =0; timer4= preset_timer4[index4++]; }

} key_release() { if((P3&0x3c) == 0x3c) flag1 &= ~1; } run_timer() { if(++one_sec>=100) { one_sec = 0; run_timer1(); run_timer2(); run_timer3(); run_timer4(); shutdown(); // run shutdown checking every second

} run_timer1() { if(timer1 != -1) // enter only preset value != -1 { if(timer1 != 0) // enter only timer1 != 0 { timer7 = 0; // reset shutdown timeout buzzer1 = off; flag1 |= 0x02; setbit(output1)

if(++timer1_clk >= 60) // timer1 is one min based! { timer1_clk = 0; timer1--; }

} else // timer1 == 0 then fire output { clrbit(output1) // fire output1 buzzer1 = on; flag1 &= ~0x02; flag2 &= ~0x01; } } else { flag1 &= ~0x02; // no blink when timer1 == -1 flag2 &= ~0x01; buzzer1 = off; } }

You might also like