微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 利用51单片机按键设置ztw电调

利用51单片机按键设置ztw电调

时间:11-19 来源:互联网 点击:
ata, L); //显示字符串//MPU6050操作函数void InitMPU6050(); //初始化MPU6050void Delay5us();void I2C_Start();void I2C_Stop();void I2C_SendACK(bit ack);bit I2C_RecvACK();void I2C_SendByte(uchar dat);uchar I2C_RecvByte();void I2C_ReadPage();void I2C_WritePage();void display_ACCEL_x();void display_ACCEL_y();void display_ACCEL_z();uchar Single_ReadI2C(uchar REG_Address); //读取I2C数据void Single_WriteI2C(uchar REG_Address, uchar REG_data); //向I2C写入数据//////////////////////////////////////////////////////////////////void init_sys(void); /*系统初始化函数*/void Delay5Ms(void);///////////////////////////////////////////////////////////////////函数功能:数码管动态扫描延时/void led_delay(void) {unsigned char j;for (j = 0; j < 200; j++);}/函数功能:按键值的数码管显示子程序/void display(unsigned char k,unsigned char z) {P2 = 0xf7; //点亮数码管DS0 1111 0111P0 = Tab[z / 10]; //显示十位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0xef; //点亮数码管DS1 1110 1111 P0 = Tab[z % 10]; //显示个位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0xbf; //点亮数码管DS6 1011 1111P0 = Tab[k / 10]; //显示十位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;P2 = 0x7f; //点亮数码管DS7 0111 1111P0 = Tab[k % 10]; //显示个位led_delay(); //动态扫描延时led_delay(); //动态扫描延时led_delay(); //动态扫描延时P0 = 0xff;}/函数功能:软件延时子程序/void delay20ms(void) {unsigned char i, j;for (i = 0; i < 100; i++)for (j = 0; j < 60; j++);}////整数转字符串//void lcd_printf(uchar *s, int temp_data) {if (temp_data < 0) {temp_data = -temp_data;*s = -;} else*s = ;*++s = temp_data / 100 + 0x30;temp_data = temp_data % 100; //取余运算*++s = temp_data / 10 + 0x30;temp_data = temp_data % 10; //取余运算*++s = temp_data + 0x30;}////延时 k 毫秒//void delayNms(unsigned int k) {unsigned int i, j;for (i = 0; i < k; i++) {for (j = 0; j < 125; j++);}}////LCD1602初始化//void InitLcd() {WriteCommandLCM(0x38, 1);WriteCommandLCM(0x08, 1);WriteCommandLCM(0x01, 1);WriteCommandLCM(0x06, 1);WriteCommandLCM(0x0c, 1);DisplayOneChar(0, 0, A); DisplayOneChar(0, 1, G); }////LCD1602写允许//void WaitForEnable(void) {DataPort = 0xff;LCM_RS = 0;LCM_RW = 1;_nop_();LCM_EN = 1;_nop_();_nop_();while (DataPort & 0x80);LCM_EN = 0;}////LCD1602写入命令//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;}////LCD1602写入数据//void WriteDataLCM(uchar dataW) {WaitForEnable();LCM_RS = 1;LCM_RW = 0;_nop_();DataPort = dataW;_nop_();LCM_EN = 1;_nop_();_nop_();LCM_EN = 0;}////LCD1602写入一个字符//void DisplayOneChar(uchar X, uchar Y, uchar DData) {Y &= 1;X &= 15;if (Y)X |= 0x40;X |= 0x80;WriteCommandLCM(X, 0);WriteDataLCM(DData);}////LCD1602显示字符串//void DisplayListChar(uchar X, uchar Y, uchar *DData, L) {uchar ListLength = 0;Y &= 0x1;X &= 0xF;while (L--) {DisplayOneChar(X, Y, DData[ListLength]);ListLength++;X++;}}////延时5微秒(STC90C52RC@12M)//不同的工作环境,需要调整此函数//当改用1T的MCU时,请调整此延时函数//void Delay5us() {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}////I2C起始信号//void I2C_Start() {SDA = 1; //拉高数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 0; //产生下降沿Delay5us(); //延时SCL = 0; //拉低时钟线}////I2C停止信号//void I2C_Stop() {SDA = 0; //拉低数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 1; //产生上升沿Delay5us(); //延时}////I2C发送应答信号//入口参数:ack (0:ACK 1:NAK)//void I2C_SendACK(bit ack) {SDA = ack; //写应答信号SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}////I2C接收应答信号//bit I2C_RecvACK() {SCL = 1; //拉高时钟线Delay5us(); //延时CY = SDA; //读应答信号SCL = 0; //拉低时钟线Delay5us(); //延时return CY;}////向I2C

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top