天和机器人音量调节说明文档。
音量调节的宏
#define VOLUME_CHANGE(x){\
X9C102_CS_GPIO_PORT->ODR &= ~X9C102_CS_PIN;\
if(x)\
X9C102_UD_GPIO_PORT->ODR &= ~X9C102_UD_PIN;\
else\
X9C102_UD_GPIO_PORT->ODR |= X9C102_UD_PIN;\
DELAY_US(100);\
for(int i=0;i<10;i++){\
X9C102_INC_GPIO_PORT->ODR |= X9C102_INC_PIN;\
DELAY_US(10);\
X9C102_INC_GPIO_PORT->ODR &= ~X9C102_INC_PIN;\
DELAY_US(10);\
}\
X9C102_CS_GPIO_PORT->ODR |= X9C102_CS_PIN;\
}
#define VOLUME_UP() VOLUME_CHANGE(1);
#define VOLUME_DOWN() VOLUME_CHANGE(0);
主函数就是一个死循环
while(1)//主程序循环,反复执行循环体里的语句
{
if(volume){
if(volume>0){
VOLUME_UP();
}else{
VOLUME_DOWN();
}
volume = 0;
}
}
这里全局变量Volume表示采集到的旋钮的动作,这个变量的更新是在timer的中断响应函数里面做的
#define CYCLE 10
static uint16_t pgCount = 0;
static uint8_t pgReady = 0;
int8_t volume = 0;
int8_t outB = 0;
/**
* @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
if(pgbPort->IDR & pgbPin){
outB = 1;
}else{
outB = 0;
}
if(pgPort->IDR & pgPin){
if(pgCount<CYCLE)
pgCount++;
}else{
if(pgCount>0)
pgCount--;
}
if(pgCount == 0){
pgReady = 1;
}
if(pgCount == CYCLE && pgReady){
if(pgbPort->IDR & pgbPin)
volume = -1;
else
volume = 1;
pgReady = 0;
}
}