L3G4200D 51单片机IIC 测试程序

合集下载

51单片机输入信号检测-汇编程序

51单片机输入信号检测-汇编程序

51单片机输入信号检测-汇编程序ORG 0000H ;(程序初地址)AJMP START ;(跳转主程序入口)ORG 000BH ;(定义定时器0入口地址)LJMP QJ ;(跳转到子程序判断6秒到了没有)ORG 0030H ;(定义主程序入口地址)START:LCALL DELAY ;(调用延时程序)MOV P1,#00H ;(将P1口清零)MOV R3,#20 ;(延时程序开始-2秒)F1:MOV R2,#100 ; 。

F2:MOV R1,#250 ; 。

F3:NOP ; 。

NOP ; 。

DJNZ R1,F3 ; 。

DJNZ R2,F2 ; 。

DJNZ R3,F1 ;(延时程序结束)MOV P1,#0FFH ;(将P1口置一)MOV P0,#00H ;(检测信号输入用I/O口清0)MOV B,#0FEH ;(置要对比的板号)AMOV TMOD,#01H ;(定义定时器工作方式为1)MOV TH0,#3CH ;(为定时器低8位赋初值)MOV TL0,#0B0H ;(为定时器高8位赋初值)MOV IE,#82H ;(打开定时器0中断)MOV SP,#0020H ;(定义堆栈底部)ISELF:JB P2.0,ISELF ;(检测是否按下启动按键)LCALL DISP ;(延时消抖)LCALL DISP ;(延时消抖)JB P2.0,ISELF ;(检测是否按下启动按键)JB P2.1,TJ1 ;(检测是选择A方式或者B方式进行测试)LJMP TJ ;(检测到是低电平就用B方式进行测试)TJ1:MOV R0,#08H ;(置入要检测的线路板数量)MOV R3,#01H ;(置要对比的板号)MOV R4,#00H ;(为记录错误的显示灯置初值)MOV R5,#120 ;(为定时器构成6秒置循环次数)MOV R6,#70 ;(置入检测完要延时的时间0.7秒)MOV R7,#01H ;(置查表用的第一个数字)LCALL DISP ;(显示错误-LED灯,显示板号-数码管)ASELF:SETB TR0 ;(打开定时器0)LCALL DISP ;(显示错误-LED灯,显示板号-数码管)MOV A,B ;(将要供电的板号送累加器A)MOV P1,A ;(打开线路板供电)MOV A,R3 ;(置要对比的板号)MOV 60H,P0 ;(输入信号送到60H地址单元)ANL 60H,A ;(只保留当前正工作的线路板的信号)XRL A,60H ;(将P0口与待对比值相比较,用来判断是否来信号)JB F0,QJ2 ;(检测6秒内是否有信号输入)JNZ ASELF ;(判断是否有信号输入)LCALL TK ;(将定时器0重置4.25秒定时)BSELF:LCALL DISP ;(显示错误-LED灯,显示板号-数码管)MOV A,R3 ;(置要对比的板号)MOV 60H,P0 ;(输入信号送到60H地址单元)ANL 60H,A ;(只保留当前正工作的线路板的信号)CLR A ;(将累加器A清0)XRL A,60H ;(将累加器A与输入信号作对比)XB:JB F0,QJ2 ;(判断4.25秒内是否信号输入完毕,检测是否会停)JNZ BSELF ;(判断是否信号输入完毕)MOV R2,#250 ;(延时程序开始)Q2:MOV R1,#250 ; 。

单片机的查表程序

单片机的查表程序

单片机的查表程序(共5页)--本页仅作为文档封面,使用时请直接删除即可----内页可以根据需求调整合适字体及大小--在单片机开发过程中.一些非线性的控制过程.最适合做一个表格来.时时改变系统的参数.达到控制的目的.最常的如产生正弦的的程.就是建一个大的数组时时改变输出的8位字节送给外部DA.由DA生成一个完整的正弦波.当然了.LED显示也是一个例子.通过建一个数组来实现段码的点亮点灭.下面就是一个LED表---digits[0]#define SEG_a 0x01#define SEG_b 0x02#define SEG_c 0x04#define SEG_d 0x08#define SEG_e 0x10#define SEG_f 0x20#define SEG_g 0x40#define SEG_dot 0x80unsigned char digits[10] = {(SEG_a|SEG_b|SEG_c|SEG_d|SEG_e|SEG_f), 过MCS-51指令系统中有专用的查表指令:MOVC A,@A+DP TR和MOV A,@A+PC.MOVC A,@A+DPTR指令,DPTR作为基址寄存器时,其值为16位而且可根据需要设计,故可用于在64K ROM范围内查表。

编写查表程序时,首先把表的首址送入DPTR中,再将要查找的数据序号(或下表值)送入A中,然后就可以使用该指令进行查表操作,并将结果送累加器A中。

MOVC A,@A+PC指令,PC作为基址寄存器时,其值由指令的位置确定,它只能设在查表指令操作码下的256个字节范围内。

编写查表程序时,首先把查表数据的序号送入A中,再把从查表指令到表的首地址间的偏移量与A值相加,然后使用该指令进行查表操作,并把结果送累加器A中。

下面是把内部RAM 30H-37H单元中的十六进制数依次转换为ASCII码,并存入内部RAM 60H-6FH单元之中。

用查表法编写程序。

51单片机经典C程序(12864)

51单片机经典C程序(12864)

/*************************************** 控制器:KS0108* MCU:AT89C5* ,晶体频率:12MHz* 取模方式:纵向字节倒序* CS1和CS2为低电平有效**************************************/#include <reg52.h>#define uchar unsigned char#define uint unsigned int#define LCD_PORT P0#define Left 1#define Right 2sbit LCD_Busy=P0^7;sbit LCD_EN = P1^2;sbit LCD_RS = P1^0;sbit LCD_RW = P1^1;sbit LCD_RST= P1^7;sbit LCD_CS2= P3^6;sbit LCD_CS1= P3^5;uchar DisBuf[32],Page_Num,Clm_Num;code char Table0[]={/*-- 文字: A --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27 ,0x38,0x20,/*-- 文字: B --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11 ,0x0E,0x00,/*-- 文字: C --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10 ,0x08,0x00,/*-- 文字: D --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10 ,0x0F,0x00,/*-- 文字: E --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20 ,0x18,0x00,/*-- 文字: F --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00 ,0x00,0x00,/*-- 文字: G --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E ,0x02,0x00,};code unsigned char Table3[]={/*-- 文字: 天 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x00,0x40,0x42,0x42,0x42,0x42,0x42,0xFE,0x42,0x42,0x42,0x42,0x42,0x42 ,0x40,0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x06,0x01,0x02,0x04,0x08,0x10,0x30,0x60 ,0x20,0x00,/*-- 文字: 仙 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x00,0x80,0x60,0xF8,0x07,0x02,0xE0,0x00,0x00,0x00,0xFF,0x00,0x00,0x00 ,0xE0,0x00,0x01,0x00,0x00,0x7F,0x00,0x20,0x7F,0x20,0x20,0x20,0x3F,0x20,0x20,0x20 ,0x7F,0x00,/*-- 文字: 妹 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x08,0x88,0x7F,0x0A,0x08,0xF8,0x80,0x88,0x88,0x88,0xFF,0x88,0x88,0x88 ,0x80,0x00,0x40,0x21,0x12,0x0C,0x3A,0x11,0x40,0x30,0x0C,0x03,0xFF,0x06,0x08,0x30 ,0x10,0x00,/*-- 文字: 妹 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x08,0x88,0x7F,0x0A,0x08,0xF8,0x80,0x88,0x88,0x88,0xFF,0x88,0x88,0x88 ,0x80,0x00,0x40,0x21,0x12,0x0C,0x3A,0x11,0x40,0x30,0x0C,0x03,0xFF,0x06,0x08,0x30 ,0x10,0x00};code unsigned char Table4[]={/*-- 文字: 我 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x20,0x20,0x22,0x22,0xFE,0x21,0x21,0x20,0x20,0xFF,0x20,0x22,0xAC,0x20 ,0x20,0x00,0x04,0x04,0x42,0x82,0x7F,0x01,0x01,0x10,0x10,0x08,0x07,0x1A,0x21,0x40 ,0xF0,0x00,/*-- 文字: 好 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x10,0xF0,0x1F,0x10,0xF0,0x80,0x82,0x82,0x82,0xF2,0x8A,0x86,0x82 ,0x80,0x00,0x80,0x43,0x22,0x14,0x0C,0x73,0x20,0x00,0x40,0x80,0x7F,0x00,0x00,0x00 ,0x00,0x00,/*-- 文字: 想 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x08,0xC8,0x38,0xFF,0x18,0x68,0x08,0x00,0xFE,0x2A,0x2A,0x2A,0x2A,0xFE ,0x00,0x00,0x01,0x40,0x70,0x01,0x38,0x40,0x40,0x44,0x59,0x41,0x41,0x61,0x01,0x09 ,0x30,0x00,/*-- 文字: 你 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x80,0x40,0xF0,0x2C,0x43,0x20,0x98,0x0F,0x0A,0xE8,0x08,0x88,0x28,0x1C ,0x08,0x00,0x00,0x00,0x7F,0x00,0x10,0x0C,0x03,0x21,0x40,0x3F,0x00,0x00,0x03,0x1C ,0x08,0x00};code unsigned char Table5[]={/*-- 调入了一幅图像:C:\Documents and Settings\sammy\桌面\小新.bmp --*//*-- 宽度x高度=64x64 --*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xE0,0xF0 ,0xF8,0x7C,0x3C,0x1E,0x1E,0x8F,0xCF,0xCF,0xCF,0xDF,0x9F,0x9F,0x1F,0x1F,0x3F,0x3E ,0x3E,0x3E,0x7C,0x7C,0x7C,0xF8,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x20,0x10,0x08,0x08,0x1E,0x3F,0x7F,0x03 ,0x00,0x00,0x00,0x06,0x07,0x07,0x03,0x03,0x03,0x0F,0x3F,0x3F,0x7E,0x78,0x78,0x30 ,0x00,0x00,0x00,0x00,0x60,0xE0,0xF0,0x78,0x39,0x39,0x73,0xFE,0x60,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x03,0x04,0x08,0x11,0x08,0x04,0x83,0x80,0x0C,0x92,0xA2,0x44 ,0x22,0x12,0x00,0x00,0x00,0x00,0x00,0x0F,0x30,0x40,0x80,0x80,0x80,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x02,0x02,0x04,0x04,0x04,0x08,0x08,0x08,0x08,0x10,0x10,0x00 ,0x00,0x00,0x00,0x40,0x40,0x80,0x80,0x80,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00 ,0x00,0x60,0x90,0x10,0x20,0x10,0x90,0x60,0x00,0x03,0x04,0x08,0x11,0x08,0x04,0x03 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x10 ,0xE0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,0x01,0x06,0xF8,0x00,0x00 ,0x00,0x00,0x00,0x01,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F ,0x7F,0xFF,0xF3,0xF3,0xFE,0xFC,0xFC,0xF8,0xF8,0xF0,0xF0,0xF0,0xE0,0xE0,0xE0,0xE0 ,0xE0,0xE0,0xE4,0xE0,0xE0,0xE0,0x10,0x10,0x10,0x08,0x08,0x04,0x02,0x01,0x00,0x00 ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 ,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xD8,0xD8,0xFC ,0xFC,0xFC,0xFD,0xFD,0xFF,0xF7,0xF7,0xF7,0xF7,0xFF,0xFF,0xFF,0xFF,0xF3,0xF7,0xF7 ,0xFF,0xFF,0xFF,0xFD,0xFD,0xFC,0xDC,0xD8,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0 ,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0 ,0xC0,0xC0,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF ,0xFF,0xFF};code uchar Table8[]={/*-- 文字: 相 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x10,0xD0,0xFF,0x30,0x50,0x90,0x00,0xFE,0x22,0x22,0x22,0x22,0xFE ,0x00,0x00,0x04,0x03,0x00,0xFF,0x00,0x00,0x01,0x00,0xFF,0x42,0x42,0x42,0x42,0xFF ,0x00,0x00,/*-- 文字: 约 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x40,0x60,0x58,0xC7,0x60,0x18,0x40,0x20,0x58,0x8F,0x08,0x08,0x08,0xF8 ,0x00,0x00,0x10,0x32,0x13,0x12,0x0A,0x0A,0x08,0x00,0x00,0x00,0x43,0x80,0x40,0x3F ,0x00,0x00,/*-- 文字: 丛 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0xFE,0x80,0x00,0x00,0x00 ,0x00,0x00,0x20,0x30,0x2C,0x23,0x20,0x21,0x36,0x2C,0x23,0x20,0x20,0x23,0x26,0x3C ,0x28,0x00,/*-- 文字: 林 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x10,0x90,0xFF,0x90,0x10,0x00,0x10,0x10,0xD0,0xFF,0x90,0x10,0x10 ,0x10,0x00,0x08,0x06,0x01,0xFF,0x00,0x13,0x08,0x04,0x03,0x00,0xFF,0x01,0x06,0x18 ,0x08,0x00};code uchar Table9[]={/*-- 文字: 浪 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x08,0x30,0x01,0xC6,0x30,0x00,0xFC,0x94,0x95,0x96,0x94,0x94,0xFC,0x00 ,0x00,0x00,0x04,0x04,0xFE,0x01,0x00,0x00,0xFF,0x40,0x21,0x06,0x08,0x34,0x62,0xC2 ,0x40,0x00,/*-- 文字: 漫 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x10,0x20,0x81,0x66,0x00,0xC0,0x5F,0xD5,0x55,0x55,0xD5,0x55,0x5F,0xC0 ,0x00,0x00,0x04,0xFC,0x03,0x00,0x00,0x81,0x85,0x4D,0x55,0x25,0x35,0x4D,0xC5,0x41 ,0x00,0x00,/*-- 文字: 野 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x00,0x7E,0x4A,0xFE,0x4A,0x4A,0x7E,0x00,0x42,0x52,0xD2,0x6A,0x46,0xC2 ,0x40,0x00,0x22,0x22,0x22,0x1F,0x12,0x12,0x12,0x00,0x40,0x80,0x7F,0x00,0x00,0x00 ,0x00,0x00,/*-- 文字: 岭 --*//*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/0x00,0xF0,0x00,0xFF,0x00,0xF0,0x40,0x20,0x10,0x2C,0x43,0x04,0x08,0x70 ,0x20,0x00,0x08,0x1F,0x08,0x07,0x04,0x07,0x01,0x05,0x09,0x11,0x29,0x65,0x03,0x01 ,0x00,0x00};void Delay_us(unsigned int DelayTime){while(DelayTime) DelayTime--;}void Delay_x10ms(unsigned char DelayTime) //***************10ms延时**********//{unsigned char i,j,k;for(i=0;i<DelayTime;i++)for(j=0;j<10;j++)for(k=0;k<120;k++) {;}}/*----------------------------------------------------------------------------下面这个函数用于液晶模块的忙信号检测。

