-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
134 lines (119 loc) · 2.58 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
#include <stm32f10x_lib.h>
#include "sys.h"
#include "delay.h"
#include "stdio.h"
#include "init.h"
u8 USART_RX_BUF[256]; //RXT buffer maxium 255bytes
u8 USART_RX_STA=0; //RX Compleate
u8 USART_RX_CNT=0; //RX Entre Count
u16 echo=0x7855;
u16 adcx1,adcx2,adcx3,adcx4,Gyro_raw,Batt;
u8 cntt=1;
char pussy='b';
int c,d,e,f;
char mem=1;
int main(void)
{
Stm32_Clock_Init(9); //system clock init always init first
IO_Init();
delay_init(72); //delay init
uart_init(72,115200); //uart init
PWM4_Init(150,18000); // ultra ping sig
// PWM2_2Init(100,1800); //count to 100, freq/1800 //IR aqusition pulse
Timer3_Init(65535,256); //time refrence for echo/ultrasonic
// setup 5 for motors?
Timer2_Init(65535,10); // motor try1
motor_Init();
EXTIX_Init();
// Adc_Init();
// LED0_PWM_VAL2_2=0x44;
// LED0_PWM_VAL2_1=0x34;
LED0_PWM_VAL4=0x34;
IRO=0;//turn on IR LEDs
while(1)
{
printf("adcx1\tadcx2\tadcx3\tadcx4\tGyro\tBatt\techo\n");
printf("%u\t%u\t%u\t%u\t%u\t%u\t%u",adcx1,adcx2,adcx3,adcx4,Gyro_raw,Batt,echo);
printf("\n");
// USART1->DR=echo/0xff;
// while((USART1->SR&0X40)==0);//wait for transfer to compleate
LED0=!LED0;
delay_us(100000);//wait for the end of transmission
}
}
void EXTI0_IRQHandler(void)
{
EXTI->PR=1<<0; //Clear Interrupt flag
// delay_us(50);
if(PBin(0)==1&&mem==0)
{
TIM3->CR1|=0x01; //start timer
mem=1;
}
else if(PBin(0)==0&&mem==1)
{
TIM3->CR1&=0xfe;//stop timer
echo=TIM3->CNT; //read value
TIM3->CNT=0x00; //clear timer
mem=0;
}
}
void USART1_IRQHandler(void)
{
u8 res;
if(USART1->SR&(1<<5))//RXT recieved
{
res=USART1->DR; //read out data and clear interrupt flag
// LED0_PWM_VAL=res;
if(!USART_RX_STA)
{
USART_RX_BUF[USART_RX_CNT++]=res;
if(USART_RX_CNT==255)USART_RX_CNT=0;//RXT data over flow,start over
}
}
}
void TIM2_IRQHandler(void)
{
if(TIM2->SR&0X0001)//Overflow Interrupt
{
CDSTEP=!CDSTEP;
ABSTEP=!ABSTEP;
}
TIM2->SR&=~(1<<0);//Clear Interrupt flag
}
void ADC1_2_IRQHandler(void)
{
switch (cntt){
case 1:
adcx1=ADC1->DR;
ADC1->DR=0;
break;
case 2:
adcx2=ADC1->DR;
ADC1->DR=0;
break;
case 3:
adcx3=ADC1->DR;
ADC1->DR=0;
break;
case 4:
adcx4=ADC1->DR;
ADC1->DR=0;
break;
case 5:
Gyro_raw=ADC1->DR;
ADC1->DR=0;
break;
case 6:
Batt=ADC1->DR;
ADC1->DR=0;
break;
}
cntt++;
if(cntt==7)
{
cntt=1;
// IRO=1;
}
// IRO=!IRO;//defult 1
}