數組的數據類型設置錯誤,temp重復定義,for循環無意義。修改好了。
#include<reg51.h>
#include<intrins.h>
/********************************************************************************************************
** Marcos define
*********************************************************************************************************/
#define uint unsigned int
#define uchar unsigned char
#define MotorTabNum 5
//IO設置
sbit QH = P3^0; //輸出端
sbit CLK = P3^1; //時鐘輸入端(上升沿有效)
sbit SPL = P3^2; //移位控制/置入控制(低電平有效)
//數碼管
sbit SMG1 = P2^0; //數碼管第一位定義
sbit SMG2 = P2^1; //數碼管第二位定義
sbit SMG3 = P2^2; //數碼管第三位定義
sbit SMG4 = P2^3; //數碼管第四位定義
sbit SMG5 = P2^4; //數碼管第五位定義
sbit SMG6 = P2^5; //數碼管第六位定義
sbit SMG7 = P2^6; //數碼管第七位定義
sbit SMG8 = P2^7; //數碼管第八位定義
uchar temp = 0;
uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}; //共陽極數碼管段值
void Display(void);
void delayms(xms);
void mDelay(unsigned int DelayTime); //延時函數
/********************************************************************************************************
* Function Name : read_int165
* Description : 接收數據
* Input : None
* Output : None
* Return : None
********************************************************************************************************/
uint read_int165(void)
{
uchar i = 0;
uint read_data = 0;
SPL = 0; //置數,讀入并行輸入口數據
_nop_();
SPL = 1; //移位,并口輸入被封鎖,串行轉換開始
_nop_();
for(i=0;i<8;i++) //8位數據
{
read_data <<= 1;
if(QH)
{
read_data|=QH;
}
CLK=0; //下降沿
_nop_();
CLK=1; //上升沿
_nop_();
}
return read_data;
}
/********************************************************************************************************
* Function Name : main
* Description : 主函數
* Input : None
* Output : None
* Return : None
********************************************************************************************************/
void main()
{
//初始化
// unsigned char i;
// uint temp = 0;
CLK=0;
while(1)
{
temp = read_int165();
//獲取高位,存放置temp
P1=temp; //接收到的字節顯示在P1 端口,顯示的值與撥碼開關對應
// for(i=0; i<10; i++)
Display();
}
}
void Display(void)
{
unsigned char B1,B2,B3,B4,B5,B6,B7,B8;//定義數碼管的每一位
B1=temp/10000000; //取g_MotorNum的千萬位
B2=temp%10000000/1000000; //取g_MotorNum的百萬位
B3=temp%1000000/100000; //取g_MotorNum的十萬位
B4=temp%100000/10000; //取g_MotorNum的萬位
B5=temp%10000/1000; //取g_MotorNum的千位
B6=temp%1000/100; //取g_MotorNum的百位
B7=temp%100/10; //取g_MotorNum的十位
B8=temp%10; //取g_MotorNum的個位
//顯示千萬位
P0=table[B1];
SMG1=1;
delayms(2);
SMG1=0;
//顯示百萬位
P0=table[B2];
SMG2=1;
delayms(5);
SMG2=0;
//顯示十萬位
P0=table[B3];
SMG3=1;
delayms(2);
SMG3=0;
//顯示萬位
P0=table[B4];
SMG4=1;
delayms(2);
SMG4=0;
//顯示千位
P0=table[B5];
SMG5=1;
delayms(2);
SMG5=0;
//顯示百位
P0=table[B6];
SMG6=1;
delayms(2);
SMG6=0;
//顯示十位
P0=table[B7];
SMG7=1;
delayms(2);
SMG7=0;
//顯示個位
P0=table[B8];
SMG8=1;
delayms(2);
SMG8=0;
}
/********************************************************************************************************
* Function Name : delayms
* Description : 延時函數
* Input : None
* Output : None
* Return : None
********************************************************************************************************/
void delayms(xms)
{
unsigned int x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
} |