Sie sind auf Seite 1von 7

UNIVERSIDAD PRIVADA DEL VALLE

EXAMEN PREVIO
Estudiante: Vladimir Quispe Condori Fecha: 23 / 04 / 2024
Carrera: IMT Asignatura: Microprocesadores II Grupo: A
Docente: Gerson Pérez Villarroel Laboratorio:
Practica N0: 6 Título de la Practica: Comunicación Mediante Interfaz SPI
Firma Estudiante: Firma Docente:

Diagrama de Bloques general.


/* USER CODE BEGIN Header */
/**

*****************************************************************************
*
* @file : main.c
* @brief : Main program body

*****************************************************************************
*
* @attention
*

*
* This software is licensed under /* USER CODE BEGIN PV */
terms that can be found in the
LICENSE file /* USER CODE END PV */
* in the root directory of this
software component. /* Private function prototypes ----
* If no LICENSE file comes with -----------------------------------
this software, it is provided AS- --------*/
IS. void SystemClock_Config(void);
* static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
*********************************** /* USER CODE BEGIN PFP */
*********************************** void lcdWrite4(uint8_t);
******** void lcdCommand(uint8_t cmd);
*/ void lcdData(uint8_t data);
/* USER CODE END Header */ void lcdInit(void);
/* Includes ----------------------- void lcdGotoxy(uint8_t x, uint8_t
----------------------------------- y);
--------*/ void lcdPrint(char * str);
#include "main.h" /* USER CODE END PFP */

/* Private includes --------------- /* Private user code --------------


----------------------------------- -----------------------------------
--------*/ --------*/
/* USER CODE BEGIN Includes */ /* USER CODE BEGIN 0 */
#include <stdio.h>
#define LCD_D4 4 /* USER CODE END 0 */
#define LCD_D5 5
#define LCD_D6 6 /**
#define LCD_D7 7 * @brief The application entry
#define LCD_RS 1 //LCD RS point.
#define LCD_EN 0 //LCD EN * @retval int
/* USER CODE END Includes */ */
int main(void)
/* Private typedef ---------------- {
----------------------------------- /* USER CODE BEGIN 1 */
--------*/
/* USER CODE BEGIN PTD */ /* USER CODE END 1 */

/* USER CODE END PTD */ /* MCU Configuration-------------


-----------------------------------
/* Private define ----------------- --------*/
-----------------------------------
--------*/ /* Reset of all peripherals,
/* USER CODE BEGIN PD */ Initializes the Flash interface and
the Systick. */
/* USER CODE END PD */ HAL_Init();

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


-----------------------------------
--------*/ /* USER CODE END Init */
/* USER CODE BEGIN PM */
/* Configure the system clock */
/* USER CODE END PM */ SystemClock_Config();

/* Private variables -------------- /* USER CODE BEGIN SysInit */


-----------------------------------
--------*/ /* USER CODE END SysInit */
SPI_HandleTypeDef hspi1;
/* Initialize all configured p[0] = (1<<7);
peripherals */ else
MX_GPIO_Init(); p[0] &=
MX_SPI1_Init(); ~(1<<7);
/* USER CODE BEGIN 2 */ lcdCommand(0x86);
lcdInit(); lcdPrint(" ");
uint8_t comandos[3] = {0xAA, val = p[0]&0xFF;
0x01, 0x02};
uint8_t datoTem[2]; sprintf(mensaje,
uint8_t valor[1]; "%d", val);
uint8_t p[2]; lcdCommand(0x86);
uint8_t o[2]; lcdPrint(mensaje);
uint8_t vel[3]; lcdPrint(',');
uint8_t a[1]; lcdPrint('a');
uint8_t adc[2] = {0x06, 0x00};
uint8_t val;
uint8_t v; HAL_GPIO_WritePin(GPIOB,
float r; GPIO_PIN_0, GPIO_PIN_RESET);
char mensaje[20];
HAL_SPI_Transmit(&hspi1, &adc[0],
/* USER CODE END 2 */ 1, HAL_MAX_DELAY);

