ZhenDangBo2024/ZDB2024/user/Src/user.c
2024-11-18 10:44:27 +08:00

503 lines
9.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* user.c
*
* Created on: Jun 24, 2024
* Author: 10425
*/
#include "user.h"
uint8_t usart_count = 0; //串口接收数据个数
uint8_t RxBuff[BUFF_LEN] = {0}; //串口接收缓冲区
uint8_t TXBuff[BUFF_LEN] = {0}; //串口发送缓冲区
uint8_t uBuff[BUFF_LEN] = {0};
uint16_t adcBuff[ADCBUFF_LEN] = {0};
uint16_t lineData[128];
uint8_t font[] ="电压脉宽频率单次重复";
enum CODER_MODE coder_mode = voltage_mode;
enum OUTPUT_MODE output_mode = none;
int16_t voltage = 0;
//单片机时钟频率60Mhz定时器3时钟分频3.定时器计数一次是50ns;
int16_t pulse = 2;
int16_t frequency = 1;
int16_t coder_number =0; //编码器值
uint8_t draw_flag =0; //曲线标记,之前的曲线已经被覆盖,才需要重新画
void user_init(void)
{
__HAL_UART_ENABLE_IT(&huart3,UART_IT_IDLE);
__HAL_UART_CLEAR_FLAG(&huart3,UART_FLAG_TC); //清除发送完成标志
__HAL_UART_CLEAR_IDLEFLAG(&huart3); //清除空闲中断标志
bsp_InitTimer();
Init_Key();
for(uint8_t i =0;i<128;i++)
{
lineData[i] = 0;
if(32<i && i<64)
{
lineData[i] = 0;
}
}
}
void coder_slave(void) //按键处理
{
static uint8_t flag_uinit = 0; //用于标记曲线的横坐标
if(coder_mode == voltage_mode)
{
int32_t temp;
voltage=voltage+coder_number;
if(voltage<0)voltage =0;
if(voltage>40)voltage =40;
LCD_setInt(0x0001,voltage);
temp = voltage*83.4*4095/3300;
if(temp>4095)temp=4095;
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, temp);
}
//调脉宽
else if(coder_mode == pulse_mode)
{
if(coder_number>0)
{
if(pulse<20)
{
pulse+=2*coder_number;
if(pulse>20)pulse=20;
}
else if(pulse<2000)
{
pulse+=20*coder_number;
if(pulse>2000)pulse =2000;
if(flag_uinit == 0)
{
uint8_t *a =(uint8_t*) " 50us";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =1;
}
}
if(pulse ==20)
{
uint8_t *a =(uint8_t*) "us";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
}
else if(coder_number<0)
{
if(pulse<=20 && pulse >2)
{
pulse+=2*coder_number;
if(pulse<2)pulse=2;
}
else if(pulse>20)
{
pulse+=20*coder_number;
if(pulse <20)pulse = 20;
if(pulse <=20)
{
if(flag_uinit == 1)
{
uint8_t *a =(uint8_t*) "500ns";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =0;
}
}
}
if(pulse ==18) //一个计数50ns
{
uint8_t *a =(uint8_t*) "ns";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
}
}
else if(coder_mode == frequency_mode)
{
frequency+=coder_number;
if(frequency<1)frequency =1;
else if(frequency>500)frequency =500;
LCD_setInt(0x0003,frequency);
__HAL_TIM_SET_AUTORELOAD(&htim2, 10000/frequency-1);
}
coder_number=0;
}
void key_slave(void) //按键处理
{
uint8_t key_code;
static uint8_t flag_uinit = 0; //用于标记曲线的横坐标
key_code = bsp_GetKey();
if(key_code == KEY_NONE)return;
switch(key_code)
{
//编码器按键
case KEY_BM4_DOWN:
LCD_setBell(100);
coder_mode++;
if(coder_mode>=enum_CodeNum)coder_mode = 0;
LCD_setFont(0x0020,coder_mode);
break;
//脉宽增加按钮,pulse计数一次50ns
case KEY_PULSE_UP_DOWN:
if(pulse<20)
{
pulse+=2;
}
else if(pulse<2000)
{
pulse+=200;
pulse = pulse/100*100;
if(flag_uinit == 0)
{
uint8_t *a =(uint8_t*) " 50us";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =1;
}
}
if(pulse ==20)
{
uint8_t *a =(uint8_t*) "us";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
break;
//脉宽减少按钮
case KEY_PULSE_DN_DOWN:
if(pulse<=20 && pulse >2)
{
pulse-=2;
}
else if(pulse>20)
{
pulse-=200;
if(pulse ==0)pulse = 20;
if(pulse <=20)
{
if(flag_uinit == 1)
{
uint8_t *a =(uint8_t*) "500ns";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =0;
}
}
}
if(pulse ==18)
{
uint8_t *a =(uint8_t*) "ns";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
break;
//频率加调节
case KEY_FREQ_UP_DOWN:
if(frequency<100)frequency +=10;
else if(frequency<500)
{
frequency +=50;
}
frequency = frequency/10*10;
LCD_setInt(0x0003,frequency);
__HAL_TIM_SET_AUTORELOAD(&htim2, 10000/frequency-1);
break;
//频率减
case KEY_FREQ_DN_DOWN:
if(frequency>100)frequency -=50;
else if(frequency>1)
{
frequency -=10;
}
frequency = frequency/10*10;
if(frequency == 0)frequency=1;
LCD_setInt(0x0003,frequency);
__HAL_TIM_SET_AUTORELOAD(&htim2, 10000/frequency-1);
break;
//异常检测开关按下
case KEY_ERR_DOWN:
HAL_GPIO_WritePin(OUTPUTCTL_GPIO_Port, OUTPUTCTL_Pin, GPIO_PIN_RESET);
break;
//异常检测开关抬起
case KEY_ERR_UP:
HAL_GPIO_WritePin(OUTPUTCTL_GPIO_Port, OUTPUTCTL_Pin, GPIO_PIN_SET);
break;
default:break;
}
}
static uint16_t head,Length,order; //帧头,指令长度,指令
void uart_slave(void) //串口处理
{
if(usart_count == 0)return;
head = uBuff[0]<<8 | uBuff[1];
Length = uBuff[2];
order = uBuff[3];
static uint8_t flag_uinit = 0; //用于标记曲线的横坐标
if(head == 0xA55A && Length == 6 && order == 0x83)
{
uint16_t key_code;
int32_t temp;
key_code = uBuff[7]<<8 | uBuff[8];
switch(key_code)
{
case 1: //电压减
if(voltage>0)voltage-=5;
voltage = voltage/5*5;
LCD_setInt(0x0001,voltage);
temp = voltage*83.4*4095/3300;
if(temp>4095)temp=4095;
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, temp);
break;
case 2: //电压加
if(voltage<40)voltage+=5;
voltage = voltage/5*5;
LCD_setInt(0x0001,voltage);
temp = voltage*83.4*4095/3300;
if(temp>4095)temp=4095;
HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, temp);
break;
case 3://脉宽减
if(pulse<=20 && pulse >2)
{
pulse-=2;
}
else if(pulse>20)
{
pulse-=200;
if(pulse ==0)pulse = 20;
if(pulse <=20)
{
if(flag_uinit == 1)
{
uint8_t *a =(uint8_t*) "500ns";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =0;
}
}
}
if(pulse ==18)
{
uint8_t *a =(uint8_t*) "ns";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
break;
case 4://脉宽加
if(pulse<20)
{
pulse+=2;
}
else if(pulse<2000)
{
pulse+=200;
pulse = pulse/100*100;
if(flag_uinit == 0)
{
uint8_t *a =(uint8_t*) " 50us";
LCD_setChar(0x0010, a,5);
HAL_Delay(1);
flag_uinit =1;
}
}
if(pulse ==20)
{
uint8_t *a =(uint8_t*) "us";
LCD_setChar(0x0005, a,2);
}
LCD_setInt(0x0002,Pulse_To_Time(pulse));
break;
case 5://频率减
if(frequency>100)frequency -=50;
else if(frequency>1)
{
frequency -=10;
}
frequency = frequency/10*10;
if(frequency == 0)frequency=1;
LCD_setInt(0x0003,frequency);
__HAL_TIM_SET_AUTORELOAD(&htim2, 10000/frequency-1);
break;
case 6://频率加
if(frequency<100)frequency +=10;
else if(frequency<500)
{
frequency +=50;
}
frequency = frequency/10*10;
LCD_setInt(0x0003,frequency);
__HAL_TIM_SET_AUTORELOAD(&htim2, 10000/frequency-1);
break;
case 7: //单次输出
if(output_mode != redo_mode)
{
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_2);
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2,pulse-1);
PWM_Start_Once();
}
else {
HAL_TIM_Base_Stop_IT(&htim2);
output_mode = none;
LCD_setFont(0x0030,3);
}
break;
case 8://重复输出
if(output_mode != redo_mode)
{
// HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_2);
// __HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2,pulse-1);
LCD_setFont(0x0030,4);
draw_flag =1;
if( pulse <= 20) //1us以内
{
for(uint8_t i =0;i<128;i++)
{
lineData[i] = 0;
if(32<i && i< 32+pulse*3.2)
{
lineData[i] = voltage;
}
}
LCD_setLine(lineData, 125);
}
else
{
for(uint8_t i =0;i<128;i++)
{
lineData[i] = 0;
if(32<i && i< 32+pulse*3.2/100)
{
lineData[i] = voltage;
}
}
LCD_setLine(lineData, 125);
}
__HAL_TIM_CLEAR_IT(&htim2, TIM_IT_UPDATE);
HAL_TIM_Base_Start_IT(&htim2);
output_mode = redo_mode;
}
else {
HAL_TIM_Base_Stop_IT(&htim2);
output_mode = none;
LCD_setFont(0x0030,3);
}
break;
default:break;
}
}
usart_count =0;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == BM1_Pin)
{
if(HAL_GPIO_ReadPin(BM1_GPIO_Port, BM1_Pin) == GPIO_PIN_RESET)
{
if(HAL_GPIO_ReadPin(BM2_GPIO_Port, BM2_Pin) == GPIO_PIN_SET)
{
coder_number++;
}
else if(HAL_GPIO_ReadPin(BM2_GPIO_Port, BM2_Pin) == GPIO_PIN_RESET)
{
coder_number--;
}
}
bsp_StartTimer(2, 100);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_2);
__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_2,pulse-1);
PWM_Start_Once();
}
}
uint16_t Pulse_To_Time(uint16_t pulse)
{
uint32_t a = pulse;
a = a*50;
if(a >= 1000)a =a/1000;
return (uint32_t)a;
}
void PWM_Start_Once()
{
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
bsp_StartTimer(1, 300);
if(draw_flag ==0)
{
draw_flag =1;
if( pulse <= 20) //1us以内
{
for(uint8_t i =0;i<128;i++)
{
lineData[i] = 0;
if(32<i && i< 32+pulse*3.2)
{
lineData[i] = voltage;
}
}
LCD_setLine_ISR(lineData, 125);
}
else
{
for(uint8_t i =0;i<128;i++)
{
lineData[i] = 0;
if(32<i && i< 32+pulse*3.2/100)
{
lineData[i] = voltage;
}
}
LCD_setLine_ISR(lineData, 125);
}
}
}
void bsp_RunPer10ms(void)
{
bsp_KeyScan10ms();
}