- 打卡等级:初来乍到
- 打卡总天数:1
- 最近打卡:2025-03-24 11:56:26
已绑定手机
新手上路
- 积分
- 12
|
#include "config.h"
#include "STC32G_Exti.h"
#include "STC32G_GPIO.h"
#include "STC32G_UART.h"
#include "STC32G_NVIC.h"
#include "STC32G_Delay.h"
#include "STC32G_Switch.h"
#include "STC32G_Timer.h"
#include "STC32G_PWM.h"
#include "type_def.h"
sbit INT0 = P3^2;
sbit INT1 = P3^3;
sbit INT2 = P3^6;
sbit INT3 = P3^7;
sbit INT4 = P3^0;
u16 countnumber = 1495;
u16 counttime = 0;
u16 Read_Timer0_Count(void);
void calculate_duty_cycle_INT2(void);
void calculate_duty_cycle_INT3(void);
void send_sent_data(void);
void GPIO_config(void);
#define SYNC_CALIBRATION_TICKS 56
#define STATUS_COMM_TICKS_MIN 12
#define STATUS_COMM_TICKS_MAX 27
#define DATA_TICKS_MIN 12
#define DATA_TICKS_MAX 27
#define CRC_TICKS 12
#define PAUSE_PULSE_TICKS 21
#define TICK_TIME 3
#define SYNC_CALIBRATION_TIME (SYNC_CALIBRATION_TICKS * TICK_TIME)
#define STATUS_COMM_TIME_MIN (STATUS_COMM_TICKS_MIN * TICK_TIME)
#define STATUS_COMM_TIME_MAX (STATUS_COMM_TICKS_MAX * TICK_TIME)
#define DATA_TIME_MIN (DATA_TICKS_MIN * TICK_TIME)
#define DATA_TIME_MAX (DATA_TICKS_MAX * TICK_TIME)
#define CRC_TIME (CRC_TICKS * TICK_TIME)
#define PAUSE_PULSE_TIME (PAUSE_PULSE_TICKS * TICK_TIME)
typedef enum {
STATE_IDLE,
STATE_SYNC,
STATE_PAUSE,
STATE_DATA_NIBBLE,
STATE_CRC,
STATE_END
} sent_state_t;
volatile sent_state_t current_state = STATE_IDLE;
volatile u8 nibble_index = 0;
u8 status_nibble = 0x00;
u8 data_nibbles[6] = {0x05, 0x0D, 0x07, 0x0B, 0x0C, 0x0A};
u8 crc_nibble = 0x0B;
volatile u32 g_us_count = 0;
void Timer1_Init(void) {
AUXR |= 0x40; // ¶¨Ê±Æ÷ʱÖÓ 1T ģʽ
TMOD &= 0x0F; // ÉèÖö¨Ê±Æ÷ģʽ
TL1 = 0xE8; // ÉèÖö¨Ê±³õʼֵ
TH1 = 0xFF; // ÉèÖö¨Ê±³õʼֵ
TF1 = 0; // Çå³ý TF1 ±êÖ¾
ET1 = 1; // ʹÄܶ¨Ê±Æ÷ 1 ÖжÏ
TR1 = 0; // ³õʼ»¯Ê±²»Æô¶¯¶¨Ê±Æ÷
}
void Timer1_Isr(void) interrupt 3 {
TF1 = 0;
g_us_count++;
}
void delay_us(u32 us) {
u32 start_count = g_us_count;
u32 target_count = start_count + us;
TR1 = 1;
while (1) {
if (target_count >= start_count) {
if (g_us_count >= start_count && g_us_count < target_count) {
continue;
} else if (g_us_count >= target_count) {
break;
}
} else {
if ((g_us_count >= start_count) || (g_us_count < target_count)) {
continue;
} else {
break;
}
}
}
TR1 = 0; // Í£Ö¹¶¨Ê±Æ÷
}
void set_high() {
P3 |= 0x08;
}
void set_low() {
P3 &= ~0x08;
}
// ·¢ËÍÒ»¸ö¸ßµçƽÂö³å
void send_high_pulse(u32 duration) {
set_high();
delay_us(duration);
set_low();
}
void send_sent_data() {
static u8 current_nibble;
current_state = STATE_SYNC;
send_high_pulse(SYNC_CALIBRATION_TIME);
current_state = STATE_PAUSE;
send_high_pulse(PAUSE_PULSE_TIME);
current_state = STATE_DATA_NIBBLE;
nibble_index = 0;
// ·¢ËÍ״̬×Ö¶Î
current_nibble = status_nibble;
send_high_pulse(STATUS_COMM_TIME_MIN);
// ·¢ËÍÊý¾Ý×Ö¶Î
for (nibble_index = 0; nibble_index < 6; nibble_index++) {
u32 pulse_duration;
current_nibble = data_nibbles[nibble_index];
if (current_nibble + 1 < 3) {
pulse_duration = DATA_TIME_MIN;
} else if (current_nibble + 1 > 7) {
pulse_duration = DATA_TIME_MAX;
} else {
pulse_duration = (current_nibble + 1 + 12 - 1) * TICK_TIME;
}
send_high_pulse(pulse_duration);
}
current_state = STATE_CRC;
current_nibble = crc_nibble;
send_high_pulse(CRC_TIME);
current_state = STATE_END;
send_high_pulse(10 * TICK_TIME);
current_state = STATE_IDLE;
}
void GPIO_config(void) {
P3_MODE_OUT_PP(GPIO_Pin_3);
set_high();
}
void main(void) {
WTST = 0;
EAXSFR();
CKCON = 0;
GPIO_config();
Timer1_Init();
EA = 1;
while (1) {
// send_sent_data();
set_high();
delay_us(10);
set_low();
delay_us(10); // ÑÓʱ 10us
}
}
|
|