int num = 500;存到eeprom;
high_new = num/256; // 获取高8位整数值
low_new = num%256; // 获取低8位整数值
这是一段C语言代码,用于将一个16位的整数num分解为高位和低位两个8位的整数high_new和low_new。
具体的实现方法是,先将num除以256,得到num的高8位整数值,再将num对256取模,得到num的低8位整数值。
#include "reg52.h"
#include "intrins.h"
#include "iic.h"
unsigned int num = 500;
unsigned char code SMG_duanma[]= //0-9
{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char high_old,low_old,high_new,low_new;
void Delay50ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 3;
j = 26;
k = 223;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay100us() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
i = 2;
j = 15;
do
{
while (--j);
} while (--i);
}
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2 = (P2 & 0x1f) | 0x80;
break;
case 5:
P2 = (P2 & 0x1f) | 0xa0;
break;
case 6:
P2 = (P2 & 0x1f) | 0xc0;
break;
case 7:
P2 = (P2 & 0x1f) | 0xe0;
break;
}
}
void DisplaySMG_Bit(unsigned char value, unsigned char pos)
{
P0 = 0xff;
SelectHC573(6);
P0 = 0x01 << pos;
SelectHC573(7);
P0 = value;
}
unsigned char eeprom_read(unsigned char pos)
{
unsigned char value;
I2CStart();
I2CSendByte(0xa0);//开写模式
I2CWaitAck();
I2CSendByte(pos);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);//开du模式
I2CSendAck(1);
value = I2CReceiveByte();
I2CStop();
return value;
}
void eeprom_write(unsigned char address,unsigned char value)
{
I2CStart();
I2CSendByte(0xa0);//eeprom写入模式
I2CWaitAck();
I2CSendByte(address);
I2CWaitAck();
I2CSendByte(value);
I2CWaitAck();
I2CStop();
}
void main()
{
low_old = eeprom_read(0);
Delay50ms();
high_old = eeprom_read(1);
num = high_old*256+low_old;
num += 100;
if(num>9999)
num =8123;
high_new = num/256;
low_new = num%256;
eeprom_write(0,low_new);
Delay50ms();
eeprom_write(1,high_new);
Delay50ms();
while(1)
{
DisplaySMG_Bit(SMG_duanma[num/1000],0);
Delay100us();
DisplaySMG_Bit(SMG_duanma[num/100%10],1);
Delay100us();
DisplaySMG_Bit(SMG_duanma[num/10%10],2);
Delay100us();
DisplaySMG_Bit(SMG_duanma[num%10],3);
Delay100us();
}
}