/* STM32 Blue Pill Cross-Communication Project - Standard Peripheral Library Version */
#include "stm32f1xx.h"
#define ADC_THRESHOLD 2048
#define LED_PIN GPIO_Pin_1 // PA1
#define PHOTO_PIN GPIO_Pin_0 // PA0
volatile uint8_t received_data = 0;
volatile uint8_t data_received = 0;
void RCC_Configuration(void) {
// Enable clocks
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO |
RCC_APB2Periph_USART1 | RCC_APB2Periph_ADC1, ENABLE);
// System clock configuration (72MHz)
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); // ADC clock = 72MHz/6 = 12MHz
}
void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure;
// PA1 (LED) as output
GPIO_InitStructure.GPIO_Pin = LED_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, LED_PIN); // LED off initially
// PA0 (Photoresistor) as analog input
GPIO_InitStructure.GPIO_Pin = PHOTO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1 pins (PA9-TX, PA10-RX)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void USART1_Configuration(void) {
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void ADC1_Configuration(void) {
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
// ADC calibration
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
uint16_t ADC_Read(void) {
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
return ADC_GetConversionValue(ADC1);
}
void USART1_SendByte(uint8_t data) {
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, data);
}
void USART1_IRQHandler(void) {
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
received_data = USART_ReceiveData(USART1);
data_received = 1;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
void Delay_ms(uint32_t ms) {
for(uint32_t i = 0; i < ms * 8000; i++) {
__NOP();
}
}
int main(void) {
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
ADC1_Configuration();
uint16_t adc_value = 0;
uint8_t my_light_state = 0;
while(1) {
// Read photoresistor
adc_value = ADC_Read();
// Determine if light is detected
uint8_t current_light = (adc_value > ADC_THRESHOLD) ? 1 : 0;
// Send state change to other STM32
if(current_light != my_light_state) {
my_light_state = current_light;
USART1_SendByte(my_light_state);
}
// Check if data received from other STM32
if(data_received) {
data_received = 0;
if(received_data == 1) {
GPIO_SetBits(GPIOA, LED_PIN); // LED on
} else {
GPIO_ResetBits(GPIOA, LED_PIN); // LED off
}
}
Delay_ms(50); // Debouncing and reduce CPU load
}
}