我的毕业设计题目是编写程序实现STM32与昆仑通态触摸屏间的双向数据通信。根据往届已有的论文我编写出了串口中断的程序,见下一。然后昆仑通态触摸屏是支持ModbusRTU协议,我又在网上找到了可以应用在STM32上的Modbus RTU可是我不知道怎么把这两个何为一体。求指导!!!!
一,RS485通信程序
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;//利用标志符TypeDef定义结构体
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOD , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_7);
/*
Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_7);
/*
Configure pins as AF pushpull */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode
= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Configure USART Rx as alternate
function */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode
= GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits
= USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
/* USART enable */
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
}
:
main()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_Config();
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;制定为IO口4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;工作方式为推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;频率为50MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_ResetBits(GPIOD,GPIO_Pin_4);
while(1)
{
}
}
u8 RxCounter =0;
u8 TxCounter=0;
u8 RxBufferSize=8;
u8 RxBuffer[64];
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE)
== SET)
{
RxBuffer[RxCounter++] =(USART_ReceiveData(USART2) & 0x7F);
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
if(RxCounter == RxBufferSize)
{
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
GPIO_SetBits(GPIOD,GPIO_Pin_4);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
RxCounter=0;
}
}
if(USART_GetITStatus(USART2, USART_IT_TXE) == SET)
{
USART_SendData(USART2, RxBuffer[TxCounter++]);
USART_ClearITPendingBit(USART2, USART_IT_TXE);
if(TxCounter >9)
{
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
GPIO_ResetBits(GPIOD,GPIO_Pin_4);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
TxCounter=0;
}
}
}
Modbus RTU的程序太大了在附件里面,我觉着不可能全用到。求指导!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
你搞的怎么样了? 我也在搞,能否讨论哈
一周热门 更多>