MPLAB X IDE 6.2/XC8(v2.00)/MCC ClassicからJH7UBC畠さんのプログラムを基にしてPIC16F1827によるロータリーエンコーダ動作テスト。
参考サイト
●PIC16F88 ロータリーエンコーダ その1
●PIC16F1705 MCC AQM0802A表示テスト
回路図
PIC16F1827ブレットボード用基板
基板にピンヘッダーハンダ後、絶縁座を端子先端に移動してICソケット足を曲げてピンヘッダーと曲げた足にハンダを流し、導通チェックしてからピンヘッダー根本に絶縁座を戻しました。
※ブレッドボード供給5V・GNDピンヘッダーピンを追加
AQM0802A
I2C接続の小型LCD搭載ボード(5V版)
.png)
※AQM0802AのコードがXC8(v3.10)だとコンパイルエラーになるので、バージョンを合わせてXC8(v2.00)に変更。
回路的にはプルアップ抵抗10KΩが入っているが、SDA SCL にプルアップ抵抗10KΩを入れないと表示しない。
ロータリーエンコーダ
プルアップ抵抗付ロータリーエンコーダ
操作性から
ピンヘッダー(直角)→ ピンヘッダー(ストレート)入替
裏面に絶縁テープ貼付
MPLAB X IDE 6.20インストール
●MPLAB ®開発エコシステム ダウンロード アーカイブ
【MPLAB X v6.20】ダウンロード
画面に従ってインストールを行う
.png)
※AQM0802AのコードがXC8(v3.10)だとコンパイルエラーになるので、バージョンを合わせてXC8(v2.00)に変更。
●XC8(v2.00)ダウンロード:MPLAB ® XC8 コンパイラ アーカイブ【https://www.microchip.com/en-us/tools-resources/archives/mplab-ecosystem#xc8】
Project Name
Project Name:任意
※Encoding修正【Tools】【Options】【Enbeded 】【 Default Charset】【Shift JIS】
MCC Classic
MCC Classicに切替OSC
I2C
MSSP1
Interrupt Module
Pin Manager
Generate
printf関数→C90
main.c
#include "mcc_generated_files/mcc.h"
#include "mcc_generated_files/examples/i2c1_master_example.h"
#define ECA RB5
#define ECB RB6
/*ロータリーエンコーダ関係定義*/
unsigned char curDat;
unsigned char befDat = 0;
unsigned char rotDir = 0;
int Count = 0;
unsigned char inputMatch;
unsigned char matchCnt;
unsigned char rotPat = 0;
/*エンコーダの回転方向判定*/
signed char CheckEnc(unsigned dat){
rotPat <<=2; //左に2bitシフト
rotPat |= (dat & 0x03);//datの下位2bitを加える
if(rotPat == 0x4B){
return 1; //時計回り(CW))
}else if(rotPat == 0x87){
return -1; //反時計回り(CCW)
}else{
return 0; //どちらでもない
}
}
#define I2CLCD_AQM0802A 0x3e
//-------- send character ------------------------
void LCD_dat(uint8_t chr)
{
I2C1_Write1ByteRegister(I2CLCD_AQM0802A, 0x40, chr);
__delay_us(30); // 30us
}
//-------- send command -------------------------
void LCD_cmd(uint8_t cmd)
{
I2C1_Write1ByteRegister(I2CLCD_AQM0802A, 0x00, cmd);
if(cmd & 0xFC) // bit6 = 1
__delay_us(30); // 30us
else
__delay_ms(2); // 2ms Clear or Home
}
//-------- clear LCD--------------------------
void LCD_clr(){
LCD_cmd(0x01);
}
//--------- Home -----------------------------
void LCD_home(){
LCD_cmd(0x02);
}
//--------- Cursor X,Y -----------------------
void LCD_cursor(uint8_t x,uint8_t y){
if (y == 0)
LCD_cmd(0x80 + x);
if (y == 1)
LCD_cmd(0xc0 + x);
}
//-------- write 1 character to LCD ----------------
void putch(uint8_t ch){
LCD_dat(ch);
}
//-------- LCD initialize ---------------------------
void LCD_init(){
__delay_ms(40); //40ms wait
LCD_cmd(0x38); //8bit,2line
LCD_cmd(0x39); //IS=1 : extention mode set
LCD_cmd(0x14); //Internal OSC Frequency
LCD_cmd(0x77); //Contrast set
LCD_cmd(0x5D); //Power/ICON/Contrast Control
LCD_cmd(0x6C); //Follower control
__delay_ms(200);//200ms wait
LCD_cmd(0x38); //IS=0 : extention mode cancel
LCD_cmd(0x0C); //Display ON
LCD_cmd(0x01); //Clear Display
__delay_ms(2); //wait more than 1.08ms
}
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
__delay_ms(500);
LCD_init();
LCD_cursor(0,0);
printf("R.Encode");
/*ロータリーエンコーダ初期値設定*/
curDat = 0;
if(ECA){
befDat |= 2;
}
if(ECB){
befDat |= 1;
}
while (1)
{
curDat = 0;
if(ECA){
curDat |= 2;
}
if(ECB){
curDat |= 1;
}
if(befDat == curDat){
//befDatとcurDatが一致したときの処理
if(!inputMatch){
matchCnt++;
if(matchCnt >=2){ //2回以上一致したらフラッグを立てる
inputMatch = 1;
int val = CheckEnc(curDat);
if(val != 0){
LCD_cursor(2,1);
Count += val;
printf("%6d",Count);
}
}
}
}else{
//befDatとcuDatが一致しなかった時の処理
__delay_ms(1);//1ms待つ
befDat = curDat;
matchCnt = 0;
inputMatch = 0;
}
}
}

.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
.png)
0 件のコメント:
コメントを投稿