스위치를 각각 아래처럼 연결하여 각 스위치의 카운터를 LCD에 출력하는 예제입니다.

// PA0 : SW1 연결,

// PA2 : SW2 연결

// PA4 : SW3 연결

// PA6 : SW4 연결

// PA7 : SW5 연결

 

 

 

 

 

WATSIM 시뮬레이션 결과

 

< SW1은 5번, SW2 는 3번, SW3은 1번, SW4는 2번 눌렀을 때의 시뮬레이션 결과입니다. >

 

 

 

 

 

WAT-AVR128 보드에서의 실행

 

 

 





WAT-AVR128 에서의 실행 결과 입니다.

 

 

 

 

 

WAT-AVR128 에서의 실행 결과 입니다. (ZOOM)

 

 

 

 

 

WAT-AVR128 과 WAT-CLCD 조립 사진 입니다.

 

 

 









 

 

메인 소스 AVR Studio 4.18 용

 

 

/**********************************************

 

switch

// PA0 : SW1 연결, SW1 카운터 증가

// PA2 : SW2 연결, SW2 카운터 증가

// PA4 : SW3 연결, SW3 카운터 증가

// PA6 : SW4 연결, SW4 카운터 증가

// PA7 : SW5 연결, 모든 카운터 초기화

 

Character LCD Control

 

PORTB ==> LCD DataLine

LCD_DATA_PORT.5 ==> LCD E

LCD_DATA_PORT.3 ==> LCD RW

LCD_DATA_PORT.1 ==> LCD RS

 

Main Clock : 11.0592Mhz

 

 

 

// Character LCD 에SW 의눌러진카운터표시

 

Tools : AVR Studio 4.16

테스트보드: WAT-AVR128 보드

 

http://avr128.com

 

*********************************************/

 

#include <avr/io.h>

#include <avr/interrupt.h>

#include <stdio.h>

 

#include "wat128.h"

 

 

#define SW1_BIT 0

#define SW2_BIT 2

#define SW3_BIT 4

#define SW4_BIT 6

#define SW5_BIT 7

 

 

 

UINT8 chTemp[17];

int g_PushCount[4] = {0,0,0,0};

 

// 스위치(키)의입력값이변경되었는지와마지막값확인용

unsigned char g_keyvalue = -1; // -1 은입력된값이없을때

unsigned char g_lastkeyvalue = -1; // -1 은입력된값이없을때

 

 

void CheckKey()

{

    g_keyvalue = PINA & 0xFF;

 

    // key 값을처음받을때는카운터하지않는다.

    if(g_lastkeyvalue == -1)

    {

        g_lastkeyvalue = g_keyvalue;

        return;

    }

 

 

// switch 의값이변경되었을때만처리하자.

 

if( g_keyvalue != g_lastkeyvalue)

{

 

if((g_lastkeyvalue& (1<<SW1_BIT)) && !(g_keyvalue & (1<<SW1_BIT)))

{

g_PushCount[0] ++;

if(g_PushCount[0] >999) g_PushCount[0] = 0;

}

 

    if((g_lastkeyvalue& (1<<SW2_BIT)) && !(g_keyvalue & (1<<SW2_BIT)))

{

         g_PushCount[1] ++;

if(g_PushCount[1] >999) g_PushCount[1] = 0;

        }

 

if((g_lastkeyvalue& (1<<SW3_BIT)) && !(g_keyvalue & (1<<SW3_BIT)))

{

             g_PushCount[2] ++;

if(g_PushCount[2] >999) g_PushCount[2] = 0;

}

 

if((g_lastkeyvalue& (1<<SW4_BIT)) && !(g_keyvalue & (1<<SW4_BIT)))

{

g_PushCount[3] ++;

if(g_PushCount[3] >999) g_PushCount[3] = 0;

}

 

if((g_lastkeyvalue& (1<<SW5_BIT)) && !(g_keyvalue & (1<<SW5_BIT)))

{

g_PushCount[0] = 0;

g_PushCount[1] = 0;

g_PushCount[2] = 0;

g_PushCount[3] = 0;

}

 

g_lastkeyvalue = g_keyvalue;

 

}

}

 

 

 

int main()

{

 

    // clcd 초기화

    CLCD_Init();

 

    // sw 초기화

    ClearBit(CLCD_CONTROL_PORT_DIR,SW1_BIT);

    ClearBit(CLCD_CONTROL_PORT_DIR,SW2_BIT);

    ClearBit(CLCD_CONTROL_PORT_DIR,SW3_BIT);

    ClearBit(CLCD_CONTROL_PORT_DIR,SW4_BIT);

    ClearBit(CLCD_CONTROL_PORT_DIR,SW5_BIT);

 

 

 

    while (1)

    {

 

        CheckKey();

 

        sprintf(chTemp,"S1:%4d S2:%4d",g_PushCount[0],g_PushCount[1] );

         CLCD_PutString(0,0,chTemp);

        sprintf(chTemp,"S3:%4d,S4:%4d",g_PushCount[2],g_PushCount[3] );

         CLCD_PutString(0,1,chTemp);

 

    }

 

}

 

 

 

 

 

 

 

 

 

주요 부품

ATMEGA128 - 1EA

Character LCD – 1EA

AX07001

Switch – 5EA

POWER 5V/1A

 

 

 

 



실행파일 다운로드

이번 예제가 추가되면서 실행파일이 업데이트 되었습니다.

이 예제를 사용하기 위해서는 최신 버전(0.2.1.2 이상)으로 패치 하시기 바랍니다.

 

실행파일:

 

 

프로젝트 파일(소스 포함):

 

 

 

 

 

 

Posted by WhiteAT
,