采用STC12C5A60S2采集AD信号小车循迹
单片机源程序如下:
- #include <STC12C5A60S2.H>
- #include "intrins.h"
- #include "Common.h" //通用宏定义
- #include "delay.h" //通用延时程序
- #include "PWM_TIME.h" //模拟PWM
- #include "ADC.H"
- #define k1 0.1
- #define k2 0.1
- void AD_send();
- fp32 AD_chuli(uchar *s1, uchar *s2);
- fp32 Engine_PWM = Engine_Left;
- uchar a1 = 100, a2 = 100;
- int error;
- uchar flag;
- uchar flag1 = 0;
- uchar flag2;
- extern INT32 kk;
- void Init0_Init()
- {
-
- IT0 = 1; //set INT0 int type (1:Falling 0:Low level)
- EX0 = 1; //enable INT0 interrupt
- EA = 1; //open global interrupt switch
- }
- void main(void)
- {
- InitADC();
- InitUart();
- Init_PWM_TIME();
- Init0_Init();
- while(1)
- {
-
-
- if(flag == 1)
- {
- a1 = 0, a2 = 0;
- Engine_PWM = Engine_Mid;
- error = -150;
- }
-
- Engine_PWM = AD_chuli(&a1, &a2);
- //AD_send();
- change_Motor(a1,a2);
- change_Engine(Engine_PWM);
- }
- }
- void AD_send()
- {
- ShowResult(1); //show ChabneP10
- ShowResult(2); //P11
- ShowResult(3); //P12
- //ShowResult(4);
- //ShowResult(7);
- //ShowResult(6);
- Delay(30);
-
- }
- fp32 AD_chuli(uchar *s1, uchar *s2)
- {
- fp32 temp;
- error = GetADCResult(2) - GetADCResult(1);
- if(flag != 1)
- {
- if(GetADCResult(1) > GetADCResult(2))
- {
-
- Engine_PWM = k2 * error + Engine_Mid;
- *s1 = 100, *s2 = 100;
- flag2 = 1;
- if(error < -60)
- {
- //Engine_PWM = k2 * error + Engine_Mid;
- flag1 = 1;
- temp = Engine_PWM;
- *s1 = 100, *s2 = 100;
- }
-
-
- }
-
- else if(GetADCResult(2) > GetADCResult(1))
- {
-
-
- Engine_PWM = k2 * error + Engine_Mid;
- *s1 = 100, *s2 = 100;
- flag2 = 2;
- if(error > 60)
- {
- //Engine_PWM = k2 * error + Engine_Mid;
- flag1 = 2;
- temp = Engine_PWM;
- *s1 = 100, *s2 = 100;
- }
- }
- }
- if(GetADCResult(2) == 0 && GetADCResult(1) == 0 && GetADCResult(3) == 0)
- {
- if(flag != 1)
- {
-
- if(flag2 != 2)
- {
- if(flag1 == 1)
- {
- Engine_PWM = temp;*s1 = 100, *s2 = 100;
- }
- else
- {
- Engine_PWM = Engine_Left;
- }
- }
- else if(flag2 != 1)
- {
- if(flag1 == 2)
- {
- Engine_PWM = temp;*s1 = 100, *s2 = 100;
- }
- else
- {
- Engine_PWM = Engine_Right;
- }
- }
-
- }
- }
-
-
-
- if(Engine_PWM <= Engine_Left)
- {
- Engine_PWM = Engine_Left;
- }
- else if(Engine_PWM >= Engine_Right)
- {
- Engine_PWM = Engine_Right;
- }
-
-
- return Engine_PWM;
-
- }
- void Int0_Routine(void) interrupt 0
- {
-
- if(kk >= 100000)
- {
- flag = 1;
- }
- else
- {
- flag = 2;
- }
-
-
- }
- /* 中断服务程序的编号
- void Int0_Routine(void) interrupt 0; 外部中断0(INT0)
- void Timer0_Rountine(void) interrupt 1; 定时器0中断
- void Int1_Routine(void) interrupt 2; 外部中断1(INT1)
- void Timer1_Rountine(void) interrupt 3; 定时器1中断
- void UART_Routine(void) interrupt 4; 串口1(UART1)中断
- void ADC_Routine(void) interrupt 5; A/D转换中断
- void LVD_Routine(void) interrupt 6; 低压检测(LVD)中断
- void PCA_Routine(void) interrupt 7; PCA中断
- void UART2_Routine(void) interrupt 8; 串口2中断
- void SPI_Routine(void) interrupt 9 SPI中断
- */
复制代码
所有资料51hei提供下载:
小车控制.zip
(119.94 KB, 下载次数: 96)
|