基于51单片机的2.4寸TFT触摸测试程序

基于51单片机的2.4寸TFT触摸测试程序

#include <reg52.h>#include"LCD_driver.h"#include"picture.h"#include"chinese.h"#include"char.h"uint colors[]={0xf800,0x07e0,0x001f,0xffe0,0x0000,0x07ff,0xf81f,0xffff //颜色数据};void LCD_Init(void){LCD_REST=1;delayms(50);LCD_REST=0;delayms(50);LCD_REST=1;delayms(5);LCD_CS =0; //打开片选使能/********************** 以下初始化代码每个命令下带有不同数量的参数**************************/LCD_Write_COM(0XF0); //参考S6D04H0驱动IC手册.pdf 文档的220页//LCD_Write_COM函数发送命令LCD_Write_Para函数传递该命令下的参数LCD_Write_Para(0X5A);LCD_Write_Para(0X5A);//APON CONTROLLCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);//SLEEP OUTLCD_Write_COM(0X11); //参考S6D04H0驱动IC手册.pdf 文档的165页delayms(10);//POWER CONTROL VC = "0111"LCD_Write_COM(0XF4); //参考S6D04H0驱动IC手册.pdf 文档的228页LCD_Write_Para(0X07);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X21);LCD_Write_Para(0X47);LCD_Write_Para(0X01);LCD_Write_Para(0X02);LCD_Write_Para(0X3F);LCD_Write_Para(0X66);LCD_Write_Para(0X05);//POWER CONTROL VC = "0111"LCD_Write_COM(0XF4); //参考S6D04H0驱动IC手册.pdf 文档的228页带有20个参数LCD_Write_Para(0X07);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X21);LCD_Write_Para(0X47);LCD_Write_Para(0X04);LCD_Write_Para(0X02);LCD_Write_Para(0X2A);LCD_Write_Para(0X7d);LCD_Write_Para(0X05);LCD_Write_COM(0XF2); //参考S6D04H0驱动IC手册.pdf 文档的221页LCD_Write_Para(0X28);LCD_Write_Para(0X60);LCD_Write_Para(0X03);LCD_Write_Para(0X04);LCD_Write_Para(0X04);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X15);LCD_Write_Para(0X48);LCD_Write_Para(0X00);LCD_Write_Para(0X07);LCD_Write_Para(0X01);LCD_Write_COM(0XF5); //参考S6D04H0驱动IC手册.pdf 文档的235页LCD_Write_Para(0X00);LCD_Write_Para(0X1d);LCD_Write_Para(0X4f);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X06);//POWER CONTROLLCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X03);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);delayms(5);//POWER CONTROLLCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X0F);LCD_Write_Para(0X09);LCD_Write_Para(0X00);LCD_Write_Para(0X00);delayms(5);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X0F);LCD_Write_Para(0X01);delayms(10);//POWER CONTROLLCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X1F);delayms(20);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X3F);delayms(10);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X3F);LCD_Write_Para(0X03);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0X7F);delayms(30);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0XFF);/*LCD_Write_COM(0XFD);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X10);LCD_Write_Para(0X14);LCD_Write_Para(0X12);LCD_Write_Para(0X00); */delayms(20);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X00);LCD_Write_Para(0XFF);LCD_Write_Para(0X1F);LCD_Write_Para(0X00);LCD_Write_Para(0X02);delayms(20);LCD_Write_COM(0XF3); //参考S6D04H0驱动IC手册.pdf 文档的227页LCD_Write_Para(0X01);LCD_Write_COM(0XF0);LCD_Write_Para(0Xa5);LCD_Write_Para(0Xa5);LCD_Write_COM(0X36); //参考S6D04H0驱动IC手册.pdf 文档的189页LCD_Write_Para(0X48);LCD_Write_COM(0X3a); //参考S6D04H0驱动IC手册.pdf 文档的195页LCD_Write_Para(0X55); //16bit (66 18bit)//gamma//red/* LCD_Write_COM(0Xf9);LCD_Write_Para(0X04);LCD_Write_COM(0XFA);LCD_Write_Para(0x0a);LCD_Write_Para(0x08);LCD_Write_Para(0X18);LCD_Write_Para(0X1d);LCD_Write_Para(0X1e);LCD_Write_Para(0X20);LCD_Write_Para(0X15);LCD_Write_Para(0X3E);LCD_Write_Para(0X2e);LCD_Write_Para(0X1c);LCD_Write_Para(0X0a);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_COM(0XFB);LCD_Write_Para(0x00);LCD_Write_Para(0x00);LCD_Write_Para(0X1F);LCD_Write_Para(0X1d);LCD_Write_Para(0X1e);LCD_Write_Para(0X20);LCD_Write_Para(0X15);LCD_Write_Para(0X3E);LCD_Write_Para(0X2f);LCD_Write_Para(0X1c);LCD_Write_Para(0X0a);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);//green/* LCD_Write_COM(0XF9);LCD_Write_Para(0X02);LCD_Write_COM(0XFA);LCD_Write_Para(0x0a);LCD_Write_Para(0x08);LCD_Write_Para(0X1d); LCD_Write_Para(0X1e); LCD_Write_Para(0X20); LCD_Write_Para(0X15); LCD_Write_Para(0X3E); LCD_Write_Para(0X2e); LCD_Write_Para(0X1c); LCD_Write_Para(0X0a); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00);LCD_Write_COM(0XFB); LCD_Write_Para(0x00); LCD_Write_Para(0x00); LCD_Write_Para(0X1F); LCD_Write_Para(0X1d); LCD_Write_Para(0X1e); LCD_Write_Para(0X20); LCD_Write_Para(0X15); LCD_Write_Para(0X3E); LCD_Write_Para(0X2f); LCD_Write_Para(0X1c); LCD_Write_Para(0X0a); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00); LCD_Write_Para(0X00);LCD_Write_COM(0XF9); LCD_Write_Para(0X01);//blueLCD_Write_COM(0XFA); LCD_Write_Para(0x0a); LCD_Write_Para(0x08); LCD_Write_Para(0X1f); LCD_Write_Para(0X1d); LCD_Write_Para(0X1e); LCD_Write_Para(0X20);LCD_Write_Para(0X3E);LCD_Write_Para(0X2e);LCD_Write_Para(0X1c);LCD_Write_Para(0X0a);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);/* LCD_Write_COM(0XFB);LCD_Write_Para(0x00);LCD_Write_Para(0x00);LCD_Write_Para(0X1F);LCD_Write_Para(0X1d);LCD_Write_Para(0X1e);LCD_Write_Para(0X20);LCD_Write_Para(0X15);LCD_Write_Para(0X3E);LCD_Write_Para(0X2f);LCD_Write_Para(0X1c);LCD_Write_Para(0X0a);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00);LCD_Write_Para(0X00); */delayms(120);LCD_Write_COM(0x29); //显示打开//参考S6D04H0驱动IC手册.pdf 文档的173页LCD_Write_COM(0x2c); //写数据到RAM中//参考S6D04H0驱动IC手册.pdf 文档的270页LCD_CS =1; //关闭使能}void LCD_Write_COM(char VL) //发送命令{LCD_RS=0;LCD_DataPortL=VL; //低位P0口LCD_WR=0;LCD_WR=1;}void LCD_Write_Para(char VL) //发送数据{LCD_RS=1;LCD_DataPortL=VL; //低位P0口LCD_WR=0;LCD_WR=1;}void delayms(int count) // /* X1ms */{int i,j;for(i=0;i<count;i++)for(j=0;j<100;j++);}void LCD_Writ_Bus(char VH,char VL) //并行数据写入函数{LCD_DataPortH=VH; //高位P1口LCD_DataPortL=VL; //低位P0口LCD_WR=0;LCD_WR=1;}void LCD_Write_DATA(char VH,char VL) //发送数据{LCD_RS=1;LCD_Writ_Bus(VH,VL);}void Pant(char VH,char VL){int i,j;LCD_CS =0; //打开片选使能Address_set(0,0,239,319);for(i=0;i<320;i++){for (j=0;j<240;j++){LCD_Write_DATA(VH,VL);}}LCD_CS =1; //关闭片选使能}void Address_set(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2){LCD_Write_COM(0X2A); //设置X坐标地址//参考S6D04H0驱动IC手册.pdf 文档的174页LCD_Write_Para(x1>>8); //开始XLCD_Write_Para(x1); //开始XLCD_Write_Para(x2>>8); //结束XLCD_Write_Para(x2); //结束XLCD_Write_COM(0X2B); //设置Y坐标地址//参考S6D04H0驱动IC手册.pdf 文档的176页LCD_Write_Para(y1>>8); //开始YLCD_Write_Para(y1); //开始YLCD_Write_Para(y2>>8); //结束YLCD_Write_Para(y2); //结束Y//LCD_Write_COM(0x13);LCD_Write_COM(0x29); //显示打开//参考S6D04H0驱动IC手册.pdf 文档的173页LCD_Write_COM(0x2c); //写数据到RAM中//参考S6D04H0驱动IC手册.pdf 文档的270页}/**********************************************************写汉字子函数**********************************************************/void ChineseChar(uint x,uint y,int size,uint For_color,uint Bk_color ,char c) {int e=0,i,j;int ed;uint ncols;uint nrows;uint nbytes;uchar *pfont;uchar *pchar;uchar *fonttable[]={(uchar *)hz16,(uchar *)hz24 , (uchar *)hz32};pfont=(uchar *)fonttable[size];if(size==0){ncols =16-1;nrows =16-1;nbytes =32; //(16*16)/8}elseif(size==1){ncols =24-1;nrows =24-1;nbytes =72; //(24*24)/8}elseif(size==2){ncols =32-1;nrows =32-1;nbytes =132; //(32*32)/8}pchar=pfont + (nbytes*(c-1));Address_set(x, y, x+nrows,y+ncols);for(i=0;i<nbytes;i++){ed=*pchar++;if(size==0){if(i%2==0)e=8;elsee=4;}elsee=8;for(j=0;j<e;j++){// unsigned char temp;if((ed>>j)&0x01){//temp = For_color&0x00ffLCD_Write_DATA(For_color>>8 , For_color ); //textcolor}else{LCD_Write_DATA( Bk_color>>8 , Bk_color); //backcolor}}}}/*********************************************************/ /********************************************************** 字符显示子函数**********************************************************/ void LCD_ShowChar(uint x,uint y,uint For_color,uint Bk_color, char ch) {uchar temp , Bytes;uchar pos,t;uint CHAR_W,CHAR_H;//CHAR_W = 8; //8*16//CHAR_H = 16;CHAR_W = 16; //16*24CHAR_H = 24;if(x>(LCD_SIZE_X-CHAR_W)||y>(LCD_SIZE_Y-CHAR_H))return;Address_set(x, y, x+CHAR_W-1, y+CHAR_H-1);ch = ch-32; //按照ASCII编码顺序的到相应字母的编码Bytes = (CHAR_W*CHAR_H)/8;for(pos=0;pos<Bytes;pos++) //CHAR_H (CHAR_W*CHAR_H)/8{// temp= Font8x16[ch][pos];//temp= Font16x24[ch][pos];temp= Font16x24[ch][pos];for(t=0;t<8;t++) //CHAR_W{if(temp&0x80)LCD_Write_DATA(For_color>>8 , For_color ); //textcolorelseLCD_Write_DATA(Bk_color>>8 , Bk_color ); //backcolortemp<<=1;}}}/**********************************************************显示字符串子函数x,y:起点坐标*p:字符串起始地址**********************************************************/void LCD_ShowString(uint x,uint y,uint For_color,uint Bk_color,char *p){while(*p!='\0'){LCD_ShowChar(x,y,For_color,Bk_color,*p);// x+=8;x+=16;p++;}}。

