专家
公告
财富商城
电子网
旗下网站
首页
问题库
专栏
标签库
话题
专家
NEW
门户
发布
提问题
发文章
STM32
RS485接收数据不正常
2019-08-14 06:21
发布
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
站内问答
/
STM32/STM8
4464
4
1682
我的主程序较短时一切功能正常,但随着代码的增加出现了数据接收错误和接收不到数据。不明白什么原因,求指教。
友情提示:
此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
4条回答
xx3360765
2019-08-14 19:11
main.c//////////////////
#include "gpio.h"
#include "usart.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include <string.h>
#include "md5.h"
#define p 5
// Hash magic number
#define HASH_MAGIC 0xC32DB952 //hashħêy
unsigned char Input[100];//={0};//={0,1,2,3,4,5,6,7,8,9}; //= {0xC3,0x2D,0xB9,0x52,0,0,1,0,18,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
unsigned char Counter = 0;
unsigned char t;
unsigned char z = 1;
int Delay(int x)
{
int i, y;
for(i = 0; i < x; i++)
for(y = 0; y < 72; y++);
return 0;
}
int main(void)
{
unsigned char i, y, a, b, c, d, e, f, g, k, temp;
unsigned char LEDOutputData[128] = {0};
unsigned char OutputData[18];
unsigned char Hash[4], Response[10];
int h, InputData[100];
unsigned char decrypt[16];
MD5_CTX md5;
y = 0;
SystemInit();
GPIO_Initial();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Initial();
/*½óêÕRS485′«à′μÄêy¾YμÄ′úÂë2¢½«êy¾Y′æÔúêy×éInputÖD*/
while(1)
{
RS485_DIR_RX;
d = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
c = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
b = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2);
a = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3);
t = (a << 3) + (b << 2) + (c << 1) + d;
if((USART_GetITStatus(USART1, USART_IT_RXNE) == RESET)&&z==0 ) //ÅD¶Ïêy¾YêÇ·ñ′«Ëííê
{
for(i = 0; i < 100; i++)
{
printf("%d,%d ",i,Input[i]);
Delay(100);
}
Hash[0] = Input[0];
Hash[1] = Input[1];
Hash[2] = Input[2];
Hash[3] = Input[3];
Input[0] = (HASH_MAGIC >> 24) & 0xFF;
Input[1] = (HASH_MAGIC >> 16) & 0xFF;
Input[2] = (HASH_MAGIC >> 8) & 0xFF;
Input[3] = HASH_MAGIC & 0xFF;
MD5Init(&md5);
MD5Update(&md5, Input, Counter);
MD5Final(decrypt, &md5);
Counter = 0;
if(Hash[0] == decrypt[12] && Hash[1] == decrypt[13] && Hash[2] == decrypt[14] && Hash[3] == decrypt[15])
{
Response[9] = 0x00;
h = Input[4];
h = (h << 8) + Input[5];
if(h == t)
{
if(Input[6] == 1)
{
h = Input[7];
h = (h << 8) + Input[8];
k = (h - 16) / 2;
for(i = 0; i < k; i++)
{
h = Input[25 + i * 2];
InputData[i] = (h << 8) + Input[26 + i * 2];
}
for(y = 0; y < k; y++)
{
if(InputData[y] % 10 != 0)
{
e = InputData[y] / 10;
f = InputData[y] % 10;
}
else
{
e = InputData[y] / 10 - 1;
f = 10;
}
for(i = 0; i < 18; i++)
{
if(i == (f + 7) || i == e)
OutputData[i] = 1;
else
OutputData[i] = 0;
}//éú3éêä3öêy¾YμÄêy×é
for(i = 0; i < 4; i++)
{
temp = OutputData[i];
OutputData[i] = OutputData[7 - i];
OutputData[7 - i] = temp;
}
for(i = 8; i < 12; i++)
{
temp = OutputData[i];
OutputData[i] = OutputData[23 - i];
OutputData[23 - i] = temp;
}//½«êä3öêy¾Y×a»»3éóëó2¼t¶Ë¿úÏà¶Ôó|μÄDÎê½//¸ù¾Y′ò¿a1ñ×óμÄoÅÂë¼ÆËã3öêä3öDÅoÅÄÄá½Î»ÖÃ0
for(i = 1; i <= 8; i++)
{
temp = OutputData[i - 1];
OutputData[i - 1] = OutputData[16 - i];
OutputData[16 - i] = temp;
}
//êä3öÇ°ê®áùλêy¾Y
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_0, OutputData[i]); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 0); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_6, OutputData[16]); //êä3öμúê®Æßλêy¾Y
GPIO_WriteBit(GPIOB, GPIO_Pin_7, OutputData[17]); //êä3öμúê®°Ëλêy¾Y
Delay(50000);
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_0, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 0); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_6, 0);
GPIO_WriteBit(GPIOB, GPIO_Pin_7, 0); //¸′λ1ñ×ó×′ì¬
}
for(y = 0; y < k; y++)
{
if(InputData[y] % 10 != 0)
g = (InputData[y] / 10) * 6 + InputData[y] - 1;
else
g = (InputData[y] / 10 - 1) * 6 + InputData[y] - 1;
for(i = 0; i < 128; i++)
LEDOutputData[g] = 1; //óéêäèëêy¾Y¼ÆËã3öòÆλ¼Ä′æÆ÷μÄμú¼¸Î»êä3öÎa0
}
for(i = 0; i < 8; i++)
{
temp = LEDOutputData[8 + 16 * i];
LEDOutputData[8 + 16 * i] = LEDOutputData[15 + 16 * i];
LEDOutputData[15 + 16 * i] = temp;
temp = LEDOutputData[9 + 16 * i];
LEDOutputData[9 + 16 * i] = LEDOutputData[14 + 16 * i];
LEDOutputData[14 + 16 * i] = temp;
}//½«êä3öêy¾Y×a»»3éóëó2¼t¶Ë¿úÏà¶Ôó|μÄDÎê½
/*½«êy¾Y×a»»3éSM16126μÄêä3öDÎê½*/
for(i = 1; i <= 128; i = i + 16)
{
for(y = 1; y <= 8; y++)
{
temp = LEDOutputData[y + i - 2];
LEDOutputData[y + i - 2] = LEDOutputData[i + 15 - y];
LEDOutputData[i + 15 - y] = temp;
}
}
for(y = 0; y < 10; y++)
{
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, LEDOutputData[i]); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT//Ëø′æêy¾Y£¬ê1¶Ôó|μÄμÆáá
Delay(50000);
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT
Delay(50000);
}//ê1μÆéá˸
}
}
}
else
Response[9] = 0x01;
Response[0] = Input[0];
Response[1] = Input[1];
Response[2] = Input[2];
Response[3] = Input[3];
Response[4] = Input[4];
Response[5] = Input[5];
Response[6] = Input[6];
Response[7] = 0x00;
Response[8] = 0x01;
RS485_DIR_TX;
for(i = 0; i < 10; i++)
USART_SendData(USART1, (uint16_t)Response[i]);
z = 1;
}
else
{
for(i = 0; i < 128; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
if((y * 16) <= i && i < ((y + 1) * 16))
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 1); //SER
else
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT//Ëø′æêy¾Y£¬ê1¶Ôó|μÄμÆáá
Delay(50000);
for(i = 0; i < 128; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT
Delay(50000);
if(y < 7)
y++;
else
y = 0;
//ê1μÆéá˸
}
}
}
加载中...
查看其它4个回答
一周热门
更多
>
相关问题
STM32F4上I2C(在PROTEUS中模拟)调试不通的问题
6 个回答
芯片供应紧张,准备换个MCU,MM32L系列替换STM32L系列的怎么样?
7 个回答
STM32同时使用两个串口进行数据收发时数据丢包的问题
5 个回答
STM32F103串口通信死机问题
4 个回答
STM32WLE5CC连接SX1268在LoRa模式下能与 SX1278互通吗?
2 个回答
相关文章
ST公司第一款无线低功耗单片机模块有效提高物联网设计生产效率
0个评论
如何实现对单片机寄存器的访问
0个评论
通过USB用STM32片内自带Bootloader下载程序及注意事项
0个评论
欲练此功必先自宫之STM32汇编启动,放慢是为了更好的前行
0个评论
×
关闭
采纳回答
向帮助了您的知道网友说句感谢的话吧!
非常感谢!
确 认
×
关闭
编辑标签
最多设置5个标签!
STM32
保存
关闭
×
关闭
举报内容
检举类型
检举内容
检举用户
检举原因
广告推广
恶意灌水
回答内容与提问无关
抄袭答案
其他
检举说明(必填)
提交
关闭
×
打开微信“扫一扫”,打开网页后点击屏幕右上角分享按钮
×
付费偷看金额在0.1-10元之间
确定
×
关闭
您已邀请
0
人回答
查看邀请
擅长该话题的人
回答过该话题的人
我关注的人
#include "gpio.h"
#include "usart.h"
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_usart.h"
#include "misc.h"
#include <string.h>
#include "md5.h"
#define p 5
// Hash magic number
#define HASH_MAGIC 0xC32DB952 //hashħêy
unsigned char Input[100];//={0};//={0,1,2,3,4,5,6,7,8,9}; //= {0xC3,0x2D,0xB9,0x52,0,0,1,0,18,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
unsigned char Counter = 0;
unsigned char t;
unsigned char z = 1;
int Delay(int x)
{
int i, y;
for(i = 0; i < x; i++)
for(y = 0; y < 72; y++);
return 0;
}
int main(void)
{
unsigned char i, y, a, b, c, d, e, f, g, k, temp;
unsigned char LEDOutputData[128] = {0};
unsigned char OutputData[18];
unsigned char Hash[4], Response[10];
int h, InputData[100];
unsigned char decrypt[16];
MD5_CTX md5;
y = 0;
SystemInit();
GPIO_Initial();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
USART1_Initial();
/*½óêÕRS485′«à′μÄêy¾YμÄ′úÂë2¢½«êy¾Y′æÔúêy×éInputÖD*/
while(1)
{
RS485_DIR_RX;
d = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0);
c = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1);
b = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_2);
a = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3);
t = (a << 3) + (b << 2) + (c << 1) + d;
if((USART_GetITStatus(USART1, USART_IT_RXNE) == RESET)&&z==0 ) //ÅD¶Ïêy¾YêÇ·ñ′«Ëííê
{
for(i = 0; i < 100; i++)
{
printf("%d,%d ",i,Input[i]);
Delay(100);
}
Hash[0] = Input[0];
Hash[1] = Input[1];
Hash[2] = Input[2];
Hash[3] = Input[3];
Input[0] = (HASH_MAGIC >> 24) & 0xFF;
Input[1] = (HASH_MAGIC >> 16) & 0xFF;
Input[2] = (HASH_MAGIC >> 8) & 0xFF;
Input[3] = HASH_MAGIC & 0xFF;
MD5Init(&md5);
MD5Update(&md5, Input, Counter);
MD5Final(decrypt, &md5);
Counter = 0;
if(Hash[0] == decrypt[12] && Hash[1] == decrypt[13] && Hash[2] == decrypt[14] && Hash[3] == decrypt[15])
{
Response[9] = 0x00;
h = Input[4];
h = (h << 8) + Input[5];
if(h == t)
{
if(Input[6] == 1)
{
h = Input[7];
h = (h << 8) + Input[8];
k = (h - 16) / 2;
for(i = 0; i < k; i++)
{
h = Input[25 + i * 2];
InputData[i] = (h << 8) + Input[26 + i * 2];
}
for(y = 0; y < k; y++)
{
if(InputData[y] % 10 != 0)
{
e = InputData[y] / 10;
f = InputData[y] % 10;
}
else
{
e = InputData[y] / 10 - 1;
f = 10;
}
for(i = 0; i < 18; i++)
{
if(i == (f + 7) || i == e)
OutputData[i] = 1;
else
OutputData[i] = 0;
}//éú3éêä3öêy¾YμÄêy×é
for(i = 0; i < 4; i++)
{
temp = OutputData[i];
OutputData[i] = OutputData[7 - i];
OutputData[7 - i] = temp;
}
for(i = 8; i < 12; i++)
{
temp = OutputData[i];
OutputData[i] = OutputData[23 - i];
OutputData[23 - i] = temp;
}//½«êä3öêy¾Y×a»»3éóëó2¼t¶Ë¿úÏà¶Ôó|μÄDÎê½//¸ù¾Y′ò¿a1ñ×óμÄoÅÂë¼ÆËã3öêä3öDÅoÅÄÄá½Î»ÖÃ0
for(i = 1; i <= 8; i++)
{
temp = OutputData[i - 1];
OutputData[i - 1] = OutputData[16 - i];
OutputData[16 - i] = temp;
}
//êä3öÇ°ê®áùλêy¾Y
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_0, OutputData[i]); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 0); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_6, OutputData[16]); //êä3öμúê®Æßλêy¾Y
GPIO_WriteBit(GPIOB, GPIO_Pin_7, OutputData[17]); //êä3öμúê®°Ëλêy¾Y
Delay(50000);
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_0, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_1, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_2, 0); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_6, 0);
GPIO_WriteBit(GPIOB, GPIO_Pin_7, 0); //¸′λ1ñ×ó×′ì¬
}
for(y = 0; y < k; y++)
{
if(InputData[y] % 10 != 0)
g = (InputData[y] / 10) * 6 + InputData[y] - 1;
else
g = (InputData[y] / 10 - 1) * 6 + InputData[y] - 1;
for(i = 0; i < 128; i++)
LEDOutputData[g] = 1; //óéêäèëêy¾Y¼ÆËã3öòÆλ¼Ä′æÆ÷μÄμú¼¸Î»êä3öÎa0
}
for(i = 0; i < 8; i++)
{
temp = LEDOutputData[8 + 16 * i];
LEDOutputData[8 + 16 * i] = LEDOutputData[15 + 16 * i];
LEDOutputData[15 + 16 * i] = temp;
temp = LEDOutputData[9 + 16 * i];
LEDOutputData[9 + 16 * i] = LEDOutputData[14 + 16 * i];
LEDOutputData[14 + 16 * i] = temp;
}//½«êä3öêy¾Y×a»»3éóëó2¼t¶Ë¿úÏà¶Ôó|μÄDÎê½
/*½«êy¾Y×a»»3éSM16126μÄêä3öDÎê½*/
for(i = 1; i <= 128; i = i + 16)
{
for(y = 1; y <= 8; y++)
{
temp = LEDOutputData[y + i - 2];
LEDOutputData[y + i - 2] = LEDOutputData[i + 15 - y];
LEDOutputData[i + 15 - y] = temp;
}
}
for(y = 0; y < 10; y++)
{
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, LEDOutputData[i]); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT//Ëø′æêy¾Y£¬ê1¶Ôó|μÄμÆáá
Delay(50000);
for(i = 0; i < 16; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT
Delay(50000);
}//ê1μÆéá˸
}
}
}
else
Response[9] = 0x01;
Response[0] = Input[0];
Response[1] = Input[1];
Response[2] = Input[2];
Response[3] = Input[3];
Response[4] = Input[4];
Response[5] = Input[5];
Response[6] = Input[6];
Response[7] = 0x00;
Response[8] = 0x01;
RS485_DIR_TX;
for(i = 0; i < 10; i++)
USART_SendData(USART1, (uint16_t)Response[i]);
z = 1;
}
else
{
for(i = 0; i < 128; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
if((y * 16) <= i && i < ((y + 1) * 16))
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 1); //SER
else
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT//Ëø′æêy¾Y£¬ê1¶Ôó|μÄμÆáá
Delay(50000);
for(i = 0; i < 128; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 0); //CLK
GPIO_WriteBit(GPIOB, GPIO_Pin_8, 0); //SER
GPIO_WriteBit(GPIOB, GPIO_Pin_9, 1); //CLK
}
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 1); //LAT
GPIO_WriteBit(GPIOB, GPIO_Pin_10, 0); //LAT
Delay(50000);
if(y < 7)
y++;
else
y = 0;
//ê1μÆéá˸
}
}
}
一周热门 更多>