新手入门
- 积分
- 16
- 金钱
- 16
- 注册时间
- 2016-1-12
- 在线时间
- 3 小时
|
2金钱
在串口2里按 串口1的设置模仿一遍 但是不知道 怎么写 串口2的发送中断!望原子哥看看设置给些建议!
串口2设置(乱码为中文 ,不管它)
#include "usart2.h"
#include "sys.h"
#include "string.h"
#include "stdarg.h"
#include "stdio.h"
#include "delay.h"
/*ê1óÃmicroLibμÄ·½·¨*/
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (uint8_t) ch);
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET) {}
return ch;
}
int GetKey (void) {
while (!(USART2->SR & USART_FLAG_RXNE));
return ((int)(USART2->DR & 0x1FF));
}
__align(8) u8 USART2_TX_BUF[USART2_SEND_LEN];
//′®¿ú2,printf oˉêy
//è·±£ò»′η¢Ëíêy¾Y2»3¬1yUSART2_MAX_SEND_LEN×Ö½ú
/*void u2_printf(char* fmt,...)
{
int cnt=0;
int len=0;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART2_TX_BUF,fmt,ap);
va_end(ap);
len=strlen((const char*)USART2_TX_BUF);
while(len--)
{
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_SendData(USART2,USART2_TX_BUF[cnt++]);
}
//while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); //μè′yí¨μà7′«êäíê3é
//USART_SendData(USART2,(uint32_t)USART2_TX_BUF); //í¨1ydma·¢Ëí3öè¥
}*/
//////////////////////////////////////////////////////////////////
#if USART2_RX_EN //èç1ûê1Äüá˽óêÕ
//′®¿ú2ÖD¶Ï·tÎñ3ìDò
//×¢òa,¶áè¡USARTx->SRÄü±üÃaÄaÃûÆäÃîμÄ′íÎó
u8 USART2_RX_BUF[USART2_RECV_LEN]; //½óêÕ»o3å,×î′óUSART_REC_LEN¸ö×Ö½ú.
//½óêÕ×′ì¬
//bit15£¬ ½óêÕíê3é±êÖ¾
//bit14£¬ ½óêÕμ½0x0d
//bit13~0£¬ ½óêÕμ½μÄóDD§×Ö½úêyÄ¿
u16 USART2_RX_STA=0; //½óêÕ×′쬱ê¼Ç
void USART2_Init(u32 pclk2,u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
USART_InitTypeDef USART2_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);//ê1ÄüUSAAT2,USART1,GPIOAê±Öó
USART_DeInit(USART2); //¸′λ′®¿ú2
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART2_InitStructure.USART_BaudRate = 9600;//ò»°ãéèÖÃÎa9600;
USART2_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö3¤Îa8λêy¾Y¸ñê½
USART2_InitStructure.USART_StopBits = USART_StopBits_1;//ò»¸öí£Ö1λ
USART2_InitStructure.USART_Parity = USART_Parity_No;//ÎTÆæżD£Ñéλ
USART2_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//ÎTó2¼têy¾Yá÷¿ØÖÆ
USART2_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //êÕ·¢Ä£ê½
USART_Init(USART2,&USART2_InitStructure);
#if USART2_RX_EN
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
#endif
//USART_ITConfig(USART2,USART_IT_TXE,ENABLE);
/*#if USART2_TX_EN
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
#endif */
USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void)
{
u8 res;
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntEnter();
#endif
if(USART_GetITStatus(USART2, USART_IT_RXNE)!= RESET)
{
//USART_ClearITPendingBit(USART1,USART_IT_RXNE);
res=USART_ReceiveData(USART2);
if((USART2_RX_STA&0x8000)==0)
{
if(USART2_RX_STA&0x4000)
{
if(res!=0x0a)USART2_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART2_RX_STA|=0x8000; //½óêÕíê3éáË
}
else //»1ûêÕμ½0X0D
{
if(res==0x0d)USART2_RX_STA|=0x4000;
else
{
USART2_RX_BUF[USART2_RX_STA&0X3FFF]=res ;
USART2_RX_STA++;
if(USART2_RX_STA>(USART2_RECV_LEN-1))USART2_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ
}
}
}
}
#ifdef OS_TICKS_PER_SEC //èç1ûê±Öó½úÅÄêy¶¨òåáË,ËμÃ÷òaê1óÃucosIIáË.
OSIntExit();
#endif
//USART_ITConfig(USART2,USART_IT_RXNE,DISABLE);
//USART_ITConfig(USART2,USART_IT_TXE,DISABLE);
}
#endif
main.c的设置输入
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "sim900a.h"
#include "usart2.h"
//ALIENTEKÕ½½¢STM32¿a·¢°åêμÑé4
//′®¿úêμÑé
//¼¼êõÖ§3Ö£owww.openedv.com
//1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾
u8 key,timex,times,len;
int main(void)
{
uint16_t t;
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
NVIC_Configuration(); //éèÖÃNVICÖD¶Ï·Ö×é2:2λÇàÕ¼óÅÏ輶£¬2λÏìó|óÅÏ輶
uart_init(9600); //′®¿ú3õê¼»ˉÎa9600
LED_Init(); //LED¶Ë¿ú3õê¼»ˉ
LED0=0;
KEY_Init(); //3õê¼»ˉóë°′¼üᬽóμÄó2¼t½ó¿ú
USART2_Init(36,9600);
key=KEY_Scan(0);
while(1)
{
if(USART_RX_STA&0x8000)
{
len=USART_RX_STA&0x3fff;//μÃμ½′Ë′νóêÕμ½μÄêy¾Y3¤¶è
printf("\r\nÄú·¢ËíμÄÏûÏ¢Îa:\r\n\r\n");
for(t=0;t<len;t++)
{
USART_SendData(USART2, USART2_RX_BUF[t]);//Ïò′®¿ú2·¢Ëíêy¾Y
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//μè′y·¢Ëí½áêø
}
printf("\r\n\r\n");//2åèë»»DD
USART_RX_STA=0;
}else
{
times++;
if(times%5000==0)
{
printf("\r\nÕ½½¢STM32¿a·¢°å ′®¿úêμÑé\r\n");
printf("ÕyμãÔ-×ó@ALIENTEK\r\n\r\n");
}
if(times%200==0)printf("Çëêäèëêy¾Y,òÔ»Ø3μ¼ü½áêø\n");
if(times%30==0)LED0=!LED0;//éá˸LED,ìáê¾Ïμí3ÕyÔúÔËDD.
delay_ms(10);
}
}
}
/* while(1)
{
//if(key)
//{
// u2_printf("AT+CSCS=\"GSM\"\r\n");
//u2_printf("AT+CMGF=1\r\n");
//u2_printf("AT+CMGS=\"13696877895\"\r\n");
//u2_printf("test for Laputa!");
for(t=0;t<8;t++)
USART_SendData(USART1,p1[t]);
//if(sim900a_send_cmd((u8*)0X1A,"+CMGS:",1000)==0);
// if((timex%20)==0)LED1=!LED1;
// }
//USART2_RX_STA=0;
if((timex%20)==0)LED0=!LED0;
timex++;
delay_ms(10);
if(USART2_RX_STA&0X8000)sim_at_response(1);
// u2_printf("");
// u2_printf("");
//sim900a_sms_test();
}*/
用串口助手 发送的 时候 发送的消息却不能再窗口显示出来 !
|
-
发送的显示不出来消息
|