如題 我只想下個別的資料但是積分不夠,程序有51單片機版本的和pic單片機的
0.png (11.74 KB, 下載次數(shù): 55)
下載附件
2017-10-16 00:03 上傳
所有資料51hei提供下載:
GY-26電子指南針模塊電子羅盤模塊+機器人配件送資料.rar
(628.2 KB, 下載次數(shù): 50)
2017-10-15 19:22 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
單片機源程序如下:
- // GY-26電子指南針模塊測試程序
- // 使用單片機:STC89C52
- // 晶振: 11.0592 M
- // 液晶屏:LCD1602
- #include<REG52.H>
- #include<math.h> //Keil library
- #include<stdio.h> //Keil library
- #include<INTRINS.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define DataPort P0 //LCD1602 數(shù)據(jù)端口
- sbit LCM_RS=P2^0; //LCD1602 控制端口
- sbit LCM_RW=P2^1; //LCD1602 控制端口
- sbit LCM_EN=P2^2; //LCD1602 控制端口
- sbit KEY_1 =P2^6;
- sbit KEY_2 =P2^5;
- sbit KEY_3 =P2^4;
- sbit KEY_4 =P2^3;
- //********定義變量*****************************
- uchar BUF[8]; //數(shù)據(jù)緩存區(qū)
- uchar cnt; //接收數(shù)據(jù)的累計值
- //********定義函數(shù)*****************************
- void delay(unsigned int k);
- void InitLcd();
- void WriteDataLCM(uchar dataW);
- void WriteCommandLCM(uchar CMD,uchar Attribc);
- void DisplayOneChar(uchar X,uchar Y,uchar DData);
- void SeriPushSend(uchar send_data);
- void ReadKey(void);
- void display(void);
- //*********************************************
- //串口中斷
- void serial_serve(void) interrupt 4
- {
- if(RI==1){
- RI=0;
- BUF[cnt]=SBUF;
- cnt++;
- // if(cnt==7)flag=1;
- }
- }
- //*********************************************
- //串口初始化
- //9600 bps @ 11.059 MHz
- void init_uart()
- {
- TMOD=0x21;
- TH1=0xfd;
- TL1=0xfd;
- SCON=0x50;
- PS=1; //串口中斷設為高優(yōu)先級別
- TR0=1; //啟動定時器
- TR1=1;
- ET0=1; //打開定時器0中斷
- ES=1;
- }
- //*********LCD1602初始化**********************
- void InitLcd()
- {
- WriteCommandLCM(0x38,1);
- WriteCommandLCM(0x08,1);
- WriteCommandLCM(0x01,1);
- WriteCommandLCM(0x06,1);
- WriteCommandLCM(0x0c,1);
- }
- //**********檢測忙信號************************
- void WaitForEnable(void)
- {
- DataPort=0xff;
- LCM_RS=0;LCM_RW=1;_nop_();
- LCM_EN=1;_nop_();_nop_();
- while(DataPort&0x80);
- LCM_EN=0;
- }
- //**********寫命令至LCD***********************
- void WriteCommandLCM(uchar CMD,uchar Attribc)
- {
- if(Attribc)WaitForEnable();
- LCM_RS=0;LCM_RW=0;_nop_();
- DataPort=CMD;_nop_();
- LCM_EN=1;_nop_();_nop_();LCM_EN=0;
- }
- //**********寫數(shù)據(jù)至LCD************************
- void WriteDataLCM(uchar dataW)
- {
- WaitForEnable();
- LCM_RS=1;LCM_RW=0;_nop_();
- DataPort=dataW;_nop_();
- LCM_EN=1;_nop_();_nop_();LCM_EN=0;
- }
- //*********寫一個字符數(shù)據(jù)到指定的目標***********
- void DisplayOneChar(uchar X,uchar Y,uchar DData)
- {
- Y&=1;
- X&=15;
- if(Y)X|=0x40;
- X|=0x80;
- WriteCommandLCM(X,0);
- WriteDataLCM(DData);
- }
- //**********延時函數(shù)***************
- void delay(unsigned int k)
- {
- unsigned int i,j;
- for(i=0;i<k;i++)
- {
- for(j=0;j<121;j++)
- {;}
- }
- }
- //*********串口數(shù)據(jù)發(fā)送******************
- void SeriPushSend(uchar send_data)
- {
- SBUF=send_data;
- while(!TI);TI=0;
- }
- //*********按鍵檢測**********************
- void ReadKey(void)
- {
- EA=0;
- if(KEY_1==0){
- delay(50);
- if(KEY_1==0){
- SeriPushSend(0XC0); //發(fā)送校準命令。
- display();
- while(KEY_1==0); //等待按鍵放開
- }
- }
- if(KEY_2==0){
- delay(50);
- if(KEY_2==0){
- SeriPushSend(0XC1); //發(fā)送停止校準命令。
- display();
- while(KEY_2==0); //等待按鍵放開
- }
- }
- if(KEY_3==0){
- delay(50);
- if(KEY_3==0){
- SeriPushSend(0XA0); //發(fā)送恢復出廠命令。
- display();
- SeriPushSend(0XAA); //發(fā)送恢復出廠命令。
- display();
- SeriPushSend(0XA5); //發(fā)送恢復出廠命令。
- display();
- SeriPushSend(0XC5); //發(fā)送恢復出廠命令。
- display();
- while(KEY_3==0); //等待按鍵放開
- }
- }
- if(KEY_4==0){
- delay(50);
- if(KEY_4==0){
- SeriPushSend(0X3); //發(fā)送設定磁偏角高8位命令。
- display();
- SeriPushSend(0X0); //發(fā)送磁偏角高8位的數(shù)據(jù)。
- display();
- SeriPushSend(0X4); //發(fā)送設定磁偏角低8位命令
- display();
- SeriPushSend(0X64); //發(fā)送磁偏角低8位的數(shù)據(jù)。此時磁偏角被設定為10.0度
- display();
- while(KEY_4==0); //等待按鍵放開
- }
- }
- /*
- if(KEY_5==0){
- delay(50);
- if(KEY_4==0){
- SeriPushSend(0XAE); //發(fā)送當前角度為0度命令。
- display();
- while(KEY_4==0); //等待按鍵放開
- }
- }
- */
- EA=1;
-
- }
- //*******************數(shù)據(jù)接收并顯示**********************
- void display(void)
- {
- uchar SUM;
- uint i ;
- for (i=0;i<3000;i++); //傳送數(shù)據(jù)延時
- if(BUF[0]==0X0D&&BUF[1]==0X0A){ //幀頭判斷
- SUM=BUF[6]+BUF[5]+BUF[4]+BUF[3]+BUF[2]+BUF[1]+BUF[0]; //校驗和
- if (BUF[7]==SUM){ //校驗和判斷
- DisplayOneChar(0,0,BUF[2]); //角度百位值 寫入LCD,因為是字符類型,所以不用轉換,直接顯示
- DisplayOneChar(1,0,BUF[3]); //角度十位值 寫入LCD
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
|