本帖最后由 GERALDWONG 于 2014-4-8 16:45 编辑
USART中断接收里数组接着数据,判断,若正确,则用DMA输出我想要输出的文字。
tool.c:
//------------------TIM2始初化-----------------//
void TIM2_init()
{
CLK_PeripheralClockConfig(CLK_Peripheral_TIM2,ENABLE);
TIM2_TimeBaseInit(TIM2_Prescaler_16,TIM2_CounterMode_Up, 1000);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_ITConfig(TIM2_IT_Update, ENABLE);
TIM2_Cmd(ENSABLE);
}
//----------------DMA始初化---------------------//
void DMA_UARTSendData(unsigned char length)
{
CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
DMA_GlobalDeInit();
DMA_DeInit(DMA1_Channel1);
DMA_Init(DMA1_Channel1, (uint16_t)(&print[0]), 0x5231,
length, DMA_DIR_MemoryToPeripheral,DMA_Mode_Normal,
DMA_MemoryIncMode_Inc, DMA_Priority_High, DMA_MemoryDataSize_Byte);
USART_DMACmd(USART1, USART_DMAReq_TX, ENABLE);
DMA_ITConfig(DMA1_Channel1,DMA_ITx_TC,ENABLE);
DMA_GlobalCmd(ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);//
}
//----------------USART始初化-----------------//
void USART_init(void)
{
USART_DeInit(USART1);
USART_ClockInit(USART1,USART_Clock_Enable,USART_CPOL_Low,USART_CPHA_2Edge,USART_LastBit_Disable);//可要可不要
CLK_PeripheralClockConfig(CLK_Peripheral_USART1, ENABLE);
USART_Init(USART1,115200,USART_WordLength_8b,USART_StopBits_1,USART_Parity_No,USART_Mode_Rx|USART_Mode_Tx);//
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
}
stm8l15x_it.c:
//------------------DMA中断--------------------//
INTERRUPT_HANDLER(DMA1_CHANNEL0_1_IRQHandler, 2)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
//while (1);
disableInterrupts();
if(DMA_GetITStatus(DMA1_IT_TC1)!=RESET)
{
DMA_ClearITPendingBit(DMA1_IT_TC1);
DMA_Cmd(DMA1_Channel1, DISABLE);
usart_sendlength=0;
}
enableInterrupts();
}
//-------------------------TIM2溢出中断-------------------------//
INTERRUPT_HANDLER(TIM2_UPD_OVF_TRG_BRK_IRQHandler, 19)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
//while (1);
disableInterrupts();
//dingshi++;
TIM2irq_flag=1;
/* Cleat Interrupt Pending bit */
TIM2_ClearITPendingBit(TIM2_IT_Update);
enableInterrupts();
}
//----------------------USART接收中断------------------//
INTERRUPT_HANDLER(USART1_RX_IRQHandler, 28)
/* In order to detect unexpected events during development,
{
it is recommended to set a breakpoint on the following instruction.
*/
{
//while(1);
disableInterrupts();
if(USART_GetITStatus(USART1, USART_IT_RXNE)!=RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
print[usart_sendlength]=USART_ReceiveData8(USART1); //用数组print来承载接收到的数据
usart_sendlength++; //数组元素位置+1
usart_freecount=0;//只要接收到数据,该标志位为0;
usart_count_task=1;//证明接收到数据
}
enableInterrupts();
}
main.c:
void Task_assigement(void)
{
if(TIM2irq_flag==1)
{
TIM2irq_flag=0;
if(usart_count_task==1)//接收中断触发usart_count_task=1
{
usart_freecount++;
if(usart_freecount>=2)//判断接收的数组是否连续
{
usart_freecount=0;
usart_count_task=0;
pocceed_signal=1;
}
}
}
if(pocceed_signal==1)
{
pocceed_signal=0;
password();//输入数据函数
}
}
unsigned char command1[4]={'1','2','3','4'};
void password(void)
{
if(String_compared(command1,4))//校验输入的是否为1234
{
u8 i;
usart_sendlength=sizeof("\r\n请输入您需要的功能\r\n1:对时\r\n2:遥信\r\n3:数据记录\r\n4:故障波形\r\n");
for(i=0;i<usart_sendlength;i++)
print=printmessage1;
DMA_UARTSendData(usart_sendlength);
usart_sendlength=0;
}
}
//--------------------------------字符逐个比较----------------------------//
unsigned char String_compared( unsigned char usartcommand[],unsigned char n)
{unsigned char i=0,j=0;
for(i=0;i<usart_sendlength;i++)
{
if(print(i)==usartcommand(i)//逐个判断接收到的数据和已经定义好的数据
{
j++;
}
else //如果任意一个不同,则跳出,节约运行时间
{
break;
}
}
usart_sendlength=0;
if(j==n)//如果接收到的数组和已经定义好的数组里的元素全部相同
{
return 1;
}
else //如果不相同
{
return 0;
}
}
结果失败,后来把判断的部分不要,直接将接收到的数据用DMA打印出来,但是都漏掉后两位数据,例如我输入123只显示1,我输入1234只显示14,我输入123456只显示14,输入123456789只显示147,这个弄了好久,望大神赐教!
|
|