如何用51单片机实现IIC通信

如何用51单片机实现IIC通信

如何用51单片机实现IIC通信在之前的MCS-51系列单片机中内部没有IIC通信资源,所如果要想用51单片机实现IIC通信,就只能通过软件模拟其时序,这样也能实现IIC通信的功能。

这个是IIC的头文件,便于使用调用:#ifndef _IIC_H_#define _IIC_H_/***ucahr和uint 的宏定义很重要,否则下面的函数无法正常运行******/#define uchar unsigned char //定义uchar型数据为无符号型#define uint unsigned int //定义uint型数据为无符号型sbit SCL = P2 ;sbit SDA = P2 ;/***申明外部函数****/extern void delay_1ms(void);extern void IIC_Init(void);//IIC初始化extern void Signal_Start(void);//IIC停止信号extern void Signal_Stop(void);//IIC停止信号extern void Write_Byte(uchar wdata);//写一个字节数据函数extern uchar Read_Byte();//读一个字节数据函数extern void Write_Add(uchar add,uchar wdata,uchar comd);//向某个IIC器件写指令,地址和数据extern uchar Read_Add(uchar add,uchar comd);//向某个IIC器件写指令读某个地址里面的数据#endif以下是IIC通信的C语言源代码:#include#include。

51单片机综合测试程序

51单片机综合测试程序

