想要实现串口助手发送数据通过RS485来传输数据控制led的亮灭
同时按键按下的数值能够通过RS485来显示在串口助手上
main.c
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_conf.h"
#include <stdio.h>
#include "string.h"
#include "gpio.h"
#include "delay.h"
#include "USART.h"
#define S1_DOWN GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 1
#define S1_UP GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) == 0
#define LED1_ON GPIO_ResetBits(GPIOC,GPIO_Pin_13)
#define LED1_OFF GPIO_SetBits(GPIOC,GPIO_Pin_13)
#define LED2_ON GPIO_ResetBits(GPIOC,GPIO_Pin_14)
#define LED2_OFF GPIO_SetBits(GPIOC,GPIO_Pin_14)
#define LED3_ON GPIO_ResetBits(GPIOC,GPIO_Pin_15)
#define LED3_OFF GPIO_SetBits(GPIOC,GPIO_Pin_15)
#define LED4_ON GPIO_ResetBits(GPIOA,GPIO_Pin_4)
#define LED4_OFF GPIO_SetBits(GPIOA,GPIO_Pin_4)
#define LED5_ON GPIO_ResetBits(GPIOA,GPIO_Pin_5)
#define LED5_OFF GPIO_SetBits(GPIOA,GPIO_Pin_5)
#define LED6_ON GPIO_ResetBits(GPIOA,GPIO_Pin_6)
#define LED6_OFF GPIO_SetBits(GPIOA,GPIO_Pin_6)
#define LED7_ON GPIO_ResetBits(GPIOC,GPIO_Pin_6)
#define LED7_OFF GPIO_SetBits(GPIOC,GPIO_Pin_6)
#define LED8_ON GPIO_ResetBits(GPIOC,GPIO_Pin_7)
#define LED8_OFF GPIO_SetBits(GPIOC,GPIO_Pin_7)
//u8 ge;
//u8 shi;
#define USART_REC_LEN 200 //¶¨òå×î′ó½óêÕ×Ö½úêy 200
#define USART_REC_LEN2 200 //¶¨òå×î′ó½óêÕ×Ö½úêy 200
#define EN_USART1_RX 1 //ê1Äü£¨1£©/½ûÖ1£¨0£©′®¿ú1½óêÕ
u8 USART_RX_BUF[USART_REC_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.Ä©×Ö½úÎa»»DD·û
u16 USART_RX_STA; //½óêÕ×′쬱ê¼Ç
u8 USART_RX_BUF2[USART_REC_LEN2]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.Ä©×Ö½úÎa»»DD·û
u16 USART_RX_STA2; //½óêÕ×′쬱ê¼Ç
#define RS485CTR_CLR GPIO_ResetBits(GPIOE, GPIO_Pin_15) //CTRÖÃμí0½óêÕ
#define RS485CTR_SET GPIO_SetBits(GPIOE, GPIO_Pin_15) //CTRÖøß1·¢Ëí
void HEXtoLcd(u8 s)
{
u8 temp;
int i;
temp=s;
for(i=1; i<=8; i++)
{
if (temp%2 ==0 )
{
switch(i)
{
case 1: LED1_ON; break;
case 2: LED2_ON; break;
case 3: LED3_ON; break;
case 4: LED4_ON; break;
case 5: LED5_ON; break;
case 6: LED6_ON; break;
case 7: LED7_ON; break;
case 8: LED8_ON; break;
}
}
else
{
switch(i)
{
case 1: LED1_OFF; break;
case 2: LED2_OFF; break;
case 3: LED3_OFF; break;
case 4: LED4_OFF; break;
case 5: LED5_OFF; break;
case 6: LED6_OFF; break;
case 7: LED7_OFF; break;
case 8: LED8_OFF; break;
}
}
temp = temp / 2;
}
}
int main(void)
{
//unsigned char i;
GPIO_Config();
GPIO_Key_Config();
USART1_Config();
USART3_Config();
RS485CTR_CLR;//CTRÖÃμí0½óêÕ
while (1)
{
//for(i=0;i<100;i++)
// {
// RS485CTR_SET; //·¢Ëí×′쬣¬CTRÎa¸ß
// USART_SendData(USART3,255-i); //½«¼üÖμ·¢Ëíμ½′ó»ú
// while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); //μè′y·¢Ëííê3é
// delay_ms(200);
unsigned char cnt=0;
if (S1_DOWN)
{
delayms(10);
if (S1_DOWN)
{
while(S1_DOWN)
{
delayms(10);
}
RS485CTR_SET;
cnt++;
USART_SendData(USART3,cnt);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);//μè′y·¢Ëííê3é
RS485CTR_CLR;
delayms(10);
}
//HEXtoLcd(USART_RX_BUF[USART_RX_STA-1]);
}
HEXtoLcd(USART_RX_BUF[USART_RX_STA-1]);
// shi=cnt/10+0x30;
// ge=cnt%10+0x30;
// // Lcd_Clear(GRAY0);
// Gui_DrawFont_GBK16(31,23,RED,GRAY0,&ge);
// Gui_DrawFont_GBK16(1,23,RED,GRAY0,&shi);
//
}
}
串口3的中断函数
void USART3_IRQHandler(void) //′®¿ú3ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART3); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))
USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
}
数据初始化函数
#include "stm32f10x.h"
#include "USART.h"
void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,uint16_t Len){
uint16_t i;
for(i=0; i<Len; i++){
USART_SendData(USARTx, Data[i]); //·¢Ëíò»¸ö×Ö½úêy¾Y
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);//ÅD¶ÏêÇ·ñ·¢Ëííê±Ï£¬RESET¶¨òåÎa0
}
}
void USART1_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE); //¿aÆôA¶Ë¿úê¼ê±Öó
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.9
//USART1_RX GPIOA.103õê¼»ˉ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIOA.10
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇàÕ¼óÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 9600;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART1, &USART_InitStructure); //3õê¼»ˉ′®¿ú1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêÕÖD¶Ï
USART_Cmd(USART1, ENABLE); //ê1Äü′®¿ú1
USART_ClearFlag(USART1,USART_FLAG_TC);
}
void USART3_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB1Periph_USART3,ENABLE); //¿aÆôA¶Ë¿úê¼ê±Öó ′®¿ú3ê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //USART3 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOB, &GPIO_InitStructure); //B¶Ë¿ú
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //USART3 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸′óÿa©êäèë
GPIO_Init(GPIOB, &GPIO_InitStructure); //B¶Ë¿ú
//Usart1 NVIC ÅäÖÃ
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//ÇàÕ¼óÅÏ輶0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μàê1Äü
NVIC_Init(&NVIC_InitStructure); //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
//USART 3õê¼»ˉéèÖÃ
USART_InitStructure.USART_BaudRate = 9600;//′®¿ú2¨ìØÂê
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART3, &USART_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//¿aÆô′®¿ú½óêÕÖD¶Ï
USART_Cmd(USART3, ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
}
#include "stm32f10x.h"
#include "gpio.h"
void GPIO_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE); //¿aÆôE¶Ë¿úê¼ê±Öó
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //¿aÆôA¶Ë¿úê¼ê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; // PE15 //CS 485
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOE, &GPIO_InitStructure); //E¶Ë¿ú
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; // TX PA2
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸′óÃíÆíìêä3ö
GPIO_Init(GPIOA, &GPIO_InitStructure); //A¶Ë¿ú
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // RX PA3
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //¸¡¿Õêäèë
GPIO_Init(GPIOA, &GPIO_InitStructure); //A¶Ë¿ú
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOA, ENABLE); //ledμÆgpio3õê¼»ˉ
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_4;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void GPIO_Key_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
一周热门 更多>