반응형

스위치를 이용한 입력으로 시리얼 전송

시리얼 문자를 이용한 LED OnOFF

 

1. CUBE MX SET

 

2. Sorce Code

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_USART2_UART_Init();
  /* USER CODE BEGIN 2 */
HAL_StatusTypeDef RcvStat ;
uint8_t bufftx[10] = "Hello!\n" ;
uint8_t b_in[10] = "b_in\n" ;
uint8_t UsartData[10] ;
uint8_t pin_state ;

HAL_UART_Transmit(&huart2, bufftx, 10, 100) ;
  /* USER CODE END 2 */

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

    /* USER CODE BEGIN 3 */
RcvStat = HAL_UART_Receive(&huart2, UsartData, 1, 100) ;

if (RcvStat == HAL_OK) {
if (UsartData[0] == 'a') {  // if serial input is 'a', led on
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET) ;
} else if (UsartData[0] == 'b') {  // if serial input is 'a', led off
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET) ;
}

HAL_UART_Transmit(&huart2, UsartData, 1, 100) ;
}

pin_state = HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) ;

if (!pin_state) {  // if b1 is low, send b_in
HAL_UART_Transmit(&huart2, b_in, 10, 100) ;
}
  }
  /* USER CODE END 3 */
}

 

SWITCH를 누르면 B_IN MSG 송신

시리얼문자 'a' 전송시 LED ON

'b'전송시 LED OFF

반응형

'Project > Nucleo-F103RB' 카테고리의 다른 글