/*功能说明:按K1键:LED2和蜂鸣器交替工作;按K6键:数码管轮流全显4.02345和HELLO*//*按K11键:是出口自发自首程序(要将DB9旁的JP7的下面两个短接),并可在数模管上显示*//*按K16键启动AD和DA,并将滑动变阻器可调端的电压值在数模管上显示(将TLC0832旁的短接块短接,并调节滑动变阻器改变可调端的电压值)*//*除上四个键外,按其他键将在不同的数码管上显示不同的数字*//*单脉冲产生电路中的开关在不同的位置分别产生1和0,可作为中断源和外部计数源*//*脉冲产生电路可产生频率和占空比都可调的方波脉冲,也可作为中断源和外部计数。

如需要也可作为一方波发生器*/#include<reg52.h>#include<stdio.h>#include<intrins.h>#define NOP _nop_()#include<math.h> //头文件//unsigned char code a[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6 f,0xa6,0xdb,0xcf,0xe6,0xed,0xff}; //数码管段位unsigned char code a[6]={0x76,0x79,0x38,0x38,0x3f,0x80}; //HELLO段码unsigned char code b[6]={0x00,0x20,0x40,0x60,0x80,0xa0}; //数码管位选unsigned char code c[6]={0xe6,0x3f,0x5b,0x4f,0x66,0x6d}; //LED扫描显示位unsigned char code d[4][4]={{0x3f,0x06,0x5b,0x4f},{0x66,0x6d,0x7d,0x07},{0 x7f,0x6f,0xa6,0xdb},{0xcf,0xe6,0xed,0xff}}; //除四个功能键外,不同按键显示不同的数字unsigned char code e[4][4]={{0x00,0x20,0x40,0x60},{0x80,0xa0,0x00,0x20},{ 0x40,0x60,0x80,0xa0},{0x00,0x20,0x40,0x60}}; //除四个功能键外,显示数字的位选unsigned int f[3]; //数组用来放AD转换出来电压值的个位,十分位和百分位unsigned int code g[3]={0x00,0x20,0x40}; //显示AD转换电压值的三个数码管的位选unsigned char data t_buffer;unsigned char data r_buffer;unsigned char data ch; //AD转换返回值unsigned char data rnum; //键盘扫描行变量unsigned char data lnum; //键盘扫描列变量unsigned int data temp; /*定义SPI总线*/sbit adcs=P3^4; //用口线模拟SPI总线,sbit csda=P3^5; //sbit是位变量,定义AD和DA的SPI总线sbit clk=P3^6;sbit addo=P3^7;sbit din=P1^4;sbit addi=P1^4;sbit a_138=P1^5; //定义3-8译码器的a,b,c sbit b_138=P1^6;sbit c_138=P1^7;/*函数声明*/void delay(unsigned char x); //AD启动短暂延时void DEL(unsigned int count); //大约1ms的延时子程序void keyscan(void); //键盘扫描unsigned int readad(void); //AD转换void readda(); //DA转换void adclk(void); //AD时钟void ledscan(void); //LED扫描void command(void); //命令处理void led_bell(void); //LED和蜂鸣器检测void Serial_inis(void); //串口初始化void serial(void); //串口发送void read_serial(void); //接收并显示void AD_display(void); //AD电压显示/*AD启动短暂延时*/void delay(unsigned char x){unsigned char data i;for(i=0;i<x;i++);}/*1ms延时子程序*/void DEL(unsigned int count){unsigned char data t;while(count--!=0){for(t=0;t<72;t++);}}/*AD启动*/void adclk(void){clk=1;delay(2);clk=0;delay(2);}/*AD转换*/unsigned int readad (void){unsigned char data i;adcs=0;adclk();addi=1;adclk();addi=1;adclk();addi=1;adclk();for(;addo==1;){adclk();}for(i=0;i<8;i++){adclk();ch=(ch<<1)|addo;}adcs=1;return(ch);}/*AD显示*/void AD_display (void){unsigned char l;l=0;while(l<255){unsigned int data k_1; //电压值unsigned int data j_1; //数码管灯显示位数unsigned int data t_1; // 小数点后两位unsigned int data m_1; // 整数unsigned int data n_1; // 小数点后一位unsigned int data p_1; // 循环次数p_1=0;l++;temp=readad(); // 得到AD转换来的电压值,temp是8位二进制数k_1=(int)((temp*125)/64); //转换为十进制的电压值,并扩大100倍m_1=(k_1/100); //电压值的个位n_1=((k_1/10)%10); //电压值的十分位t_1=((k_1%100)%10); //电压值的百分位switch(m_1) //电压值的个位是0时,显示0.;个位是1时,显示1.;依次类推{case 0: //电压为0-5V,所以个位只能是0,1,2,3,4,5f[0]=0xbf;break;case 1:f[0]=0x86;break;case 2:f[0]=0xdb;break;case 3:f[0]=0xcf;break;case 4:f[0]=0xe6;break;case 5:f[0]=0xed;break;default:break;}switch(n_1) //十分位显示,0时显示0;1时显示1;2时显示2,依次类推{case 0:f[1]=0x3f;break;case 1:f[1]=0x06;break;case 2:f[1]=0x5b;break;case 3:f[1]=0x4f;break;case 4:f[1]=0x66;break;case 5:f[1]=0x6d;break;case 6:f[1]=0x7d;break;case 7:f[1]=0x07;break;case 8:f[1]=0x7f;break;case 9:f[1]=0x6f;break;default:break;}switch(t_1) //百分位显示,同十分位显示{case 0:f[2]=0x3f;break;case 1:f[2]=0x06;break;case 2:f[2]=0x5b;break;case 3:f[2]=0x4f;break;case 4:f[2]=0x66;break;case 5:f[2]=0x6d;break;case 6:f[2]=0x7d;break;case 7:f[2]=0x07;break;case 8:f[2]=0x7f;break;case 9:f[2]=0x6f;break;default:break;}while(p_1<500) //将电压值在数模管动态显示{for(j_1=0;j_1<3;j_1++){P0=f[j_1];P1=g[j_1];DEL(3);p_1++;}}}}/*DA转换*/void readda (void) //DA转换,将AD转换的电压值又经过DA转换位电压值,用万用表可测得DA转换出的电压值与数码管值一致{bit bdata bData;unsigned int data i;unsigned int data Data_h,Data_l;Data_h=0x09; //DA控制字Data_l=temp; //DA要转换的8位二进制数NOP;NOP;clk=0;csda=0;for(i=0;i<8;i++){bData=Data_h&0x80;NOP;NOP;din=bData;clk=1;NOP;NOP;clk=0;Data_h=(Data_h<<1);}NOP;NOP;for(i=0;i<8;i++){bData=Data_l&0x80;NOP;NOP;din=bData;clk=1;NOP;NOP;clk=0;Data_l=(Data_l<<1);}csda=1;}/*键盘扫描*/void keyscan (void) {P2=0x0f;if((P2&0x0f)!=0x0f){DEL(2); //消抖延时,确定有键按下if((P2&0x0f)!=0x0f) //有键按下,判断行{if(P2==0x0e)rnum=1;if(P2==0x0d)rnum=2;if(P2==0x0b)rnum=3;if(P2==0x07)rnum=4;}P2=0xf0; //有键按下,判断列if((P2&0xf0)!=0xf0){if(P2==0xe0) //行和列交叉即为按下的键lnum=1;if(P2==0xd0)lnum=2;if(P2==0xb0)lnum=3;if(P2==0x70)lnum=4;}}if((rnum-lnum)==0) command();else{P0=d[rnum-1][lnum-1];P1=e[rnum-1][lnum-1];DEL(1000);}}/*初始化程序*/void Serial_inis(void) //串口初始化,波特率设置{TMOD=0x20;SCON=0x50;TH1=0xfd; /*波特率为19200*/TL1=0xfd;PCON=0x80;EA=1;ES=1;ET1=0;PS=1;TR1=1;}/*串口发送程序*/void serial(void) //串口发送,没有用中断,而是用查询{t_buffer=0x01;SBUF=t_buffer;while(TI==0); //查询数据数据发送完毕TI=0; //中断标志位清零,为下一次发送数据准备read_serial();t_buffer=0x02;SBUF=t_buffer;while(TI==0);TI=0;read_serial();t_buffer=0x03;SBUF=t_buffer;while(TI==0);TI=0;read_serial();}/*串口接收程序及显示*/void read_serial(void) //接收,用查询是否发送完毕{while(RI==0);RI=0;r_buffer=SBUF;switch(r_buffer){case 0x01:{P0=0x3f;P1=0x00;DEL(1500);}break;case 0x02:{P0=0x06;P1=0x20;DEL(1500);}break;case 0x03:{P0=0x5b;P1=0x40;DEL(1500);}break;default:break;}}/*LED和蜂鸣器检测*/void led_bell(void){unsigned char data i; //3-8译码器次高位选通蜂鸣器响i=0;a_138=0; //蜂鸣器响b_138=1;c_138=1;DEL(3000);a_138=1; //灯亮//3-8译码器最高位选通,LED2亮b_138=1;c_138=1;DEL(3000);}/*LED扫描显示*/void ledscan(void) //LED动态扫描,六个数码管同时亮{unsigned int data j;unsigned int data i=0;unsigned int data k=0;while(i<800){for(j=0;j<6;j++){P0=c[j];P1=b[j];DEL(3);i++;}}while(k<800){for(j=0;j<6;j++){P0=a[j];P1=b[j];DEL(3);k++;}}}/*按11,22,33,44显示不同功能*/void command(void) //四个功能键的功能处理程序{unsigned char data jcnum;jcnum=(rnum+lnum);switch(jcnum){case 2:led_bell();break;case 4:ledscan();break;case 6:Serial_inis();serial();break;case 8:temp=readad();readda();AD_display();break;default:break;}}/*主程序*/void main(void){while(1){keyscan();//一直在扫描键盘,有动作即作出相应判断}}。

单片机实验带程序的

单片机实验带程序的

单片机实验带程序的《单片机实验带程序》单片机作为现代电子技术的重要组成部分,在控制、测量、通信等众多领域发挥着关键作用。

通过单片机实验,我们能够更深入地理解其工作原理和应用,同时编写相应的程序来实现各种功能。

在进行单片机实验之前,我们需要准备一些必要的硬件设备。

首先是单片机开发板,这是实验的核心平台,上面集成了单片机芯片、电源电路、时钟电路、下载接口等。

常见的单片机开发板有 51 单片机开发板、STM32 开发板等。

此外,还需要一台电脑用于编写和编译程序,以及下载线将程序传输到开发板上。

以一个简单的点亮 LED 灯的实验为例,让我们来看看单片机实验的过程和程序编写。

首先,在硬件连接方面,我们将一个 LED 灯的阳极通过限流电阻连接到单片机的一个 I/O 引脚,阴极接地。

接下来就是编写程序。

以下是使用 C 语言编写的 51 单片机点亮 LED 灯的程序示例:```cinclude <reg52h> //包含 51 单片机的头文件sbit LED = P1^0; //定义 LED 连接的引脚void main(){LED = 0; //将 LED 引脚置低电平,点亮 LED 灯while(1);//让程序保持在这个状态}```在上述程序中,`include <reg52h>`语句用于包含 51 单片机的相关寄存器定义和函数声明。

`sbit LED = P1^0;`定义了连接 LED 灯的引脚为 P10。

在`main` 函数中,通过将`LED` 引脚置低电平来点亮 LED 灯,并使用`while(1);`让程序保持在这个状态,使 LED 灯持续点亮。

当我们编写好程序后,就可以使用相应的开发工具(如 Keil C51)进行编译。

编译成功后,通过下载线将生成的二进制文件下载到单片机开发板中,此时就能看到 LED 灯被点亮了。

除了点亮 LED 灯,单片机还可以实现更多复杂的功能。

比如,通过定时器来实现精确的定时控制,或者使用中断来处理外部事件。

GY电子罗盘51单片机IIC程序

GY电子罗盘51单片机IIC程序

