//-----------------------------------------main.c---------------------------------------------- #include <ioCC2530.h> #include <string.h> #include <stdio.h> #include "UART.h" #include "OLED.h" //*------------------------------------------------------------- //*功能说明: 定时器3,可用于系统定时器,System1MsCnt毫秒计时器 //*全局参数: System1MsCnt--毫秒计数器 //*------------------------------------------------------------- unsigned long System1MsCnt=0; void Timer3Init(void) { T3CTL |= 0x08 ; //开溢出中断 T3IE = 1; //开总中断和T3中断 T3CTL &= ~0x03; //自动重装00->0xff T3CTL |= 0x10; //启动 EA = 1; //开总中断 } #pragma vector = T3_VECTOR __interrupt void T3_ISR(void) { static unsigned char cnt=0; IRCON = 0x00; //清中断标志, 也可由硬件自动完成 if(cnt++ > 51) //1ms 示波器观察1.02ms { //经过示波器测量确保精确 cnt = 0; //计数清零 System1MsCnt++; } } void clock_setup(void) { CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振 while(CLKCONSTA & 0x40); //等待晶振稳定 CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ Timer3Init(); //可选项,系统1ms计数器 } void InitBuzzer(void) { P1DIR |= 0x08; //P1_3 定义为输出 BUZZER = 0; //BUZZER 初始化关闭 } void init_sensor_io(void) { //1.设置IO为普通IO P0SEL |= 0xC0; P1SEL |= 0x07; //2.设置编码口为输入 P0DIR &= ~0xC0; P1DIR &= ~0x07; } uchar get_sensor_id(void) { volatile uchar temp = P0_6<<4|P0_7<<3|P1_0<<2|P1_1<<1|P1_2; return temp; } void InitKey(void) { P2DIR &= ~0x01; //P20定义为输入 KEY = 1; } uchar CheckKey(void) { if(KEY == 0) { Delay_ms(10); if(KEY == 0) { KEY = 1; return 1; } } return 0; } void main(void) { uchar temp8; uchar buf[16]; uchar yy[20]; clock_setup(); //系统时钟设置,精确延时必须要设置 Delay_ms(1000); //让设备稳定 port_init(); initial_lcd(); InitBuzzer(); InitUart(9600); clear_screen(); //清屏 disp_string_8x16_16x16(1,1,"武汉唯众智创科技"); //显示字符串,括号里的参数分别为(PAGE,列,字符串指针) disp_string_8x16_16x16(3,1,"----实验3.2-----"); Delay_ms(1000); while(1) { temp8 = get_sensor_id(); if(temp8 != 2){ disp_string_8x16_16x16(1,1,"请插语音识别模块"); disp_string_8x16_16x16(3,1,"----实验3.2-----"); Delay_ms(1000); continue; } temp8 = UartRecvBytes(yy,0,50); if(temp8 > 2){ clear_screen(); disp_string_8x16_16x16(1,1,"识别完成:"); if(strstr(yy,"YES")){ //一级指令"小唯"识别成功 disp_string_8x16_16x16(3,1,"请说出指令"); }else if(strstr(yy,"REPEAT")){ //请说一级指令"小唯" disp_string_8x16_16x16(3,1,"请说'小唯'!"); }else if(strstr(yy,"OK:")){ //识别成功 sscanf(yy+3,"%[^\n]\n",buf);//跳过"OK:" if(strstr(yy,"KD")){ disp_string_8x16_16x16(3,1,"开灯!"); }else if(strstr(yy,"GD")){ disp_string_8x16_16x16(3,1,"关灯!"); } } } } } //-----------------------------------------UART.c---------------------------------------------- #include <ioCC2530.h> #include "UART.H" #include "Delay.h" // 串口初始化函数 void InitUart(unsigned int baud) { PERCFG = 0x00; //位置1 P0口 P0SEL = 0x0c; //P0_2,P0_3用作串口,第二功能 P2DIR &= ~0xC0; //P0 优先作为UART0 ,优先级 U0CSR |= 0xC0; //UART 方式 switch(baud){ case 9600: U0GCR |= 8; //U0GCR与U0BAUD配合 U0BAUD |= 59; //波特率设为9600 break; case 57600: U0GCR |= 10; U0BAUD |= 216; //波特率设为57600 break; case 115200: U0GCR |= 11; U0BAUD |= 216; //特率设为115200 break; } UTX0IF = 0; //UART0 TX 中断标志初始置位0 URX0IF = 0; IEN0 |= 0x80; //开总中断 Timer3Init(); //开启定时器3,用于超时判定 } //串口发送函数 void UartSendBytes(unsigned char *Data, int len) { int j; for(j=0;j<len;j++) { U0DBUF = *Data++; while(UTX0IF == 0); UTX0IF = 0; } } //*------------------------------------------------------------- //*功能说明: 串口接收函数,使用T3超时判定 //*入口参数: *Data--接收缓存,要足够大 //* len--需要接收的长度,0表示接收最大长度 //* time--超时长度,单位ms //*------------------------------------------------------------- extern unsigned long System1MsCnt; int UartRecvBytes(unsigned char *Data,int len,unsigned int time) { unsigned int i=0; unsigned long curtime = System1MsCnt; while(1){ if(URX0IF == 1){ Data[i++] = U0DBUF; URX0IF = 0; } if(len!=0 && i==len+1){ //接收到了指定的长度 return len; } if(System1MsCnt-curtime>time){ break; //超时,接收多少字节就返回多少字节 } } return i; } |