📄 main.c
字号:
{
PullDownDQ();
Delay_us(2); //2us(>1us)
ReleaseDQ(); //(上述1-15)
Delay_us(85); //86us(45- x,总时间>60)
}else
{
PullDownDQ();
Delay_us(85); //86us(60-120)
}
ReleaseDQ();
Delay_us(2); //2us(>1us)
}
/********************************************************
* 写Byte DS18B20 *
********************************************************/
void Ds18b20_WriteByte(uchar chrdata)
{
uchar ii;
for(ii = 0; ii < 8; ii++)
{
Ds18b20_WriteBit(chrdata & 0x01);
chrdata >>= 1;
}
}
/********************************************************
* 读bit From DS18B20 *
********************************************************/
uchar Ds18b20_ReadBit(void)
{
uchar bitdata;
IoOut_DQ();
PullDownDQ();
Delay_us(2); //2us( >1us)
ReleaseDQ();
Delay_us(8); //8us( <15us)
IoIn_DQ();
bitdata = ReadDQ();
Delay_us(85); //85us(上述总时间要>60us)
return bitdata;
}
/********************************************************
* 读Byte DS18B20 *
********************************************************/
uchar Ds18b20_ReadByte(void)
{
uchar ii,chardata;
for(ii = 0; ii < 8; ii++)
{
chardata >>= 1;
if(Ds18b20_ReadBit()) chardata |= 0x80;
}
return chardata;
}
/********************************************************
* 读 DS18B20 EE *
********************************************************/
uchar Ds18b20_ReadEE(uchar *p_readdata) //成功返0,失败返1
{
uchar ii = 9;
if(Ds18b20_Init()) return 1;
Delay_us(1);
Ds18b20_WriteByte(SkipROM);
Delay_us(1);
Ds18b20_WriteByte(ReadScr);
Delay_us(1);
while(ii--)
{
*p_readdata = Ds18b20_ReadByte();
p_readdata++;
}
return 0;
}
/********************************************************
* 温度采集计算 *
********************************************************/
uchar TempCal(uchar *p_fuhao,uchar*p_wendu_zhensu,uchar *p_wendu_yusu)
//成功返0,失败返1 (温度范围-55 --- +128)
{
uchar temp[9],ii,crc_data = 0;
uint tmp = 0;
uchar tmp_ys = 0;
*p_fuhao = 0;
//读暂存器和CRC值-----------------------
if(Ds18b20_ReadEE(temp))
{
return 1;
}
//-------------------------------------
//CRC校验------------------------------
for(ii = 0; ii < 9; ii++)
{
crc_data = CrcTable[crc_data^temp[ii]];
}
//-------------------------------------
if(crc_data == 0)
{
tmp = temp[1]; //
tmp <<= 8; //
tmp |= temp[0]; //组成温度的两字节合并
//温度正负数处理-----------------------
if(temp[1] >>= 4) //温度为负
{
tmp = ~tmp + 1;
*p_fuhao = 1; //返回值,1为负0为正
}
//-------------------------------------
//温度计算-----------------------------
tmp_ys =tmp % 16; //取十进制温度的余数
tmp_ys = (tmp_ys * 10) / 16; //十进制温度的小数*10(取小数点后一位)
*p_wendu_zhensu = tmp / 16;
*p_wendu_yusu = tmp_ys;
//-------------------------------------
}
//开始温度转换-------------------------
while(Ds18b20_Init() & (--ii) );
//Ds18b20_Init();
Ds18b20_WriteByte(SkipROM);
Ds18b20_WriteByte(Convert);
ReleaseDQ(); //寄生电源时要拉高DQ
//------------------------------------
ds= '0';
return 0;
}
//===============================
void temG()
{ uchar ii;
ii=10;
while(Ds18b20_Init() & (--ii) );
Ds18b20_WriteByte(SkipROM);
Ds18b20_WriteByte(Convert);
ReleaseDQ();
Delay_ms(2000);
}
//====================================================
// Function : flash_readFLASH
void ReadFlash(uint wAddr,uchar *bBuf,uchar bLen)
{
while (bLen--)
*bBuf++=*(uchar *)wAddr++;
return;
}
//********************************************
// Function : flash_writeFLASH
// Parameter : *dst : address within the FLASH page
// value : BYTE that has to be written to FLASH
void flash_writeByte(int dst, uchar value)
{
int * dst1;
dst1=(int *)dst;
FCTL2 = FWKEY | FSSEL0 | 20; //clock source is MCLK, divisor is 20
do //Fclk is 250~470hz
{
_NOP();
} while(FCTL3 & 0x0001); // wait for BUSY to reset
FCTL3 = FWKEY; // reset the LOCK bit to enable program/erase
FCTL1 = FWKEY | WRT; // set WRT for single acces
*dst1 = value; // do the write as a byte
return;
}
//*******************************
//Function : flash_eraseFLASH
//Parameter : *seg : any address within the FLASH page that is to be erased
//********************************
void flash_eraseFLASH(uchar *seg)
{
FCTL2 = FWKEY | FSSEL0 | 20; //clock source is MCLK, divisor is 20
do
{
_NOP();
} while(FCTL3 & 0x0001); // wait for BUSY to reset
FCTL3 = FWKEY; // reset the LOCK bit to enable program/erase
FCTL1 = FWKEY | ERASE; // set single segment erase function
*seg = 0xFF; // do a dummy write to start erase
FCTL3 = FWKEY | LOCK; // lock the flash again
return;
}
void mu1(void)//mu
{
y1=1;
LCDMEM[0] = digit[10]; //显示"|"
LCDMEM[1] = digit[0]; //====0
LCDMEM[2] = digit[xx[1]]; //一级菜单
LCDMEM[3]= digit[xx[1]];
LCDMEM[4]= digit[xx[1]];
LCDMEM[5]= digit[xx[1]];
LCDMEM[6]= digit[xx[1]];
LCDMEM[7]= digit[xx[1]];
LCDMEM[8]= digit[xx[1]];
}
void mu2(void)//上
{
if (y2<a[x])
y2++;
else if (y2=a[x])
y2=1;
}
void mu3(void)//下
{
if (y2>1)
y2--;
}
void mu4(void)//enter
{
if (y1<c[x])
y1++;
}
//================================================9600
// Function Transmits Character from RXTXData Buffer
void TX_Byte (void)
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL0 & CCIE ); // Wait for TX completion
}
// Function Readies UART to Receive Character into RXTXData Buffer
void RX_Ready (void)
{
BitCnt = 0x8; // Load Bit counter
CCTL0 = SCS + CCIS0 + OUTMOD0 + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture
}
// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
CCR0 += Bitime; // Add Offset to CCR0
// RX
if (CCTL0 & CCIS0) // RX on CCI0B?
{
if( CCTL0 & CAP ) // Capture mode = start bit edge
{
CCTL0 &= ~ CAP; // Capture to compare mode
CCR0 += Bitime_5;
}
else
{
RXTXData = RXTXData >> 1;
if (CCTL0 & SCCI) // Get bit waiting in receive latch
RXTXData |= 0x80;
BitCnt --; // All bits RXed?
if ( BitCnt == 0)
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
CCTL0 &= ~ CCIE; // All bits RXed, disable interrupt
_BIC_SR_IRQ(CPUOFF); // Clear LPM0 bits from 0(SR)
}
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
// TX
else
{
if ( BitCnt == 0)
CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt
else
{
CCTL0 |= OUTMOD2; // TX Space
if (RXTXData & 0x01)
CCTL0 &= ~ OUTMOD2; // TX Mark
RXTXData = RXTXData >> 1;
BitCnt --;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -