Minervamcgonall
Newbie level 3
i m working on line follower robot with 3 sensors using at89c2051. but main circuit i picked is from this link
i wrote the code in keil uvision but problem is that i want to generate pwm for 12V dc motor speed control. here the code is
i wrote the code in keil uvision but problem is that i want to generate pwm for 12V dc motor speed control. here the code is
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 #include <REG2051.H> unsigned char flag,high,low,speedright,speedleft,i,rout,lout,pwmr_width,pwml_width; void Start(void); void Run(void); void Turnright(void); void Turnleft(void); void forward(char); void reverse(char); void delay(void); void pwmr_setup(void); void pwml_setup(void); #define rout P1_0 #define lout P1_1 void main() { P1=0x40; P3=0xff; high = 80; low = 30; speedright=160; speedleft=160; Start(); while(1) { P3=0x0f; Run(); } } void Start() { char exit,key; exit =1; while(exit) {key=P1_6; if(key==0) {exit=0;} } } void Run() { char left,right,mid; left=P3_0; right=P3_7; mid=P3_1; if(left==1 ) { Turnright(); } if(right==1) { Turnleft(); } if(mid==1) { forward(high); } if((left==0) && (right==0) && (mid==0)) { reverse(low);} if ((left==1) && (right==1) && (mid==1)) {forward(low);} } void forward(char speed) { P1=0x17; speedright=speed+10; speedleft=speed+10; delay(); } void Turnright() { P1=0x27; speedright=low+5; speedleft=low; delay(); } void Turnleft() { P1=0x1b; speedright=low; speedleft=low+5; delay(); } void reverse(char speed) { P1=0x2b; speedright=speed; speedleft=speed; delay(); } void delay() { unsigned char i,j; for(i=0;i<50;i++) for(j=0;j<50;j++); } // problem is here..i was using for loop for pwm but my teacher said to use timers. i did use timers but dont know how to link them with the program i wrote before. so plz i need help here. my project is due monday... void pwmr_setup(){ TMOD = 0; pwmr_width= speedright; EA = 1; ET0 = 1; TR0 = 1; } void pwml_setup(){ TMOD = 1; pwml_width=speedleft; EA = 1; ET1 = 1; TR0 = 1; } void timer0() interrupt 1 { if(!F0) { //Start of High level F0 = 1; //Set flag rout = 1; //Set PWM o/p pin TH0 = pwmr_width; //Load timer TF0 = 0; //Clear interrupt flag return; //Return } else { //Start of Low level F0 = 0; //Clear flag rout = 0; //Clear PWM o/p pin TH0 = 255 - pwmr_width; //Load timer TF0 = 0; //Clear Interrupt flag return; //return } } void timer1() interrupt 1 { if(!F1) { //Start of High level F1 = 1; //Set flag lout = 1; //Set PWM o/p pin TH1 = pwml_width; //Load timer TF0 = 0; //Clear interrupt flag return; //Return } else { //Start of Low level F1 = 0; //Clear flag lout = 0; //Clear PWM o/p pin TH1 = 255 - pwml_width; //Load timer TF0 = 0; //Clear Interrupt flag return; //return } }
Last edited by a moderator: