組込みとともに -13ページ目

Twiame (Qtouch 3歩目~ソフトウェアとりあえず~)


前回の日記『Qtouch2.5歩目~回路図更新~

今回からプロジェクト名をTwiame(ついあめ)としました^^

Twitterでの関連情報には#twiameタグを使用します!


今回はいよいよソフトウェアに入りますが、今回はサンプルプログラムをベースに設計した回路にあうように微調整するところまでです。

ポイントは、

・タッチセンサーを1つに変更

・デバッグ出力はRS232C(38400bps(仮))

・IO初期設定など


次回以降でチャレンジするところは

・WIZ812MJ

・PING(通信テスト)

・Twitterへupdate

てな感じです。


マイコンはATMEGA88を使用することにしましたので参考プログラムは『avr4g2_8qt_example』となります。

Twiameプロジェクトですので名称をtwiameに統一するなどしました。

メインプログラム(twiame.c)は下記の通りです

-----

/*----------------------------------------------------------------------------
 compiler information 
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
  include files
----------------------------------------------------------------------------*/
#include "twiame.h" <-名称変更
/* Compiler differences. (Will be automatically selected based on compiler being used) */
#if defined(__GNUC__)
    #include <avr/io.h>
    #include <avr/interrupt.h>
    #define INTERRUPT_SERVICE_ROUTINE(name, vect) ISR(vect)
    #define __delay_cycles(n)     __builtin_avr_delay_cycles(n)
    #define __enable_interrupt()  sei()
#endif
/*----------------------------------------------------------------------------
  manifest constants
----------------------------------------------------------------------------*/
/* Board ID has to be defined here. This has to be defined by the user.*/
#define EVK2080A 0u
#define BOARD_ID EVK2080A
/* Debug port and pins to be defined here. This has to be enabled and defined by the user.*/
#if defined(_DEBUG_INTERFACE_)
    // UART
    #define BPS_38400H (uint8_t)0  /* 16MHz / (16 * 38400) - 1 => 25 */
    #define BPS_38400L (uint8_t)25 /* 16MHz / (16 * 38400) - 1 => 25 */