// GY-26 IIC测试程序// 使用单片机STC89C51// 晶振:11.0592M// 显示:LCD1602// 编译环境Keil uVision2// 程序中把模块当24c04进行通信#include <REG51.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数据端口#define WriteDeviceAddress 0xe0 //定义器件在IIC总线中的地址#define ReadDeviceAddress 0xe1 //器件在IIC总线中地址+1 typedef unsigned char BYTE;typedef unsigned short WORD;BYTE BUF[8]; //数据缓存区BYTE code res[6] _at_ 0x23;sbit SCL=P1^0; //iic时钟引脚定义sbit SDA=P1^1; //iic数据引脚定义sbit LCM_RS=P2^0; //LCD1602命令端口sbit LCM_RW=P2^1; //LCD1602命令端口sbit LCM_EN=P2^2; //LCD1602命令端口sbit KEY_1 =P2^6; //按键1sbit KEY_2 =P2^5; //按键2sbit KEY_3 =P2^4; //按键3sbit KEY_4 =P2^3; //按键4uchar templ,temph,cnt;uchar ge,shi,bai,qian;uint dis_data,dis_data1;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);uchar HEX_TO_ASCII(uchar HEX_data);void conversion(uint temp_data);//------------------------------------void Delay5us();void Delay5ms();void AT24C04_Start();void AT24C04_Stop();void AT24C04_SendACK(bit ack);bit AT24C04_RecvACK();void AT24C04_SendByte(BYTE dat);BYTE AT24C04_RecvByte();void AT24C04_ReadPage();void AT24C04_WritePage();void read_cmp(void);//-----------------------------------void cal_on();void cal_off();void recovery_factory();void CMP_OFFS();void IIC_ADDR_Change();void delay_10ms();//********************************************************* uchar HEX_TO_ASCII(uchar HEX_data){uchar ASCII_data;switch(HEX_data){case 0x00:ASCII_data=0x30;break;case 0x01:ASCII_data=0x31;break;case 0x02:ASCII_data=0x32;break;case 0x03:ASCII_data=0x33;break;case 0x04:ASCII_data=0x34;break;case 0x05:ASCII_data=0x35;break;case 0x06:ASCII_data=0x36;break;case 0x07:ASCII_data=0x37;break;case 0x08:ASCII_data=0x38;break;case 0x09:ASCII_data=0x39;break;case 0x0a:ASCII_data=0x41;break;case 0x0b:ASCII_data=0x42;break;case 0x0c:ASCII_data=0x43;break;case 0x0d:ASCII_data=0x44;break;case 0x0e:ASCII_data=0x45;break;case 0x0f:ASCII_data=0x46;break;}return(ASCII_data);}//********************************************************* void conversion(uint temp_data){uchar ge_data,shi_data,bai_data ,qian_data;qian_data=temp_data/1000 ;temp_data=temp_data%1000; //取余运算bai_data=temp_data/100 ;temp_data=temp_data%100; //取余运算shi_data=temp_data/10 ;temp_data=temp_data%10; //取余运算ge_data=temp_data;qian= qian_data;bai = bai_data;shi = shi_data;ge = ge_data ;}//********************************************************* void main(){unsigned int i;delay(500);InitLcd();EA=0;cnt=0;while(1) //循环{read_cmp(); //读出数据//---------显示角度dis_data=0;dis_data=BUF[1];dis_data<<=8;dis_data|=BUF[2];conversion(dis_data);ge=HEX_TO_ASCII(ge);shi=HEX_TO_ASCII(shi);bai=HEX_TO_ASCII(bai);qian=HEX_TO_ASCII(qian);DisplayOneChar(0,0,qian);DisplayOneChar(1,0,bai);DisplayOneChar(3,0,'.');DisplayOneChar(4,0,ge);//---------显示温度dis_data=0;dis_data=BUF[5];dis_data<<=8;dis_data|=BUF[6];if(dis_data>4095){ //如果bit-12为1,即符号位为负DisplayOneChar(0,1,'-'); //显示负号dis_data&=0xefff; //清符号位}else DisplayOneChar(0,1,' ');//如果bit-12为0,空格,清空conversion(dis_data);ge=HEX_TO_ASCII(ge);shi=HEX_TO_ASCII(shi);bai=HEX_TO_ASCII(bai);qian=HEX_TO_ASCII(qian);DisplayOneChar(1,1,qian);DisplayOneChar(2,1,bai);DisplayOneChar(3,1,shi);DisplayOneChar(4,1,'.');DisplayOneChar(5,1,ge);DisplayOneChar(6,1,0XDF); //温度单位DisplayOneChar(7,1,'C'); //温度单位//-------显示磁偏量dis_data1=0;dis_data1=BUF[3];dis_data1<<=8;dis_data1|=BUF[4];conversion(dis_data1);ge=HEX_TO_ASCII(ge);shi=HEX_TO_ASCII(shi);bai=HEX_TO_ASCII(bai);qian=HEX_TO_ASCII(qian);DisplayOneChar(7,0,qian);DisplayOneChar(8,0,bai);DisplayOneChar(10,0,'.');DisplayOneChar(11,0,ge);//-------显示校准等级conversion(BUF[7]);ge=HEX_TO_ASCII(ge);shi=HEX_TO_ASCII(shi);bai=HEX_TO_ASCII(bai);qian=HEX_TO_ASCII(qian);DisplayOneChar(10,1,bai);DisplayOneChar(11,1,shi);DisplayOneChar(12,1,ge);//----------按键检测if(KEY_1==0){delay_10ms();if(KEY_1==0){cal_on();while(KEY_1==0)delay_10ms();}}if(KEY_2==0){delay_10ms();if(KEY_2==0){cal_off();while(KEY_2==0)delay_10ms();}}if(KEY_3==0){delay_10ms();if(KEY_3==0){recovery_factory();while(KEY_3==0)delay_10ms();}}if(KEY_4==0){delay_10ms();if(KEY_4==0){// CMP_OFFS();IIC_ADDR_Change();while(KEY_4==0)delay_10ms();}}for (i=0;i<4000;i++);}}/*******************************/void delay(unsigned int k){unsigned int i,j;for(i=0;i<k;i++){for(j=0;j<121;j++){;}}}/*******************************/void WaitForEnable(void){DataPort=0xff;LCM_RS=0;LCM_RW=1;_nop_();LCM_EN=1;_nop_();_nop_();while(DataPort&0x80);LCM_EN=0;}/*******************************/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;}/*******************************/void WriteDataLCM(uchar dataW){WaitForEnable();LCM_RS=1;LCM_RW=0;_nop_();DataPort=dataW;_nop_();LCM_EN=1;_nop_();_nop_();LCM_EN=0;}/***********************************/void InitLcd(){WriteCommandLCM(0x38,1);WriteCommandLCM(0x08,1);WriteCommandLCM(0x01,1);WriteCommandLCM(0x06,1);WriteCommandLCM(0x0c,1);}/***********************************/void DisplayOneChar(uchar X,uchar Y,uchar DData){Y&=1;X&=15;if(Y)X|=0x40;X|=0x80;WriteCommandLCM(X,0);WriteDataLCM(DData);}/***********************************//*void DisplayListChar(uchar X,uchar Y,uchar code *DData){uchar ListLength=0;Y&=0x1;X&=0xF;while(X<=15){DisplayOneChar(X,Y,DData[ListLength]);ListLength++;X++;}}*//***********************************///*********************************************************void read_cmp(void){uchar i;AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0x31); //发送一次测量角度AT24C04_Stop(); //停止信号Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms(); //延时,实际应用中可以执行其他程序Delay5ms();Delay5ms();Delay5ms();AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0x35); //发送一次测量温度AT24C04_Stop(); //停止信号Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms();Delay5ms(); //延时,实际应用中可以执行其他程序AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //发送存储单元地址,从0开始AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress+1); //发送设备地址+读信号for (i=0; i<8; i++){BUF[i] = AT24C04_RecvByte();if (i == 7){AT24C04_SendACK(1); //最后一个数据需要回NOACK}else{AT24C04_SendACK(0); //回应ACK}}AT24C04_Stop(); //停止信号}//--------------------------------------------------/**************************************延时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_();}/**************************************延时5毫秒(STC90C52RC@12M)不同的工作环境,需要调整此函数当改用1T的MCU时,请调整此延时函数**************************************/void Delay5ms(){WORD n = 560;while (n--);}/**************************************起始信号**************************************/void AT24C04_Start(){SDA = 1; //拉高数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 0; //产生下降沿Delay5us(); //延时SCL = 0; //拉低时钟线}/**************************************停止信号**************************************/ void AT24C04_Stop(){SDA = 0; //拉低数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 1; //产生上升沿Delay5us(); //延时}/**************************************发送应答信号入口参数:ack (0:ACK 1:NAK)**************************************/ void AT24C04_SendACK(bit ack){SDA = ack; //写应答信号SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}/**************************************接收应答信号**************************************/bit AT24C04_RecvACK(){SCL = 1; //拉高时钟线Delay5us(); //延时CY = SDA; //读应答信号SCL = 0; //拉低时钟线Delay5us(); //延时return CY;}/**************************************向IIC总线发送一个字节数据**************************************/void AT24C04_SendByte(BYTE dat){BYTE i;for (i=0; i<8; i++) //8位计数器{dat <<= 1; //移出数据的最高位SDA = CY; //送数据口SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}AT24C04_RecvACK();}/**************************************从IIC总线接收一个字节数据**************************************/BYTE AT24C04_RecvByte(){BYTE i;BYTE dat = 0;SDA = 1; //使能内部上拉,准备读取数据,for (i=0; i<8; i++) //8位计数器{dat <<= 1;SCL = 1; //拉高时钟线Delay5us(); //延时dat |= SDA; //读数据SCL = 0; //拉低时钟线Delay5us(); //延时}return dat;}//*********************************************************void cal_on(void){ //校准AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xC0); //发送校准命令AT24C04_Stop(); //发送停止信号}//*********************************************************void cal_off(void) //关闭校准{AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xC1); //发送停止校准命令AT24C04_Stop(); //发送停止信号}//*********************************************************void recovery_factory(void) //恢复出厂设置{AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xA0); //发送数据0xA0AT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xAA); //发送数据0xAAAT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xA5); //发送数据0xA5AT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xC5); //发送0xC5,恢复出厂命令AT24C04_Stop(); //发送停止信号}//*********************************************************void IIC_ADDR_Change(void) //iic地址更改{AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xA0); //发送数据0xA0AT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xAA); //发送数据0xAAAT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xA5); //发送数据0xA5AT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x00); //0表示命令AT24C04_SendByte(0xE8); //新的IIC地址E0-FE中偶数(参考PDF文档)AT24C04_Stop(); //发送停止信号}//*********************************************************void CMP_OFFS(void) //磁偏角{AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x03); //0表示命令AT24C04_SendByte(0x00); //磁偏角高8位,写入0AT24C04_Stop(); //发送停止信号AT24C04_Start(); //起始信号AT24C04_SendByte(WriteDeviceAddress); //发送设备地址+写信号AT24C04_SendByte(0x04); //0表示命令AT24C04_SendByte(0x64); //磁偏角低8位,写入100(实际是10.0度)AT24C04_Stop(); //发送停止信号}//*********************************************************void Delay_10ms(void){int n=560;while (n--);}//参考宏晶网站24c04通信程序。

最新自己整理的单片机51C程序

最新自己整理的单片机51C程序

第一章LED闪烁的简单试验/****************************************************************************** ********* LED闪烁的简单试验*** 连接方法:JP11(P2)和JP1用8PIN排线连接起来********************************************************************************** ********/#include <reg51.h> //此文件中定义了51的一些特殊功能寄存器void delay(unsigned int i); //声明延时函数main(){P2 = 0x00; //置P0口为低电平delay(600); //调用延时程序P2 = 0xff; //置P0口为高电平delay(600); // 调用延时程序}/*******延时函数*************/void delay(unsigned int i){unsigned char j;for(i; i > 0; i--) //循环600*255 次for(j = 255; j > 0; j--);}第二章二进制加法试验/****************************************************************************** **; 二进制加法试验*; p2口八个灯作二进制加法。

理解二进值的计算 * ; 硬件连接:p2口8路指示灯*;*;* 描述: *; p2口八个灯作二进制加法。

