codevision AVR + Proteus problem

Thread Starter

huy1999

Joined Apr 5, 2021
1
I have a problem with the increase and decrease of the motor speed of the circuit, please help me because this is my graduation thesis. can you review and help me with the problem of speed reduction?
1628694653697.png

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
#include <mega8.h>
#define C0 PORTC.3
#define C1 PORTC.4
#define C2 PORTB.2
#define C3 PORTB.3
#define C4 PORTB.4
#define C5 PORTB.5
#define C PINC.0
#define B PINC.1
#define A PINC.2
#define Stop PIND.3
#define Back PIND.4
#define Foward PIND.5
//------------------------------------------------------------------
unsigned char a; int h;
//-------------------------------------------------------------------------
void CW(void)
{
//Phase 6: 001 // 010 010
if (((C==0)&&(B==0)&&(A==1))==1){C5=0;C4=0;C3=1; C2=0;C1=0;C0=1;};
//Phase 4: 010 // 001 001
if (((C==0)&&(B==1)&&(A==0))==1){C5=0;C4=1;C3=1; C2=0;C1=0;C0=0;};
//Phase 5: 011 // 011 000
if(((C==0)&&(B==1)&&(A==1))==1){C5=1;C4=0;C3=0; C2=1;C1=0;C0=0;};
//Phase 2: 100 // 100 100
if (((C==1)&&(B==0)&&(A==0))==1){C5=0;C4=0;C3=0; C2=1;C1=1;C0=0;};
//Phase 1: 101 // 000 110
if (((C==1)&&(B==0)&&(A==1))==1){C5=1;C4=0;C3=0; C2=0;C1=0;C0=1;};
//Phase 3: 110 // 100 001
if (((C==1)&&(B==1)&&(A==0))==1){C5=0;C4=1;C3=0; C2=0;C1=1;C0=0;};
}
void CCW(void)
{
//ccw
//Phase 6: 001 // 100 001
if (((C==0)&&(B==0)&&(A==1))==1){C5=0;C4=0;C3=0; C2=1;C1=1;C0=0;};
//Phase 4: 010 // 000 110
if (((C==0)&&(B==1)&&(A==0))==1){C5=1;C4=0;C3=0; C2=1;C1=0;C0=0;};
//Phase 5: 011 // 100 100
if (((C==0)&&(B==1)&&(A==1))==1){C5=0;C4=1;C3=1; C2=0;C1=0;C0=0;};
//Phase 2: 100 // 011 000
if (((C==1)&&(B==0)&&(A==0))==1){ C5=0;C4=0;C3=1; C2=0;C1=0;C0=1;};
//Phase 1: 101 // 001 001
if (((C==1)&&(B==0)&&(A==1))==1){C5=0;C4=1;C3=0; C2=0;C1=1;C0=0;};
//Phase 3: 110 // 010 010
if (((C==1)&&(B==1)&&(A==0))==1){C5=1;C4=0;C3=0; C2=0;C1=0;C0=1;};
}
void STOP(void)
{ C5=0;C4=0;C3=0; C2=0;C1=0;C0=0;}
// External Interrupt 0 service routine
//----------------------------------------------------------
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
STOP(); while(Stop==1);
}
#define FIRST_ADC_INPUT 0
#define LAST_ADC_INPUT 5
unsigned char adc_data[LAST_ADC_INPUT-FIRST_ADC_INPUT+1];
#define ADC_VREF_TYPE 0x20
// ADC interrupt service routine
// with auto input scanning
interrupt [ADC_INT] void adc_isr(void)
{
unsigned char input_index=0;
// Read the 8 most significant bits
// of the AD conversion result
adc_data[input_index]=ADCH;
// Select next ADC input
if (++input_index > (LAST_ADC_INPUT-FIRST_ADC_INPUT))
input_index=0;
ADMUX=(FIRST_ADC_INPUT|ADC_VREF_TYPE)+input_index;
// Start the AD conversion
ADCSRA|=0x40;
}
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
// Place your code here
a=adc_data[5];
h=a;
OCR1A=100*h;
}
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=In
// State7=T State6=T State5=0 State4=0 State3=0 State2=0 State1=0 State0=T
PORTB=0x00;
DDRB=0x3E;
// Port C initialization
// Func6=In Func5=In Func4=Out Func3=Out Func2=In Func1=In Func0=In
// State6=T State5=T State4=0 State3=0 State2=T State1=T State0=T
PORTC=000;
DDRC=0x18;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 12000.000 kHz
// Mode: Fast PWM top=ICR1
// OC1A output: Non-Inv.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x82;
TCCR1B=0x19;
TCNT1H=0x00;
TCNT1L=0x00;
//ICR1H=0x00;
//ICR1L=0x00;
ICR1=20000; //T
//OCR1AH=0x00;
//OCR1AL=0x00;
OCR1A=10000; //khoi tao
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 1500.000 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x02;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
GICR|=0x40;
MCUCR=0x03;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;
// Analog Comparator initializationt
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 187.500 kHz
// ADC Voltage Reference: AREF pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=FIRST_ADC_INPUT|ADC_VREF_TYPE;
ADCSRA=0xCE;
// Global enable interrupts
#asm("sei")
while (1)
{
// Place your code here
if(Foward==1)CW();
if(Back==1) CCW();
};
}
 

click_here

Joined Sep 22, 2020
548
It would be better if you placed your code in code blocks

(Remove space)

[ code] ... [ /code]

Global variables in ISRs should be volatile
 

crutschow

Joined Mar 14, 2008
34,450
Not clear as to the problem.
Is it that the motor speed varies with time, i.e. the control loop is not stable?

What is the exact purpose of the circuit?
 

nsaspook

Joined Aug 27, 2009
13,275
Your BLDC motor trapezoidal drive speed (H-bridge sequence) should be a factor of the timer counter that gets set with a value from the ADC result. Make sure the timer actually changes the time between CW() or CCW() function calls.
 
Top