gpt4 book ai didi

sensors - 如何使用 TSL2301 并在 usart 中进行时钟延迟

转载 作者:行者123 更新时间:2023-12-02 04:53:58 24 4
gpt4 key购买 nike

我正在研究一个名为 TSL2301 的线扫描 ccd 传感器。我想通过 stm32f103 的 USART 读取像素,但我总是只能收到 0xFF,有人使用这个传感器来帮助我吗?我使用了 STM32f10x 的 Usart 同步模式,我通过 Usart 向传感器发送了一些命令,并希望在每个命令后延迟 8 个时钟。我该怎么做?

int count=0;
int i=0;
uint8_t data[102]={0};
USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
USART_ClockInit(USARTy, &USART_ClockInitStructure);

USART_InitStructure.USART_BaudRate = 2200000;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USARTy, &USART_InitStructure);

/* Configure the USARTy */
USART_Init(USARTy, &USART_InitStructure);

/* Enable the USARTy */
USART_Cmd(USARTy, ENABLE);

while(1)
{
while(count < 3)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count=0;
USART_SendData(USARTy,0x1b); //RESET command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

USART_SendData(USARTy,0x1b);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

USART_SendData(USARTy,0x1b);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

USART_SendData(USARTy,0x08); //StartInt Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

while(count < 20 )//DelayIntegrationTime() ;
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;
USART_SendData(USARTy,0x10); //SampleInt Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

USART_SendData(USARTy,0x02); //ReadPixel Command
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);

while(count < 2)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;

USART_Cmd(USARTy, DISABLE);
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_Init(USARTy, &USART_InitStructure);
USART_Cmd(USARTy, ENABLE);

/*read pixels*/
for (i = 0; i < 102; i++)
{
while(USART_GetFlagStatus(USARTy, USART_FLAG_RXNE) == RESET);
data[i] = USART_ReceiveData(USARTy);
while(count < 1)
{
USART_SendData(USARTy,0xFF);
while(USART_GetFlagStatus(USARTy, USART_FLAG_TC) == RESET);
count++;
}
count = 0;
}

USART_Cmd(USARTy, DISABLE);
USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInit(USARTy, &USART_ClockInitStructure);
USART_Init(USARTy, &USART_InitStructure);
USART_Cmd(USARTy, ENABLE);

最佳答案

STM32F103引用手册和TAOS数据表中有不同的USART时序图。我认为它们不会在 USART 模式下一起工作。

作为一个Idea,你可以尝试用UART代替USART。并使用 MCO 或其他时钟源为设备计时。我认为它应该有效。

在这种情况下,您可以使用计时器进行所有延迟。

PS:但是你还需要解决一个问题,就是UART的输出与时钟源同步。我需要更仔细地阅读 stm32 手册来告诉你如何去做。我稍后再做

关于sensors - 如何使用 TSL2301 并在 usart 中进行时钟延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24851568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com