1.uart c Դ??
uart c Դ??
#include <reg.h>
#include <math.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define INBUF_LEN 6 //数据长度
unsigned char inbuf1[INBUF_LEN]={ '0','0','0','0','0','0'};//发送缓冲区
unsigned char code table[] = { 0xc0,0xf9,0xa4,0xb0,0x,0x,0x,0xf8,0x,0x,0x,0x,0xc6,0xa1,0x,0x8e};//共阳数码管显示0~F
unsigned char code dis_8[]={ '0','1','2','3','4','5','6','7','8','9',' ','-'};
sbit CS=P2^5; //定义片选信号IO口
sbit SCLK=P2^4; //定义时钟信号IO口
sbit DIN=P2^3; //定义数据输入IO口
uint temp;
void init_serialcomm( void )
{
SCON = 0x ; //SCON: serail mode 1, 8-bit UART, enable ucvr
T2CON=0x;
TH2=0x;
TL2=0x;
RCAP2H=0xFF;
RCAP2L=0xDC;
TR2=1;
}
//向串口发送一个字符
void send_char_com( unsigned char ch)
{
SBUF=ch;
while (TI== 0 );
TI= 0 ;
}
//向串口发送一个字符串,strlen 为该字符串长度
void send_string_com( unsigned char *str,车辆定位 源码 unsigned int strlen)
{
unsigned int k= 0 ;
do
{
send_char_com(*(str + k));
k++;
} while (k < strlen);
}
void delay(uint k) //延时函数
{
uint i,j;
for(i=0;i<k;i++){
for(j=0;j<;j++)
{ ;}}
}
//实现DA转换
void DAConvert(uint Data){
uchar i;
Data<<=6;
CS=0;
SCLK=0;
for (i=0;i<;i++){
DIN=(bit)(Data&0x);
SCLK=1;
Data<<=1;
SCLK=0;
}
CS=1;
SCLK=0;
for (i=0;i<;i++);
}
//定时器0中断处理中键扫描和显示
void KeyAndDis_Time0(void) interrupt 1 using 2
{
TH0=0xdc;
TL0=0x;//设定时值为ms
P0= table[ temp/];
P2=0x;
delay(2);
P0= table[ (temp%)/];
P2=0x;
delay(2);
P0= table[ (temp%)/];
P2=0x;
delay(2);
P0= table[ temp%];
P2=0x;
delay(2);
}
//主程序
main(){
uint i;
init_serialcomm(); //初始化串口
EA = 1; //允许CPU中断
ET0 = 1; //定时器0中断打开
TMOD = 0x; //设定时器0为模式1,位模式
TH0=0xdc;
TL0=0x; //设定时值为ms
TR0 = 1;
while(1){
for(i=;i<;i+=2){
DAConvert(i);//启动转换
temp=i;
inbuf1[0]=dis_8[temp/];
inbuf1[1]=dis_8[(temp%)/];
inbuf1[2]=dis_8[(temp%)/];
inbuf1[3]=dis_8[temp%];
inbuf1[4]=0x0d;
inbuf1[5]=0;
send_string_com(inbuf1,源码管理中心INBUF_LEN);//串口输出到上位机
delay(1);
}
for(i=;i>;i-=2){ DAConvert(i);//启动转换
temp=i;
inbuf1[0]=dis_8[temp/];
inbuf1[1]=dis_8[(temp%)/];
inbuf1[2]=dis_8[(temp%)/];
inbuf1[3]=dis_8[temp%];
inbuf1[4]=0x0d;
inbuf1[5]=0;
send_string_com(inbuf1,INBUF_LEN);
delay(1);
}
}
}
希望对你有帮助。
eclipse无法观看源码eclipse无法观看源码