Thông báo

Collapse
No announcement yet.

I2C và DS1307 với arm cortex M4.. Help me..

Collapse
X
 
  • Lọc
  • Giờ
  • Show
Clear All
new posts

  • I2C và DS1307 với arm cortex M4.. Help me..

    Cái đề tài của e thế này: Giao tiếp với em DS1307 lấy thời gian thực rồi hiển thị ra LCD 16x2, có 1 nút nhấn để chuyển đổi hiển thị ngày-tháng-năm sang hiển thị giờ-phút-giây, em làm xong, build 0 error mà ko hiểu sao ko chạy các bác,mong các ae tìm hiểu giải thích và góp ý giùm e, e cảm ơn nhiều.
    ****Đây là main.c****

    #include <stdint.h>
    #include "stm32f4xx.h"
    #include "misc.h"
    #include "stm32f4xx_conf.h"
    #include "LCD.h"
    #include "I2C.h"

    void DISPLAY_CALENDAR();
    void DISPLAY_TIME();

    void RRC_Config(void);
    void DS1307_Init(void);
    void init_GPIO(void);
    void Delay(__IO uint32_t num);

    int main(void)
    {
    RRC_Config();
    lcd_init();
    DS1307_Init();
    init_GPIO();

    DS1307_Set_Time(06,30,00);
    DS1307_Set_Calendar(01,01,12);
    DS1307_Set_Day(2);

    while(1)
    {
    DISPLAY_TIME();
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==1) // cho nút nhan len 1
    {
    DISPLAY_CALENDAR();
    }
    Delay(3000000L); // delay de chong rung cho nut nhan
    }
    }

    void RRC_Config(void)
    {
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
    }

    void Delay(__IO uint32_t num)
    {
    __IO uint32_t index = 0;

    /* default system clock is 72MHz */
    for(index = (720000 * num); index != 0; index--)
    {
    }
    }

    ****Đây là I2C.c****

    #include "I2C.h"

    #define Address_DS1307 0xD0

    void DS1307_Init(void)
    {
    I2C_InitTypeDef I2C_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

    GPIO_PinAFConfig(GPIOB,GPIO_PinSource6, GPIO_AF_I2C1);
    GPIO_PinAFConfig(GPIOB,GPIO_PinSource9, GPIO_AF_I2C1);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    I2C_DeInit(I2C1);
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_ClockSpeed = 100000;
    I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_OwnAddress1 = 0x33;

    I2C_Cmd(I2C1, ENABLE);
    I2C_Init(I2C1, &I2C_InitStructure);
    }

    uint8_t DS1307_READ(uint8_t address)
    {
    uint8_t data;

    I2C_AcknowledgeConfig(I2C1, ENABLE);
    I2C_GenerateSTART(I2C1, ENABLE);

    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) // EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); // Send slave address for write
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) // EV6: ADDR=1, cleared by reading SR1 register follwed by reading SR2
    {
    }
    I2C_SendData(I2C1, address); /* Send the specified register data pointer */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) // EV8: TxE=1, shift register not empty, data register empty, cleared by writing DR register
    {
    }

    I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
    {
    }
    I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
    I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV7: RxNE=1, cleared by reading DR register
    {
    }
    data = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
    return (data);
    }

    void DS1307_WRITE(uint8_t address, uint8_t data)
    {
    I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
    {
    }
    I2C_SendData(I2C1, address); /* Send the specified register data pointer */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
    {
    }
    I2C_SendData(I2C1, data); /* Send I2C1 data */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
    {
    }
    I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
    }

    void DS1307_Set_Time(uint8_t hour, uint8_t minute, uint8_t second)
    {
    uint8_t x1,x2 ;

    I2C_AcknowledgeConfig(I2C1, ENABLE);
    I2C_GenerateSTART(I2C1, ENABLE);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

    I2C_SendData(I2C1, 0x00); // send address write
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = second/10 ; // set second
    x2 = second%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = minute/10 ; // set minute
    x2 = minute%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = hour/10 ; // set hour
    x2 = hour%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

    I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
    }

    void DS1307_Set_Calendar(uint8_t date, uint8_t month, uint8_t year)
    {
    uint8_t x1,x2 ;

    I2C_AcknowledgeConfig(I2C1, ENABLE);
    I2C_GenerateSTART(I2C1, ENABLE);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

    I2C_SendData(I2C1, 0x04); // send address write
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = date/10 ; // set date
    x2 = date%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = month/10 ; // set month
    x2 = month%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = year/10 ; // set year
    x2 = year%10 ;
    x1 = (x1 << 4) | x2;

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

    I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
    }

    void DS1307_Set_Day(uint8_t day)
    {
    uint8_t x1 ;

    I2C_AcknowledgeConfig(I2C1, ENABLE);
    I2C_GenerateSTART(I2C1, ENABLE);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

    I2C_SendData(I2C1, 0x03); // send address write
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
    x1 = day; // set day

    I2C_SendData(I2C1, x1 );
    while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

    I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
    }

    void Get_time(uint8_t *hour, uint8_t *minute, uint8_t *second)
    {
    uint8_t x1,x2,x3 ;
    I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
    I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
    {
    }

    I2C_SendData(I2C1, 0x00); /* Send the specified register data pointer */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
    {
    }

    I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver);
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
    {
    }
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x2 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

    I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
    I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x3 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

    *second = (x1 >> 4)*10 + (x1 & 0x0F) ;
    *minute = (x2 >> 4)*10 + (x2 & 0x0F) ;
    *hour = (x3 >> 4)*10 + (x3 & 0x0F) ;
    }

    void Get_calendar(uint8_t *date, uint8_t *month, uint8_t *year)
    {
    uint8_t x1,x2,x3 ;
    I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
    I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
    {
    }

    I2C_SendData(I2C1, 0x04); /* Send the specified register data pointer */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
    {
    }

    I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver); /* Send STLM75 slave address for read */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
    {
    }
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x2 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

    I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
    I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x3 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

    *date = (x1 >> 4)*10 + (x1 & 0x0F) ;
    *month = (x2 >> 4)*10 + (x2 & 0x0F) ;
    *year = (x3 >> 4)*10 + (x3 & 0x0F) ;
    }

    void Get_day(uint8_t *day)
    {
    uint8_t x1 ;
    I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
    I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
    {
    }

    I2C_SendData(I2C1, 0x03); /* Send the specified register data pointer */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
    {
    }

    I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
    {
    }
    I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver); /* Send STLM75 slave address for read */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
    {
    }
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
    I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
    while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
    {
    }

    x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

    *day = x1;
    }

    void init_GPIO(void)
    {
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; // we want to configure PA0
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; // we want it to be an input
    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; // this enables the pulldown resistor --> we want to detect a high level
    GPIO_Init(GPIOA, &GPIO_InitStruct); // this passes the configuration to the Init function which takes care of the low level stuff
    }

    ****Và cuối cùng là LCD.c****

    #include "stm32f4xx.h"

    #define LCD_RS_GPIO_PORT GPIOC
    #define LCD_RS_GPIO_CLK RCC_AHB1Periph_GPIOC
    #define LCD_RS_GPIO_PIN GPIO_Pin_12

    #define LCD_RW_GPIO_PORT GPIOC
    #define LCD_RW_GPIO_CLK RCC_HB1Periph_GPIOC
    #define LCD_RW_GPIO_PIN GPIO_Pin_11

    #define LCD_EN_GPIO_PORT GPIOC
    #define LCD_EN_GPIO_CLK RCC_AHB1Periph_GPIOC
    #define LCD_EN_GPIO_PIN GPIO_Pin_10

    #define LCD_D4 GPIO_Pin_0
    #define LCD_D5 GPIO_Pin_1
    #define LCD_D6 GPIO_Pin_2
    #define LCD_D7 GPIO_Pin_3
    #define LCD_DATA_GPIO_PINS (LCD_D4|LCD_D5|LCD_D6|LCD_D7)
    #define LCD_DATA_GPIO_PORT GPIOC
    #define LCD_DATA_GPIO_CLK RCC_AHB1Periph_GPIOC

    void lcd_init(void);
    void LCD_STROBE(void);
    void LCD_DATA(u8 cX );
    void lcd_write(u8 c);
    void lcd_clear(void);
    void lcd_putchar(u8 c);
    void lcd_putsf(u8 *c);
    void lcd_gotoxy(u8 row,u8 col);

    void LCD_Delay_US(__IO uint32_t num)
    {
    __IO uint32_t index = 0;
    for(index = (72 * num); index != 0; index--)
    {
    }
    }

    void LCD_STROBE(void)
    {
    GPIO_SetBits(LCD_DATA_GPIO_PORT, LCD_EN_GPIO_PIN);
    LCD_Delay_US(30);
    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ;
    LCD_Delay_US(30);
    }

    void LCD_DATA(u8 cX )
    {
    GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D4, (BitAction)(cX & 0x01) );
    GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D5, (BitAction)((cX & 0x02) >> 1) );
    GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D6, (BitAction)((cX & 0x04) >> 2) );
    GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D7, (BitAction)((cX & 0x08) >> 3) );
    }

    void lcd_write(u8 c)
    {
    LCD_DATA( c >> 4 );
    LCD_STROBE();
    LCD_DATA( c );
    LCD_STROBE();
    }

    void lcd_clear(void)
    {
    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
    lcd_write(0x01);
    LCD_Delay_US(2000);
    }

    void lcd_putchar(u8 c)
    {
    GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN);
    lcd_write( c );
    }

    void lcd_putsf(u8 *c)
    {
    GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN);
    while(*c)
    lcd_write(*c++);
    }

    void lcd_gotoxy(u8 row,u8 col)
    {
    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
    switch(row)
    {
    case 0 : lcd_write( 0x80 + col ) ; //dia chi bat dau + col (cua LCD)
    break ;
    case 1 : lcd_write( 0xC0 + col ) ;
    break ;
    case 2 : lcd_write( 0x94 + col ) ;
    break ;
    case 3 : lcd_write( 0xD4 + col ) ;
    break ;
    }
    }

    void lcd_init(void)
    {
    GPIO_InitTypeDef GPIO_InitStruct;

    RCC_APB2PeriphClockCmd(LCD_DATA_GPIO_CLK, ENABLE);

    GPIO_InitStruct.GPIO_Pin = LCD_RS_GPIO_PIN | LCD_RW_GPIO_PIN | LCD_EN_GPIO_PIN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7 ;
    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
    GPIO_Init(LCD_DATA_GPIO_PORT, &GPIO_InitStruct);

    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ;
    GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RW_GPIO_PIN) ;

    LCD_Delay_US(15000);
    LCD_DATA(0x03);
    LCD_STROBE();
    LCD_Delay_US(5000);
    LCD_STROBE();
    LCD_Delay_US(200);
    LCD_STROBE();
    LCD_Delay_US(200);
    LCD_DATA(2);
    LCD_STROBE();

    lcd_write(0x28); // Set interface length
    lcd_write(0x0C); // Display On, Cursor On, Cursor Blink
    lcd_clear(); // Clear screen
    lcd_write(0x6);
    }

    void DISPLAY_CALENDAR()
    {
    struct
    {
    uint8_t day;
    uint8_t date;
    uint8_t month;
    uint8_t year;
    }calendar;

    uint8_t so1,so2 ;

    while (1)
    {
    Get_calendar(&calendar.date, &calendar.month, &calendar.year);
    Get_day(&calendar.day);

    lcd_gotoxy(0,2);
    lcd_putsf("CALENDAR - ");

    lcd_gotoxy(0,13);
    so1 = (calendar.day) | 0x30;
    lcd_putchar(so1);

    lcd_gotoxy(1,2);

    so1 = (calendar.date/10) | 0x30;
    so2 = (calendar.date%10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);
    lcd_putchar('-');

    so1 = (calendar.month /10) | 0x30;
    so2 = (calendar.month %10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);
    lcd_putchar('-');

    so1 = (calendar.year /10) | 0x30;
    so2 = (calendar.year %10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);

    Delay(10);
    }
    }

    void DISPLAY_TIME()
    {
    struct
    {
    uint8_t day;
    uint8_t hour;
    uint8_t minute;
    uint8_t second ;
    }time;

    uint8_t so1,so2 ;

    while (1)
    {

    Get_time(&time.hour, &time.minute, &time.second);
    Get_day(&time.day);

    lcd_gotoxy(0,4);
    lcd_putsf("CLOCK - ");

    lcd_gotoxy(0,12);
    so1 = (time.day) | 0x30;
    lcd_putchar(so1);

    lcd_gotoxy(1,2);

    so1 = (time.hour/10) | 0x30;
    so2 = (time.hour%10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);
    lcd_putchar(':');

    so1 = (time.minute /10) | 0x30;
    so2 = (time.minute %10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);
    lcd_putchar(':');

    so1 = (time.second /10) | 0x30;
    so2 = (time.second %10) | 0x30;
    lcd_putchar(so1);
    lcd_putchar(so2);

    Delay(10);
    }
    }

    Em tìm lỗi mà không hiểu vì sao không chạy, phần cứng thì cắm rất cẩn thận. Hic hic, cac bac cuu em.

  • #2
    có chức năng show code trong khung bạn nên sử dụng để đỡ rối,nhìn code của bạn thì ko thể giúp gì được rồi vì ko biết bạn gặp vấn đề ở đâu,bạn có nói không chạy vậy thì ko chạy ở phần nào,mình chỉ cho bạn cách test nhé: bạn có 4 vấn đề phải làm sáng tỏ: đầu tiên bạn phải test với các io của chip xem các io đã đến hết với các linh kiện khác trong mạch hay chưa?đã nhận được tín hiệu từ key hay chưa?lcd đã hiển thị được 1 ký tự nào chưa?sau cùng là vấn đề giao tiếp i2c với ic ds1307,bạn cũng cần phải test xem i2c đã làm việc hay chưa rồi hãy tiếp tục giao tiếp,đối với ds1307 thì bạn cứ làm đúng như datasheet của nó là đã chạy rồi,tham khảo thêm ở đây Đồng hồ thời gian thực DS1307 còn i2c thì bạn vẫn có thể dùng io để test chứ không nhất thiết phải dùng thư viện chuẩn i2c(vì khi dùng io thì mình dễ debug và làm việc theo ý muốn mình hơn)
    các vấn đề về sdcard, usb, tcp/ip, upgrate firmware,...
    trên các dòng chip: stm32, lpc of nxp
    please cell phone: 01649895559

    Comment


    • #3
      Nguyên văn bởi dangkhoa79 Xem bài viết
      Cái đề tài của e thế này: Giao tiếp với em DS1307 lấy thời gian thực rồi hiển thị ra LCD 16x2, có 1 nút nhấn để chuyển đổi hiển thị ngày-tháng-năm sang hiển thị giờ-phút-giây, em làm xong, build 0 error mà ko hiểu sao ko chạy các bác,mong các ae tìm hiểu giải thích và góp ý giùm e, e cảm ơn nhiều.
      ****Đây là main.c****

      #include <stdint.h>
      #include "stm32f4xx.h"
      #include "misc.h"
      #include "stm32f4xx_conf.h"
      #include "LCD.h"
      #include "I2C.h"

      void DISPLAY_CALENDAR();
      void DISPLAY_TIME();

      void RRC_Config(void);
      void DS1307_Init(void);
      void init_GPIO(void);
      void Delay(__IO uint32_t num);

      int main(void)
      {
      RRC_Config();
      lcd_init();
      DS1307_Init();
      init_GPIO();

      DS1307_Set_Time(06,30,00);
      DS1307_Set_Calendar(01,01,12);
      DS1307_Set_Day(2);

      while(1)
      {
      DISPLAY_TIME();
      if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==1) // cho nút nhan len 1
      {
      DISPLAY_CALENDAR();
      }
      Delay(3000000L); // delay de chong rung cho nut nhan
      }
      }

      void RRC_Config(void)
      {
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
      }

      void Delay(__IO uint32_t num)
      {
      __IO uint32_t index = 0;

      /* default system clock is 72MHz */
      for(index = (720000 * num); index != 0; index--)
      {
      }
      }

      ****Đây là I2C.c****

      #include "I2C.h"

      #define Address_DS1307 0xD0

      void DS1307_Init(void)
      {
      I2C_InitTypeDef I2C_InitStructure;
      GPIO_InitTypeDef GPIO_InitStructure;

      RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

      GPIO_PinAFConfig(GPIOB,GPIO_PinSource6, GPIO_AF_I2C1);
      GPIO_PinAFConfig(GPIOB,GPIO_PinSource9, GPIO_AF_I2C1);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
      GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
      GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
      GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

      GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
      GPIO_Init(GPIOB, &GPIO_InitStructure);

      I2C_DeInit(I2C1);
      I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
      I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
      I2C_InitStructure.I2C_ClockSpeed = 100000;
      I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
      I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
      I2C_InitStructure.I2C_OwnAddress1 = 0x33;

      I2C_Cmd(I2C1, ENABLE);
      I2C_Init(I2C1, &I2C_InitStructure);
      }

      uint8_t DS1307_READ(uint8_t address)
      {
      uint8_t data;

      I2C_AcknowledgeConfig(I2C1, ENABLE);
      I2C_GenerateSTART(I2C1, ENABLE);

      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) // EV5: SB=1, cleared by reading SR1 register followed by writing DR register with Address
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); // Send slave address for write
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) // EV6: ADDR=1, cleared by reading SR1 register follwed by reading SR2
      {
      }
      I2C_SendData(I2C1, address); /* Send the specified register data pointer */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) // EV8: TxE=1, shift register not empty, data register empty, cleared by writing DR register
      {
      }

      I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
      {
      }
      I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
      I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) // EV7: RxNE=1, cleared by reading DR register
      {
      }
      data = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
      return (data);
      }

      void DS1307_WRITE(uint8_t address, uint8_t data)
      {
      I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
      {
      }
      I2C_SendData(I2C1, address); /* Send the specified register data pointer */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
      {
      }
      I2C_SendData(I2C1, data); /* Send I2C1 data */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
      {
      }
      I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
      }

      void DS1307_Set_Time(uint8_t hour, uint8_t minute, uint8_t second)
      {
      uint8_t x1,x2 ;

      I2C_AcknowledgeConfig(I2C1, ENABLE);
      I2C_GenerateSTART(I2C1, ENABLE);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

      I2C_SendData(I2C1, 0x00); // send address write
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = second/10 ; // set second
      x2 = second%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = minute/10 ; // set minute
      x2 = minute%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = hour/10 ; // set hour
      x2 = hour%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

      I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
      }

      void DS1307_Set_Calendar(uint8_t date, uint8_t month, uint8_t year)
      {
      uint8_t x1,x2 ;

      I2C_AcknowledgeConfig(I2C1, ENABLE);
      I2C_GenerateSTART(I2C1, ENABLE);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

      I2C_SendData(I2C1, 0x04); // send address write
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = date/10 ; // set date
      x2 = date%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = month/10 ; // set month
      x2 = month%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = year/10 ; // set year
      x2 = year%10 ;
      x1 = (x1 << 4) | x2;

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

      I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
      }

      void DS1307_Set_Day(uint8_t day)
      {
      uint8_t x1 ;

      I2C_AcknowledgeConfig(I2C1, ENABLE);
      I2C_GenerateSTART(I2C1, ENABLE);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT) ) ; //check EV5

      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter);
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) ) ; //check EV6

      I2C_SendData(I2C1, 0x03); // send address write
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8
      x1 = day; // set day

      I2C_SendData(I2C1, x1 );
      while( !I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED) ) ; //check EV8

      I2C_GenerateSTOP(I2C1, ENABLE); //Send I2C1 STOP Condition
      }

      void Get_time(uint8_t *hour, uint8_t *minute, uint8_t *second)
      {
      uint8_t x1,x2,x3 ;
      I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
      I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
      {
      }

      I2C_SendData(I2C1, 0x00); /* Send the specified register data pointer */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
      {
      }

      I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver);
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
      {
      }
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x2 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

      I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
      I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x3 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

      *second = (x1 >> 4)*10 + (x1 & 0x0F) ;
      *minute = (x2 >> 4)*10 + (x2 & 0x0F) ;
      *hour = (x3 >> 4)*10 + (x3 & 0x0F) ;
      }

      void Get_calendar(uint8_t *date, uint8_t *month, uint8_t *year)
      {
      uint8_t x1,x2,x3 ;
      I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
      I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
      {
      }

      I2C_SendData(I2C1, 0x04); /* Send the specified register data pointer */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
      {
      }

      I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver); /* Send STLM75 slave address for read */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
      {
      }
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x2 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

      I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
      I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x3 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

      *date = (x1 >> 4)*10 + (x1 & 0x0F) ;
      *month = (x2 >> 4)*10 + (x2 & 0x0F) ;
      *year = (x3 >> 4)*10 + (x3 & 0x0F) ;
      }

      void Get_day(uint8_t *day)
      {
      uint8_t x1 ;
      I2C_AcknowledgeConfig(I2C1, ENABLE); /* Enable I2C1 acknowledgement if it is already disabled by other function */
      I2C_GenerateSTART(I2C1, ENABLE); /* Send I2C1 START condition */

      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Transmitter); /* Send STLM75 slave address for write */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) /* EV6 */
      {
      }

      I2C_SendData(I2C1, 0x03); /* Send the specified register data pointer */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) /* EV8 */
      {
      }

      I2C_GenerateSTART(I2C1, ENABLE); /* Send Re-STRAT condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT)) /* EV5 */
      {
      }
      I2C_Send7bitAddress(I2C1, Address_DS1307, I2C_Direction_Receiver); /* Send STLM75 slave address for read */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) /* EV6 */
      {
      }
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      I2C_AcknowledgeConfig(I2C1, DISABLE); /* Disable I2C1 acknowledgement */
      I2C_GenerateSTOP(I2C1, ENABLE); /* Send I2C1 STOP Condition */
      while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
      {
      }

      x1 = I2C_ReceiveData(I2C1); /* Store I2C1 received data */

      *day = x1;
      }

      void init_GPIO(void)
      {
      GPIO_InitTypeDef GPIO_InitStruct;

      RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

      GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; // we want to configure PA0
      GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; // we want it to be an input
      GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this sets the pin type to push / pull (as opposed to open drain)
      GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_DOWN; // this enables the pulldown resistor --> we want to detect a high level
      GPIO_Init(GPIOA, &GPIO_InitStruct); // this passes the configuration to the Init function which takes care of the low level stuff
      }

      ****Và cuối cùng là LCD.c****

      #include "stm32f4xx.h"

      #define LCD_RS_GPIO_PORT GPIOC
      #define LCD_RS_GPIO_CLK RCC_AHB1Periph_GPIOC
      #define LCD_RS_GPIO_PIN GPIO_Pin_12

      #define LCD_RW_GPIO_PORT GPIOC
      #define LCD_RW_GPIO_CLK RCC_HB1Periph_GPIOC
      #define LCD_RW_GPIO_PIN GPIO_Pin_11

      #define LCD_EN_GPIO_PORT GPIOC
      #define LCD_EN_GPIO_CLK RCC_AHB1Periph_GPIOC
      #define LCD_EN_GPIO_PIN GPIO_Pin_10

      #define LCD_D4 GPIO_Pin_0
      #define LCD_D5 GPIO_Pin_1
      #define LCD_D6 GPIO_Pin_2
      #define LCD_D7 GPIO_Pin_3
      #define LCD_DATA_GPIO_PINS (LCD_D4|LCD_D5|LCD_D6|LCD_D7)
      #define LCD_DATA_GPIO_PORT GPIOC
      #define LCD_DATA_GPIO_CLK RCC_AHB1Periph_GPIOC

      void lcd_init(void);
      void LCD_STROBE(void);
      void LCD_DATA(u8 cX );
      void lcd_write(u8 c);
      void lcd_clear(void);
      void lcd_putchar(u8 c);
      void lcd_putsf(u8 *c);
      void lcd_gotoxy(u8 row,u8 col);

      void LCD_Delay_US(__IO uint32_t num)
      {
      __IO uint32_t index = 0;
      for(index = (72 * num); index != 0; index--)
      {
      }
      }

      void LCD_STROBE(void)
      {
      GPIO_SetBits(LCD_DATA_GPIO_PORT, LCD_EN_GPIO_PIN);
      LCD_Delay_US(30);
      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ;
      LCD_Delay_US(30);
      }

      void LCD_DATA(u8 cX )
      {
      GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D4, (BitAction)(cX & 0x01) );
      GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D5, (BitAction)((cX & 0x02) >> 1) );
      GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D6, (BitAction)((cX & 0x04) >> 2) );
      GPIO_WriteBit(LCD_DATA_GPIO_PORT, LCD_D7, (BitAction)((cX & 0x08) >> 3) );
      }

      void lcd_write(u8 c)
      {
      LCD_DATA( c >> 4 );
      LCD_STROBE();
      LCD_DATA( c );
      LCD_STROBE();
      }

      void lcd_clear(void)
      {
      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
      lcd_write(0x01);
      LCD_Delay_US(2000);
      }

      void lcd_putchar(u8 c)
      {
      GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN);
      lcd_write( c );
      }

      void lcd_putsf(u8 *c)
      {
      GPIO_SetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN);
      while(*c)
      lcd_write(*c++);
      }

      void lcd_gotoxy(u8 row,u8 col)
      {
      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
      switch(row)
      {
      case 0 : lcd_write( 0x80 + col ) ; //dia chi bat dau + col (cua LCD)
      break ;
      case 1 : lcd_write( 0xC0 + col ) ;
      break ;
      case 2 : lcd_write( 0x94 + col ) ;
      break ;
      case 3 : lcd_write( 0xD4 + col ) ;
      break ;
      }
      }

      void lcd_init(void)
      {
      GPIO_InitTypeDef GPIO_InitStruct;

      RCC_APB2PeriphClockCmd(LCD_DATA_GPIO_CLK, ENABLE);

      GPIO_InitStruct.GPIO_Pin = LCD_RS_GPIO_PIN | LCD_RW_GPIO_PIN | LCD_EN_GPIO_PIN | LCD_D4 | LCD_D5 | LCD_D6 | LCD_D7 ;
      GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
      GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
      GPIO_Init(LCD_DATA_GPIO_PORT, &GPIO_InitStruct);

      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RS_GPIO_PIN) ;
      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_EN_GPIO_PIN) ;
      GPIO_ResetBits(LCD_DATA_GPIO_PORT,LCD_RW_GPIO_PIN) ;

      LCD_Delay_US(15000);
      LCD_DATA(0x03);
      LCD_STROBE();
      LCD_Delay_US(5000);
      LCD_STROBE();
      LCD_Delay_US(200);
      LCD_STROBE();
      LCD_Delay_US(200);
      LCD_DATA(2);
      LCD_STROBE();

      lcd_write(0x28); // Set interface length
      lcd_write(0x0C); // Display On, Cursor On, Cursor Blink
      lcd_clear(); // Clear screen
      lcd_write(0x6);
      }

      void DISPLAY_CALENDAR()
      {
      struct
      {
      uint8_t day;
      uint8_t date;
      uint8_t month;
      uint8_t year;
      }calendar;

      uint8_t so1,so2 ;

      while (1)
      {
      Get_calendar(&calendar.date, &calendar.month, &calendar.year);
      Get_day(&calendar.day);

      lcd_gotoxy(0,2);
      lcd_putsf("CALENDAR - ");

      lcd_gotoxy(0,13);
      so1 = (calendar.day) | 0x30;
      lcd_putchar(so1);

      lcd_gotoxy(1,2);

      so1 = (calendar.date/10) | 0x30;
      so2 = (calendar.date%10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);
      lcd_putchar('-');

      so1 = (calendar.month /10) | 0x30;
      so2 = (calendar.month %10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);
      lcd_putchar('-');

      so1 = (calendar.year /10) | 0x30;
      so2 = (calendar.year %10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);

      Delay(10);
      }
      }

      void DISPLAY_TIME()
      {
      struct
      {
      uint8_t day;
      uint8_t hour;
      uint8_t minute;
      uint8_t second ;
      }time;

      uint8_t so1,so2 ;

      while (1)
      {

      Get_time(&time.hour, &time.minute, &time.second);
      Get_day(&time.day);

      lcd_gotoxy(0,4);
      lcd_putsf("CLOCK - ");

      lcd_gotoxy(0,12);
      so1 = (time.day) | 0x30;
      lcd_putchar(so1);

      lcd_gotoxy(1,2);

      so1 = (time.hour/10) | 0x30;
      so2 = (time.hour%10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);
      lcd_putchar(':');

      so1 = (time.minute /10) | 0x30;
      so2 = (time.minute %10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);
      lcd_putchar(':');

      so1 = (time.second /10) | 0x30;
      so2 = (time.second %10) | 0x30;
      lcd_putchar(so1);
      lcd_putchar(so2);

      Delay(10);
      }
      }

      Em tìm lỗi mà không hiểu vì sao không chạy, phần cứng thì cắm rất cẩn thận. Hic hic, cac bac cuu em.
      Trên DS1307 chân SCL & SDA bạn fai kéo thêm 2 con trở 4,7k lên VCC. (code này sao giống code mình viết thế)
      (^_^) hoangnv.3i@gmail.com

      Comment


      • #4
        Nguyên văn bởi hoang_3i Xem bài viết
        Trên DS1307 chân SCL & SDA bạn fai kéo thêm 2 con trở 4,7k lên VCC. (code này sao giống code mình viết thế)
        Uh, minh lay code tren 4rum nay ve de tham khao roi viet theo de lam de tai minh ay' ma`. Tks ban Hoang_3i nhieu` nha'. Code nay thi` mach ds1307 minh co 2 con tro 4.7k keo' len roi`, ma` van chua len. Ko hieu ket noi phan cung minh` day du~ chua nua~. mach 1307 thi noi voi 2 chan SCL, SDA voi arm, 2 chan nguon va gnd nua. LCD thi noi nguon, GND, 3 chan RS,RW,EN voi 4 chan du lieu DB4,5,6,7. Van dang tim hieu...

        Comment


        • #5
          Trên lcd anh phải mắc thêm 1 biến trở để chình độ tương phản nữa nhé.
          Mạch vẫn chạy đó nhưng do độ tương phản chưa chỉnh nên nó không hiện lên màn hình đó anh.
          Mắc chân số 3 V0 của lcd vào biến trở 10k.
          |

          Comment


          • #6
            hic,1 chủ thớt show code dài thòn và 1 mem quote code cũng dài thòn,tìm được lỗi cũng hay
            các vấn đề về sdcard, usb, tcp/ip, upgrate firmware,...
            trên các dòng chip: stm32, lpc of nxp
            please cell phone: 01649895559

            Comment


            • #7
              Nguyên văn bởi thanh_nhan93 Xem bài viết
              Trên lcd anh phải mắc thêm 1 biến trở để chình độ tương phản nữa nhé.
              Mạch vẫn chạy đó nhưng do độ tương phản chưa chỉnh nên nó không hiện lên màn hình đó anh.
              Mắc chân số 3 V0 của lcd vào biến trở 10k.
              Có biến trở đó luôn rồi bạn ơi. hi

              Comment


              • #8
                Nguyên văn bởi dangkhoa79 Xem bài viết
                Có biến trở đó luôn rồi bạn ơi. hi
                Mình cũng đang làm cái đồ án mạch hiển thị thời gian dùng 89c52 +ds1302 xuất ra lcd mà cũng chưa được nè.
                Đang kiểm tra lại code mà chẳng biết lỗi ở đâu cả,hic.
                |

                Comment


                • #9
                  STM nó có sẵn RTC mà ko dùng lại lôi ds1307 vào làm gì thế @@.
                  Web:
                  ->Nhận thiết kế, hoàn thiện dự án, sản phẩm điện tử<-
                  -->Giải pháp GSM/GPRS/GPS - Công nghệ RFID<--

                  Comment


                  • #10
                    Nguyên văn bởi demon52 Xem bài viết
                    STM nó có sẵn RTC mà ko dùng lại lôi ds1307 vào làm gì thế @@.
                    Yêu cầu đồ án của mình nó thế bạn

                    Comment


                    • #11
                      Nguyên văn bởi dangkhoa79 Xem bài viết
                      Uh, minh lay code tren 4rum nay ve de tham khao roi viet theo de lam de tai minh ay' ma`. Tks ban Hoang_3i nhieu` nha'. Code nay thi` mach ds1307 minh co 2 con tro 4.7k keo' len roi`, ma` van chua len. Ko hieu ket noi phan cung minh` day du~ chua nua~. mach 1307 thi noi voi 2 chan SCL, SDA voi arm, 2 chan nguon va gnd nua. LCD thi noi nguon, GND, 3 chan RS,RW,EN voi 4 chan du lieu DB4,5,6,7. Van dang tim hieu...
                      vẫn chưa đủ, DS1307 phải có thêm PIN 3V vào chân 3 nữa mới chạy
                      (^_^) hoangnv.3i@gmail.com

                      Comment


                      • #12
                        Nguyên văn bởi hoang_3i Xem bài viết
                        vẫn chưa đủ, DS1307 phải có thêm PIN 3V vào chân 3 nữa mới chạy
                        Có luôn rồi, mình nghĩ là do code thôi, mà cái project của b đem ra phần cứng chạy ok ko?

                        Comment


                        • #13
                          Nguyên văn bởi dangkhoa79 Xem bài viết
                          Có luôn rồi, mình nghĩ là do code thôi, mà cái project của b đem ra phần cứng chạy ok ko?
                          cái này đã test thực tế chạy ok rồi ,chỉ khác là mình dùng con stm32f103 thôi chứ M4 thì chưa biết
                          (^_^) hoangnv.3i@gmail.com

                          Comment


                          • #14
                            HELP I2C CMPS10 và Stm32f4???

                            mình cũng đang làm cmps10 và stm32f4 mà làm mãi chưa xong

                            Comment

                            Về tác giả

                            Collapse

                            dangkhoa79 Tìm hiểu thêm về dangkhoa79

                            Bài viết mới nhất

                            Collapse

                            Đang tải...
                            X