今天碰到一個奇怪的問題,我用74HC595級聯,有兩塊板子,其中一塊物料是TM74HC595,另一塊物料是74HC595D。
在調試下面程序時,出現了問題,兩塊板子出現01這個數據竟然不是同一個位置,有知道原因的嗎?
- #include "stm32f10x.h"
- // 定義74HC595芯片引腳連接
- #define SER_PIN GPIO_Pin_0
- #define SRCLK_PIN GPIO_Pin_1
- #define RCLK_PIN GPIO_Pin_2
- #define GPIO_PORT GPIOA
- // 字符編碼數據,使用負邏輯(低電平為亮)
- const uint8_t font[][8] = {
- {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
- {0xff 0xff, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff},
- // 添加更多數據...
- };
- // 函數聲明
- void delay(uint32_t time);
- void sendByte(uint8_t data);
- void sendCommand(uint8_t cmd);
- void sendData(uint8_t data);
- void displayMatrix(const uint8_t matrix[8]);
- int main(void) {
- // 初始化GPIO和時鐘配置
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = SER_PIN | SRCLK_PIN | RCLK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIO_PORT, &GPIO_InitStructure);
- while (1)
- {
- // 顯示字符A
- displayMatrix(font[1]);
- delay(1000); // 延時1秒
- }
- }
- // 延時函數
- void delay(uint32_t time) {
- while (time--);
- }
- // 發送一個字節到74HC595芯片
- void sendByte(uint8_t data) {
- uint8_t i;
- for (i = 0; i < 8; i++) {
- GPIO_ResetBits(GPIO_PORT, SRCLK_PIN); // 時鐘信號置低
- if ((data & 0x80) == 0x80)
- GPIO_SetBits(GPIO_PORT, SER_PIN); // 輸出數據為1
- else
- GPIO_ResetBits(GPIO_PORT, SER_PIN); // 輸出數據為0
- data <<= 1;
- GPIO_SetBits(GPIO_PORT, SRCLK_PIN); // 時鐘信號置高,數據移位
- }
- }
- // 發送命令到74HC595芯片(鎖存數據)
- void sendCommand(uint8_t cmd) {
- GPIO_ResetBits(GPIO_PORT, RCLK_PIN); // 時鐘信號置低
- sendByte(cmd); // 發送數據
- GPIO_SetBits(GPIO_PORT, RCLK_PIN); // 時鐘信號置高,鎖存數據
- }
- // 發送數據到74HC595芯片(顯示數據)
- void sendData(uint8_t data) {
- GPIO_SetBits(GPIO_PORT, RCLK_PIN); // 時鐘信號置高
- sendByte(data); // 發送數據
- GPIO_ResetBits(GPIO_PORT, RCLK_PIN); // 時鐘信號置低
- }
- // 顯示一個8x8點陣圖案
- void displayMatrix(const uint8_t matrix[8]) {
- uint8_t row;
- for (row = 0; row < 8; row++) {
- sendData(matrix[row]);
- }
- }
復制代碼
|