#include
#include"ldc1314.h"
#include"lcd12864cog.h"
//#include"rtc.h"
#include
unsigned int Just_ch0,Just_ch1,Just_ch2,Just_ch3;
unsigned int Now_ch0,Now_ch1,Now_ch2,Now_ch3;
unsigned int PULSE_NUMBER = 0;
unsigned int beep=0;
int FIVE_DIV_TURNS_NUMBER = 0; //One fifth of a circle
float TURNS_NUMBER = 0;
float CIRCUM = 0.2041; //The circumference of the tire, in m
float ONE_PULSE_DISTANCE = 0.2041/390;
float RT_DISTANCE =0; //Real-time distance
unsigned int half_s=0;
int RTC_SEC=0;
#define BEEP_ON P7OUT|=BIT0;
#define BEEP_OFF P7OUT&=~BIT0;
#define BEEP_DIR P7DIR|=BIT0;
#define Right1 Just_ch0-Now_ch0
#define Right2 Just_ch3-Now_ch3
#define Left1 Just_ch1-Now_ch1
#define Left2 Just_ch2-Now_ch2
#define A_PWM_MOTOR TA0CCR3 //P1.4
#define B_PWM_MOTOR TA0CCR4 //P1.5
#define MOTOR_PWM_DUTY TA0CCR0 //Cycle length
#define Normal_speed 3500
#define Turn_S_speed 3500
#define Turn_B_speed 2500
void initalRTC(void)
{
RTCCTL01 = RTCMODE + RTCBCD + RTCHOLD + RTCTEV_1;
RTCHOUR = 0x04;
RTCMIN = 0x30;
RTCSEC = 0x00;
RTCDAY = 0x01;
RTCMON = 0x01;
RTCYEAR = 0x2011;
RTCCTL01 &= ~RTCHOLD; //Start the real-time clock
RTCPS1CTL = RT1IP_5 + RT1PSIE;
RTCPS0CTL = RT0IP_7 + RT0PSIE;
RTCCTL0 |= RTCRDYIE + RTCTEVIE;
}
void A_MOTOR(A_DIR,A_SPEED)//P1.4
{
if(A_DIR==1)//Forward
{
P4OUT |= BIT1; P4OUT &= ~BIT2;
A_PWM_MOTOR = A_SPEED;
}
if(A_DIR==2)//reverse
{
P4OUT |= BIT2; P4OUT &= ~BIT1;
A_PWM_MOTOR = A_SPEED;
}
if(A_DIR==3)
{
P4OUT |= BIT1+BIT2;
}
}
void B_MOTOR(B_DIR,B_SPEED)//P1.5
{
if(B_DIR==1)//Forward
{
P4OUT |= BIT3; P3OUT &= ~BIT3;
B_PWM_MOTOR = B_SPEED;
}
if(B_DIR==2)//reverse
{
P3OUT |= BIT3; P4OUT &= ~BIT3;
B_PWM_MOTOR = B_SPEED;
}
if(B_DIR==3)
{
P4OUT |= BIT3; P3OUT |= BIT3;
}
}
void CAR_WORK_MODE(Status,A_SPEED,B_SPEED)
{
if(Status==1)//Go forward
{ A_MOTOR(1,A_SPEED); B_MOTOR(1,B_SPEED); }
if(Status==2)//Back
{ A_MOTOR(2,A_SPEED); B_MOTOR(2,B_SPEED); }
if(Status==3)//Turn left
{ A_MOTOR(1,A_SPEED); B_MOTOR(3,B_SPEED); }
if(Status==4)//Turn right
{ A_MOTOR(3,A_SPEED); B_MOTOR(1,B_SPEED); }
if(Status==5)//Stop
{ A_MOTOR(3,A_SPEED); B_MOTOR(3,B_SPEED); }
}
void L298N_IN14_INIT()
{
P4DIR|=BIT1+BIT2+BIT3; P3DIR|=BIT3;
P4OUT|=BIT1+BIT2+BIT3; P3OUT|=BIT3;
}
void MOTOR_PWM_SET(unsigned int PWM_DUTY)
{
TA0CCTL2 = CM_1 + SCS + CAP + CCIE; // rising edge capture, synchronous capture, capture mode, interrupt enable
P1DIR&=~BIT3;
P1SEL|=BIT3; //Rising edge input capture pin
P1DIR |= BIT4+BIT5;
P1SEL |= BIT4+BIT5; //Motor PWM output pin
MOTOR_PWM_DUTY = PWM_DUTY;
TA0CCTL3 = OUTMOD_7;
TA0CCTL4 = OUTMOD_7;
TA0CTL = TASSEL_2 + MC_1 + TACLR; //Increment count mode
}
void MOTOR_ENABLE()
{
P2DIR|=BIT7;
P2OUT|=BIT7; //Motor drive enable
}
void LCD_PORT_CONFIGRE()
{
cs_DIR; //Macro definition in LCD header file
cd_DIR;
reset_DIR;
sclk_DIR;
sda_DIR;
}
void LDC1314_FIRST_DATA()
{
LDC1314_INIT();
_delay_cycles(4000000); //Power-on delay to wait for LDC1314 to initialize and stabilize the first reading value
LDC1314_Read();
Just_ch0 = SENSOR_CH[0];
Just_ch1 = SENSOR_CH[1];
Just_ch2 = SENSOR_CH[2];
Just_ch3 = SENSOR_CH[3];
}
void TIMER_INIT(unsigned int TIME)//timing time = TIME/32768
{
P4DIR|=BIT7; //Timer display, LED on and off
TA1CCTL0 = CCIE;
TA1CCR0 = TIME;
TA1CTL = TASSEL_1 + MC_1 + TACLR;
}
void FIXED_DISAPPER()
{
display_string_8x16(1,70,"SES:");//time separator
display_string_8x16(5,70,"DST:");//time separator
}
int main(void)
{
WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer
MOTOR_PWM_SET(10000); //10KHz period
L298N_IN14_INIT();
TIMER_INIT(1000); //Timer time = TIME/32768
LDC1314_FIRST_DATA();
MOTOR_ENABLE();
BEEP_DIR;
BEEP_OFF;
LCD_PORT_CONFIGRE();
LCD_init();
delay(50);
clear_screen(); //Clear screen on power on
FIXED_DISAPPER();
initalRTC();
__bis_SR_register(GIE); //General interrupt is turned on
while(1)
{
if((Now_ch0>180)||(Now_ch1>180)||(Now_ch2>180)||(Now_ch3>180))
beep=1;
if((Now_ch0<170)&&(Now_ch1<170)&&(Now_ch2<170)&&(Now_ch3<170))
beep=2;
switch(beep)
{
case 1: BEEP_ON;break;
case 2: BEEP_OFF;break;
default :break;
}
if((Right1==0)&&(Left1==0 )&&( Right2==0)&&(Left2==0))
{
CAR_WORK_MODE(1,Normal_speed,Normal_speed);
}
//Inner channel-small curve turning mode
if(((Left1==1)||(Right1==1))&&(Left2==0)&&(Right2==0))
{
while((Left1==1)&&(Right1==0)&&(Left2==0)&&(Right2==0))
{
CAR_WORK_MODE(4,Turn_S_speed ,Turn_S_speed);
}
while((Right1==1)&&(Left1==0)&&(Left2==0)&&(Right2==0))
{
CAR_WORK_MODE(3,Turn_S_speed ,Turn_S_speed);
}
}
//Outer channel-large curve turning mode
if((Left2==1)||(Right2==1)&&(Left1==0)&&(Right1==0))
{
while((Left2==1)&&(Right2==0)&&(Left1==0)&&(Right1==0))
{
CAR_WORK_MODE(4,Turn_B_speed,Turn_B_speed);
while(Right1==0)
;//Wait for the turn to complete
}
while((Right2==1)&&(Left2==0)&&(Left1==0)&&(Right1==0))
{
CAR_WORK_MODE(3,Turn_B_speed,Turn_B_speed);
while(Left1==0)
;//Wait for the turn to complete
}
}
//When the coils on the same side are detected at the same time:
if(((Right1==1)&&(Right2==1))||((Left1==1)&&(Left2==1)))
{
while((Right1==0)&&(Right2==0))
CAR_WORK_MODE(3,Turn_B_speed,Turn_B_speed);
while((Left1==0)&&(Left2==0))
CAR_WORK_MODE(4,Turn_B_speed,Turn_B_speed);
}
}
}
#pragma vector=TIMER1_A0_VECTOR
__interrupt void TIMER1_A0_ISR(void)
{
P4OUT^=BIT7; //Timing indication
_delay_cycles(50);
LDC1314_Read();
Now_ch0 = SENSOR_CH[0]; //Read
Now_ch1 = SENSOR_CH[1]; //Right channel
Now_ch2 = SENSOR_CH[2]; //Right channel
Now_ch3 = SENSOR_CH[3]; //Read
}
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
PULSE_NUMBER++;
if(PULSE_NUMBER==195)//1/5 lap Full lap 390
{
PULSE_NUMBER = 0; // clear and restart counting
//FIVE_DIV_TURNS_NUMBER++;
TURNS_NUMBER++;
//Real-time display of driving distance:
RT_DISTANCE = TURNS_NUMBER*CIRCUM;
display_number_8x16(5,100,RT_DISTANCE,1); //Real-time distance, two effective decimal places
}
TA0CCTL2 &=~CCIFG; // Clear interrupt flag
}
#pragma vector=RTC_VECTOR
__interrupt void RTC_ISR(void)
{
switch (__even_in_range(RTCIV, RTC_RT1PSIFG))
{
case RTC_NONE:
break;
case RTC_RTCRDYIFG:
// __bic_SR_register_on_exit(LPM3_bits);
break;
case RTC_RTCTEVIFG:
break;
case RTC_RTCAIFG:
break;
case RTC_RT0PSIFG:
// P4OUT ^= BIT7;
break;
case RTC_RT1PSIFG:
// P1OUT ^= BIT0; //Generate a 0.5s signal and generate a 1s signal by doubling
half_s++;
while(half_s==2)
{
half_s=0;
RTC_SEC++;
// while(RTC_SEC>delay_time) //RTC module is placed before reading the value, the reading delay time needs to be subtracted
display_number_8x16(1,100,RTC_SEC,-1);
display_number_8x16(1,10,Now_ch0,-1);
}
break;
default:
break;
}
}
Previous article:MSP430F5529 LCD SPI communication protocol
Next article:Detailed explanation and selection of pull-up resistors and pull-down resistors for microcontrollers
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- I need help from an expert to look at the program and tell me how to modify it so that the buzzer can sound three times and then pause for 0.5 seconds before sounding again.
- MSP430G2755 Main Memory Bootloader UART Porting Guide
- McAsp multi-channel understanding
- Please help me look at this oscillator circuit
- About NCP1236 automatic recovery function test after removing short circuit and overload test
- Zigbee IoT module market: Differences between LTE standard Cat.1 and nbiot wireless communication modules
- ADS Installation Issues
- A Brief Discussion on FPGA Resource Evaluation
- [Atria Development Board AT32F421 Review] - Experience Sharing
- MCU newbie asks for help regarding data transfer instructions