#endif /*_DEBUG_INTERFACE_*/
/*----------------------------------------------------------------------------
  type definitions
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
  prototypes
----------------------------------------------------------------------------*/
/* initialise host app, pins, watchdog, etc */
static void init_system( void );
/* configure timer ISR to fire regularly */
static void init_timer_isr( void );
void qt_init_globals( void );
extern int16_t qt_get_sensor_delta( uint8_t );
#if defined(_DEBUG_INTERFACE_)
/* debug functions */
void report_debug_data( void );
void output_to_debugger( const uint8_t *p, uint8_t count );
void send_debug_byte( uint8_t data );
void init_debug_if( void );
#endif /*_DEBUG_INTERFACE_*/
/*----------------------------------------------------------------------------
  macros
----------------------------------------------------------------------------*/
/* utility macro for expanding port registers */
#define JOIN( x, y ) x ## y
/*
* Macro to build register writes for controlling ports. The intermediate
* JOIN macro is required for correct expansion of the args.
*/
#define REG( REGISTER, SIDE ) JOIN( REGISTER, SIDE )
/*----------------------------------------------------------------------------
  global variables
----------------------------------------------------------------------------*/
extern qt_touch_lib_config_data_t qt_config_data;
extern qt_touch_lib_measure_data_t qt_measure_data;
uint8_t sensor_config[QT_NUM_CHANNELS];
board_info_t board_info;
/*----------------------------------------------------------------------------
  static variables
----------------------------------------------------------------------------*/
/* flag set by timer ISR when it's time to measure touch */
static volatile uint8_t time_to_measure_touch = 0u;
/* current time, set by timer ISR */
static volatile uint16_t current_time_ms_touch = 0u;
/*============================================================================
Name : main
------------------------------------------------------------------------------
Purpose: main code entry point
Input  : n/a
Output : n/a
Notes  :
============================================================================*/
int main( void )
{
    /* initialise host app, pins, watchdog, etc */
    init_system();
/* void qt_enable_key( channel_t channel,
                       aks_group_t aks_group,
                       threshold_t detect_threshold,
                       hysteresis_t detect_hysteresis );
   The parameter are as follows
      * channel           : タッチセンサーのチャンネル
      * aks_group         : AKSグループ
      * detect_threshold  : 検知閾値
      * detect_hysteresis : オンオフヒステリシス
      
   qt_touch_status..sensor_status[]にて最新のキーの状態(ON/OFF)を確認できる
 */
    qt_enable_key( CHANNEL_3, AKS_GROUP_1, 10u, HYST_6_25 );
    /* configure the debug data reported to the PC */
    sensor_config[0] = SENSOR_CONFIG( CHANNEL_3, CHANNEL_3, SENSOR_TYPE_KEY );
    /* initialise touch sensing */
    qt_init_sensing();
    /* pass channel configuration parameters to global variable. */
    qt_init_globals( );
    /* configure timer ISR to fire regularly */
    init_timer_isr();
    /* configure the debug data reported to the PC */
    board_info.qt_max_num_rotors_sliders_board_id = ( ( QT_MAX_NUM_ROTORS_SLIDERS << 4 ) | BOARD_ID );
    board_info.qt_num_channels = QT_NUM_CHANNELS;
    /* enable interrupts */
    __enable_interrupt();
    /* loop forever */
    for( ; ; )
    {
        if ( time_to_measure_touch )
        {
            /* clear flag: it's time to measure touch */
            time_to_measure_touch = 0u;
            /* measure touch sensors */
            qt_measure_sensors( current_time_ms_touch );
#if defined(_DEBUG_INTERFACE_)
            /* report debug data to host */
            report_debug_data();
#endif /*_DEBUG_INTERFACE_*/
        }
    /* host application code goes here */
    }
}
/*============================================================================
  Name : qt_init_globals
------------------------------------------------------------------------------
Purpose: initialize globals variables
Input  : n/a
Output : n/a
Notes  : initialize configuration data for processing
============================================================================*/
void qt_init_globals( void )
{
    /* reset global parameters */
    qt_config_data.qt_di              = DEF_QT_DI;
    qt_config_data.qt_neg_drift_rate  = DEF_QT_NEG_DRIFT_RATE;
    qt_config_data.qt_pos_drift_rate  = DEF_QT_POS_DRIFT_RATE;
    qt_config_data.qt_max_on_duration = DEF_QT_MAX_ON_DURATION;
    qt_config_data.qt_drift_hold_time = DEF_QT_DRIFT_HOLD_TIME;
    qt_config_data.qt_recal_threshold = DEF_QT_RECAL_THRESHOLD;
}
/*============================================================================
  Name : init_timer_isr
------------------------------------------------------------------------------
Purpose: configure timer ISR to fire regularly
Input  : n/a
Output : n/a
Notes  :
============================================================================*/
void init_timer_isr( void )
{
    /* set timer compare value (how often timer ISR will fire) */
    OCR1A = ( TICKS_PER_MS * MEASUREMENT_PERIOD_MS );
    /* enable timer ISR */
    TIMSK1 |= (1u << OCIE1A);
    /* timer prescaler = system clock / 8 */
    TCCR1B |= (1u << CS11);
    /* timer mode = CTC (count up to compare value, then reset) */
    TCCR1B |= (1u << WGM12);
}
/*============================================================================
  Name : init_system
------------------------------------------------------------------------------
Purpose: initialise host app, pins, watchdog, etc
Input  : n/a
Output : n/a
Notes  :
============================================================================*/
void init_system( void )
{
    /* disable pull-ups */
    MCUCR |= (1u << PUD);
    
/* 
 * Port B
     bit0: NC     -> output (DDR=1, DR=0)
     bit1: QT_LED -> output (DDR=1, DR=1)
     bit2: /SCS   -> output (DDR=1, DR=1)
     bit3: MOSI   -> output (DDR=1, DR=1)
     bit4: MISO   -> inoput (DDR=1, DR=1)
     bit5: SCK    -> output (DDR=1, DR=1)
     bit6: XTAL1  -> input  (DDR=0, DR=0)
     bit7: XTAL2  -> input  (DDR=0, DR=0)
 * Port C
     bit0: NC     -> output (DDR=1, DR=0)
     bit1: NC     -> output (DDR=1, DR=0)
     bit2: NC     -> output (DDR=1, DR=0)
     bit3: NC     -> output (DDR=1, DR=0)
     bit4: NC     -> output (DDR=1, DR=0)
     bit5: NC     -> output (DDR=1, DR=0)
     bit6: RES    -> input  (DDR=0, DR=0)
     bit7: ---
 * Port D
     bit0: RXD    -> input  (DDR=0, DR=0)
     bit1: TXD    -> output (DDR=1, DR=1)
     bit2: NC     -> output (DDR=1, DR=0)
     bit3: NC     -> output (DDR=1, DR=0)
     bit4: NC     -> output (DDR=1, DR=0)
     bit5: NC     -> output (DDR=1, DR=0)
     bit6: SNSK   -> output (DDR=1, DR=0)
     bit7: SNS    -> output (DDR=1, DR=0)
 */
    PORTB = 0x3e;
    DDRB  = 0x3f;
    PORTC = 0x00;
    DDRC  = 0x7f;
    PORTD = 0x02;
    DDRD  = 0xfe;
#if defined(_DEBUG_INTERFACE_)    
    /* inilize debug ports */
    init_debug_if( );
#endif
}
/*============================================================================
  Name : timer_isr
------------------------------------------------------------------------------
Purpose: timer 1 compare ISR
Input  : n/a
Output : n/a
Notes  :
============================================================================*/
INTERRUPT_SERVICE_ROUTINE(timer_isr, TIMER1_COMPA_vect)
{
    /* set flag: it's time to measure touch */
    time_to_measure_touch = 1u;
    /* update the current time */
    current_time_ms_touch += MEASUREMENT_PERIOD_MS;
}
#if defined(_DEBUG_INTERFACE_)
/*============================================================================
  Name : report_debug_data
------------------------------------------------------------------------------
Purpose: report debug data to host
Input  : n/a
Output : n/a
Notes  : n/a
============================================================================*/
void report_debug_data( void )
{
    uint8_t i;
    int16_t sensor_delta;
    output_to_debugger( (uint8_t *) &board_info, (uint8_t) sizeof( board_info ) );
    output_to_debugger( (uint8_t *) qt_measure_data.channel_signals, (uint8_t) sizeof( qt_measure_data.channel_signals ) );
    output_to_debugger( (uint8_t *) qt_measure_data.channel_references, (uint8_t) sizeof( qt_measure_data.channel_references ) );
    for (i = 0u; i < QT_NUM_CHANNELS; i++ )
    {
        sensor_delta = qt_get_sensor_delta( i );
        output_to_debugger( (uint8_t *) &sensor_delta, sizeof( int16_t ) );
    }
    output_to_debugger( (uint8_t *) &qt_measure_data.qt_touch_status, (uint8_t) sizeof( qt_measure_data.qt_touch_status ) );
    output_to_debugger( (uint8_t *) sensor_config, (uint8_t) sizeof( sensor_config ) );
}
/*============================================================================
  Name : output_to_debugger
------------------------------------------------------------------------------
Purpose: transmit multiple bytes over the debug interface
Input  : p = ptr to bytes to transmit
         count = number of bytes to transmit
Output : n/a
Notes  :
============================================================================*/
void output_to_debugger( const uint8_t *p, uint8_t count )
{
    uint8_t i;
    uint8_t data;
    for (i = 0u; i < count; i++ )
    {
        /* get next byte to transmit */
        data = *p;
        /* transmit a byte over the debug interface */
        send_debug_byte( data );
        /* point to next byte to transmit */
        p++;
    }
}
/*============================================================================
  Name : init_debug_if
------------------------------------------------------------------------------
Purpose: report debug data to host
Input  : n/a
Output : n/a
Notes  : n/a
============================================================================*/
void init_debug_if( void )
{
 /* UART */
    UBRR0H = BPS_38400H;
    UBRR0L = BPS_38400L; /* 38400bps */
    UCSR0B = 0x08; /* 受信禁止 送信許可 */
}
/*============================================================================
  Name : send_debug_byte
------------------------------------------------------------------------------
Purpose: transmit a byte over the debug interface
Input  : data = byte to be transmitted
Output : n/a
Notes  :
============================================================================*/
void send_debug_byte( uint8_t data ) <-UART送信に変更
{
    UDR0 = (uint8_t)data; 
    while( bit_is_set(UCSR0A, UDRE0 ) == 0);  /* 送信可能状態となるまで待機 */
}
#endif /*_DEBUG_INTERFACE_*/

