Scilab を使ってLPC1343とシリアル通信 & データ処理 回路&LPC1343準備編
回路+LPC1343のコードの準備です。
回路
回路は以前のエントリ 「LPC1343 ADC データ測定 リアルタイムグラフ表示」と同じで
フォトダイオードを使います。
フォトダイオードの出力をAD0でAD変換します。
変換結果はUSB-シリアル変換 FT232RLを使ってPCへ転送します。
準備物
FT232RL USBシリアル変換モジュール
フォトダイオードS9648-100 秋月電子 120円
このフォトダイオードはアンプも不要で大きな出力が出たのでちょうど良かったです。
コード
Hyper terminal やTera termなどの通信ソフトで
ad0 と LPC1343に送信すると ADコンバート処理をして結果を返すというものです。
以下のエントリをベースに行います。
どのような動きをするものなのかは以下を参照
動作結果、接続図(2012.2.18)
コードの構成および概略の動作は以下エントリを参照。
コード(2012.2.18)
実際のコードは下に残します。
以前のエントリからの差は
main.cでHello worldを削除
uart.cでローカルエコー削除とCRで改行→CR+LFで改行としています。(受信部)
main.c
#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");
}}
}
}
uart.c
CR+LFで改行として使ってます。
#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 == '\n') { // LFでブレイク
// detected end of line
// uart_puts("\r\n"); ローカルエコー削除
break;
}
else if(ch!='\r'){ //CRでバッファ更新なし
// uart_putc(ch); ローカルエコー削除
buf[n++] = ch;
}
}
buf[n] = 0;
return buf;
}
adc0.c
#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=ADC0LPC_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 nowreturn(data);
}
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_ */
adc0.h
#ifndef ADC0_H_
#define ADC0_H_extern void adc0_init(void);
extern int adc0read(void);#endif /* ADC0_H_ */