검색결과 리스트
글
[Tiva] tm4c 시리즈 MCU로 TextLCD(CLCD) 동작
MCU로 프로그램을 실행할 때 자신이 확인하고 싶은 값을 보기 위해서 주로 UART통신 또는 TextLCD에 값을 띄워보는 방법이 있습니다.
MCU가 컴퓨터와 항상 연결되어 있을 때엔 당연히 UART를 통해 원하는 값을 모니터에 출력하는 것이 가능합니다. 그러나 일반적으로 MCU를 응용하시는 분들께서는 컴퓨터와 연결하여 사용하는 경우는 많지 않으실 겁니다. 이럴 때 가장 좋은 방법이 TextLCD를 활용하는 것이지요.
자신이 원하는 값이 숫자와 같은 수치라면 FND를 사용하는 방법또한 있습니다만 FND의 경우 원하는 값을 얻기애 연결해야 하는 핀의 수가 TextLCD보다 상대적으로 많이 사용되는 편이기도 합니다. 심지어 TextLCD는 텍스트 값 또한 출력이 가능하기 때문에 간단한 수치를 확인하는 목적 이상을 원한다면 TextLCD를 사용하는 쪽이 바람직할 듯 합니다.
가장 많이 사용되는 TextLCD는 2 X 16일 겁니다. 인터넷 상에서도 Atmega128을 기반으로 한 소스코드도 쉽게 구할 수 있어 생각보다 번거롭지 않게 TextLCD에 값을 출력하실 수 있을 것입니다.
아래의 코드는 Atmega128에서 많이 사용되는 코드를 TivaWare를 활용하는 MCU에서 바로 사용할 수 있게 작성되었습니다.
참고로 회로는 다음과 같이 연결하시면 되겠습니다.
RS = A6, RW = GND, EN = A7
데이터0~7 = B0 ~ B7
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #include <stdint.h> #include <stdbool.h> #include "inc/hw_types.h" #include "inc/hw_memmap.h" #include "driverlib/sysctl.h" #include "driverlib/gpio.h" void EnablePulse(void) { GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, GPIO_PIN_7); SysCtlDelay(SysCtlClockGet() / 1200); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_7, 0); } void sendLCDcommand(unsigned char command) { GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, 0); GPIOPinWrite(GPIO_PORTB_BASE, 0xFF, command); EnablePulse(); SysCtlDelay(SysCtlClockGet() / 1200); } void InitLcd(void) { SysCtlDelay(SysCtlClockGet() / 10); sendLCDcommand(0x38); sendLCDcommand(0x0C); sendLCDcommand(0x01); SysCtlDelay(SysCtlClockGet() / 1200); } void sendLCDdata(unsigned char data) { GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6, GPIO_PIN_6); GPIOPinWrite(GPIO_PORTB_BASE, 0xFF, data); EnablePulse(); SysCtlDelay(SysCtlClockGet() / 1200); } void displayStr(char *str){ while(*str){ sendLCDdata(*str); str++; } } void setLocate(int x, int y){ unsigned char addr; if(x==0) addr = 0x80+y; else addr = 0xC0+y; sendLCDcommand(addr); } void setLcdShift(char a){ unsigned char shift; //0 == Left 1 == Right if(a==0) shift = 0x18; else if(a==1) shift = 0x1C; sendLCDcommand(shift); SysCtlDelay(SysCtlClockGet() / 10); } int main(void) { SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB); GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7); GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7); GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_6 | GPIO_PIN_7, 0x00); GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7, 0x00); InitLcd(); setLocate(0,1); displayStr("Hello, World!"); setLocate(1,0); displayStr("http://elecs.tistory.com/"); while (1) { setLcdShift(0); } } |
해당 코드를 런치패드에 실행시키면 CLCD에 다음과 같은 값이 출력됨을 확인하실 수 있습니다.
'임베디드 > TI' 카테고리의 다른 글
| [Tiva] I2CMasterDataGet() (0) | 2014.08.26 |
|---|---|
| [Tiva] I2CMasterControl() (0) | 2014.08.25 |
| [Tiva]startup_ccs.c 파일에 외부 인터럽트 추가하기 (0) | 2014.08.09 |
| [Tiva] I2CMasterSlaveAddrSet() (0) | 2014.08.01 |
| [Tiva] TM4C의 I2C 통신 파형 보는 방법 (4) | 2014.07.28 |
설정
트랙백
댓글
글
URL을 통해 안드로이드로 xml 파일 다운로드
안드로이드 프로그램에서 필요한 자료를 받기 위해서는 URL을 통해 해당 주소에 있는 원하는 자료를 다운로드 받아 사용할 수 있습니다.
특히 RSS와 같이 지속적으로 갱신되는 파일의 경우 실시간으로 새로운 정보를 수신해야 하기 때문에 이를 활용해 xml 파서를 이용하기도 하지요.
만약 용량이 큰 파일을 다운로드할 경우 프로그램이 다운로드를 완전히 마치지 않고 바로 다음 단계로 넘어가는 경우가 있습니다. 다운로드를 완전히 끝내기 위해서 자바의 Thread를 사용하였습니다. Thread의 join() 함수는 Thread가 종료될 때 까지 함수가 선언한 단계에서 다음 단계로 넘어가지 않고 기다립니다.
아래는 rss를 다운로드 하여 String으로 객체화 시킨 예제입니다.
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 | import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; public class MainActivity extends ActionBarActivity { TextView textview1; String xml; URL url; HttpURLConnection conn; InputStreamReader isr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView) findViewById(R.id.textview1); try { rcvXml RX = new rcvXml(); RX.start(); RX.join(); textview1.setText(xml); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } class rcvXml extends Thread { public void run() { try { url = new URL("http://www.kma.go.kr/wid/queryDFS.jsp?gridx=60&gridy=126"); BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream(), "UTF-8")); xml = in.readLine(); while(true){ String temp; temp = in.readLine(); if(temp==null) break; xml += temp; } } catch (Exception e) { } } } } |
'안드로이드 > 애플리케이션 제작' 카테고리의 다른 글
| 안드로이드 SDK 업데이트 후 이클립스에서 실행이 안될 때(Android ADT 재설치) (0) | 2014.09.27 |
|---|---|
| 숨겨진 Activity 혹은 Fragment의 Thread를 종료시키는 방법 (1) | 2014.09.11 |
| Didn't find class "android.support.v4.view.ViewPager" (0) | 2014.08.24 |
| (JSON) 기상청으로부터 지역코드 받아오기 (2) | 2014.08.15 |
| 좌표값 위도와 경도로 위치주소 구하기 (3) | 2014.08.10 |
설정
트랙백
댓글
글
(JSON) 기상청으로부터 지역코드 받아오기
날씨과 관련된 프로그램을 제작하는 분들이라면 기상청에서 제공하는 api가 거의 반드시 필요하실 것이라 생각합니다.
기상청으로부터 자신이 원하는 지역의 날씨를 받아오기 위해서는 기상청에서 제공하는 RSS를 수신하여 이를 가지고 날씨를 알아낼 수 있습니다. 이를 위해서는 기상청에서 제공하는 지역코드를 통해 해당 지역의 날씨를 받아와야 하지요.
기상청에서는 해당 지역코드를 알아내기 위해 다음과 같은 JSON파일을 제공합니다.
mdl.11.json에서 11은 위에서 알아댄 시,도 코드입니다. 해당 코드를 입력하면 해당 시,도의 시군구 목록이 나타납니다.
이는 JSON파일을 읽어들이는데 인코딩 문제로 인해 나타는 경우입니다. 이를 제대로 보기 위해서 오른쪽 버튼을 클릭한 후 인코딩 → UTF-8 로 변경하시면 됩니다.
그럼 이 JSON파일을 사용하여 지역코드를 받아오는 코드를 간단하게 짜보도록 하겠습니다.
※안드로이드에서는 기본적으로 JSON을 활용하는 클래스가 설정되어 있어 간단하게 사용이 가능합니다. 순수 자바로 구현할 경우 별도의 과정이 추가로 필요합니다.
import org.json.JSONArray;
import org.json.JSONObject;
1. 먼저 JSON을 통해 인터넷에 접속하기 위해서는 안드로이드에 인터넷 접속을 허용하는 permission을 다음과 같이 추가합니다.
AndroidMenifest.xml
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 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.framework.json" > <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> | cs |
2. 다음으로 아래의 코드를 삽입해줍니다.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | public class MainActivity extends AppCompatActivity { URL RSSurl = null; rcvJson RJ; URLConnection connect; JSONObject JStoken; String getJSON; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); .... TextView textview = (TextView) findViewById(R.id.textView); try { String Local1 = "서울특별시"; String Local2 = "용산구"; String Local3 = "서빙고동"; String temp; String x="0",y="0"; JSONArray JSA; RSSurl = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/top.json.txt"); RJ = new rcvJson(); RJ.start(); RJ.join(); JSA = new JSONArray(getJSON); temp=""; for(int i=0; i < JSA.length(); i++){ JStoken = JSA.getJSONObject(i); if(JStoken.get(JStoken.names().getString(0)).equals(Local1)){ temp += JStoken.get(JStoken.names().getString(1)); break; } } RSSurl = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/mdl."+temp+".json.txt"); RJ = new rcvJson(); RJ.start(); RJ.join(); JSA = new JSONArray(getJSON); temp=""; for(int i=0; i < JSA.length(); i++){ JStoken = JSA.getJSONObject(i); if(JStoken.get(JStoken.names().getString(0)).equals(Local2)){ temp += JStoken.get(JStoken.names().getString(1)); break; } } RSSurl = new URL("http://www.kma.go.kr/DFSROOT/POINT/DATA/leaf."+temp+".json.txt"); RJ = new rcvJson(); RJ.start(); RJ.join(); JSA = new JSONArray(getJSON); temp=""; for(int i=0; i < JSA.length(); i++){ JStoken = JSA.getJSONObject(i); x = JStoken.get(JStoken.names().getString(2)).toString(); y = JStoken.get(JStoken.names().getString(1)).toString(); if(JStoken.get(JStoken.names().getString(0)).equals(Local3)) break; } textview.setText(x + ","+ y); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 | class rcvJson extends Thread { public void run() { try { BufferedReader in = new BufferedReader(new InputStreamReader( RSSurl.openStream(), "UTF-8")); getJSON = in.readLine(); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } |
위의 소스코드를 실행하면 아래와 같은 결과를 확인하실 수 있습니다.
'안드로이드 > 애플리케이션 제작' 카테고리의 다른 글
| 안드로이드 SDK 업데이트 후 이클립스에서 실행이 안될 때(Android ADT 재설치) (0) | 2014.09.27 |
|---|---|
| 숨겨진 Activity 혹은 Fragment의 Thread를 종료시키는 방법 (1) | 2014.09.11 |
| Didn't find class "android.support.v4.view.ViewPager" (0) | 2014.08.24 |
| URL을 통해 안드로이드로 xml 파일 다운로드 (0) | 2014.08.17 |
| 좌표값 위도와 경도로 위치주소 구하기 (3) | 2014.08.10 |