倾斜角测量仪C语言程序.docx
#include<reg52.h>/包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义#include<intrins.h>#include<math.h>#include<stdio.h>#include<absacc.h>#include<ctype.h>#include<stdlib.h>#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 通道自检#define 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;sbit 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; double 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(a<0)a=fabs(a);LCD_Write_String(O,O/X:-H); LCD_Display(O,a);) else LCD_Write_String(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(b<0)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;sbitPl_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 P3_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);unsigned 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_WriteByte( 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 < n ; +i)(rtemp «= 1;SPI_Delay(10);if( MISO = 1 )rtemp |= 0x0001;elsertemp &= OxFFFE;)SCK= 1;SPI_Delay(10);SCK 二 0;SPI_Delay(10);)return rtemp;读X方向角度unsigned int SPI_GetX( void ) (unsigned int result;SPI_Start();SPI_WriteByte( RD AX);result 二 SPI_ReadBits(ll);SPI_Stop();return result;)读丫方向角度unsigned int SPI_GetY( void )(unsigned int result;SPI_Start();SPI_WriteByte( RDAY);result = SPI_ReadBits(ll);SPLStopO;return result;)/SPI测量模式void SPI_Init( void) (SPI_Start();SPI_WriteByte( MEAS );SPI_Stop();) unsigned char code C=O,T,2,3,4,5,6,7,8,9;uS延时函数,含有输入参数unsigned char t,无返回值 unsigned char是定义无符号字符变量,其值的范围是 0-255这里使用晶振12M,精确延时请使用汇编,大致延时 长度如下T=tx2+5 uSvoid DelayUs2x(unsigned char t) while(t);)mS延时函数,含有输入参数unsigned char t,无返回值 unsigned char是定义无符号字符变量,其值的范围是 0-255这里使用晶振12M,精确延时请使用汇编void DelayMs(unsigned char t)while(t)(大致延时ImSDelayUs2x(245);DelayUs2x(245);)判忙函数*/bit LCD_Check_Busy(void)DataPort= OxFF;RS CLR;RW_SET;EN CLR;_nop_();EN SET;return (bit)(DataPort & 0x80);写入命令函数*/void LCD_Write_Com(unsigned char com)while(LCD_Check_Busy();/忙则等待 RS_CLR;RW CLR;EN_SET;DataPort= com;_nop_();EN_CLR;写入数据函数*/void LCD_Write_Data(unsigned char Data)while(LCD_Check_Busy(); /ft则等待 RS.SET;RW CLR;EN.SET;DataPort= Data;_nop_();EN_CLR;清屏函数刃void LCD_Clear(void)(LCD_Write_Com(0x01);DelayMs(5);写入字符串函数*/void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)(if (y = 0) 一LCD_Write_Com(0x80 + x); 表示第一行)else _LCD_Write_Com(0xC0 + x);表示第二行)while (*s)(LCD_Write_Data( *s);s +;)/*写入字符函数*/void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) (if (y = 0)(LCD_Write_Com(0x80 + x);)else(LCD_Write_Com(0xC0 + x);)LCD_Write_Data( Data);角度转化函数 */ float angle_transfer( double result) double angle,f;f=asin(result-1024)/1638);angle二f*l 80/3.14;return(angle);)初始化函数void LCD_Init(void) 一LCD_Write_Com(0x38);/* 显示模式设置 */DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);DelayMs(5);LCD_Write_Com(0x38);LCD_Write_Com(0x()8);/* 显示关闭*/LCD_Write_Com(0x01);/* 显示清屏*/LCD_Write_Com(0x06);/*显示光标移动设置*/DelayMs(5);LCD_Write_Com(OxOC);/*显示开及光标设置*/)LCD显示函数 */ void LCD_Display(unsigned int i,double angle) int m,n,c,d,e,an;an=(int)(angle* 1000);m=an%10;LCD-Write_Char(8,i,Cm);DelayMs(245);DelayMs(245);an=an/10;n=an%10;LCD_Write_Char(7,i,Cn);DelayMs(245);DelayMs(245);an=an/10;c=an%10;LCD_Write_Char(6,i,Cc);DelayMs(245);DelayMs(245);an=an/10;d=an%10;LCD_Write_Char(4,i,Cd);DelayMs(245);DelayMs(245);e=an/10;LCD_Write_Char(3 ,i,Ce);DelayMs(245);DelayMs(245);LCD_Write_Char(5,i;/);)定时器初始化子程序*/void Init_TimerO(void)(TMOD |= 0x01;使用模式1, 16位定时器,使用T符号可以在使用多个定时器时不受影响/TH0=0x00;/TL0=0x00;EA=1;ETO=1;TRO=1;)给定初值总中断打开 定时器中断打开 定时器开关打开定时器中断子程序*/void TimerO_isr(void) interrupt 1(TH0=0xfe;重新赋值TLO=frq;低8位值在主程序中不断累加SPK=!SPK; 端口电平取反报警函数 */ bit Warning(double angle,unsigned char x,unsigned char y) (int g;angle=fabs(angle);if(angle>20)g=0;LCD_Write_String(x,y,"DANGER!");while(!P2_0)