Você está na página 1de 6

Analisa

Pada praktikum kali ini kami melakukan pembuatan program dan menggunakan bantuan software
visual studio untuk membuat tampilan kotak dialog pada PC. Dalam percobaan kali ini adalah
terkait dengan komunikasi serial 2 arah dimana komunikasi dilakukan dari PC ke ARM, begitupun
sebaliknya dari ARM ke PC. Dalam melakukan komunikasi serial dari ARM ke PC, akan
diimplementasikan dengan pengaktifan dan pengnonaktifan LED pada modul ARM. Dan untuk
komunikasi serial dari PC ke ARM adalah dengan pembacaan sensor tegangan dengan potensio
yang nantinya akan tampil dalam PC. Untuk program yang digunakan adalah

/* Includes ------------------------------------------------------------------*/

#include "stm32f4xx_hal.h"

/* USER CODE BEGIN Includes */

#include "lcd_character.h"

#include "stdlib.h"

#define RX_BUFFER_SIZE 12 //ukuran penyimpanan

uint8_t rx_buffer [RX_BUFFER_SIZE]; // deklarasi

uint8_t rx_data,datasend[]={"OK\n"}; //

unsigned int rx_index,n;

char buffer[RX_BUFFER_SIZE];

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

ADC_HandleTypeDef hadc1;

DMA_HandleTypeDef hdma_adc1;

UART_HandleTypeDef huart1;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_DMA_Init(void);

static void MX_ADC1_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

/* Private function prototypes -----------------------------------------------*/


//char* bufftr="Hello!\n\r";

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */

__IO uint16_t Nilai_ADC[8];

/* USER CODE END 0 */

int main(void)

/* USER CODE BEGIN 1 */

char simpan[20];

//int r,s,t,x,a,b,c,y,u;

int a;

/* USER CODE END 1 */

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

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

HAL_Init();

/* Configure the system clock */

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_DMA_Init();

MX_ADC1_Init();

MX_USART1_UART_Init();

/* USER CODE BEGIN 2 */

__HAL_UART_ENABLE_IT(&huart1, UART_IT_TC);

HAL_UART_Receive_IT(&huart1,&rx_data,1);

lcd_init();

HAL_ADC_Start_DMA(&hadc1, (uint32_t*) &Nilai_ADC, 8);

/* USER CODE END 2 */

/* Infinite loop */

/* USER CODE BEGIN WHILE */

while (1)

/* USER CODE END WHILE */

/* USER CODE BEGIN 3 */

a=Nilai_ADC[1];
//lcd_gotoxy(0,0);

//sprintf(simpan,"%4d\r",a);

//HAL_UART_Transmit_IT(&huart1, (uint8_t *)simpan, 8);

HAL_Delay (10);

//lcd_puts(simpan);

int ch, add_dev,add_data,data_analog;

char buffer[5];

add_dev=1; lcd_gotoxy(0,1);

lcd_data(rx_buffer[0]); lcd_data(rx_buffer[1]);

lcd_data(rx_buffer[2]); lcd_data(rx_buffer[3]);

lcd_data(rx_buffer[4]); lcd_data(rx_buffer[5]);

lcd_data(rx_buffer[6]); lcd_data(rx_buffer[7]);

lcd_data(rx_buffer[8]); lcd_data(rx_buffer[9]);

lcd_data(rx_buffer[10]); lcd_data(rx_buffer[11]);

if(rx_buffer[0]=='*'&&rx_buffer[7]=='#' )// cek data

{ if(rx_buffer[1]=='W'||rx_buffer[1]=='w')// cek data

{ sprintf(buffer,"%c%c",rx_buffer[2],rx_buffer[3]);

add_data=atoi(buffer);

if (add_data==add_dev)

{ sprintf(buffer,"%c%c",rx_buffer[4],rx_buffer[5]);

ch=atoi(buffer)

if(ch==0)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_0,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_0,GPIO_PIN_RESET); }

else if (ch==1)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_1,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_1,GPIO_PIN_RESET); }

else if (ch==2)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_2,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_2,GPIO_PIN_RESET);}

else if (ch==3)

{
if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET) }

else if (ch==4)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET); }

else if (ch==5)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET); }

else if (ch==6)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET); }

else if (ch==7)

if(rx_buffer[6]=='1') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_SET);

else if(rx_buffer[6]=='0') HAL_GPIO_WritePin(GPIOE,GPIO_PIN_3,GPIO_PIN_RESET); }

//rx_buffer[7]=0;

else if (rx_buffer[0]=='*'&&rx_buffer[6]=='#' )

if(rx_buffer[1]=='R'||rx_buffer[1]=='r')// cek data

sprintf(buffer,"%c%c",rx_buffer[2],rx_buffer[3]);

add_data=atoi(buffer);

if (add_data==add_dev)

sprintf(buffer,"%c%c",rx_buffer[4],rx_buffer[5]);

ch=atoi(buffer);

if(ch==0)

sprintf(simpan,"0100%4d",a);
HAL_UART_Transmit_IT(&huart1, (uint8_t *)simpan,8);

lcd_gotoxy(8,0);

HAL_Delay (10);

lcd_puts(simpan); }

else if (ch==1)

sprintf(simpan,"0101%4d",a);

HAL_UART_Transmit_IT(&huart1, (uint8_t *)simpan, 8);

lcd_gotoxy(8,0);

HAL_Delay (10);

lcd_puts(simpan);

else if (ch==2)

sprintf(simpan,"Val:%4d",a);

HAL_UART_Transmit_IT(&huart1, (uint8_t *)simpan, 8);

lcd_gotoxy(0,0);

HAL_Delay (10);

lcd_puts(simpan);

else if (ch==3)

sprintf(simpan,"0103%4d",a);

HAL_UART_Transmit_IT(&huart1, (uint8_t *)simpan, 8);

lcd_gotoxy(8,0);

HAL_Delay (10);

lcd_puts(simpan);

rx_buffer[6]=' ';

/* USER CODE END 3 */

}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

HAL_UART_Receive_IT(&huart1,&rx_data,1);

if(rx_data=='*'||rx_index>RX_BUFFER_SIZE)

{rx_index=0;

rx_buffer [7]=' ';

rx_buffer [rx_index]=rx_data;

rx_index++;

Você também pode gostar