-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
154 lines (120 loc) · 2.54 KB
/
main.c
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
#define F_CPU 8000000UL
#include <avr/io.h>
#include "avr/sfr_defs.h"
#include "USART_128.h"
#include <avr/interrupt.h>
#include <util/delay.h>
#define PWM_FL OCR1A
#define PWM_BL OCR1B
#define PWM_CL OCR1C
#define PWM_FR OCR3A
#define PWM_BR OCR3B
#define PWM_CR OCR3C
#define DIR_DDR DDRC
#define DIR_PORT PORTC
#define DIR_FL PINC0
#define DIR_BL PINC1
#define DIR_CL PINC2
#define DIR_FR PINC3
#define DIR_BR PINC4
#define DIR_CR PINC5
void forward(void);
void backward(void);
void sharp_left(void);
void sharp_right(void);
void left_Turn(void);
void right_Turn(void);
void stop(void);
char m;
int main(void)
{ DDRA=0XFF;
DDRE=0xFF;
DDRB=0xFF;DDRC=0xFF;
DIR_DDR = 0xFF; //direction pin
sei();
USART_Init(51,1); //9600 baud
USART_InterruptEnable(1);
USART_Init(51,1);
USART_InterruptEnable(1);
TCCR1A |= (1<<WGM11)|(1<<COM1A1)|(1<<COM1B1)|(1<<COM1C1); //Fast PWM,Non Inverting mode
TCCR1B |=(1<<WGM12)|( 1<<WGM13)|(1<<CS11);
TCCR3A |=( 1<<WGM31)|(1<<COM3A1)|(1<<COM3B1)|(1<<COM3C1);//Fast PWM ,Non Inverting mode
TCCR3B |=(1<<WGM32)|( 1<<WGM33)|(1<<CS31);
ICR1 =10000;
ICR3 =10000;
while (1)
{
}
}
ISR(USART1_RX_vect)
{
m = USART_Receive(1);
//USART_Transmitchar(m,0);
switch (m)
{
case 'F':
forward();
break;
case 'B':
backward();
break;
case 'R':
sharp_right();
break;
case 'L':
sharp_left();
break;
case 'q' :
PWM_BL = 10000;
PWM_CL = 10000;
PWM_FL = 10000;
PWM_BR = 10000;
PWM_CL = 10000;
PWM_CR = 10000;
break;
default:
stop();
}
if (m>='0' && m<='9')
{
PWM_BL = 10000*((m-'0')/10.0);
PWM_CL = 10000*((m-'0')/10.0);
PWM_FL = 10000*((m-'0')/10.0);
PWM_BR = 10000*((m-'0')/10.0);
PWM_CR = 10000*((m-'0')/10.0);
PWM_FR = 10000*((m-'0')/10.0);
}
USART_TransmitNumber(PWM_BL,1);
USART_Transmitchar(0x0D,1);
USART_TransmitNumber(DIR_PORT,1);
USART_Transmitchar(0x0D,1);
}
void forward(void)
{
DIR_PORT= 0b00000000;
USART_TransmitString("FORWARD",1);
}
void backward(void)
{
DIR_PORT = 0b11111111;
USART_TransmitString("BACKWARD",1);
}
void sharp_right(void)
{
DIR_PORT = (1<<DIR_CR)|(1<<DIR_BR)|(1<<DIR_FR);
USART_TransmitString("SHRAP RIGHT",1);
}
void sharp_left(void)
{
DIR_PORT = (1<<DIR_CL) | (1<<DIR_BL) |(1<<DIR_FL);
USART_TransmitString("SHARP LEFT",1);
}
void stop(void)
{
PWM_BL = 0;
PWM_CL = 0;
PWM_FL = 0;
PWM_BR = 0;
PWM_CL = 0;
PWM_CR = 0;
}