0% found this document useful (0 votes)
14 views8 pages

Codene

Uploaded by

stm32f411vet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

Codene

Uploaded by

stm32f411vet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

*thời gian thực (i2c)

/* USER CODE BEGIN Includes */


#include "stdio.h"
/* USER CODE END Includes */

/* Private typedef
-----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define
------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro
-------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables
---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;

I2S_HandleTypeDef hi2s3;

SPI_HandleTypeDef hspi1;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes


-----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_I2C1_Init(void);
static void MX_I2S3_Init(void);
static void MX_SPI1_Init(void);
void MX_USB_HOST_Process(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code


---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t DevAddress = 0xD0; // Địa chỉ của thiết bị
uint8_t Sec_Reg = 0x00; // Địa chỉ của thanh ghi thứ hai
uint8_t Data_Wr[8] = {0x00,0x06,0x06,0x12,0x04,0x015,0x04,0x02}; //
Dữ liệu sẽ được ghi (ghi hai byte)
uint8_t Data_Rd[7]={}; // Biến lưu dữ liệu sẽ được đọc

#define SLAVE_ADDRESS_LCD 0x4E // change this according to ur setup


void lcd_send_cmd (char cmd)
{
char data_u, data_l;
data_u = (cmd&0xf0);
data_l = ((cmd<<4)&0xf0);
uint8_t data_t[4];
data_t[0] = data_u|0x0C; //en=1, rs=0 -> bxxxx1100
data_t[1] = data_u|0x08; //en=0, rs=0 -> bxxxx1000
data_t[2] = data_l|0x0C; //en=1, rs=0 -> bxxxx1100
data_t[3] = data_l|0x08; //en=0, rs=0 -> bxxxx1000
HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *)
data_t, 4, 100);
}
void lcd_send_data (char data)
{
char data_u, data_l;
uint8_t data_t[4];
data_u = (data&0xf0);
data_l = ((data<<4)&0xf0);
data_t[0] = data_u|0x0D; //en=1, rs=1 -> bxxxx1101
data_t[1] = data_u|0x09; //en=0, rs=1 -> bxxxx1001
data_t[2] = data_l|0x0D; //en=1, rs=1 -> bxxxx1101
data_t[3] = data_l|0x09; //en=0, rs=1 -> bxxxx1001
HAL_I2C_Master_Transmit (&hi2c1, SLAVE_ADDRESS_LCD,(uint8_t *)
data_t, 4, 100);
}
void lcd_init (void)
{
// 4 bit initialisation
HAL_Delay(50); // wait for >40ms
lcd_send_cmd (0x30);
HAL_Delay(5); // wait for >4.1ms
lcd_send_cmd (0x30);
HAL_Delay(1); // wait for >100us
lcd_send_cmd (0x30);
HAL_Delay(10);
lcd_send_cmd (0x20); // 4bit mode
HAL_Delay(10);

// display initialisation
lcd_send_cmd (0x28); // Function set --> DL=0 (4 bit mode), N = 1 (2
line display) F = 0 (5x8 characters)
HAL_Delay(1);
lcd_send_cmd (0x08); //Display on/off control --> D=0,C=0, B=0 --->
display off
HAL_Delay(1);
lcd_send_cmd (0x01); // clear display
HAL_Delay(2);
lcd_send_cmd (0x06); //Entry mode set --> I/D = 1 (increment cursor)
& S = 0 (no shift)
HAL_Delay(1);
lcd_send_cmd (0x0C); //Display on/off control --> D = 1, C and B = 0.
(Cursor and blink, last two bits)
}
void lcd_send_string (char *str)
{
while (*str) lcd_send_data (*str++);
}
void lcd_put_cur(int row, int col)
{
switch (row)
{
case 0:
col |= 0x80;
break;
case 1:
col |= 0xC0;
break;
}
lcd_send_cmd (col);
}
uint8_t BCD_to_Decimal(uint8_t bcd_value) {
return ((bcd_value >> 4) * 10) + (bcd_value & 0x0F);
}
/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU
Configuration--------------------------------------------------------
*/

/* Reset of all peripherals, Initializes the Flash interface and the


Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */


SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */


MX_GPIO_Init();
MX_I2C1_Init();
MX_I2S3_Init();
MX_SPI1_Init();
MX_USB_HOST_Init();
/* USER CODE BEGIN 2 */
lcd_init ();
// lcd_put_cur(0, 0);
// lcd_send_string ("HELLO WORLD");
// lcd_put_cur(1, 0);
// lcd_send_string("from CTECH");
HAL_I2C_Master_Transmit(&hi2c1, DevAddress, &Data_Wr[0], 8, 100);
char buffer1[16];
char buffer2[16];
lcd_send_cmd(0x01);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
MX_USB_HOST_Process();

/* USER CODE BEGIN 3 */


HAL_I2C_Master_Transmit(&hi2c1, DevAddress, &Sec_Reg, 1, 100);
HAL_I2C_Master_Receive(&hi2c1, DevAddress, &Data_Rd[0],7 , 100);
uint8_t seconds = BCD_to_Decimal(Data_Rd[0]);
uint8_t min = BCD_to_Decimal(Data_Rd[1]);
uint8_t hour = BCD_to_Decimal(Data_Rd[2]);
sprintf(buffer1, "gio:%02d-%02d-%02d",seconds,min,hour);
lcd_put_cur(0, 0); // Đặt con trỏ LCD tại hàng 0, cột 0
lcd_send_string(buffer1); // Gửi chuỗi lên LCD
uint8_t date = BCD_to_Decimal(Data_Rd[4]);
uint8_t month = BCD_to_Decimal(Data_Rd[5]);
uint8_t year = BCD_to_Decimal(Data_Rd[6]);
sprintf(buffer2, "ngay:%02d-%02d-20%02d",date,month,year);
lcd_put_cur(1, 0); // Đặt con trỏ LCD tại hàng 0, cột 0
lcd_send_string(buffer2); // Gửi chuỗi lên LCD

}
/* USER CODE END 3 */
}
Code đọc điện trở (adc)

/* USER CODE END PFP */

/* Private user code


---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
float dutycycle;
uint32_t ADC_c;
/* USER CODE END 0 */

/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */

/* MCU
Configuration--------------------------------------------------------
*/

/* Reset of all peripherals, Initializes the Flash interface and the


Systick. */
HAL_Init();

/* USER CODE BEGIN Init */

/* USER CODE END Init */

/* Configure the system clock */


SystemClock_Config();

/* USER CODE BEGIN SysInit */

/* USER CODE END SysInit */

/* Initialize all configured peripherals */


MX_GPIO_Init();
MX_ADC1_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */


HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 10);
ADC_c = HAL_ADC_GetValue(&hadc1);
dutycycle = (ADC_c*200)/4095;
__HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,dutycycle);
}
/* USER CODE END 3 */
}

You might also like