《倾斜角测量仪C语言程序.docx》由会员分享,可在线阅读,更多相关《倾斜角测量仪C语言程序.docx(10页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、#include/包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include#include#include#include#include#include#define RS_CLR RS=O#define RS_SET RS=1#define RW_CLR RW=O#define RW_SET RW=1#define EN_CLR EN=O#define EN_SET EN=1#define DataPort PO#define MEAS 0x00 测量模式#define RWTR 0x08 /读写温度数据寄存器#define STX OxOE /x 通道自检#defi
2、ne STY OxOF /Y 通道自检#define RDAX Ox 10 /读X通道加速度值#define RDAY Ox 11 读Y通道加速度值sbit RS = P2A4;定义端口sbit RW = P2A5;sbit EN = P2A6;sbit SPK=P2A0;sbit SCK=P1A7;sbit MISOP 1 八6;sbit MOSI=P1A5;sbit CSB=P1A4;unsigned char frq;sbit P0_0 = P0A0;sbit P0 1 =P0Al;sbit P0 2 = P0A2;sbit P0_3 = P0A3;sbit P0 4 = P0A4;sb
3、it P0_5 = P0A5;Init_TimerO(); 初始化定时器DelayMs(l);延时1ms,累加频率值 frq+;)else(LCD_Write_String(x,y,H”);EAR;g=l;) return(g);)发送函数 */ void Send(float a)(TMOD=0x20;TLl=0xE6;THl=0xE6;PCON=OxOO;TRE;SCON=0x50;/*串口 8位异步收发,波特率可变*/if(RI=l)RI=0;SBUF=a;while(TI!=l);TI=0;)主函数*/ void main(void)unsigned int result; doubl
4、e angle,a,b; LCD_Init();LCD_Clear(); 清屏 SPI_Reset();SPI_Delay(1000);SPI_Init();SPI_Delay(lOOO);while (1)(result = SPI_GetX(); angle=angle_transfer(result); a=angle;Send(a);P2_0=Warning(angle,9,0);P2_l=Warning(angle,9,0); if(a0)a=fabs(a);LCD_Write_String(O,O/X:-H); LCD_Display(O,a);) else LCD_Write_S
5、tring(090,nX:+n);LCD_Display(O,a);)result = SPI_GetY(); angle=angle_transfer(result); b=angle;Send(b);P2_0=Warning(angle,9,1);P2_2=Warning(angle,9 J); if(b0)b=fabs(b);LCD_Write_String(O,l /Y:-n); LCD_Display(hb);elseLCD_Write_String(O;l/Y:+H); LCD_Display(l,b);)sbit PO 6 = P0A6; sbit P0_7 = P0A7;sbi
6、tPl_O = PlAO; sbit Pl 1 =P1A1; sbit Pl 2 = P1A2; sbitPl_3 = PlA3; sbitPl_4 = PlA4; sbitPl_5 = PlA5; sbitPl_6 = PlA6; sbit Pl 7 = P1A7;sbit P2_0 = P2A0; sbit P2_l =P2A1; sbit P2 2 = P2A2; sbit P2 3 = P2A3; sbit P2_4 = P2A4; sbit P2_5 = P2A5; sbit P2_6 = P2A6; sbit P2 7 = P2A7;sbit P3_O = P3AO; sbit P
7、3_l =P3A1; sbit P3_2 = P3A2; sbit P3 3 = P3A3; sbit P3_4 = P3A4; sbit P3_5 = P3A5; sbit P3 6 = P3A6; sbit P3_7 二 P3A7;unsigned int SPI_ReadBits(unsigned char n ); void SPI_WriteByte( unsigned char wdata ); void SPI_Start(void);void SPI_Stop(void);void SPI_Reset(void);void SPI_Delay(unsigned char);un
8、signed int SPI_GetX( void );unsigned int SPI_GctY( void );void SPI_Init( void);void SPI_Stop( void)SCK = 0;CSB = 1;SPI_Delay(10);)void SPI_Reset(void)(SPI_Stop();)void SPI_Delay(unsigned char n)(while(n);)void SPI_Start(void)(SCK = 0;CSB = 0;SPI_Delay(10);)主机写一个BYTE到器件最终状态:CLK = LOWvoid SPI_WriteByt
9、e( unsigned char wdata ) (unsigned char i;for( i = 0 ; i 8 ; +i)(SCK = 0;SPI_Delay(10);MOSI 二 wdata & 0x80;wdata = wdata 1;SPI_Delay(10);SCK= 1;SPI_Delay(10);)SCK = 0;)主机读入N个比特/n限制为16及其以下结束状态CLK = 0unsigned int SPI_ReadBits( unsigned char n ) (unsigned int rtemp,i;MISO = 1;for( i = 0 ; i 20)g=0;LCD_Write_String(x,y,DANGER!);while(!P2_0)