芯片破解stm32F407单片机实现RS485通讯程序
芯片破解stm32F407单片机源程序如下:
- /*485-2 */
- #define RS485_RE_GPIO_PORT GPIOB
- #define RS485_RE_GPIO_PIN SYS_GPIO_PIN15
- #define RS485_RE_GPIO_CLK_ENABLE() do{ RCC->AHB1ENR |= 1 << 1; }while(0) /* PB口时钟使能 */
- #define RS485_TX_GPIO_PORT GPIOD
- #define RS485_TX_GPIO_PIN SYS_GPIO_PIN8
- #define RS485_TX_GPIO_AF 7 /* AF功能选择 */
- #define RS485_TX_GPIO_CLK_ENABLE() do{ RCC->AHB1ENR |= 1 << 3; }while(0) /* PD口时钟使能 */
- #define RS485_RX_GPIO_PORT GPIOD
- #define RS485_RX_GPIO_PIN SYS_GPIO_PIN9
- #define RS485_RX_GPIO_AF 7 /* AF功能选择 */
- #define RS485_RX_GPIO_CLK_ENABLE() do{ RCC->AHB1ENR |= 1 << 3; }while(0) /* PD口时钟使能 */
- #define RS485_UX USART3
- #define RS485_UX_IRQn USART3_IRQn
- #define RS485_UX_IRQHandler USART3_IRQHandler
- #define RS485_UX_CLK_ENABLE() do{ RCC->APB1ENR |= 1 << 18; }while(0) /* USART3 时钟使能 */
- /*485-2 END */
- /* 控制RS485_RE脚, 控制RS485发送/接收状态
- * RS485_RE = 0, 进入接收模式
- * RS485_RE = 1, 进入发送模式
- */
- #define RS485_RE(x) sys_gpio_pin_set(RS485_RE_GPIO_PORT, RS485_RE_GPIO_PIN, x)
- #define RS485_REC_LEN 64 /* 定义最大接收字节数 64 */
- #define RS485_EN_RX 1 /* 使能(1)/禁止(0)RS485接收 */
- extern uint8_t g_RS485_rx_buf[RS485_REC_LEN]; /* 接收缓冲,最大RS485_REC_LEN个字节 */
- extern uint8_t g_RS485_rx_cnt; /* 接收数据长度 */
- void rs485_init(uint32_t sclk, uint32_t baudrate); /* RS485初始化 */
- void rs485_send_data(uint8_t *buf, uint8_t len); /* RS485发送数据 */
- void rs485_receive_data(uint8_t *buf, uint8_t *len);/* RS485接收数据 */
- extern void UartDriver(void); /* 485接收数据处理 */
- #endif
- //串口驱动函数,检测数据帧的接收,调度功能函数,需在主循环中调用
- void UartDriver(void)
- {
- unsigned char i=0,cnt;
- unsigned int crc;
- unsigned char crch,crcl;
- static uint8_t len;
- static uint8_t buf[60];
- if(flagFrame) //帧接收完成标志,即接收到一帧新数据
- {
- flagFrame=0; //帧接收完成标志清零
- len = UartRead(buf,sizeof(buf)); //将接收到的命令读到缓冲区中
- if(buf[0]==0x01) //判断地址是不是0x01
- {
- crc=GetCRC16(buf,len-2); //计算CRC校验值,出去CRC校验值
- crch=crc>>8; //crc高位
- crcl=crc&0xFF; //crc低位
- if((buf[len-2]==crch)&&(buf[len-1]==crcl)) //判断CRC校验是否正确
- {
- switch (buf[1]) //按功能码执行操作
- {
- case 0x03: //读数据
- if((buf[2]==0x00)&&(buf[3]<=0x05)) //寄存器地址支持0x0000~0x0005
- {
-
- if(buf[3]<=0x04)
- {
- i=buf[3];//提取寄存器地址
- cnt=buf[5]; //提取待读取的寄存器数量
- buf[2]=cnt*2; //读取数据的字节数,为寄存器*2,因modbus定义的寄存器为16位
- len=3;
- while(cnt--)
- {
- buf[len++]=0x00; //寄存器高字节补0
- buf[len++]=regGroup[i++]; //低字节
- }
-
- }
- break;
- }
- else //寄存器地址不被支持时,返回错误码
- {
- buf[1]=0x83; //功能码最高位置1
- buf[2]=0x02; //设置异常码为02-无效地址
- len=3;
- break;
- }
- case 0x06: //写入单个寄存器
- if((buf[2]==0x00)&&(buf[3]<=0x05)) //寄存器地址支持0x0000-0x0005
- {
- if(buf[3]<=0x04)
- {
- i=buf[3]; //提取寄存器地址
- regGroup[i]=buf[5]; //保存寄存器数据
- LED0(0);
- }
- len -=2; //长度-2以重新计算CRC并返回原帧
- break;
- }

芯片解密