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.
****Đâ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.
Comment