理解二进值的计算*;* 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来* ******************************************************************************* **/#include <reg52.h>void delay(unsigned int i); //声明延时函数main(){unsigned char Num = 0xff;while (1){P2 = Num;delay(1000); //延时函数Num--;}}/*******延时函数*************/void delay(unsigned int i){unsigned char j;for(i; i > 0; i--)for(j = 255; j > 0; j--);}第三章LED闪烁的简单试验/****************************************************************************** ********* LED闪烁的简单试验** 延时实现p2口LED流水灯效果(用循环移位指令)* 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来********************************************************************************** ********/#include <reg51.h> //此文件中定义了51的一些特殊功能寄存器#include <intrins.h>void delayms(unsigned char ms)// 延时子程序{unsigned char i;while(ms--){for(i = 0; i < 120; i++);}}main(){unsigned char LED;LED = 0xfe; //0xfe = 1111 1110while(1){P2 = LED;delayms(250);LED = LED << 1; //循环右移1位,点亮下一个LED "<<"为左移位if(P2 == 0x00 ) {LED = 0xfe; } // 0xfe = 1111 1110}}第四章跑马灯程序/****************************************************************************** ** 描述: 跑马灯程序** 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来********************************************************************************* /#include<reg51.h>#include <intrins.h>/***************************************************************************** * 延时子程序* * * ******************************************************************************/ void delayms(unsigned char ms){unsigned char i;while(ms--){for(i = 0; i < 120; i++);}}/***************************************************************************** * 主程序* * * ******************************************************************************/ main(){unsigned char LED;LED = 0xfe;P2 = LED;while(1){delayms(250);LED = _crol_(LED,1); //循环右移1位,点亮下一个LED 此函数位库函数P2 = LED;}}第五章跑马灯实验/****************************************************************************** **;* 跑马灯实验*;*; 延时实现LED流水灯效果p2口八个灯作跑马灯。

三轴数字陀螺仪篇

三轴数字陀螺仪篇
2
还是刚才的那个行人,可是这回的情况不同了,这次他最后摔倒了。这个行人,他起初是在正常行走,一不留神,脚踩到一个西瓜皮,这个人挣扎了几下却无济于事,结果就摔到了。这种现象归类为自动跳位系统现象,如下面的流程图3-2所示。
图3-2 自纠正跳位系统图
当稳定的系统受到外界干扰的时候,系统就面临着失稳的风险。如果系统的自纠正能力在某一方面不是很强,那么它就会被另外的稳定态所代替。在这个例子里,正常行走的行人就是稳定态(A)。突然踩到一个西瓜皮代表一个很强的外界干扰因素随机性地进入系统,行人马上会四肢翻腾,这个翻腾的过程就是过度态。行人在拼命保持自己的重心,但是最后他还是摔倒了。摔倒后,坐在了地上就是稳定态(B)。我们把踩到西瓜皮的瞬间归类为跳出段,把四肢翻腾但是仍然倒下的过程归类为跳位段。
机器人衡态调整
三轴数字陀螺仪篇
北京森汉科技有限公司

一.硬件介绍
1
如下图1-1所示,三轴数字陀螺仪L3G4200D芯片X、Y、Z三轴互相正交分布。
图1-1 坐标方向及背面图
图1-1中明确标出了L3G4200D芯片X、Y、Z轴的坐标方向及背面图。本陀螺仪芯片可供用户选择其尺度量程,量程范围从 ±250 dps ~ ±2000 dps,低量程数值用于高精度慢速运动测量,而高量程则用于测量超快速的手势和运动。这款器件提供一个16位数据输出,以及可配置的低通和高通滤波器等嵌入式数字功能。就算时间推移或温度变化,这款器件仍然保持连续稳定的输出。
图3-3 陀螺仪应用系统图
1
在机器人行走过程中,当处在双腿着地某一姿态时,循环启动陀螺仪周期性地采集三轴数字陀螺仪的三轴数字值,紧接着存储在数组L3G4200D_dis_data[3]变量当中,并比较数组L3G4200D_dis_data[3]与对应设定角速度幅限值,依据是否超幅限值改变相应从0到11号舵机的值,调整机器人姿势使其保持当前姿态稳定。当机器人保持在其当前姿态时,就退出循环,继续行走,同样也依旧进入下一个姿态当中去。若在某一姿态的平衡状态被迫改变,就会同样进入循环当中,及时修正机器人的姿势。本调整程序工作逻辑方式是,快速反馈,快速回程。

51单片机板子功能测试

51单片机板子功能测试

51单片机板子功能测试
1.芯片检测:通过读取板子上的芯片信息,确认是否使用了正确的
51单片机芯片,以及芯片的版本信息。

2.闪存测试:通过在闪存中写入测试代码,然后执行代码,验证闪存
的可读写性以及执行功能正常。

3.存储器测试:包括片内RAM和片外扩展存储器的测试,通过写入和
读取随机数据,并进行校验,验证存储器的可靠性。

4.输入输出测试:测试板子上的各种输入输出接口是否能正常工作,
包括数字输入输出口、模拟输入输出口、通信接口(如串口、I2C、SPI
等)等。

5.定时器测试:通过配置定时器,产生不同频率和占空比的脉冲信号,来测试定时器的准确性和稳定性。

6.PWM测试:通过配置PWM输出,测量占空比和频率,并验证输出的
正常性。

7.中断测试:通过外部中断引脚或定时器中断,测试中断的触发和处
理功能是否正常。

8.ADC/DAC测试:测试模拟输入输出的精度和准确性,通过模拟信号
输入和输出,验证ADC和DAC的功能。

9.LCD测试:测试板子上的LCD接口是否能正常显示字符或图形,并
验证显示的准确性。

10.通信测试:测试板子上的通信接口(如串口、I2C、SPI)是否能
与其他设备进行正常通信,并检查通信的准确性和稳定性。

通过以上这些功能测试,可以全面验证51单片机板子的各项功能是否正常,确保板子的质量和可靠性。

同时可以帮助开发人员在开发过程中排查和解决各种问题,提高开发效率。

51单片机i2c读写程序的详细讲解

51单片机i2c读写程序的详细讲解

51单片机i2c读写程序的详细讲解下载提示:该文档是本店铺精心编制而成的,希望大家下载后,能够帮助大家解决实际问题。

文档下载后可定制修改,请根据实际需要进行调整和使用,谢谢!本店铺为大家提供各种类型的实用资料,如教育随笔、日记赏析、句子摘抄、古诗大全、经典美文、话题作文、工作总结、词语解析、文案摘录、其他资料等等,想了解不同资料格式和写法,敬请关注!Download tips: This document is carefully compiled by this editor. I hope that after you download it, it can help you solve practical problems. The document can be customized and modified after downloading, please adjust and use it according to actual needs, thank you! In addition, this shop provides you with various types of practical materials, such as educational essays, diary appreciation, sentence excerpts, ancient poems, classic articles, topic composition, work summary, word parsing, copy excerpts, other materials and so on, want to know different data formats and writing methods, please pay attention!《51单片机i2c读写程序的详细讲解》一、概述51单片机是一种常用的单片机,在很多应用中使用的最多的是i2c接口,那么如何在51单片机上进行i2c的读写操作呢?下面将详细讲解i2c读写操作的程序设计过程。

L3G4200D 51单片机IIC 测试程序

L3G4200D  51单片机IIC 测试程序

//这是L3G4200D 51单片机IIC 测试程序//这是1602 头文件保存为1602.h#define DataPort P0 //LCD1602数据端口sbit LCD_RS=P2^4; //LCD1602命令端口sbit LCD_RW=P2^5; //LCD1602命令端口sbit LCD_EN=P2^6; //LCD1602命令端口#include <INTRINS.H>#define uchar unsigned charvoid WaitForEnable(void){DataPort=0xff;LCD_RS=0;LCD_RW=1;_nop_();LCD_EN=1;_nop_();_nop_();while(DataPort&0x80);LCD_EN=0;}void WriteCommandLCD(uchar CMD,uchar Attribc) {if(Attribc)WaitForEnable();LCD_RS=0;LCD_RW=0;_nop_();DataPort=CMD;_nop_();LCD_EN=1;_nop_();_nop_();LCD_EN=0;}void WriteDataLCD(uchar dataW){WaitForEnable();LCD_RS=1;LCD_RW=0;_nop_();DataPort=dataW;_nop_();LCD_EN=1;_nop_();_nop_();LCD_EN=0;}void InitLcd(){WriteCommandLCD(0x38,1);WriteCommandLCD(0x08,1);WriteCommandLCD(0x01,1);WriteCommandLCD(0x06,1);WriteCommandLCD(0x0c,1);}void DisplayOneChar(uchar X,uchar Y,uchar DData) {Y&=1;X&=15;if(Y)X|=0x40;X|=0x80;WriteCommandLCD(X,0);WriteDataLCD(DData);}//下面是主程序部分//*************************************** // L3G4200D三轴陀螺仪IIC测试程序// 使用单片机STC89C51// 晶振:11.0592M// 显示:LCD1602// 编译环境Keil uVision2// 参考宏晶网站24c04通信程序// 时间:2011年3月1日//**************************************** #include <reg52.h>#include "1602.h"#include <math.h>#include <stdio.h>#include <INTRINS.H>#define uchar unsigned char#define uint unsigned intsbit SCL=P1^0 ; //IIC时钟引脚定义sbit SDA=P1^1 ; //IIC数据引脚定义//********************#define WHO_AM_I 0x0F#define CTRL_REG1 0x20 // 0010 0000#define CTRL_REG2 0x21#define CTRL_REG3 0x22#define CTRL_REG4 0x23#define CTRL_REG5 0x24#define OUT_X_L 0x28#define OUT_X_H 0x29#define OUT_Y_L 0x2A#define OUT_Y_H 0x2B#define OUT_Z_L 0x2C#define OUT_Z_H 0x2D#define SlaveAddress 0xD2 //定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改typedef unsigned char BYTE;typedef unsigned short WORD;BYTE BUF[8]; //接收数据缓存区long temp;uchar ge,shi,bai,qian,wan; //显示变量int dis_data;//变量//int temp;void delay(unsigned int k);void InitL3G4200D(uchar ); //初始化L3G4200Dvoid conversion(uint temp_data);void Single_WriteL3G4200D(uchar REG_Address,uchar REG_data); //单个写入数据uchar Single_ReadL3G4200D(uchar REG_Address); //单个读取内部寄存器数据void Delay5ms();//void Multiple_readL3G4200D(void);//------------------------------------void Delay5us();void L3G4200D_Start();void L3G4200D_Stop();void L3G4200D_SendACK(bit ack);void L3G4200D_RecvACK();void L3G4200D_SendByte(BYTE dat);BYTE L3G4200D_RecvByte();void display_x();void display_y();void display_z();//-----------------------------------//********************************************************* void conversion(uint temp_data){temp_data*=0.875;wan=temp_data/10000+0x30 ;temp_data=temp_data%10000; //取余运算qian=temp_data/1000+0x30 ;temp_data=temp_data%1000; //取余运算bai=temp_data/100+0x30 ;temp_data=temp_data%100; //取余运算shi=temp_data/10+0x30 ;temp_data=temp_data%10; //取余运算ge=temp_data+0x30;}/*******************************/void delay(unsigned int k){unsigned int i,j;for(i=0;i<k;i++){for(j=0;j<121;j++){;}}}/*****************************************************************延时5微秒(STC90C52RC@12M)不同的工作环境,需要调整此函数,注意时钟过快时需要修改当改用1T的MCU时,请调整此延时函数**************************************/void Delay5us(){_nop_();_nop_();_nop_();_nop_();}/**************************************延时5毫秒(STC90C52RC@12M)不同的工作环境,需要调整此函数当改用1T的MCU时,请调整此延时函数**************************************//*void Delay5ms(){WORD n = 560;while (n--);}*//**************************************起始信号**************************************/ void L3G4200D_Start(){SDA = 1; //拉高数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 0; //产生下降沿Delay5us(); //延时SCL = 0; //拉低时钟线}/**************************************停止信号**************************************/ void L3G4200D_Stop(){SDA = 0; //拉低数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 1; //产生上升沿Delay5us(); //延时}/**************************************发送应答信号入口参数:ack (0:ACK 1:NAK)**************************************/void L3G4200D_SendACK(bit ack){SDA = ack; //写应答信号SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}/**************************************接收应答信号**************************************/void L3G4200D_RecvACK(){SCL = 1; //拉高时钟线Delay5us(); //延时while(SDA==1);SCL = 0; //拉低时钟线Delay5us(); //延时}/**************************************向IIC总线发送一个字节数据**************************************/void L3G4200D_SendByte(BYTE dat){BYTE i;for (i=0; i<8; i++) //8位计数器{SDA = dat&0x80; //送数据口dat=dat<<1;SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}L3G4200D_RecvACK();}/**************************************从IIC总线接收一个字节数据**************************************/BYTE L3G4200D_RecvByte(){BYTE i;BYTE dat = 0;SDA = 1; //使能内部上拉,准备读取数据,for (i=0; i<8; i++) //8位计数器{dat <<= 1; //这里的1是00000001SCL = 1; //拉高时钟线Delay5us(); //延时dat |= SDA; //读数据SDA 00000000 or 00000001SCL = 0; //拉低时钟线Delay5us(); //延时}return dat;}//单字节写入*******************************************void Single_WriteL3G4200D(uchar REG_Address,uchar REG_data){L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf22页L3G4200D_SendByte(REG_data); //内部寄存器数据,请参考中文pdf22页L3G4200D_Stop(); //发送停止信号}//单字节读取*****************************************uchar Single_ReadL3G4200D(uchar REG_Address){ uchar REG_data;L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(REG_Address); //发送存储单元地址,从0开始L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress+1); //发送设备地址+读信号REG_data=L3G4200D_RecvByte(); //读出寄存器数据L3G4200D_SendACK(1);L3G4200D_Stop(); //停止信号return REG_data;}//*************************************************/*void Multiple_readL3G4200D(void){ uchar i;L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(0x28); //发送存储单元地址L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress+1); //发送设备地址+读信号for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF{BUF[i] = L3G4200D_RecvByte()&0xFF; //BUF存储数据if (i == 5){L3G4200D_SendACK(1); //最后一个数据需要回NOACK}else{L3G4200D_SendACK(0); //回应ACK}}L3G4200D_Stop(); //停止信号Delay5ms();}*///*****************************************************************//初始化L3G4200D,根据需要请参考pdf,第27页,进行修改************************void InitL3G4200D(){Single_WriteL3G4200D(CTRL_REG1, 0x4f); // 0x0f=00001111 200hz普通模式X Y Z 启用。

