寫了不到一天時間,就自己瞎玩一玩,大家看看,哈哈哈
所有資料51hei提供下載:
實驗40 踩百塊哈哈哈.rar
(1.21 MB, 下載次數: 137)
2017-8-6 17:18 上傳
點擊文件名下載附件
觸摸,打地鼠 下載積分: 黑幣 -5
stm32單片機源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "key.h"
- #include "sram.h"
- #include "malloc.h"
- #include "usmart.h"
- #include "sdio_sdcard.h"
- #include "malloc.h"
- #include "w25qxx.h"
- #include "ff.h"
- #include "timer.h"
- #include "exfuns.h"
- #include "fontupd.h"
- #include "text.h"
- #include "rng.h"
- #include "touch.h"
- #include "rtc.h"
- //ALIENTEK 探索者STM32F407開發板 實驗40
- //漢字顯示 實驗 -庫函數版本
- //清空屏幕并在右上角顯示"RST" t
- u32 qwer;
- u32 random=300;
- u32 dom=300;
- u32 miao;
- void TIM3_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM3,TIM_IT_Update)==SET) //溢出中斷
- {
- miao++;
- LCD_ShowNum(30+3*16,170,miao,5,16); //shijian
- }
- TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中斷標志位
- }
- void Load_Drow_Dialog(void)
- {
- // LCD_Clear(WHITE);//清屏
- POINT_COLOR=BLUE;//設置字體為藍色
- LCD_ShowString(lcddev.width-24,0,200,16,16,"RST");//顯示清屏區域
- POINT_COLOR=RED;//設置畫筆藍色
- }
- ////////////////////////////////////////////////////////////////////////////////
- //電容觸摸屏專有部分
- //畫水平線
- //x0,y0:坐標
- //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);
- }
- //畫實心圓
- //x0,y0:坐標
- //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);
- }
- }
- //兩個數之差的絕對值
- //x1,x2:需取差值的兩個數
- //返回值:|x1-x2|
- u16 my_abs(u16 x1,u16 x2)
- {
- if(x1>x2)return x1-x2;
- else return x2-x1;
- }
- //畫一條粗線
- //(x1,y1),(x2,y2):線條的起始坐標
- //size:線條的粗細程度
- //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; //計算坐標增量
- delta_y=y2-y1;
- uRow=x1;
- uCol=y1;
- if(delta_x>0)incx=1; //設置單步方向
- 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; //選取基本增量坐標軸
- else distance=delta_y;
- for(t=0;t<=distance+1;t++ )//畫線輸出
- {
- gui_fill_circle(uRow,uCol,size,color);//畫點
- xerr+=delta_x ;
- yerr+=delta_y ;
- if(xerr>distance)
- {
- xerr-=distance;
- uRow+=incx;
- }
- if(yerr>distance)
- {
- yerr-=distance;
- uCol+=incy;
- }
- }
- }
- ////////////////////////////////////////////////////////////////////////////////
- //5個觸控點的顏色(電容觸摸屏用)
- const u16 POINT_COLOR_TBL[OTT_MAX_TOUCH]={RED,GREEN,BLUE,BROWN,GRED};
- ////電阻觸摸屏測試函數
- void rtp_test(void)
- {
- u8 key;
- u8 i=0;
- while(1)
- {
- key=KEY_Scan(0);
- tp_dev.scan(0);
- if(tp_dev.sta&TP_PRES_DOWN) //觸摸屏被按下
- {
- if(tp_dev.x[0]<lcddev.width&&tp_dev.y[0]<lcddev.height)
- {
- if(tp_dev.x[0]>(lcddev.width-24)&&tp_dev.y[0]<16)Load_Drow_Dialog();//清除
- else TP_Draw_Big_Point(tp_dev.x[0],tp_dev.y[0],RED); //畫圖
- }
- }else delay_ms(10); //沒有按鍵按下的時候
- if(key==KEY0_PRES) //KEY0按下,則執行校準程序
- {
- LCD_Clear(WHITE); //清屏
- TP_Adjust(); //屏幕校準
- TP_Save_Adjdata();
- Load_Drow_Dialog();
- }
- i++;
- // if(i%20==0)LED0=!LED0;
- }
- }
- //電容觸摸屏測試函數
- void ctp_test(void)
- {
-
- u8 t=0;
- u8 i=0;
- u16 lastpos[5][2]; //最后一次的數據
-
- while(1)
- {
-
- tp_dev.scan(0);
- for(t=0;t<OTT_MAX_TOUCH;t++)
- {
- if((tp_dev.sta)&(1<<t))
- {
- if(tp_dev.x[t]<lcddev.width&&tp_dev.y[t]<lcddev.height)
- {
- if(lastpos[t][0]==0XFFFF)
- {
- lastpos[t][0] = tp_dev.x[t];
- lastpos[t][1] = tp_dev.y[t];
- }
- lcd_draw_bline(lastpos[t][0],lastpos[t][1],tp_dev.x[t],tp_dev.y[t],2,POINT_COLOR_TBL[t]);//畫線
- lastpos[t][0]=tp_dev.x[t];
- lastpos[t][1]=tp_dev.y[t];
-
- if(tp_dev.x[t]>random&&tp_dev.x[t]<random+30&&tp_dev.y[t]>dom&&tp_dev.y[t]<dom+30) //水平方向,可以來進行類似的按鍵檢測
- {
- random=RNG_Get_RandomRange(35,365);//獲取[0,9]區間的隨機數 x
- dom=RNG_Get_RandomRange(235,665);//獲取[0,9]區間的隨機數 y
- delay_ms(10);
- Load_Drow_Dialog();//清除
- delay_ms(10);
- LCD_Fill(33,233,400,700,WHITE);
- LED1=!LED1;
- LCD_Fill(random,dom,random+30,dom+30,BLUE);
- qwer=qwer+17;
- LCD_ShowNum(30+5*16,150,qwer,5,16); //顯示隨機數
- if(qwer>1000)
- {
- Show_Str(30,210,200,16,"您是MVP:",16,0);
- Show_Str(30,210,200,16,"您是MVP:",16,0);
- }
-
- }
- }
- }else lastpos[t][0]=0XFFFF;
- }
-
- delay_ms(5);i++;
- //if(i%20==0)LED0=!LED0;
- }
- }
-
-
- void retc()
- {
- RTC_TimeTypeDef RTC_TimeStruct;
- RTC_DateTypeDef RTC_DateStruct;
- u8 x;
- u8 tbuf[40];
- x++;
- if((x%10)==0) //每100ms更新一次顯示數據 RTC_TimeStruct.RTC_Hours
- {
- RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
-
- sprintf((char*)tbuf,"Time:%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
- LCD_ShowString(30,140,210,16,16,tbuf);
-
- RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
-
- sprintf((char*)tbuf,"Date:20%02d-%02d-%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date);
- LCD_ShowString(30,160,210,16,16,tbuf);
- sprintf((char*)tbuf,"Week:%d",RTC_DateStruct.RTC_WeekDay);
- LCD_ShowString(30,180,210,16,16,tbuf);
-
- }
- }
- void jiemian()
- {
- LCD_Fill(33,33,333,400,GREEN);
- LCD_Fill(533,33,750,400,BLUE);
- }
-
- int main(void)
- {
- RTC_TimeTypeDef RTC_TimeStruct;
- RTC_DateTypeDef RTC_DateStruct;
- u8 x;
- u8 tbuf[40];
- u32 fontcnt;
- u8 i,j;
- u8 t;
- u8 fontx[2];//gbk碼
- u32 miao=0;
- u8 key;//t;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設置系統中斷優先級分組2
- delay_init(168); //初始化延時函數
- uart_init(9600); //初始化串口波特率為115200
- LED_Init(); //初始化LED
- LCD_Init(); //LCD初始化
- KEY_Init(); //按鍵初始化
- RNG_Init();
- //My_RTC_Init(); //初始化RTC
- tp_dev.init(); //觸摸屏初始化
- TIM3_Int_Init(10000-1,8400-1); //定時器時鐘84M,分頻系數8400,所以84M/8400=10Khz的計數頻率,計數5000次為500ms
- W25QXX_Init(); //初始化W25Q128
- usmart_dev.init(84); //初始化USMART
- my_mem_init(SRAMIN); //初始化內部內存池
- my_mem_init(SRAMCCM); //初始化CCM內存池
- exfuns_init(); //為fatfs相關變量申請內存
- f_mount(fs[0],"0:",1); //掛載SD卡
- f_mount(fs[1],"1:",1); //掛載FLASH.
-
-
- //RTC_Set_WakeUp(RTC_WakeUpClock_CK_SPRE_16bits,0); //配置WAKE UP中斷,1秒鐘中斷一次
-
-
- while(font_init()) //檢查字庫
- {
- UPD:
- LCD_Clear(WHITE); //清屏
- POINT_COLOR=RED; //設置字體為紅色
- LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
- // while(SD_Init()) //檢測SD卡
- // {
- // LCD_ShowString(30,70,200,16,16,"SD Card Failed!");
- // delay_ms(200);
- // LCD_Fill(30,70,200+30,70+16,WHITE);
- // delay_ms(200);
- // }
- LCD_ShowString(30,70,200,16,16,"SD Card OK");
- LCD_ShowString(30,90,200,16,16,"Font Updating...");
- key=update_font(20,110,16,"0:");//更新字庫
- while(key)//更新失敗
- {
- LCD_ShowString(30,110,200,16,16,"Font Update Failed!");
- delay_ms(200);
- LCD_Fill(20,110,200+20,110+16,WHITE);
- delay_ms(200);
- }
- LCD_ShowString(30,110,200,16,16,"Font Update Success! ");
- delay_ms(1500);
- LCD_Clear(WHITE);//清屏
- }
- POINT_COLOR=RED;
- jiemian();
- if(tp_dev.touchtype!=0XFF)LCD_ShowString(30,130,200,16,16,"Press KEY0 to Adjust");//電阻屏才顯示
- delay_ms(1500);
- Load_Drow_Dialog();
- LCD_Fill(random,dom,random+30,dom+30,BLUE);
- if(tp_dev.touchtype&0X80)ctp_test();//電容屏測試
- else rtp_test(); //電阻屏測試
- while(1)
- {
- fontcnt=0;
- //retc();
-
-
- Show_Str(30,210,200,16,"您是MVP:",16,0);
-
-
- x++;
- if((x%10)==0) //每100ms更新一次顯示數據 RTC_TimeStruct.RTC_Hours
- {
- RTC_GetTime(RTC_Format_BIN,&RTC_TimeStruct);
-
- sprintf((char*)tbuf,":%02d:%02d:%02d",RTC_TimeStruct.RTC_Hours,RTC_TimeStruct.RTC_Minutes,RTC_TimeStruct.RTC_Seconds);
- LCD_ShowString(530,140,210,24,24,tbuf);
-
- RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
-
- sprintf((char*)tbuf,"日期:20%02d-%02d-%02d",RTC_DateStruct.RTC_Year,RTC_DateStruct.RTC_Month,RTC_DateStruct.RTC_Date);
- LCD_ShowString(530,180,210,24,24,tbuf);
-
- sprintf((char*)tbuf,"Week:%d",RTC_DateStruct.RTC_WeekDay);
- LCD_ShowString(530,220,210,24,24,tbuf);
-
- }
-
-
-
-
-
-
-
-
-
-
-
- for(i=0x81;i<0xff;i++)
- {
- fontx[0]=i;
- // LCD_ShowNum(118,150,i,3,16); //顯示內碼高字節
-
- for(j=0x40;j<0xfe;j++)
- {
- if(j==0x7f)continue;
- fontcnt++;
- // LCD_ShowNum(118,170,j,3,16); //顯示內碼低字節
- // LCD_ShowNum(118,190,fontcnt,5,16);//漢字計數顯示
- fontx[1]=j;
- // Show_Font(30+132,220,fontx,24,0);
- // Show_Font(30+144,244,fontx,16,0);
- // Show_Font(30+108,260,fontx,12,0);
- // t=200;
-
-
- //
- //
- //
- // while(t--)//延時,同時掃描按鍵
- // {
- // delay_ms(1);
- // key=KEY_Scan(0);
- // if(key==KEY0_PRES)goto UPD;
- // }
- // // LED0=!LED0;
- }
- }
- }
- }
復制代碼
|