למשל להלן תוכנה שלי שעושה דברים אחד אחרי השני שאני כתבתי לבד,
הוספתי הערות באנגלית כי הקומפיילר שלי הופך עברית אז אם אתם רואים שגיאות כתיב אתכם הסליחה
המערך what2do שולט על פעולת הרובוט.
המערך howlong קובע את זמן הפעולה בשניות.
PCCAP1H שולט על מהירות המנוע הימני,
PCCAP2H שולט על מהירות המנוע השמאלי.
שימוש במעבד של ATMEL: at89c5131.
בP1 מחוברים המנועים (עם גשר או בלי איך שבא לכם):
P1_0 מחובר הכיוון של המנוע.
P1_1 מחובר הכיוון של המנוע השני.
P1_4 מחובר האפשר של המנוע
P1_5 מחובר האפשר של המנוע השני.
ב P3_2 מחובר לחצן או מה שבא לכם שיבדוק שהרובוט פגע בקיר.
לקח לי בערך חצי שעה לכתוב את התוכנית ואת הרובוט קצת יותר (לוקח לי הרבה זמן להשיג חלקים
והקוד שלי:
- קוד: בחר הכל
#include <at89c5131.h>
unsigned char POINTER; //The Position Of the ToDo List
unsigned char what2do[5]; //What to do
unsigned char howlong[5]; //How Long to do it
unsigned char PCCAP1H[5]; //Speed (PWM) Of motorA
unsigned char PCCAP2H[5]; //Speed (PWM) Of motorB
#define farward 0x3
#define backward 0x0
#define left 0x1
#define right 0X2
void external1 (void) interrupt 0{ //What To do While Interrupt
//**************************************************************
// Here We Write New Commands
// Because That The Robot Has
// Saw A Wall
//**************************************************************
TR0=0; //cancel timer0
POINTER=1; //Set Pointer To Start
TH0=0X84; //Restart the counter to start point
TL0=0X80; //Restart the counter to start point
what2do[POINTER]=backward; //set motors to go backward
howlong[POINTER]=0x01; //For 1 sec
PCCAP1H[POINTER]=210; //PWM1 = 210
PCCAP2H[POINTER]=210; //PWM2 = 210
POINTER++; //inc pointer to write the next command
what2do[POINTER]=left; //set motors to go left
howlong[POINTER]=0x02; //For 2 sec
PCCAP1H[POINTER]=210; //PWM1 = 210
PCCAP2H[POINTER]=210; //PWM2 = 210
POINTER++; //inc pointer to write the next command
what2do[POINTER]=farward; //set motors to go farward
howlong[POINTER]=0xff;; //For endless time
PCCAP1H[POINTER]=190; //PWM1 = 190
PCCAP2H[POINTER]=190; //PWM2 = 190
POINTER=1; //set pointer back to the start point
TR0=1; //enable timer0
return;}
void Enable_Timer0(void){ //Enabled Timer0
//**************************************************************
// Here We Enable the timers
//**************************************************************
TMOD=0x01; //mode1 - counters
EA=1; //Enable All
ET0=1; //Enable Timer0
TH0=0X84; //TimerO Start Point High
TL0=0X80; //TimerO Start Point LOW
TR0=1; //TIMER0 RUN
EX0=1; //External Interrupt0 enable
return;}
void init_pwm(){
//**************************************************************
// Here We Enable the PWM
// (pulse width modulation)
//**************************************************************
CCON= 0x40;
CCAPM1=0x42; //PWM1=1, ECOM1=1
CCAPM2=0x42; //PWM2=1, ECOM2=
return;}
void Timer (void) interrupt 1{ //Timer0 Function
//**************************************************************
// External interrupt0 code.
// do what the arry "what2do"
// tell us to do, for "howlong"
// seconds
//**************************************************************
static unsigned char count=0;
TR0=0; //Disable timer 0
TH0=0X84; //reload timer0 high for 1 sec
TL0=0X80; //reload timer0 low for 1 sec
count++;
if(count==0x1E){ // if it count 0x1E times 0X8480 (for 1 sec delay) then
if(howlong[POINTER]<0xff){ //if its not endelss then
howlong[POINTER]--;} //count -1 sec
if(howlong[POINTER]<=0){ //if it finished to count seconds that we write in howlong[POINTER] then
POINTER++; //INC the pointer
CCAP1H=PCCAP1H[POINTER]; //reload PWM (control the motorA speed)
CCAP2H=PCCAP2H[POINTER]; //reload PWM (control the motorB speed)
}
count=0;} //reCount
//P2=howlong[POINTER]; //TEST POINT!
TR0=1; //Enable Timer0
return;}
void MOTORS(unsigned char direc){
//**************************************************************
// Function that controll the motors
// left or right.
//**************************************************************
if((direc&0x01)==1){
P1_0=1;}
else if((direc&0x01)==0){
P1_0=0;}
if((direc&0x02)==2){
P1_1=1;}
else if((direc&0x02)==0){
P1_1=0;}
return;}
void main(void){
//**************************************************************
// Function that controll the motors
// left or right.
//**************************************************************
Enable_Timer0(); //enable timer0
init_pwm(); //enable PWM to control motor speed
POINTER=0; //set the pointer to the start
what2do[POINTER]=farward; //set command to go farward
howlong[POINTER]=0xff; //for enless time
PCCAP1H[POINTER]=190; //speed controller motorA
PCCAP2H[POINTER]=190; //speed controller motorB
CCAP1H=PCCAP1H[POINTER]; //speed controller motorA
CCAP2H=PCCAP2H[POINTER]; //speed controller motorB
while(1){
MOTORS(what2do[POINTER]);} //Set the motor directions
}
נורא נורא מקווה שאהבתם אותו ואשמח לתגובות (אני חדש פה
אם תאהבו אותו או שיהיו תגובות או בקשות אשמח לכתוב קודים נוספים.
ד"א,
אחרי קימפול התוכנה תופסת 346 בתים, אם יש לכם עצה יעילה שתפחית את הקוד אז אחלה



