51單片機串口輸出
本文引用地址:http://www.ex-cimer.com/article/201611/323460.htm//***************************************// HMC5883 51串口測試程序// 使用單片機STC89C51 // 晶振:11.0592M// 顯示:PC串口// 編譯環(huán)境 Keil uVision2// 參考宏晶網(wǎng)站24c04通信程序// 時間:2011年3月1日//****************************************#include #include //Keil library #include //Keil library #include #define uchar unsigned char#define uint unsigned int //使用的端口,請按照以下接線#define DataPort P0 //LCD1602數(shù)據(jù)端口sbit SCL=P1^0; //IIC時鐘引腳定義sbit SDA=P1^1; //IIC數(shù)據(jù)引腳定義#define SlaveAddress 0x3C //定義器件5883在IIC總線中的從地址typedef unsigned char BYTE;typedef unsigned short WORD;BYTE BUF[8]; //接收數(shù)據(jù)緩存區(qū) uchar ge,shi,bai,qian,wan; //顯示變量int dis_data; //變量//************//************void delay(unsigned int k);void Init_HMC5883(void); //初始化5883void conversion(uint temp_data);void Single_Write_HMC5883(uchar REG_Address,uchar REG_data); //單個寫入數(shù)據(jù)//uchar Single_Read_HMC5883(uchar REG_Address); //單個讀取內(nèi)部寄存器數(shù)據(jù)void Multiple_Read_HMC5883(); //連續(xù)的讀取內(nèi)部寄存器數(shù)據(jù)//以下是模擬iic使用函數(shù)-------------void Delay5us();void Delay5ms();void HMC5883_Start();void HMC5883_Stop();void HMC5883_SendACK(bit ack);bit HMC5883_RecvACK();void HMC5883_SendByte(BYTE dat);BYTE HMC5883_RecvByte();void HMC5883_ReadPage();void HMC5883_WritePage();//-----------------------------------//*********************************************//串口初始化//9600 bps @ 11.059 MHz void init_uart(){TMOD=0x21; TH1=0xfd; TL1=0xfd; SCON=0x50;PS=1; //串口中斷設(shè)為高優(yōu)先級別TR0=1; //啟動定時器 TR1=1;ET0=1; //打開定時器0中斷 ES=1; }//*********串口數(shù)據(jù)發(fā)送******************void SeriPushSend(uchar send_data){SBUF=send_data; while(!TI);TI=0; }//*********************************************************void conversion(uint temp_data) { 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
評論