Cristinuk
Newbie level 3
I'm trying to control a dc motor(12 V) with two relays by using PWM. I've made the program(in MicroCfor PIC,because I'm using a PIC16F877A microcontroller) and the simulation in ISIS Proteus.
I have a problem, the motor doesn't run continously,it stops,then it starts again. I think it's something with PIC's frequency but I can't find the problem.
I've set the PWM frequency at 300 Hz...if I put it bigger,it doesn't work at all.The external crystal is set at 4 MHz.
I've attached also a picture with the simulation.
Here's the code:
I have a problem, the motor doesn't run continously,it stops,then it starts again. I think it's something with PIC's frequency but I can't find the problem.
I've set the PWM frequency at 300 Hz...if I put it bigger,it doesn't work at all.The external crystal is set at 4 MHz.
I've attached also a picture with the simulation.
Here's the code:
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 unsigned short current_duty, current_duty1; void InitMain() { ADCON1=7; //Configure pins as digital CMCON=0x07; //Disable comparators PORTA.RA0 = 0xff; PORTA.RA1 = 0xff; PORTA.RA2 = 0xff; PORTA.RA3 = 0xff; PORTA.RA4 = 0xff; TRISA.RA0 = 1; TRISA.RA1 = 1; TRISA.RA2 = 1; TRISA.RA3 = 1; TRISA.RA4 = 1; TRISA.RA5 = 0; TRISE.RE0 = 0; TRISE.RE1 = 0; TRISE.RE2 = 0; TRISC.RC0 = 0; // designate PORTC pins as output TRISC.RC1 = 0; TRISC.RC2 = 0; TRISC.RC5 = 0; TRISC.RC6 = 0; TRISC.RC7 = 0; TRISD.RD0 = 0; TRISD.RD1 = 0; TRISD.RD2 = 0; TRISD.RD3 = 0; TRISD.RD4 = 0; TRISD.RD5 = 0; TRISC.RC1 = 0; TRISB.RB0 = 1; TRISB.RB1 = 1; TRISB.RB2 = 1; // PORTB.RB0 = 0xff; // PORTB.RB1 = 0xff; // PORTB.RB2 = 0xff; PWM1_Init(300); // Initialize PWM1 module at 300Hz PWM2_Init(300); // Initialize PWM2 module at 300Hz } void main() { InitMain(); current_duty = 0; // initial value for current_duty current_duty1 = 0; PWM1_Start(); PWM2_Start(); // start PWM1 PWM1_Set_Duty(current_duty); // Set current duty for PWM1 // start PWM1 PWM2_Set_Duty(current_duty1); // Set current duty for PWM1 PORTA.RA5 = 0xff; PORTE.RE0 = 0xff; PORTE.RE1 = 0xff; PORTE.RE2 = 0xff; PORTC.RC0 = 0xff; PORTC.RC5 = 0x00; PORTC.RC6 = 0x00; PORTC.RC7 = 0x00; PORTD.RD0 = 0x00; PORTD.RD1 = 0x00; PORTC.RC1 = 0x00; while (1) // endless loop { if (RA0_bit) // button on RA0 pressed---> first place from the parking { PORTA.RA5=0x00; PORTC.RC5=0xff; PORTD.RD2=0x00; PORTD.RD3=0x00; current_duty=current_duty+1; // increment current_duty PWM1_Set_Duty(current_duty); } // touching the first microlimitator-----> decrease speed if (RB1_bit) { PORTD.RD2=0x00; PORTD.RD3=0x00; current_duty=current_duty-1; // decrement current_duty PWM1_Set_Duty(current_duty); } //touching the second microlimitator----> motor stops if (RB2_bit) { PWM1_Stop(); PORTC.RC2=0x00; } if (RA1_bit) // button on RA1 pressed -----> second place of parking { PORTE.RE0=0x00; PORTC.RC6=0xff; PORTD.RD2=0xff; PORTD.RD3=0xff; current_duty1=current_duty1+1; // increment current_duty PWM2_Set_Duty(current_duty1); } if (RB4_bit) { PORTD.RD2=0xff; PORTD.RD3=0xff; current_duty1=current_duty1-1; // decrement current_duty PWM2_Set_Duty(current_duty1); } //touching the second microlimitator----> motor stops if (RB6_bit) { PWM2_Stop(); PORTC.RC2=0x00; } if (RA2_bit) // button RA2 pressed-touching the third place of parking { PORTE.RE1=0x00; PORTC.RC7=0xff; PORTD.RD2=0x00; PORTD.RD3=0x00; current_duty=current_duty+1; PWM1_Set_Duty(current_duty); } if (RA3_bit) //button RA3 pressed -touching the fourth place of parking { PORTE.RE2=0x00; PORTD.RD0=0xff; PORTD.RD4=0x00; Delay_ms(3000); PORTD.RD5=0x00; Delay_ms(2000); PORTD.RD5=0xff; PORTD.RD4=0xff; } if (RA4_bit) //-touching the fifth place of parking // if (current_duty1<250) { PORTC.RC0=0x00; PORTD.RD1=0xff; PORTD.RD2=0xff; PORTD.RD3=0xff; current_duty1=current_duty1+1; PWM2_Set_Duty(current_duty1); } } }
Last edited by a moderator: