[mw_shl_code=c,true]#ifndef __Init_h
#define __Init_h
#include "stm32f10x.h"
#include "sys.h"
#include "LowInit.h"
#include "MediumInit.h"
#include "ATK_HC05.h"
#include "lcd.h"
#endif
#include "Init.h"
void HC05_Role_Show(void)
{
if(HC05_Get_Role()==1)LCD_ShowString(30,140,200,16,16,"ROLE:Master"); //?÷?ú
else LCD_ShowString(30,140,200,16,16,"ROLE:Slave "); //???ú
}
//????ATK-HC05???é??????×???
void HC05_Sta_Show(void)
{
if(HC05_LED)LCD_ShowString(120,140,120,16,16,"STA:Connected "); //????????
else LCD_ShowString(120,140,120,16,16,"STA
isconnect"); //??????
}
int main(void)
{
u8 t,a=1;
u8 key;
u8 sendmask=0;
u8 sendcnt=0;
u8 sendbuf[20];
u8 reclen=0;
delay_init();
NVIC_Configuration();
LowInit_Config();
MediumInit1_Config();
LCD_Init(); //??????LCD
POINT_COLOR=BLUE;
LCD_Clear(BLACK);
LCD_ShowString(30,30,200,16,16,"WELCOME HERE! ^-^");
LCD_ShowString(30,50,200,16,16,"HC05 BLUETOOTH COM TEST");
LCD_ShowString(30,70,200,16,16,"Let's Go");
while(HC05_Init()) //??????ATK-HC05???é
{
POINT_COLOR=RED;
LCD_ShowString(30,90,200,16,16,"ATK-HC05 Error! T_T");
delay_ms(500);
LCD_ShowString(30,90,200,16,16,"Please Check!!!");
delay_ms(100);
}
POINT_COLOR=BLUE;
LCD_ShowString(30,90,200,16,16,"WK_UP:ROLE KEY0:SEND/STOP");
LCD_ShowString(30,110,200,16,16,"ATK-HC05 Standby!");
LCD_ShowString(30,160,200,16,16,"Send:");
LCD_ShowString(30,180,200,16,16,"Receive:");
POINT_COLOR=WHITE;
HC05_Role_Show();
while(1)
{
key=KEY_Scan(0);
if(key==KEY_UP) //???????é?÷???è??
{
key=HC05_Get_Role();
if(key!=0XFF)
{
key=!key; //×?????·?
if(key==0)HC05_Set_Cmd("AT+ROLE=0");
else HC05_Set_Cmd("AT+ROLE=1");
HC05_Role_Show();
HC05_Set_Cmd("AT+RESET"); //????ATK-HC05???é
}
}else if(key==KEY_RIGHT)
{
sendmask=!sendmask; //·???/????·???
if(sendmask==0)LCD_Fill(30+40,160,240,160+16,BLACK);//????????
}else delay_ms(10);
if(t==50)
{
if(sendmask) //?¨?±·???
{
sprintf((char*)sendbuf,"ALIENTEK HC05 %d\r\n",sendcnt);
LCD_ShowString(30+40,160,200,16,16,sendbuf); //????·???????
u2_printf("ALIENTEK HC05 %d\r\n",sendcnt); //·????????????é
sendcnt++;
if(sendcnt>99)sendcnt=0;
}
HC05_Sta_Show();
t=0;
a=!a;
LED0(a);
}
if(USART2_RX_STA&0X8000) //????????????????
{
LCD_Fill(30,200,240,320,BLACK); //????????
reclen=USART2_RX_STA&0X7FFF; //?????????¤??
USART2_RX_BUF[reclen]=0; //?????á??·?
if(reclen==9||reclen==8) //????DS1?ì??
{
if(strcmp((const char*)USART2_RX_BUF,"+LED1 ON")==0){LED1(ON);} //?ò??LED1
if(strcmp((const char*)USART2_RX_BUF,"+LED1 OFF")==0){LED1(OFF);}//??±?LED1
while(strcmp((const char*)USART2_RX_BUF,"+Change Colour")==0)
{
if(strcmp((const char*)USART2_RX_BUF,"+x")==0)
{LED2(ON);LED3(OFF);LED4(OFF);}
else if(strcmp((const char*)USART2_RX_BUF,"+y")==0)
{LED2(OFF);LED3(ON);LED4(OFF);}
else if(strcmp((const char*)USART2_RX_BUF,"+z")==0)
{LED2(OFF);LED3(OFF);LED4(ON);}
}
}
LCD_ShowString(30,200,209,119,16,USART2_RX_BUF);//????????????????
USART2_RX_STA=0;
}
t++;
}
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //?è??NVIC????·?×é2:2??????????????2???ì????????
}
#include "MediumInit.h"
__align(8) u8 USART2_TX_BUF[USART2_MAX_SEND_LEN]; //·???????,×??óUSART2_MAX_SEND_LEN×??? //??????????????
u8 USART2_RX_BUF[USART2_MAX_RECV_LEN],USART_RX_BUF[USART_REC_LEN];;
u16 USART2_RX_STA=0,USART_RX_STA=0;
/*********************************************************
??????????MediumInit1_Config
?????????????®??1??DMA???®????????????TIM4?¨??
**********************************************************/
void MediumInit1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //????USART1??GPIOA?±??
USART_DeInit(USART1); //?????®??1
//USART1_TX PA.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; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA10
//Usart1 NVIC ????
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
//USART ???????è??
USART_InitStructure.USART_BaudRate = 9600;//??°??è????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×??¤??8??????????
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(USART1, &USART_InitStructure); //???????®??
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????
USART_Cmd(USART1, ENABLE);
}
/*********************************************************
??????????MediumInit2_Config
?????????????®??1??2??DMA???®????????????TIM4?¨??
**********************************************************/
void MediumInit2_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // GPIOA?±??
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //?®??2?±??????
USART_DeInit(USART2); //?????®??2
//USART2_TX PA.2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //???????ì????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA2
//USART2_RX PA.3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????????
GPIO_Init(GPIOA, &GPIO_InitStructure); //??????PA3
USART_InitStructure.USART_BaudRate = 9600;//?¨??????°??è????9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//×??¤??8??????????
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); //???????®?? 2
//?¨?????è??
USART_DMACmd(USART2,USART_DMAReq_Tx,ENABLE); //?????®??2??DMA·???
/********************************DMA1*********************************/
DMA_DeInit(DMA1_Channel7);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART2->DR; //DMA???èADC?ù???·
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)USART2_TX_BUF; //DMA?????ù???·
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; //????????·??ò????????????·????????è
DMA_InitStructure.DMA_BufferSize = 0; //DMA?¨????DMA???????ó??
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //???è???·?????÷??±?
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //???????·?????÷????
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //?????í????8??
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //?????í????8??
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //?¤×÷??????????????
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium; //DMA?¨?? x????????????
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA?¨??x?????è??????????????????
DMA_Init(DMA1_Channel7, &DMA_InitStructure); //?ù??DMA_InitStruct?????¨????????????DMA???¨??USART1_Tx_DMA_Channel?ù±ê?????????÷
USART_Cmd(USART2, ENABLE); //?????®??
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//????????
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
TIM_TimeBaseStructure.TIM_Period = 99; //?è???????????ü??????×°????????×?????×°???????÷????????
TIM_TimeBaseStructure.TIM_Prescaler =7199; //?è??????×÷??TIMx?±?????????????¤·?????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM?ò??????????
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //?ù?????¨????????????TIMx???±???ù??????
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //???????¨??TIM4????,???í?ü??????
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷
USART2_RX_STA=0; //????
TIM4_Set(0); //??±??¨?±?÷4
}
/*********************************************************
??????????MTIM_Set
???????????¨?±?÷4????
**********************************************************/
void TIM4_Set(u8 sta)
{
if(sta)
{
TIM_SetCounter(TIM4,0);//?????÷????
TIM_Cmd(TIM4, ENABLE); //????TIMx
}else TIM_Cmd(TIM4, DISABLE);//??±??¨?±?÷4
}
/*********************************************************
??????????UART_DMA_Enable
??????????DMA????????
**********************************************************/
void UART_DMA_Enable(DMA_Channel_TypeDef*DMA_CHx,u8 len)
{
DMA_Cmd(DMA_CHx, DISABLE ); //??±? ???????¨??
DMA_SetCurrDataCounter(DMA_CHx,len);//DMA?¨????DMA???????ó??
DMA_Cmd(DMA_CHx, ENABLE); //????DMA????
}
/*********************************************************
??????????u2_printf
???????????®??2printf
**********************************************************/
void u2_printf(char* fmt,...)
{
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART2_TX_BUF,fmt,ap);
va_end(ap);
while(DMA_GetCurrDataCounter(DMA1_Channel7)!=0); //?????¨??7?????ê??
UART_DMA_Enable(DMA1_Channel7,strlen((const char*)USART2_TX_BUF)); //?¨??dma·???????
}
/*********************************************************
??????????USART1_IRQHandler
???????????®??1????????
**********************************************************/
void USART1_IRQHandler(void)
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) //????????(????????????±?????0x0d 0x0a?á??)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //????????????????
if((USART_RX_STA&0x8000)==0)//???????ê??
{
if(USART_RX_STA&0x4000)//????????0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?????í?ó,????????
else USART_RX_STA|=0x8000; //?????ê????
}
else //????????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;//?????????í?ó,????????????
}
}
}
}
}
/*********************************************************
??????????USART2_IRQHandler
???????????®??2????????
**********************************************************/
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//??????????
{
res =USART_ReceiveData(USART2);
if(USART2_RX_STA<USART2_MAX_RECV_LEN) //??????????????
{
TIM_SetCounter(TIM4,0);//?????÷????
if(USART2_RX_STA==0)TIM4_Set(1); //?????¨?±?÷4??????
USART2_RX_BUF[USART2_RX_STA++]=res; //??????????????
}else
{
USART2_RX_STA|=1<<15; //????±ê???????ê??
}
}
}
/*****************************************************************
??????????TIM4_IRQHandler
???????????¨?±?÷4????
*****************************************************************/
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET)//???ü??????
{
USART2_RX_STA|=1<<15; //±ê???????ê??
TIM_ClearITPendingBit(TIM4, TIM_IT_Update ); //????TIMx?ü??????±ê??
TIM4_Set(0); //??±?TIM4
}
}
#if 1
#pragma import(__use_no_semihosting)
//±ê×????è?????§??????
struct __FILE
{
int handle;
};
FILE __stdout;
//?¨??_sys_exit()??±???????°??÷?ú????
_sys_exit(int x)
{
x = x;
}
//???¨??fputc????
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//???··???,?±??·????ê±?
USART1->DR = (u8) ch;
return ch;
}
#endif
#include "ATK_HC05.h"
/*********************************************
??????????HC05_Init
??????????????ATK_HC05??LED??KEY????
*********************************************/
u8 HC05_Init(void)
{
u8 retry=10,t;
u8 temp=1;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); //????PORTC??C?±??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; // ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //????????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO????????50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //?ù???è?¨??????????A4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // ????????
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //???ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO????????50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //?ù???è?¨??????????GPIOC4
GPIO_SetBits(GPIOC,GPIO_Pin_4);
MediumInit2_Config();
while(retry--)
{
HC05_KEY=1; //KEY????,????AT????
delay_ms(10);
u2_printf("AT\r\n"); //·???AT????????
HC05_KEY=0; //KEY????,????AT????
for(t=0;t<10;t++) //×??¤????50ms,??????HC05???é??????
{
if(USART2_RX_STA&0X8000)break;
delay_ms(5);
}
if(USART2_RX_STA&0X8000) //????????????????
{
temp=USART2_RX_STA&0X7FFF; //?????????¤??
USART2_RX_STA=0;
if(temp==4&&USART2_RX_BUF[0]=='O'&&USART2_RX_BUF[1]=='K')
{
temp=0;//??????OK?ì??
break;
}
}
}
if(retry==0)temp=1; //?ì???§°?
return temp;
}
/*********************************************
??????????HC05_Get_Role
??????????????ATK-HC05???é??????
*********************************************/
u8 HC05_Get_Role(void)
{
u8 retry=0X0F;
u8 temp,t;
while(retry--)
{
HC05_KEY=1; //KEY????,????AT????
delay_ms(10);
u2_printf("AT+ROLE?\r\n"); //?é??????
for(t=0;t<20;t++) //×??¤????200ms,??????HC05???é??????
{
delay_ms(10);
if(USART2_RX_STA&0X8000)break;
}
HC05_KEY=0; //KEY????,????AT????
if(USART2_RX_STA&0X8000) //????????????????
{
temp=USART2_RX_STA&0X7FFF; //?????????¤??
USART2_RX_STA=0;
if(temp==13&&USART2_RX_BUF[0]=='+')//?????????·????????
{
temp=USART2_RX_BUF[6]-'0';//?????÷????????
break;
}
}
}
if(retry==0)temp=0XFF;//?é???§°?.
return temp;
}
/*********************************************
??????????HC05_Set_Cmd
??????????ATK-HC05?è???ü????????????·???
OK???????ü??
*********************************************/
u8 HC05_Set_Cmd(u8* atstr)
{
u8 retry=0X0F;
u8 temp,t;
while(retry--)
{
HC05_KEY=1; //KEY????,????AT????
delay_ms(10);
u2_printf("%s\r\n",atstr); //·???AT×?·??®
HC05_KEY=0; //KEY????,????AT????
for(t=0;t<20;t++) //×??¤????100ms,??????HC05???é??????
{
if(USART2_RX_STA&0X8000)break;
delay_ms(5);
}
if(USART2_RX_STA&0X8000) //????????????????
{
temp=USART2_RX_STA&0X7FFF; //?????????¤??
USART2_RX_STA=0;
if(temp==4&&USART2_RX_BUF[0]=='O')//?????????·????????
{
temp=0;
break;
}
}
}
if(retry==0)temp=0XFF;//?è???§°?.
return temp;
}
/*********************************************
??????????HC05_CFG_CMD
???????????¨????????,????????USMART,
?÷???????®??2????ATK-HC05???é
*********************************************/
void HC05_CFG_CMD(u8 *str)
{
u8 temp;
u8 t;
HC05_KEY=1; //KEY????,????AT????
delay_ms(10);
u2_printf("%s\r\n",(char*)str); //·???????
for(t=0;t<50;t++) //×??¤????500ms,??????HC05???é??????
{
if(USART2_RX_STA&0X8000)break;
delay_ms(10);
}
HC05_KEY=0; //KEY????,????AT????
if(USART2_RX_STA&0X8000) //????????????????
{
temp=USART2_RX_STA&0X7FFF; //?????????¤??
USART2_RX_STA=0;
USART2_RX_BUF[temp]=0; //???á??·?
printf("\r\n%s",USART2_RX_BUF);//·??????????????®??1
}
}
[/mw_shl_code]