-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodule2-1(hal).c
More file actions
183 lines (144 loc) · 5.4 KB
/
module2-1(hal).c
File metadata and controls
183 lines (144 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <string.h>
#include "stm32f4xx_hal.h"
// Define UART and I2C handler structures
UART_HandleTypeDef huart2;
I2C_HandleTypeDef hi2c1;
// Function prototypes
void MX_GPIO_Init(void);
void MX_USART2_UART_Init(void);
void MX_I2C1_Init(void);
void Error_Handler(void);
// I2C address for the DS1631 temperature sensor (7-bit address left-shifted by 1)
#define DS1631_ADDRESS 0x90
volatile uint32_t msTicks = 0; /* Counts 1ms timeTicks */
// SysTick Handler for timing
void SysTick_Handler(void) {
HAL_IncTick(); /* Increment HAL tick count */
msTicks++; /* Increment the msTicks variable */
}
// Simple debug print function to send a string via UART
void Debug_Print(char* message) {
HAL_UART_Transmit(&huart2, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
}
// Function to initialize the DS1631 sensor
void DS1631_Init(void) {
uint8_t config = 0x00; // Continuous conversion mode
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(&hi2c1, DS1631_ADDRESS, 0xAC, I2C_MEMADD_SIZE_8BIT, &config, 1, HAL_MAX_DELAY);
if (status != HAL_OK) {
Debug_Print("Failed to initialize DS1631\r\n");
Error_Handler();
}
}
void Start_Temperature_Conversion(void) {
uint8_t startCmd = 0x51; // Start Convert T command for DS1631
HAL_I2C_Mem_Write(&hi2c1, DS1631_ADDRESS, 0x51, I2C_MEMADD_SIZE_8BIT, &startCmd, 1, HAL_MAX_DELAY);
}
// Function to read temperature from the DS1631 sensor
// Function to read temperature from the DS1631 sensor
float Read_Temperature(void) {
uint8_t tempData[2];
int16_t tempValue;
float temperature;
HAL_I2C_Mem_Read(&hi2c1, DS1631_ADDRESS, 0xAA, I2C_MEMADD_SIZE_8BIT, tempData, 2, HAL_MAX_DELAY);
tempValue = (int16_t)((tempData[0] << 8) | tempData[1]);
// Debug print the raw data
char buffer[50];
snprintf(buffer, sizeof(buffer), "Raw Data: 0x%02X 0x%02X\r\n", tempData[0], tempData[1]);
Debug_Print(buffer);
// Convert to temperature (assuming the DS1631 format)
temperature = (float)tempValue / 256.0;
return temperature;
}
void I2C_Scan() {
char buffer[32];
HAL_StatusTypeDef result;
for (uint8_t i = 1; i < 128; i++) {
result = HAL_I2C_IsDeviceReady(&hi2c1, (i << 1), 1, 10);
if (result == HAL_OK) {
snprintf(buffer, sizeof(buffer), "I2C device found at address 0x%02X\r\n", i);
Debug_Print(buffer);
}
}
}
int main(void) {
HAL_Init();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
I2C_Scan(); // Check if sensor is detected
DS1631_Init(); // Initialize the sensor
Start_Temperature_Conversion(); // Start temperature conversion (if needed)
while (1) {
float temperature = Read_Temperature();
char buffer[50];
snprintf(buffer, sizeof(buffer), "Temperature: %.2f C\r\n", temperature);
Debug_Print(buffer);
HAL_Delay(1000); // Wait 1 second
}
}
// USART2 Initialization
void MX_USART2_UART_Init(void) {
__HAL_RCC_USART2_CLK_ENABLE(); // Enable the USART2 clock
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable the GPIOA clock
// Configure PA2 (USART2_TX) and PA3 (USART2_RX) pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
// UART configuration
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
// Initialize UART and check for errors
if (HAL_UART_Init(&huart2) != HAL_OK) {
Error_Handler();
}
}
// I2C1 Initialization
void MX_I2C1_Init(void) {
__HAL_RCC_I2C1_CLK_ENABLE(); // Enable the I2C1 clock
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable the GPIOB clock
// Configure I2C SCL (PB8) and SDA (PB9) pins
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
// I2C configuration
hi2c1.Instance = I2C1;
hi2c1.Init.ClockSpeed = 100000; // 100 kHz standard mode
hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
// Initialize I2C and check for errors
if (HAL_I2C_Init(&hi2c1) != HAL_OK) {
Error_Handler();
}
}
// GPIO Initialization
void MX_GPIO_Init(void) {
__HAL_RCC_GPIOA_CLK_ENABLE(); // Enable the GPIOA clock
__HAL_RCC_GPIOB_CLK_ENABLE(); // Enable the GPIOB clock
// Additional GPIO initialization can be added here
}
// Error Handler
void Error_Handler(void) {
// Stay in this loop in case of error
while (1) {
}
}