LPC1343 UARTでADCを制御する 2/2

UARTでADCを制御する!のコードです。

回路構成は前の記事参照

その他、開発記事List参照

今回は少しコード量が多かったので複数ファイル構成にしました。
C言語もよくわかってない自分ですが何とかできました!!
インターネット上で親切に説明していただいているサイトが多くあったので
難しくはなかったです。

コード構成は以下です。
C projectを作成してファイルを追加してコピペで動作するはずです。

main.c : ハイパーターミナルで"ad0"と入力するとADC実行&値を返す
adc0.c : AD0のイニシャライズとAD変換実行
adc0.h
uart.c : UART部分(エレキジャックさんの記事参照)
uart.h


main.c
ベースのコードがあるエントリ:「UART Rx回路を入れてみる」
変更点

  • UARTとADCを別ファイル化
  • ADC制御はadc0_initとadc0readという関数に分けて使用
  • ADC結果をUARTで送信するときにcharに変換しなければならないのでitoa関数を使用
  • itoa関数を使うためにをinclude

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif

#include
#include
#include "adc0.h"
#include "uart.h"

__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

#include
#include    //int-> char変換 itoi
// Main loop
int main(void) {
// Variable declaration
char line[40]; // Line buffer;
char *s; // return value
char p[4]; // AD0 value
int ad0data;

// Initialize UART
uart_init(9600);
uart_puts("HELLO WORLD\r\n");
adc0_init();

// Enter an infinite loop, just echo lines
while (1) {
// Get a line from UART
s = uart_gets(line, sizeof line);
if (s) {
// Put a line into UART if valid
if(!strcmp(s,"ad0")){
ad0data=adc0read();
itoa(ad0data, p, 10);
uart_puts(p);
uart_puts("\r\n");
}
else{
uart_puts("na");
uart_puts("\r\n");
}
}
}
}


adc.c
ベースのコードがあるエントリ:「ADCを使ってみる。」
変更点

  • adc0_init,adc0readの二つの関数に分割

#include "LPC13xx.h" /* LPC13xx Peripheral Registers */
#include "adc0.h"


void adc0_init(void) {
LPC_SYSCON->PDRUNCFG &= ~(0x1<<4); // See data sheet
LPC_SYSCON->SYSAHBCLKCTRL |= (0x1<<13); // See data sheet
LPC_IOCON->JTAG_TDI_PIO0_11 &= ~0x9F; // PIO0_11=Analog input
LPC_IOCON->JTAG_TDI_PIO0_11 |= 0x02; // PIO0_11=ADC0

LPC_ADC->CR |=0x00000001; // ADC0=on
//Clkdiv This time clk=4MHz (max=4.5MHz)
LPC_ADC->CR = ((SystemCoreClock/LPC_SYSCON->SYSAHBCLKDIV)/4000000-1)<<8;

return;
}

int adc0read(void){

int data;

LPC_ADC->CR |=0x01000000; //ADC Start now
while(!(LPC_ADC->DR0 & 0x80000000) );

data=((LPC_ADC->DR0 & 0x0000FFC0)>>6);
LPC_ADC->CR &= 0xF8FFFFFF; //stop ADC now

return(data);
}

adc.h

#ifndef ADC0_H_
#define ADC0_H_

extern void adc0_init(void);
extern int adc0read(void);

#endif /* ADC0_H_ */


uart.c
ベースのコードがあるエントリ:
「UART Rx回路を入れてみる」
「UART Txのプログラムを入れてみる。」

/*
* uart.c
*
* Created on: 2012/02/16
* Author: katsu
*/

#ifdef __USE_CMSIS
#include "LPC13xx.h"
#endif

#include
#include
#include "uart.h"



////
// Flag mask for UART_LSR register.
const uint32_t LSR_THRE = 0x20;
const uint32_t LSR_RDR = 0x01;

// Initialize UART module with specified baud-rate
void uart_init(uint32_t baudrate) {
// Divisor ratio
uint32_t Fdiv;

// Configure P1[7] as TXD output.
LPC_IOCON->PIO1_7 &= ~0x07; // FUNC=000 (GPIO)
LPC_IOCON->PIO1_7 |= 0x01; // FUNC=001 (TXD)

// Configure P1[6] as RXD input.
LPC_IOCON->PIO1_6 &= ~0x07; // FUNC=000 (GPIO)
LPC_IOCON->PIO1_6 |= 0x01; // FUNC=001 (RXD)

// Enable UART clock
LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12); // UART=1

// Enable UART peripheral clock
LPC_SYSCON->UARTCLKDIV = 0x01; // DIV=1

// Calculate baud rate divisor
Fdiv = (
SystemCoreClock // System clock frequency
* LPC_SYSCON->SYSAHBCLKDIV // AHB clock divider
) / (
LPC_SYSCON->UARTCLKDIV // UART clock divider
* 16 * baudrate // baud rate clock
);

// Set the baud rate divisor value
LPC_UART->LCR |= 0x80;
LPC_UART->DLM = Fdiv / 256;
LPC_UART->DLL = Fdiv % 256;
LPC_UART->LCR &= ~0x80;


// Configure UART module as
// 8 bit, 1 stop bit, no parity
// Enable and reset TX and RX FIFO.
LPC_UART->LCR = 0x03;
LPC_UART->FCR = 0x07;
}

// send a character via TXD
void uart_putc(const char c) {
// Wait for TX buffer empty
while (!(LPC_UART->LSR & LSR_THRE));
// Put a character
LPC_UART->THR = c;
}

// send a string via TXD
int uart_puts(const char *s) {
int n;
for (n = 0; *s; s++, n++) {
uart_putc(*s);
}
return n;
}

// receive a character via RXD
int uart_getc(void) {
// Wait for RX buffer valid
while (!(LPC_UART->LSR & LSR_RDR));
return LPC_UART->RBR;
}

// receive a string via RXD
char *uart_gets(char * const buf, const int size) {
int ch;
int n = 0;

while (n < size - 1) {
ch = uart_getc();
if (ch == '\r') {
// detected end of line
uart_puts("\r\n");
break;
}
uart_putc(ch);
buf[n++] = ch;
}
buf[n] = 0;
return buf;
}

uart.h

#ifndef UART_H_
#define UART_H_

extern void uart_init(uint32_t baudrate);
extern void uart_putc(const char c);
extern int uart_puts(const char *s);
extern int uart_getc(void) ;
extern char *uart_gets(char * const buf, const int size);

#endif /* UART_H_ */