/* Infinite loop */ HAL_SPI_TransmitReceive(&hspi1,


/* USER CODE BEGIN WHILE */ &adc[0], &o[0], 1, HAL_MAX_DELAY);
while (1)
{ HAL_SPI_Receive(&hspi1, &o[0], 1,
HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOB,
GPIO_PIN_0, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,
GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOB, o[0] &= 0B00001111;
GPIO_PIN_1, GPIO_PIN_RESET); valor[0] |= (o[1]);

HAL_GPIO_WritePin(GPIOA, r = valor[0];
GPIO_PIN_2, GPIO_PIN_SET); r *= r*0.0154;
HAL_Delay(100); r += 62;
v = r;
HAL_SPI_Transmit(&hspi1, v = v<<1;
&comandos[0], 1, HAL_MAX_DELAY); vel[0] = (v)&(0xFF);
vel[1] = 0;
HAL_GPIO_WritePin(GPIOA,
GPIO_PIN_3, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,
GPIO_PIN_0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOB,
GPIO_PIN_1, GPIO_PIN_SET); HAL_GPIO_WritePin(GPIOB,
HAL_Delay(100); GPIO_PIN_1, GPIO_PIN_SET);
HAL_Delay(100);
HAL_SPI_Receive(&hspi1, p, 2,
HAL_MAX_DELAY); HAL_SPI_Transmit(&hspi1,
&comandos[1], 1, HAL_MAX_DELAY);
HAL_GPIO_WritePin(GPIOA,
GPIO_PIN_2, GPIO_PIN_RESET); HAL_SPI_Transmit(&hspi1, &vel[1],
1, HAL_MAX_DELAY);
if(p[0]%2)
a[0] = '5'; HAL_GPIO_WritePin(GPIOB,
else GPIO_PIN_1, GPIO_PIN_RESET);
a[0] = '0';
p[0] = p[0]>>1; lcdCommand(0xC4);
if(p[1]&128) lcdPrint(" ");
sprintf(mensaje, */
"%d", v >> 1); RCC_OscInitStruct.OscillatorType
lcdCommand(0xC4); = RCC_OSCILLATORTYPE_HSI;
lcdPrint(mensaje); RCC_OscInitStruct.HSIState =
RCC_HSI_ON;
r=valor[0];
r *= r*0.0154; RCC_OscInitStruct.HSICalibrationVal
r = 64.0-r; ue = RCC_HSICALIBRATION_DEFAULT;
val = r; RCC_OscInitStruct.PLL.PLLState =
vel[0] = val; RCC_PLL_NONE;
vel[1] = 0; if
vel[0] <<= 1; (HAL_RCC_OscConfig(&RCC_OscInitStru
ct) != HAL_OK)
HAL_GPIO_WritePin(GPIOB, {
GPIO_PIN_0, GPIO_PIN_RESET); Error_Handler();
}
HAL_GPIO_WritePin(GPIOB,
GPIO_PIN_1, GPIO_PIN_SET); /** Initializes the CPU, AHB and
HAL_Delay(50); APB buses clocks
*/
HAL_SPI_Transmit(&hspi1, RCC_ClkInitStruct.ClockType =
&comandos[2], 1, HAL_MAX_DELAY); RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SY
SCLK
HAL_SPI_Transmit(&hspi1, &vel[0],
1, HAL_MAX_DELAY); |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_
PCLK2;
HAL_GPIO_WritePin(GPIOA, RCC_ClkInitStruct.SYSCLKSource =
GPIO_PIN_1, GPIO_PIN_RESET); RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider =
lcdCommand(0xCB); RCC_SYSCLK_DIV1;
lcdPrint(" "); RCC_ClkInitStruct.APB1CLKDivider
sprintf(mensaje, = RCC_HCLK_DIV1;
"%d", val); RCC_ClkInitStruct.APB2CLKDivider
lcdCommand(0xCB); = RCC_HCLK_DIV1;
lcdPrint(mensaje);
/* USER CODE END WHILE */ if
(HAL_RCC_ClockConfig(&RCC_ClkInitSt
/* USER CODE BEGIN 3 */ ruct, FLASH_LATENCY_0) != HAL_OK)
} {
/* USER CODE END 3 */ Error_Handler();
} }
}
/**
* @brief System Clock /**
Configuration * @brief SPI1 Initialization
* @retval None Function
*/ * @param None
void SystemClock_Config(void) * @retval None
{ */
RCC_OscInitTypeDef static void MX_SPI1_Init(void)
RCC_OscInitStruct = {0}; {
RCC_ClkInitTypeDef
RCC_ClkInitStruct = {0}; /* USER CODE BEGIN SPI1_Init 0 */

/** Initializes the RCC /* USER CODE END SPI1_Init 0 */


Oscillators according to the
specified parameters /* USER CODE BEGIN SPI1_Init 1 */
* in the RCC_OscInitTypeDef
structure. /* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1; /*Configure GPIO pins : PA0 PA1
hspi1.Init.Mode = PA4 PA5
SPI_MODE_MASTER; PA6 PA7
hspi1.Init.Direction = */
SPI_DIRECTION_2LINES; GPIO_InitStruct.Pin =
hspi1.Init.DataSize = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GP
SPI_DATASIZE_8BIT; IO_PIN_5
hspi1.Init.CLKPolarity =
SPI_POLARITY_LOW; |GPIO_PIN_6|GPIO_PIN_7;
hspi1.Init.CLKPhase = GPIO_InitStruct.Mode =
SPI_PHASE_1EDGE; GPIO_MODE_OUTPUT_PP;
hspi1.Init.NSS = SPI_NSS_SOFT; GPIO_InitStruct.Pull =
hspi1.Init.BaudRatePrescaler = GPIO_NOPULL;
SPI_BAUDRATEPRESCALER_2; GPIO_InitStruct.Speed =
hspi1.Init.FirstBit = GPIO_SPEED_FREQ_LOW;
SPI_FIRSTBIT_MSB; HAL_GPIO_Init(GPIOA,
hspi1.Init.TIMode = &GPIO_InitStruct);
SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = /* USER CODE BEGIN MX_GPIO_Init_2
SPI_CRCCALCULATION_DISABLE; */
hspi1.Init.CRCPolynomial = 10; /* USER CODE END MX_GPIO_Init_2 */
if (HAL_SPI_Init(&hspi1) != }
HAL_OK)
{ /* USER CODE BEGIN 4 */
Error_Handler(); void lcdWrite4(uint8_t dato)
} //Enviar nibble alto a D4-D7.
/* USER CODE BEGIN SPI1_Init 2 */ {
GPIOA -> ODR = (GPIOA -> IDR
/* USER CODE END SPI1_Init 2 */ & 0x0F) | (dato & 0xF0); //Enviar
el nibble alto a D4-D7.
} GPIOA -> ODR |= (1<<LCD_EN);
//EN = 1, pulso de alto a bajo.
/** HAL_Delay(1); //Retardo para
* @brief GPIO Initialization pulso.
Function GPIOA -> ODR &=
* @param None ~(1<<LCD_EN); //EN = 0, pulso de
* @retval None alto a bajo.
*/ }
static void MX_GPIO_Init(void) void lcdCommand(uint8_t cmd) //OJO
{ solo parte del puerto es del LCD.
GPIO_InitTypeDef GPIO_InitStruct |=.
= {0}; {
/* USER CODE BEGIN MX_GPIO_Init_1 GPIOA -> ODR &=
*/ ~(1<<LCD_RS); //RS = 0 para
/* USER CODE END MX_GPIO_Init_1 */ comandos.
lcdWrite4(cmd); //Enviar
/* GPIO Ports Clock Enable */ nibble alto a D4 – D7.
__HAL_RCC_GPIOA_CLK_ENABLE(); HAL_Delay(1);
__HAL_RCC_GPIOB_CLK_ENABLE(); lcdWrite4(cmd << 4);
//Enviar el nibble bajo a D4-
/*Configure GPIO pin Output Level D7._delay_us(100);
*/ }
HAL_GPIO_WritePin(GPIOA, void lcdData(uint8_t data) //OJO
GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GP solo parte del puerto es del LCD.
IO_PIN_5 |=.
{
|GPIO_PIN_6|GPIO_PIN_7, GPIOA -> ODR |= (1<<LCD_RS);
GPIO_PIN_RESET); //RS = 1 para datos.
lcdWrite4(data); //Nibble * @brief This function is
alto. executed in case of error
HAL_Delay(1); occurrence.
lcdWrite4(data << 4); * @retval None
//Nibble bajo. */
HAL_Delay(1); void Error_Handler(void)
} {
void lcdGotoxy(uint8_t x, uint8_t /* USER CODE BEGIN
y) Error_Handler_Debug */
{ /* User can add his own
uint8_t firstCharAdr[] = implementation to report the HAL
{0x80, 0xC0, 0x94, 0xD4}; error return state */
lcdCommand(firstCharAdr[y] + __disable_irq();
x); while (1)
HAL_Delay(1); {
} }
void lcdPrint(char * str) /* USER CODE END
{ Error_Handler_Debug */
uint8_t i = 0; }
while(str[i] != 0)
{ #ifdef USE_FULL_ASSERT
lcdData(str[i]); /**
i++; * @brief Reports the name of the
} source file and the source line
} number
void lcdInit(void) * where the assert_param
{ error has occurred.
HAL_Delay(101); //Espera * @param file: pointer to the
inicialización de encendido de LCD. source file name
GPIOA -> ODR &= * @param line: assert_param
~(1<<LCD_EN);//LCD_EN = 0 error line source number
GPIOA -> ODR &= * @retval None
~(1<<LCD_RS); //RS = 0 para */
comandos. void assert_failed(uint8_t *file,
lcdWrite4(0x30); uint32_t line)
HAL_Delay(5); {
lcdWrite4(0x30); /* USER CODE BEGIN 6 */
HAL_Delay(1); /* User can add his own
lcdWrite4(0x30); implementation to report the file
HAL_Delay(1); name and line number,
lcdWrite4(0x20); ex: printf("Wrong parameters
HAL_Delay(1); value: file %s on line %d\r\n",
lcdCommand(0x28); //2 file, line) */
lineas, caracter5x7. /* USER CODE END 6 */
HAL_Delay(1); }
lcdCommand(0x0E); //Display #endif /* USE_FULL_ASSERT */
ON, cursor ON.
HAL_Delay(1); /**
lcdCommand(0x01); //Limpiar * @brief SPI MSP Initialization
LCD. * This function configures the
HAL_Delay(5); hardware resources used in this
lcdCommand(0x06); example
//Desplazar cursor a la derecha. * @param hspi: SPI handle pointer
HAL_Delay(1); * @retval None
} */
/* USER CODE END 4 */ void
HAL_SPI_MspInit(SPI_HandleTypeDef*
/** hspi)
{
GPIO_InitTypeDef GPIO_InitStruct HAL_GPIO_Init(GPIOB,
= {0}; &GPIO_InitStruct);
if(hspi->Instance==SPI1)
{ GPIO_InitStruct.Pin =
/* USER CODE BEGIN SPI1_MspInit 0 GPIO_PIN_4;
*/ GPIO_InitStruct.Mode =
GPIO_MODE_INPUT;
/* USER CODE END SPI1_MspInit 0 GPIO_InitStruct.Pull =
*/ GPIO_NOPULL;
/* Peripheral clock enable */ HAL_GPIO_Init(GPIOB,
__HAL_RCC_SPI1_CLK_ENABLE(); &GPIO_InitStruct);

__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_AFIO_REMAP_SPI1_ENABLE();
/**SPI1 GPIO Configuration
PB3 ------> SPI1_SCK /* USER CODE BEGIN SPI1_MspInit 1
PB4 ------> SPI1_MISO */
PB5 ------> SPI1_MOSI
*/ /* USER CODE END SPI1_MspInit 1
GPIO_InitStruct.Pin = */
GPIO_PIN_3|GPIO_PIN_5; }
GPIO_InitStruct.Mode =
GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed =
GPIO_SPEED_FREQ_HIGH;

Das könnte Ihnen auch gefallen