u16 UDR[3];
u16 u16_data;
u16 DATA_R;
u16 DATA_G;
u16 DATA_B;
u8 RXB8=0;
u8 address=1;
u8 address_counter=0;
u8 counter=0;
u8 start_code=0;
u8 break1=0;
void Uart_Init()
{
UART1_DeInit();
UART1_Init((u32)250000,UART1_WORDLENGTH_9D, UART1_STOPBITS_1, \
UART1_PARITY_NO,UART1_SYNCMODE_CLOCK_DISABLE,UART1_MODE_RX_ENABLE);
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE); //开启接收中断
UART1_Cmd(ENABLE);
}
int main(void)
{
/* 系统时钟初始化 */
SystemClock_Init();
//CLK->PCKENR1|=0X04;
//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//设置时钟为内部时钟16MHz
GPIO_Init(GPIOA,GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST ); /*!< Output push-pull, high level, 10MHz */
GPIO_Init(GPIOC,GPIO_PIN_3, GPIO_MODE_OUT_PP_HIGH_FAST );
GPIO_Init(GPIOC,GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST );
GPIO_WriteLow(GPIOA,GPIO_PIN_3);//高电平点亮
GPIO_WriteLow(GPIOC,GPIO_PIN_4);
GPIO_WriteLow(GPIOC,GPIO_PIN_3);
Uart_Init();
enableInterrupts();
while(1)//while(!(进入中断的标志=0))
{
GPIO_WriteHigh(GPIOA,GPIO_PIN_3);
}
}
#pragma vector=20
__interrupt void UART1_RX_IRQHandler(void)
{
//static u8 i=0;
GPIO_WriteLow(GPIOA,GPIO_PIN_3);
GPIO_WriteHigh(GPIOC,GPIO_PIN_4);
//UART1_ClearITPendingBit(UART1_IT_RXNE);
u16_data=UART1_ReceiveData9();
RXB8=u16_data&0X0100;
if(UART1_GetITStatus(UART1_IT_RXNE)!=RESET)
{
if(!RXB8)
{
break1=1;
start_code=0;
address_counter=0;
}
if(break1)
{
u16_data=UART1_ReceiveData9();
if((counter==0)&&(u16_data==0))
{
start_code=1;
}
else if(start_code)
{
address_counter++;
if(address_counter==address)
{
DATA_R=UART1_ReceiveData9();
}
if(address_counter==address+1)
{
DATA_G=UART1_ReceiveData9();
}
if(address_counter==address+2)
{
DATA_B=UART1_ReceiveData9();
}
if(address_counter>512)
{
address_counter=0;
break1=0;
start_code=0;
}
}
}
}
/* if(UART1_GetITStatus(UART1_IT_RXNE)!=RESET)
{
if(UART1->CR1&=0X00){n=0;return;}//接收数据的第九位
if((n>=address)&&(n<address+3))
UDR[i]=UART1_ReceiveData9();
i++;
//RXB8=(UDR[i]&0x0008);
UART1_ClearITPendingBit(UART1_IT_RXNE);
} */
GPIO_WriteHigh(GPIOC,GPIO_PIN_3);//landeng
}
|