51单片机多点温度采集系统程序清单

51单片机多点温度采集系统程序清单

#include <AT89X52.h>#include <Intrins.h>#define DATA P1 //1602驱动端口//ROM操作命令#define READ_ROM 0x33 //读ROM#define SKIP_ROM 0xCC //跳过ROM#define MATCH_ROM 0x55 //匹配ROM#define SEARCH_ROM 0xF0 //搜索ROM#define ALARM_SEARCH 0xEC //告警搜索//存储器操作命令#define ANEW_MOVE 0xB8 //重新调出E^2数据#define READ_POWER 0xB4 //读电源#define TEMP_SWITCH 0x44 //启动温度变换#define READ_MEMORY 0xBE //读暂存存储器#define COPY_MEMORY 0x48 //复制暂存存储器#define WRITE_MEMORY 0x4E //写暂存存储器//数据存储结构typedef struct tagTempData{unsigned char btThird; //百位数据unsigned char btSecond; //十位数据unsigned char btFirst; //个位数据unsigned char btDecimal; //小数点后一位数据unsigned char btNegative; //是否为负数}TEMPDATA;TEMPDATA m_TempData;//引脚定义sbit DQ = P2^7; //数据线端口sbit RS= P2^0;sbit RW= P2^1;sbit E= P2^2;//DS18B20序列号,通过调用GetROMSequence()函数在P1口读出(读8次)const unsigned char code ROMData1[8] = {0x28, 0x33, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0xD7};const unsigned char code ROMData2[8] = {0x28, 0x30, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0x8E};//U2const unsigned char code ROMData3[8] = {0x28, 0x31, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0xB9};//U3const unsigned char code ROMData4[8] = {0x28, 0x32, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0xE0};//U4const unsigned char code ROMData5[8] = {0x28, 0x34, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0x52};//U5const unsigned char code ROMData6[8] = {0x28, 0x35, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0x65};//U6const unsigned char code ROMData7[8] = {0x28, 0x36, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0x3C};//U7const unsigned char code ROMData8[8] = {0x28, 0x37, 0xC5, 0xB8, 0x00, 0x00, 0x00, 0x0B};//U8//判断忙指令void Busy(){DATA = 0xff;RS = 0;RW = 1;while(DATA & 0x80){E = 0;E = 1;}}//写指令程序void WriteCommand(unsigned char btCommand) {Busy();RS = 0;RW = 0;E = 1;DATA = btCommand;E = 0;}//写数据程序void WriteData(unsigned char btData){Busy();RS = 1;RW = 0;E = 1;DATA = btData;E = 0;}void Clear(){WriteCommand(1);}//初始化void Init(){WriteCommand(0x0c); //开显示,无光标显示WriteCommand(0x06); //文字不动,光标自动右移WriteCommand(0x38); //设置显示模式:8位2行5x7点阵}//显示单个字符void DisplayOne(bit bRow, unsigned char btColumn, unsigned char btData, bit bIsNumber) {if (bRow) WriteCommand(0xc0 + btColumn);else WriteCommand(0x80 + btColumn);if (bIsNumber) WriteData(btData + 0x30);else WriteData(btData);}//显示字符串函数void DisplayString(bit bRow, unsigned char btColumn, unsigned char *pData) {while (*pData != '\0'){if (bRow) WriteCommand(0xc0 + btColumn);//显示在第1行else WriteCommand(0x80 + btColumn); //显示在第0行WriteData(*(pData++)); //要显示的数据btColumn++; //列数加一}}//延时16us子函数void Delay16us(){unsigned char a;for (a = 0; a < 4; a++);}//延时60us子函数void Delay60us(){unsigned char a;for (a = 0; a < 18; a++);}//延时480us子函数void Delay480us(){unsigned char a;for (a = 0; a < 158; a++); }//延时240us子函数void Delay240us(){unsigned char a;for (a = 0; a < 78; a++); }//延时500ms子函数void Delay500ms(){unsigned char a, b, c;for (a = 0; a < 250; a++)for (b = 0; b < 3; b++)for (c = 0; c < 220; c++);}//芯片初始化void Initialization(){while(1){DQ = 0;Delay480us(); //延时480usDQ = 1;Delay60us(); //延时60usif(!DQ) //收到ds18b20的应答信号{DQ = 1;Delay240us(); //延时240usbreak;}}}//写一个字节(从低位开始写)void WriteByte(unsigned char btData){unsigned char i, btBuffer;for (i = 0; i < 8; i++){btBuffer = btData >> i;if (btBuffer & 1){DQ = 0;_nop_();_nop_();DQ = 1;Delay60us();}else{DQ = 0;Delay60us();DQ = 1;}}}//读一个字节(从低位开始读) unsigned char ReadByte(){unsigned char i, btDest;for (i = 0; i < 8; i++){btDest >>= 1;DQ = 0;_nop_();_nop_();DQ = 1;Delay16us();if (DQ) btDest |= 0x80;Delay60us();}return btDest;}//序列号匹配void MatchROM(const unsigned char *pMatchData) {unsigned char i;Initialization();WriteByte(MATCH_ROM);for (i = 0; i < 8; i++) WriteByte(*(pMatchData + i)); }//得到64位ROM序列(在P1口显示,必须与Proteus联调且在单步调试下才能得到) /*void GetROMSequence(){unsigned char i;Initialization();WriteByte(READ_ROM);for (i = 0; i < 8; i++)P1 = ReadByte();}*///读取温度值TEMPDATA ReadTemperature(){TEMPDATA TempData;unsigned int iTempDataH;unsigned char btDot, iTempDataL;static unsigned char i = 0;TempData.btNegative = 0; //为0温度为正i++;if (i == 9) i = 1;Initialization();WriteByte(SKIP_ROM); //跳过ROM匹配WriteByte(TEMP_SWITCH); //启动转换Delay500ms(); //调用一次就行Delay500ms();Initialization();//多个芯片的时候用MatchROM(ROMData)换掉WriteByte(SKIP_ROM)switch (i){case 1 : MatchROM(ROMData1); break; //匹配1case 2 : MatchROM(ROMData2); break; //匹配2case 3 : MatchROM(ROMData3); break; //匹配3case 4 : MatchROM(ROMData4); break; //匹配4case 5 : MatchROM(ROMData5); break; //匹配5case 6 : MatchROM(ROMData6); break; //匹配6case 7 : MatchROM(ROMData7); break; //匹配7case 8 : MatchROM(ROMData8); break; //匹配8}//WriteByte(SKIP_ROM); //跳过ROM匹配(单个芯片时用这句换掉上面的switch)WriteByte(READ_MEMORY); //读数据iTempDataL = ReadByte();iTempDataH = ReadByte();iTempDataH <<= 8;iTempDataH |= iTempDataL;if (iTempDataH & 0x8000){TempData.btNegative = 1;iTempDataH = ~iTempDataH + 1; //负数求补}//为了省去浮点运算带来的开销,而采用整数和小数部分分开处理的方法(没有四舍五入)btDot = (unsigned char)(iTempDataH & 0x000F); //得到小数部分iTempDataH >>= 4; //得到整数部分btDot *= 5; //btDot*10/16得到转换后的小数数据btDot >>= 3;//数据处理TempData.btThird = (unsigned char)iTempDataH / 100;TempData.btSecond = (unsigned char)iTempDataH % 100 / 10;TempData.btFirst = (unsigned char)iTempDataH % 10;TempData.btDecimal = btDot;return TempData;}//数据处理子程序void DataProcess(){m_TempData = ReadTemperature();if (m_TempData.btNegative) DisplayOne(1, 6, '-', 0);else DisplayOne(1, 6, m_TempData.btThird, 1);DisplayOne(1, 7, m_TempData.btSecond, 1);DisplayOne(1, 8, m_TempData.btFirst, 1);DisplayOne(1, 10, m_TempData.btDecimal, 1);}void main(){//GetROMSequence();Clear();Init();DisplayString(0, 0, " Temperature");DisplayOne(1, 9, '.', 0);while (1) DataProcess();}。

