|
stm32手寫識(shí)別
單片機(jī)源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "malloc.h"
- #include "sdio_sdcard.h"
- #include "w25qxx.h"
- #include "ff.h"
- #include "exfuns.h"
- #include "text.h"
- #include "atk_ncr.h"
- #include "touch.h"
- //最大記錄的軌跡點(diǎn)數(shù)
- atk_ncr_point READ_BUF[200];
- //畫水平線
- //x0,y0:坐標(biāo)
- //len:線長度
- //color:顏色
- void gui_draw_hline(u16 x0,u16 y0,u16 len,u16 color)
- {
- if(len==0)return;
- LCD_Fill(x0,y0,x0+len-1,y0,color);
- }
- //畫實(shí)心圓
- //x0,y0:坐標(biāo)
- //r:半徑
- //color:顏色
- void gui_fill_circle(u16 x0,u16 y0,u16 r,u16 color)
- {
- u32 i;
- u32 imax = ((u32)r*707)/1000+1;
- u32 sqmax = (u32)r*(u32)r+(u32)r/2;
- u32 x=r;
- gui_draw_hline(x0-r,y0,2*r,color);
- for (i=1;i<=imax;i++)
- {
- if ((i*i+x*x)>sqmax)// draw lines from outside
- {
- if (x>imax)
- {
- gui_draw_hline (x0-i+1,y0+x,2*(i-1),color);
- gui_draw_hline (x0-i+1,y0-x,2*(i-1),color);
- }
- x--;
- }
- // draw lines from inside (center)
- gui_draw_hline(x0-x,y0+i,2*x,color);
- gui_draw_hline(x0-x,y0-i,2*x,color);
- }
- }
- //兩個(gè)數(shù)之差的絕對(duì)值
- //x1,x2:需取差值的兩個(gè)數(shù)
- //返回值:|x1-x2|
- u16 my_abs(u16 x1,u16 x2)
- {
- if(x1>x2)return x1-x2;
- else return x2-x1;
- }
- //畫一條粗線
- //(x1,y1),(x2,y2):線條的起始坐標(biāo)
- //size:線條的粗細(xì)程度
- //color:線條的顏色
- void lcd_draw_bline(u16 x1, u16 y1, u16 x2, u16 y2,u8 size,u16 color)
- {
- u16 t;
- int xerr=0,yerr=0,delta_x,delta_y,distance;
- int incx,incy,uRow,uCol;
- if(x1<size|| x2<size||y1<size|| y2<size)return;
- delta_x=x2-x1; //計(jì)算坐標(biāo)增量
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1; //設(shè)置單步方向
- else if(delta_x==0)incx=0;//垂直線
- else {incx=-1;delta_x=-delta_x;}
- if(delta_y>0)incy=1;
- else if(delta_y==0)incy=0;//水平線
- else{incy=-1;delta_y=-delta_y;}
- if( delta_x>delta_y)distance=delta_x; //選取基本增量坐標(biāo)軸
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )//畫線輸出
- {
- gui_fill_circle(uRow,uCol,size,color);//畫點(diǎn)
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- int main(void)
- {
- u8 i=0;
- u8 tcnt=0;
- u8 res[10];
- u8 key;
- u16 pcnt=0;
- u8 mode=4; //默認(rèn)是混合模式
- u16 lastpos[2]; //最后一次的數(shù)據(jù)
-
- delay_init(); //延時(shí)函數(shù)初始化
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置中斷優(yōu)先級(jí)分組為組2:2位搶占優(yōu)先級(jí),2位響應(yīng)優(yōu)先級(jí)
- uart_init(115200); //串口初始化為115200
- LED_Init(); //初始化與LED連接的硬件接口
- KEY_Init(); //初始化按鍵
- LCD_Init(); //初始化LCD
- W25QXX_Init(); //初始化W25Q128
- tp_dev.init(); //初始化觸摸屏
- my_mem_init(SRAMIN); //初始化內(nèi)部內(nèi)存池
- exfuns_init(); //為fatfs相關(guān)變量申請(qǐng)內(nèi)存
- f_mount(fs[0],"0:",1); //掛載SD卡
- f_mount(fs[1],"1:",1); //掛載FLASH.
- alientek_ncr_init(); //初始化手寫識(shí)別
- POINT_COLOR=RED;
- while(font_init()) //檢查字庫
- {
- LCD_ShowString(30,50,200,16,16,"Font Error!");
- delay_ms(200);
- LCD_Fill(30,50,240,66,WHITE);//清除顯示
- }
- RESTART:
- Show_Str(30,10,200,16,"戰(zhàn)艦 STM32開發(fā)板",16,0);
- Show_Str(30,30,200,16,"手寫識(shí)別實(shí)驗(yàn)",16,0);
- Show_Str(30,50,200,16,"正點(diǎn)原子@ALIENTEK",16,0);
- Show_Str(30,70,200,16,"KEY0:MODE KEY_UP:Adjust",16,0);
- Show_Str(30,90,200,16,"識(shí)別結(jié)果:",16,0);
- LCD_DrawRectangle(19,114,lcddev.width-20,lcddev.height-5);
- POINT_COLOR=BLUE;
- Show_Str(96,207,200,16,"手寫區(qū)",16,0);
- tcnt=100;
- tcnt=100;
- while(1)
- {
- key=KEY_Scan(0);
- if(key==WKUP_PRES&&(tp_dev.touchtype&0X80)==0)
- {
- TP_Adjust(); //屏幕校準(zhǔn)
- LCD_Clear(WHITE);
- goto RESTART; //重新加載界面
- }
- if(key==KEY0_PRES)
- {
- LCD_Fill(20,115,219,314,WHITE);//清除當(dāng)前顯示
- mode++;
- if(mode>4)mode=1;
- switch(mode)
- {
- case 1:
- Show_Str(80,207,200,16,"僅識(shí)別數(shù)字",16,0);
- break;
- case 2:
- Show_Str(64,207,200,16,"僅識(shí)別大寫字母",16,0);
- break;
- case 3:
- Show_Str(64,207,200,16,"僅識(shí)別小寫字母",16,0);
- break;
- case 4:
- Show_Str(88,207,200,16,"全部識(shí)別",16,0);
- break;
- }
- tcnt=100;
- }
- tp_dev.scan(0);//掃描
- if(tp_dev.sta&TP_PRES_DOWN)//有按鍵被按下
- {
- delay_ms(1);//必要的延時(shí),否則老認(rèn)為有按鍵按下.
- tcnt=0;//松開時(shí)的計(jì)數(shù)器清空
- if((tp_dev.x[0]<(lcddev.width-20-2)&&tp_dev.x[0]>=(20+2))&&(tp_dev.y[0]<(lcddev.height-5-2)&&tp_dev.y[0]>=(115+2)))
- {
- if(lastpos[0]==0XFFFF)
- {
- lastpos[0]=tp_dev.x[0];
- lastpos[1]=tp_dev.y[0];
- }
- lcd_draw_bline(lastpos[0],lastpos[1],tp_dev.x[0],tp_dev.y[0],2,BLUE);//畫線
- lastpos[0]=tp_dev.x[0];
- lastpos[1]=tp_dev.y[0];
- if(pcnt<200)//總點(diǎn)數(shù)少于200
- {
- if(pcnt)
- {
- if((READ_BUF[pcnt-1].y!=tp_dev.y[0])&&(READ_BUF[pcnt-1].x!=tp_dev.x[0]))//x,y不相等
- {
- READ_BUF[pcnt].x=tp_dev.x[0];
- READ_BUF[pcnt].y=tp_dev.y[0];
- pcnt++;
- }
- }else
- {
- READ_BUF[pcnt].x=tp_dev.x[0];
- READ_BUF[pcnt].y=tp_dev.y[0];
- pcnt++;
- }
- }
- }
- }else //按鍵松開了
- {
- lastpos[0]=0XFFFF;
- tcnt++;
- delay_ms(10);
- //延時(shí)識(shí)別
- i++;
- if(tcnt==40)
- {
- if(pcnt)//有有效的輸入
- {
- printf("總點(diǎn)數(shù):%d\r\n",pcnt);
- alientek_ncr(READ_BUF,pcnt,6,mode,(char*)res);
- printf("識(shí)別結(jié)果:%s\r\n",res);
- pcnt=0;
- POINT_COLOR=BLUE;//設(shè)置畫筆藍(lán)色
- LCD_ShowString(30+72,90,200,16,16,res);
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
實(shí)驗(yàn)45 手寫識(shí)別實(shí)驗(yàn).rar
(1.82 MB, 下載次數(shù): 14)
2018-8-11 17:21 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|