这样接线:
hx711
oled
串口线
/* 3.3V供电 2.2接SCK 2.3接DT */
/*
/|\| XIN|-
| | | 32kHz
--|RST XOUT|-
| |
| P1.2/UCA0TXD|------------>
| | 9600 - 8N1
| P1.1/UCA0RXD|<------------
*/
// ------------------------------------------------------------
// IO口模拟I2C通信
// SCL接P2^0
// SDA接P2^1
cppchar dis_str[15];
uchar count = 0;
uchar korkg = 0;
uchar bluetoo = 0;
uchar item = 0;
int zhouqi = 1000;
int zhouqi1 = 0;
int zhouqi_count = 0;
uchar count_time = 0;
uchar ifg_time0 = 0;
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; /* Stop WDT */
if ( CALBC1_8MHZ == 0xFF ) /* If calibration constant erased */
{
while ( 1 )
; /* do not load, trap CPU!! */
}
DCOCTL = 0; /* Select lowest DCOx and MODx settings */
BCSCTL1 = CALBC1_8MHZ; /* Set range */
DCOCTL = CALDCO_8MHZ; /* Set DCO step + modulation * / */
uart_init();
PutString( "bluetooth init" );
P1DIR |= BIT3;
P1OUT |= BIT3;
OLED_Init(); /* OLED初始化 */
/* OLED_ShowString(0, 0, "system"); */
hx711_port_init();
Get_Maopi(); /* 重新获取毛皮重量 */
delay_ms( 1000 );
Get_Maopi(); /* 重新获取毛皮重量 */
P1DIR &= ~(BIT4 + BIT5 + BIT6);
P1REN |= (BIT4 + BIT5 + BIT6);
P1OUT |= (BIT4 + BIT5 + BIT6);
if ( bluetoo == 1 )
OLED_ShowString( 0, 4, "ON " );
else
OLED_ShowString( 0, 4, "OFF" );
memset( dis_str, 0, 15 );
sprintf( dis_str, " %d ", zhouqi );
OLED_ShowString( 30, 4, dis_str );
CCTL0 = CCIE; /* CCR0 interrupt enabled */
CCR0 = 32 * 50;
TACTL = TASSEL_1 + MC_1; /* SMCLK, upmode */
_EINT();
while ( 1 )
{
/* 重量检测 */
if ( ++count_time == 4 )
{
count_time = 0;
Get_Weight();
if ( Flag_Error == 0 )
{
/* printf( "净重量 = %d g\r\n", Weight_Shiwu); / * 打印 * / */
if ( korkg == 0 )
{
count = 0;
dis_str[count++] = Weight_Shiwu % 1000000 / 100000 + '0';
dis_str[count++] = Weight_Shiwu % 100000 / 10000 + '0';
dis_str[count++] = Weight_Shiwu % 10000 / 1000 + '0';
dis_str[count++] = Weight_Shiwu % 1000 / 100 + '0';
/*
* dis_str[count++] = '.';
* dis_str[count++] = Weight_Shiwu % 100 / 10 + '0';
* dis_str[count++] = Weight_Shiwu % 10 + '0';
*/
dis_str[count++] = ' ';
dis_str[count++] = 'g';
dis_str[count++] = ' ';
dis_str[count++] = ' ';
dis_str[count++] = 0;
} else {
count = 0;
dis_str[count++] = Weight_Shiwu % 1000000 / 100000 + '0';
dis_str[count++] = '.';
dis_str[count++] = Weight_Shiwu % 100000 / 10000 + '0';
dis_str[count++] = Weight_Shiwu % 10000 / 1000 + '0';
dis_str[count++] = Weight_Shiwu % 1000 / 100 + '0';
/*
* dis_str[count++] = Weight_Shiwu % 100 / 10 + '0';
* dis_str[count++] = Weight_Shiwu % 10 + '0';
*/
dis_str[count++] = ' ';
dis_str[count++] = 'k';
dis_str[count++] = 'g';
dis_str[count++] = ' ';
dis_str[count++] = ' ';
dis_str[count++] = 0;
}
OLED_ShowString( 0, 2, dis_str );
} else {
OLED_ShowString( 0, 2, "OVERWEI" );
}
}
/* 打开蓝牙了就发送 否则不发送 */
if ( bluetoo == 1 )
{
zhouqi1 = zhouqi / 50;
zhouqi_count++;
if ( zhouqi_count == zhouqi1 ) /* 到了蓝牙发送周期 */
{
zhouqi_count = 0;
PutString( dis_str );
PutString( " \r\n" );
}
}
/* 超重检测 */
if ( Weight_Shiwu > 50000 )
{
P1OUT &= ~BIT3;
} else {
P1OUT |= BIT3;
}
/*按键1 k/kg */
if ( (P1IN & BIT4) == 0 )
{
delay_ms( 10 );
if ( (P1IN & BIT4) == 0 )
{
while ( (P1IN & BIT4) == 0 )
;
if ( item == 0 )
{
korkg = !korkg; /* 切换数值 下次显示就刷新显示 */
} else if ( item == 1 )
{
bluetoo = !bluetoo; /* 打开或关闭蓝牙 */
if ( bluetoo )
OLED_ShowString( 0, 4, "ON " );
else
OLED_ShowString( 0, 4, "OFF" );
} else if ( item == 2 )
{
zhouqi += 50;
memset( dis_str, 0, 15 );
sprintf( dis_str, " %d ", zhouqi );
OLED_ShowString( 30, 4, dis_str );
}
}
}
/*按键2 去皮 */
if ( (P1IN & BIT5) == 0 )
{
delay_ms( 10 );
if ( (P1IN & BIT5) == 0 )
{
while ( (P1IN & BIT5) == 0 )
;
if ( item == 0 )
{
Get_Maopi(); /* 重新获取毛皮重量 */
} else if ( item == 1 )
{
bluetoo = !bluetoo; /* 打开或关闭蓝牙 */
if ( bluetoo )
OLED_ShowString( 0, 4, "ON " );
else
OLED_ShowString( 0, 4, "OFF" );
} else if ( item == 2 )
{
zhouqi -= 50;
memset( dis_str, 0, 15 );
sprintf( dis_str, " %d ", zhouqi );
OLED_ShowString( 30, 4, dis_str );
}
}
}
/*按键3 */
if ( (P1IN & BIT6) == 0 )
{
delay_ms( 10 );
if ( (P1IN & BIT6) == 0 )
{
while ( (P1IN & BIT6) == 0 )
;
item = (item + 1) % 3; /* 0 1 2 */
}
}
while ( ifg_time0 )
; /* 等待50ms过去 */
ifg_time0 = 1;
}
}
/* Timer A0 interrupt service routine */
#pragma vector=TIMER0_A0_VECTOR
__interrupt void Timer_A( void )
{
ifg_time0 = 0; /* 50ms进入这里 */
}
csharphttps://docs.qq.com/sheet/DUEdqZ2lmbmR6UVdU?tab=BB08J2
本文作者:Dong
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC。本作品采用《知识共享署名-非商业性使用 4.0 国际许可协议》进行许可。您可以在非商业用途下自由转载和修改,但必须注明出处并提供原作者链接。 许可协议。转载请注明出处!