Nucleo-F103RB Serial 통신3  (0) 2019.05.27
Nucleo-F103RB Serial 통신2  (0) 2019.05.21
Nucleo-F103RB Serial 통신1  (0) 2019.05.21
Nucleo-F103RB LED 켜기  (0) 2019.05.21
Nucleo-F103RB 개발환경  (0) 2019.05.21
반응형
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
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
#include "lcd.h"
#include <stdio.h>
#include <stdlib.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
 
 
int main(void)
{
    io_init();
    lcd_init();
    lcd_clear();
    
    DDRE = 0x00;
    PORTE = 0xff;
    
    int count=0;
    char str[16];
    
    while (1
    {    
        itoa(PINE,str,2);
        sprintf(str,"C:%d",count);
    }
}
 
cs

 

 

 

// 스위치를 입력할 경우 LCD 2진수가 출력된다.

//디버깅에 용이하다

반응형
반응형

AVR

io레지스터

DDRA = 0b1111 1111; //출력설정

PORTA = 0b1111 1111; // 포트를 출력한다.

DDRA = 0b0000 0000; // 입력설정

PORTA = 0b11111111; // Software pull-up

//

if(PINA == 0b0111 1111) // PIN 레지스터는 Read-Only 읽기만 가능.

{

PORTA = 0b0000 0000; // PORT 레지스터읽기 쓰기 둘다 가능하다.

}

 

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
/*
 * avr_test.cpp
 *
 * Created: 2018-09-03 오전 10:56:46
 * Author : USER
 */ 
#include <avr/io.h>
#define F_CPU 16000000
#include <util/delay.h>
 
#define LED_SEL1 (*(volatile unsigned char *)0xA000)
#define LED_SEL2 (*(volatile unsigned char *)0xB000)
#define FND (*(volatile unsigned char *)0x8000)
 
 
void io_init(void)
{
    MCUCR|=(1<<SRE) | (1<<SRW10);
    XMCRA=(1<<SRL2) | (0<<SRL1) | (0<<SRL0) | (1<<SRW01) | (1<<SRW00) | (1<<SRW11);
    XMCRB |= 0x00;
}
 
void fndController (char loc , char number){
    FND = (number <<3)|loc;
}
void fndDisplay (unsigned long long number){
    fndController(5,number%10);_delay_ms(1);
    fndController(4,(number%100)/10);_delay_ms(1);
    fndController(3,(number%1000)/100);_delay_ms(1);
    fndController(2,(number%10000)/1000);_delay_ms(1);
    fndController(1,(number%100000)/10000);_delay_ms(1);
    fndController(0,number/100000);_delay_ms(1);
    
}
int main(void)
{
    io_init();
    DDRE = 0x00;
    PORTE = 0xff;
    //int count =0;
    
    bool state = false;
    int randnum =0, dispnum =0;
    while (1)
    {
        
        /* //눌렀을 때 증가하는 소스
        if (PINE == 0b11101111 && state ==false){ count ++;state =true;}
        else if (PINE ==0b11111111&& state==true){ state =false;}
        fndDisplay(count);
        */
        // 0~100 사이의 랜덤수 출력하는 소스
        randnum++;
        if (randnum>100) randnum=0;
        if (PINE == 0b11101111 && state ==false){  dispnum= randnum;state =true;}
        else if (PINE ==0b11111111&& state==true){ state =false;}
        fndDisplay(dispnum);
        
    }
}
 
cs

 

 

반응형
반응형

* 인터럽트 : 방해하다, 훼방놓다의 뜻, 긴급하거나 불규칙적인 사건의 처리, 외부와의 인터페이스, 폴링(polling)으로 처리하기에는 프로세싱 타임 낭비

HOW 

 - 현재 수행중인 일을 잠시 중단하고 급한 일을 처리

 - 일이 끝나면 본래의 일을 다시 이어서 수행

 - 이때, 급한 일을 해결하는 작업을 인터럽트 서비스 루틴이라 하는데, 각 인터럽트마다 고유의 인터럽트 서비스 루틴 존재

 - 인터럽트가 여러 개 동시에 걸리면 우선 순위에 따라 조치

* 인터럽트 서비스 루틴

 - 인터럽트가 발생하면 프로세서는 현재 수행중인 프로그램을 멈추고 상태 레지스터와 PC 등을 스택에 잠시 저장 후 인터럽트 서비스 루틴으로 점프, 인터럽트 서비스 루틴을 실행한 후에는 이전의 프로그램으로 복귀하여 정상적인 절차를 실행한다.


* 인터럽트 스위치로 1/100 스톱워치 만들기

 - 메인

1. 초기설정

2. state 변수 값 설정 : stop

3. 인터럽트 활성화

4. state 검사 후 stop 상태시 복사된 10초, 1초, 1/10초, 1/100초 변수를 디스플레이하고, go 상태면 현재의 10초, 1초, 1/10초, 1/100초 변수를 디스플레이
 - 인터럽트 INT4
1. state 값이 stop시 go 변경
2. state go 일시 stop으로 변경 후 이때 시간 값 복사 저장
 - 인터럽트 INT5
1. 변수 및 복사된 시간 값 0으로 초기화
2. state 값 stop으로 초기화

#include <avr/io.h> 

#include <avr/interrupt.h>

#define F_CPU 16000000UL

#define __DELAY_BACKWARD_COMPATIBLE__

#include <util/delay.h>

#define STOP 0

#define GO 1

volatile int cur_time = 0;

volatile int stop_time = 0;

volatile int state = STOP; 

unsigned char digit[]= {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7c, 0x07, 0x7f, 0x67};

unsigned char fnd_sel[4] = {0x01, 0x02, 0x04, 0x08};

SIGNAL(SIG_INTERRUPT4) 

if (state == STOP)

 state = GO; 

  else 

state = STOP; 

  stop_time = cur_time; 

SIGNAL(SIG_INTERRUPT5) 

{

state = STOP; 

cur_time = 0; stop_time = 0; 

}

void init( ) 

{

DDRC = 0xff; // FND Data 

DDRG = 0x0f; // FND Select 

DDRE = 0xcf; // INT4, 5

PORTC = digit[0]; PORTG = 0x0f; EICRB = 0x0a; //falling edge 

EIMSK = 0x30; //interrupt en 

sei(); 

}


void display_fnd(int count) // 수행시간 = 약 10ms 

int i, fnd[4]; 

fnd[3] = (count/1000)%10; // 천 자리

fnd[2] = (count/100)%10; // 백 자리

fnd[1] = (count/10)%10; // 십 자리 

fnd[0] = count%10; // 일 자리 

for (i=0; i<4; i++)

{

PORTC = digit[fnd[i]];

PORTG = fnd_sel[i];

_delay_ms(2+i%2); 

}

}


int main()

{

init( );

while(1)

if (state == STOP)

display_fnd(stop_time);

else display_fnd(cur_time);

cur_time++;

}

}

반응형

'Project > j-kit-128-1실습' 카테고리의 다른 글

FND_Display with Timer/Counter  (0) 2018.10.01
LED 제어  (0) 2016.02.26
FND 실습  (0) 2016.02.26
FND 실습 1/100초 스탑워치, 24시간 디지털 시계만들기  (0) 2016.02.26
회로도 참고  (0) 2016.02.26

+ Recent posts