以前的代碼,你可以參考下.里面傳的是float,long int只要改下就行了
//=================頭文件======================
#ifndef __UART_H_
#define __UART_H_
#include <stc8f.h>
#include <intrins.h>
typedef union //float聯合體
{
float value;
unsigned char byte[4];
}float_data;
#define FOSC 11059200UL //晶振頻率
#define BRT (65536-FOSC/9600/4) //波特率設置
void UartInit();
void UartSend(char dat);
void SendUint(unsigned int value);
void UartSendStr(char *p);
void SendFloat(float_data* float_union);
#endif
//===================C文件=====================
#include "uart.h"
char Uart_get_char;
void UartIsr() interrupt 4
{
RI = 0;
Uart_get_char = SBUF; //讀
}
void UartInit()
{
SCON = 0x50;
T2L = BRT; //波特率設置
T2H = BRT>>8;
AUXR = 0x15;
ES = 1;
EA = 1;
}
void UartSend(char dat) //送1個字節
{
SBUF = dat;
while(!TI);
TI = 0;
}
void SendUint(unsigned int value) //送U16數據
{
unsigned char valueH = 0;
unsigned char valueL = 0;
valueL |= value;
valueH |= value >> 8;
UartSend(valueL);
UartSend(valueH);
}
void SendFloat(float_data* float_union) //送float數據
{
UartSend(float_union->byte[3]);
UartSend(float_union->byte[2]);
UartSend(float_union->byte[1]);
UartSend(float_union->byte[0]);
}
//==============================================
基本都是從手冊上照搬的代碼,稍作修改整理就行了. |