标题: 74HC595级联问题 [打印本页]

作者: zyftank    时间: 2023-11-7 22:42
标题: 74HC595级联问题
今天碰到一个奇怪的问题,我用74HC595级联,有两块板子,其中一块物料是TM74HC595,另一块物料是74HC595D。
在调试下面程序时,出现了问题,两块板子出现01这个数据竟然不是同一个位置,有知道原因的吗?
  1. #include "stm32f10x.h"

  2. // 定义74HC595芯片引脚连接
  3. #define SER_PIN    GPIO_Pin_0
  4. #define SRCLK_PIN  GPIO_Pin_1
  5. #define RCLK_PIN   GPIO_Pin_2
  6. #define GPIO_PORT  GPIOA

  7. // 字符编码数据,使用负逻辑(低电平为亮)
  8. const uint8_t font[][8] = {
  9.     {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  10.     {0xff 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff},
  11.     // 添加更多数据...
  12. };

  13. // 函数声明
  14. void delay(uint32_t time);
  15. void sendByte(uint8_t data);
  16. void sendCommand(uint8_t cmd);
  17. void sendData(uint8_t data);
  18. void displayMatrix(const uint8_t matrix[8]);

  19. int main(void) {
  20.     // 初始化GPIO和时钟配置
  21.     GPIO_InitTypeDef GPIO_InitStructure;
  22.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
  23.     GPIO_InitStructure.GPIO_Pin = SER_PIN | SRCLK_PIN | RCLK_PIN;
  24.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  25.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26.     GPIO_Init(GPIO_PORT, &GPIO_InitStructure);

  27.     while (1)
  28.     {
  29.         // 显示字符A
  30.         displayMatrix(font[1]);
  31.         delay(1000);  // 延时1秒
  32.     }
  33. }

  34. // 延时函数
  35. void delay(uint32_t time) {
  36.     while (time--);
  37. }

  38. // 发送一个字节到74HC595芯片
  39. void sendByte(uint8_t data) {
  40.     uint8_t i;
  41.     for (i = 0; i < 8; i++) {
  42.         GPIO_ResetBits(GPIO_PORT, SRCLK_PIN);  // 时钟信号置低
  43.         if ((data & 0x80) == 0x80)
  44.             GPIO_SetBits(GPIO_PORT, SER_PIN);  // 输出数据为1
  45.         else
  46.             GPIO_ResetBits(GPIO_PORT, SER_PIN);  // 输出数据为0
  47.         data <<= 1;
  48.         GPIO_SetBits(GPIO_PORT, SRCLK_PIN);  // 时钟信号置高,数据移位
  49.     }
  50. }

  51. // 发送命令到74HC595芯片(锁存数据)
  52. void sendCommand(uint8_t cmd) {
  53.     GPIO_ResetBits(GPIO_PORT, RCLK_PIN);  // 时钟信号置低
  54.     sendByte(cmd);  // 发送数据
  55.     GPIO_SetBits(GPIO_PORT, RCLK_PIN);  // 时钟信号置高,锁存数据
  56. }

  57. // 发送数据到74HC595芯片(显示数据)
  58. void sendData(uint8_t data) {
  59.     GPIO_SetBits(GPIO_PORT, RCLK_PIN);  // 时钟信号置高
  60.     sendByte(data);  // 发送数据
  61.     GPIO_ResetBits(GPIO_PORT, RCLK_PIN);  // 时钟信号置低
  62. }

  63. // 显示一个8x8点阵图案
  64. void displayMatrix(const uint8_t matrix[8]) {
  65.     uint8_t row;
  66.     for (row = 0; row < 8; row++) {
  67.         sendData(matrix[row]);
  68.     }
  69. }
复制代码




作者: gzdavy    时间: 2024-4-20 20:16
不是同一家的芯片,很容易出现级联问题。我用同一家的芯片,级联到最后一级的时候都会出错,因线路电容有影响。
作者: xiaobendan001    时间: 2024-4-21 07:14
gzdavy 发表于 2024-4-20 20:16
不是同一家的芯片,很容易出现级联问题。我用同一家的芯片,级联到最后一级的时候都会出错,因线路电容有影 ...

你说线路电容的问题,我用两片,各种手段,结果还是不行,不稳定,只有一个牌子的可以用,而且稳定
作者: zhuls    时间: 2024-4-21 13:42
void sendCommand(uint8_t cmd)
void sendData(uint8_t data)
这两个函数的时序要认真斟酌一下。
595的本质是D触发器,在CLK的上升沿把SDA的数据读入。闲时CLK要保持低电平。
你在void sendByte(uint8_t data)中一开始是低电平,最后时刻是高电平,
所以有可能在sendCommand和sendData的一开始就锁存了一个不可预知的电平进595了。




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1