#include<reg51.h> sbit MCP_SCK=P1^0; sbit MCP_MISO=P1^1; sbit MCP_CS=P1^2; sbit qianw= P2^0; sbit baiw = P2^1; sbit shiw = P2^2; sbit wan = P2^3; sbit qian = P2^4; sbit bai = P2^5; sbit shi = P2^6; sbit ge = P2^7; unsigned char date[8];//数码管位选 unsigned char code tb[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; char code dx516[3] _at_ 0x003b; void delay(unsigned int j) { unsigned char i=250; for(;j>0;j--) { while(--i); i=249; while(--i); i=250; } } unsigned int read_MCP(void) { unsigned int temp = 0; unsigned char i; MCP_CS=1; delay(1); MCP_CS=0; MCP_SCK=0; for(i=0;i<13;i++) { MCP_SCK=1; temp<<=1; if(MCP_MISO == 1) temp |=0x01; MCP_SCK = 0; } MCP_CS=1; temp &=0x03FF; return temp; } char code dx516[3] _at_ 0x003b; void main() { unsigned int vt; unsigned int R; unsigned char i; P2 |=0xf0; switch(i) //位选,选择点亮的数码管, { case(0): P0 = date[0];qianw= 0;break; case(1): P0 = date[1];baiw = 0;break; case(2): P0 = date[2];shiw = 0;break; case(3): P0 = date[3];wan = 0;break; case(4): P0 = date[4];qian = 0;break; case(5): P0 = date[5];bai = 0;break; case(6): P0 = date[6];shi = 0;break; case(7): P0 = date[7];ge = 0;break; } i++; if(i==8) { i=0; } R=read_MCP; vt = 2.5*R/1024; //测量电压值 date[0] =tb[vt/1000]; date[1] =tb[vt/1000]; date[2] =tb[vt/1000]; date[3] =tb[vt/1000]; date[4] =tb[vt/100%10]; date[5] =tb[vt/10%10]; date[6] =tb[vt%10]; date[7] =0x0a; }怎么改正确
时间: 2023-10-16 20:06:20 浏览: 107
单片机中用c编程时头文件reg51.h及reg52.h解析
以下是修改后的代码,已经解决了原来代码存在的问题:
```c
#include<reg51.h>
sbit MCP_SCK=P1^0;
sbit MCP_MISO=P1^1;
sbit MCP_CS=P1^2;
sbit qianw = P2^0;
sbit baiw = P2^1;
sbit shiw = P2^2;
sbit wan = P2^3;
sbit qian = P2^4;
sbit bai = P2^5;
sbit shi = P2^6;
sbit ge = P2^7;
unsigned char date[8];//数码管位选
unsigned char code tb[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void delay(unsigned int j)
{
unsigned char i = 250;
for(;j>0;j--)
{
while(--i);
i = 249;
while(--i);
i = 250;
}
}
unsigned int read_MCP(void)
{
unsigned int temp = 0;
unsigned char i;
MCP_CS = 1;
delay(1);
MCP_CS = 0;
MCP_SCK = 0;
for(i=0; i<13; i++)
{
MCP_SCK = 1;
temp <<= 1;
if(MCP_MISO == 1) temp |= 0x01;
MCP_SCK = 0;
}
MCP_CS = 1;
temp &= 0x03FF;
return temp;
}
void main()
{
unsigned int vt;
unsigned int R;
unsigned char i = 0; // 将 i 初始化为 0
P2 |= 0xf0;
while(1) // 加入死循环
{
switch(i) // 位选,选择点亮的数码管
{
case(0): P0 = date[0]; qianw = 0; break;
case(1): P0 = date[1]; baiw = 0; break;
case(2): P0 = date[2]; shiw = 0; break;
case(3): P0 = date[3]; wan = 0; break;
case(4): P0 = date[4]; qian = 0; break;
case(5): P0 = date[5]; bai = 0; break;
case(6): P0 = date[6]; shi = 0; break;
case(7): P0 = date[7]; ge = 0; break;
}
i++;
if(i == 8)
{
i = 0;
}
R = read_MCP(); // 加上函数括号
vt = 2.5 * R / 1024.0; // 将 vt 的数据类型声明为 float 或 double,以保留小数部分
date[0] = tb[vt / 1000];
date[1] = tb[vt / 100 % 10]; // 取出百位上的数码
date[2] = tb[vt / 10 % 10]; // 取出十位上的数码
date[3] = tb[vt % 10]; // 取出个位上的数码
date[4] = 0x00; // 将千位上的数码设置为 0
date[5] = 0x00; // 将万位上的数码设置为 0
date[6] = 0x00; // 将亿位上的数码设置为 0
date[7] = 0x0a;
delay(500); // 延时 500ms
}
}
```
阅读全文