-----

twiame.cの変更部分の詳細は元ファイル『main_atmega88.c』と差分取ればわかります!


twiame.h (元ファイル名touch_api_atmega88.h)の変更点は

・QT_NUM_CHANNELS 8→1

・SNSK B→D


回路図を一部変更しました

・RXD, TXDにプルアップ抵抗を追加しました(回路図中付け忘れ)

組込みとともに-ついあめソース

ちなみにまだ製作していませんのでこの状態で動くかどうか不明です^^;


つづく

組込みエンジニア集まれ!



アメブロに『組み込みエンジニア』グルッぽ作りました!!!
いわゆる組込みエンジニアのためのコミュニティです。

現在数名です
今現在定員は300名
今がチャンス!!
組込みエンジニアもしくは組込みに興味のある方にもっともっと集まって集まってください!!!

気楽に楽しく意見交換できるような場ができればと思います(^-^)

みなさんよろしくお願いします!

英語力が上達しない訳

(いろんなところでいろんな意見がありますが一意見と受け止めてください)

英語力を上達したいと思ってる方はおおいですよねー

でも中学から初めて4大卒業まで10年習ってそれなりに単位をとったはずなんですが、何故か喋れない、聴けない、書けない
唯一読みはなんとかなりますが、時間がかかる。たまにだけど大事な事を間違って解釈してしまう・・・


なぜでしょうか?

例えば幼稚園児・保育園児の未就学児は習ったわけではないのにそれなりに日本語を話せます。
逆に読み書きは苦手です。

日本の英語教育は読み書き中心でリスニングをおまけ的に
高度なレベルに達するとようやくスピーチが入ります。

日本の英語教育を例えるとまだ喋れない入園前の子に日本語を文法から教えているようなものです。
おかしくありませんか?

会話ができると自然に英語のフレーズが肌感覚でわかる。会話の基本は反射的に(←考えずに)言葉がでることだと思います。読み書きするときは反射的に書いてるのではなく、頭の中で話をしています。つまり話せない言語をどうして読み書きできるのでしょうか?
(今の教育はそうなってるかもしれませんが)

で英会話の上達方法は英語のフレーズを日常的によく耳にすることと、一杯話をすること。
とりあえず聴かなきゃ始まらないの(聴けるようになるまで)でニュースか何か(もちろん英語の)ポッドキャストを通勤時に聞くとか、YouTubeを鑑賞するとかで何とかなるかな。1年続いたら大きな成果でると思います。

話す方はそうは簡単にできませんが。心配は入りません。聞けるようになってから考えても十分です。

『ローマの道も一歩から』先ずは英語をたくさん聴こう!!