51单片机IO口线模拟IIC总线3部曲之第2部

51单片机IO口线模拟IIC总线3部曲之第2部

// 51单片机IO口线模拟IIC总线3部曲之第2部 IICLowDriver.H// 把本文全部复制保存为IICLowDriver.H//**************************************************************************** // 文件名 IICLowDriver.H// 简介:// 使用51单片机IO口线来模拟IIC总线。

适用于12T周期单片机,不能用于单T周期单片机。

// 本文件中是IIC最底层的驱动函数//// 这些底层驱动函数包括IIC总线的起动(IICStart)与关闭(IICStop),MCU对应答信号// 的发送(IICMCUSendACK)与接收(IICMCURcvACK),1字节数据的发送(IICMCUSendSDAByte)// 与接收(IICMCURcvSDAByte)这6个函数。

这6个函数是相互独立的,没有互相调用。

// 针对不同的IIC器件读写时序的不同,您应该自己编写IIC器件的驱动。

// 另外为了适应不同频率的时钟,本驱动中作了相关的设置。

// 详细使用说明请参见《IIC底层驱动包使用说明》。

// 移植说明:// 请修改本文件中的“移植修改”部分来完成移植。

仅需要改动3个地方,SCL// SDA和N_12MHz。

其他预处理命令请不要改动。

//*********************************************************************#define uchar unsigned char//--------------------------头文件包含------------------------------//#include<reg52.h>//-----------------------------移植修改-----------------------------////-----------------------------信号定义-----------------------------//#if defined(SDA_ON)sbit SDA = P1^7; // 模拟I2C数据传送位,移植时请修改此处的IO口#endif#if defined(SCL_ON)sbit SCL = P1^6; // 模拟I2C时钟控制位,移植时请修改此处的IO口#endif//-----------------------------晶振频率之比-------------------------//// N_12MHz:所用晶振频率F(单位MHz)与12MHz的比值。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

//这是L3G4200D 51单片机IIC 测试程序//这是1602 头文件保存为1602.h#define DataPort P0 //LCD1602数据端口sbit LCD_RS=P2^4; //LCD1602命令端口sbit LCD_RW=P2^5; //LCD1602命令端口sbit LCD_EN=P2^6; //LCD1602命令端口#include <INTRINS.H>#define uchar unsigned charvoid WaitForEnable(void){DataPort=0xff;LCD_RS=0;LCD_RW=1;_nop_();LCD_EN=1;_nop_();_nop_();while(DataPort&0x80);LCD_EN=0;}void WriteCommandLCD(uchar CMD,uchar Attribc) {if(Attribc)WaitForEnable();LCD_RS=0;LCD_RW=0;_nop_();DataPort=CMD;_nop_();LCD_EN=1;_nop_();_nop_();LCD_EN=0;}void WriteDataLCD(uchar dataW){WaitForEnable();LCD_RS=1;LCD_RW=0;_nop_();DataPort=dataW;_nop_();LCD_EN=1;_nop_();_nop_();LCD_EN=0;}void InitLcd(){WriteCommandLCD(0x38,1);WriteCommandLCD(0x08,1);WriteCommandLCD(0x01,1);WriteCommandLCD(0x06,1);WriteCommandLCD(0x0c,1);}void DisplayOneChar(uchar X,uchar Y,uchar DData) {Y&=1;X&=15;if(Y)X|=0x40;X|=0x80;WriteCommandLCD(X,0);WriteDataLCD(DData);}//下面是主程序部分//*************************************** // L3G4200D三轴陀螺仪IIC测试程序// 使用单片机STC89C51// 晶振:11.0592M// 显示:LCD1602// 编译环境Keil uVision2// 参考宏晶网站24c04通信程序// 时间:2011年3月1日//**************************************** #include <reg52.h>#include "1602.h"#include <math.h>#include <stdio.h>#include <INTRINS.H>#define uchar unsigned char#define uint unsigned intsbit SCL=P1^0 ; //IIC时钟引脚定义sbit SDA=P1^1 ; //IIC数据引脚定义//********************#define WHO_AM_I 0x0F#define CTRL_REG1 0x20 // 0010 0000#define CTRL_REG2 0x21#define CTRL_REG3 0x22#define CTRL_REG4 0x23#define CTRL_REG5 0x24#define OUT_X_L 0x28#define OUT_X_H 0x29#define OUT_Y_L 0x2A#define OUT_Y_H 0x2B#define OUT_Z_L 0x2C#define OUT_Z_H 0x2D#define SlaveAddress 0xD2 //定义器件在IIC总线中的从地址,根据ALT ADDRESS地址引脚不同修改typedef unsigned char BYTE;typedef unsigned short WORD;BYTE BUF[8]; //接收数据缓存区long temp;uchar ge,shi,bai,qian,wan; //显示变量int dis_data;//变量//int temp;void delay(unsigned int k);void InitL3G4200D(uchar ); //初始化L3G4200Dvoid conversion(uint temp_data);void Single_WriteL3G4200D(uchar REG_Address,uchar REG_data); //单个写入数据uchar Single_ReadL3G4200D(uchar REG_Address); //单个读取内部寄存器数据void Delay5ms();//void Multiple_readL3G4200D(void);//------------------------------------void Delay5us();void L3G4200D_Start();void L3G4200D_Stop();void L3G4200D_SendACK(bit ack);void L3G4200D_RecvACK();void L3G4200D_SendByte(BYTE dat);BYTE L3G4200D_RecvByte();void display_x();void display_y();void display_z();//-----------------------------------//********************************************************* void conversion(uint temp_data){temp_data*=0.875;wan=temp_data/10000+0x30 ;temp_data=temp_data%10000; //取余运算qian=temp_data/1000+0x30 ;temp_data=temp_data%1000; //取余运算bai=temp_data/100+0x30 ;temp_data=temp_data%100; //取余运算shi=temp_data/10+0x30 ;temp_data=temp_data%10; //取余运算ge=temp_data+0x30;}/*******************************/void delay(unsigned int k){unsigned int i,j;for(i=0;i<k;i++){for(j=0;j<121;j++){;}}}/*****************************************************************延时5微秒(STC90C52RC@12M)不同的工作环境,需要调整此函数,注意时钟过快时需要修改当改用1T的MCU时,请调整此延时函数**************************************/void Delay5us(){_nop_();_nop_();_nop_();_nop_();}/**************************************延时5毫秒(STC90C52RC@12M)不同的工作环境,需要调整此函数当改用1T的MCU时,请调整此延时函数**************************************//*void Delay5ms(){WORD n = 560;while (n--);}*//**************************************起始信号**************************************/ void L3G4200D_Start(){SDA = 1; //拉高数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 0; //产生下降沿Delay5us(); //延时SCL = 0; //拉低时钟线}/**************************************停止信号**************************************/ void L3G4200D_Stop(){SDA = 0; //拉低数据线SCL = 1; //拉高时钟线Delay5us(); //延时SDA = 1; //产生上升沿Delay5us(); //延时}/**************************************发送应答信号入口参数:ack (0:ACK 1:NAK)**************************************/void L3G4200D_SendACK(bit ack){SDA = ack; //写应答信号SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}/**************************************接收应答信号**************************************/void L3G4200D_RecvACK(){SCL = 1; //拉高时钟线Delay5us(); //延时while(SDA==1);SCL = 0; //拉低时钟线Delay5us(); //延时}/**************************************向IIC总线发送一个字节数据**************************************/void L3G4200D_SendByte(BYTE dat){BYTE i;for (i=0; i<8; i++) //8位计数器{SDA = dat&0x80; //送数据口dat=dat<<1;SCL = 1; //拉高时钟线Delay5us(); //延时SCL = 0; //拉低时钟线Delay5us(); //延时}L3G4200D_RecvACK();}/**************************************从IIC总线接收一个字节数据**************************************/BYTE L3G4200D_RecvByte(){BYTE i;BYTE dat = 0;SDA = 1; //使能内部上拉,准备读取数据,for (i=0; i<8; i++) //8位计数器{dat <<= 1; //这里的1是00000001SCL = 1; //拉高时钟线Delay5us(); //延时dat |= SDA; //读数据SDA 00000000 or 00000001SCL = 0; //拉低时钟线Delay5us(); //延时}return dat;}//单字节写入*******************************************void Single_WriteL3G4200D(uchar REG_Address,uchar REG_data){L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(REG_Address); //内部寄存器地址,请参考中文pdf22页L3G4200D_SendByte(REG_data); //内部寄存器数据,请参考中文pdf22页L3G4200D_Stop(); //发送停止信号}//单字节读取*****************************************uchar Single_ReadL3G4200D(uchar REG_Address){ uchar REG_data;L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(REG_Address); //发送存储单元地址,从0开始L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress+1); //发送设备地址+读信号REG_data=L3G4200D_RecvByte(); //读出寄存器数据L3G4200D_SendACK(1);L3G4200D_Stop(); //停止信号return REG_data;}//*************************************************/*void Multiple_readL3G4200D(void){ uchar i;L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress); //发送设备地址+写信号L3G4200D_SendByte(0x28); //发送存储单元地址L3G4200D_Start(); //起始信号L3G4200D_SendByte(SlaveAddress+1); //发送设备地址+读信号for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF{BUF[i] = L3G4200D_RecvByte()&0xFF; //BUF存储数据if (i == 5){L3G4200D_SendACK(1); //最后一个数据需要回NOACK}else{L3G4200D_SendACK(0); //回应ACK}}L3G4200D_Stop(); //停止信号Delay5ms();}*///*****************************************************************//初始化L3G4200D,根据需要请参考pdf,第27页,进行修改************************void InitL3G4200D(){Single_WriteL3G4200D(CTRL_REG1, 0x4f); // 0x0f=00001111 200hz普通模式X Y Z 启用。

相关文档
最新文档