2024年5月25日土曜日

pic family/pic16f1938 and aqm1602

 

---------pic family--------------------------

16F876A       mpasm only  :: japanese  datasheet!  スペックは1827超え!
                           cf https://ww1.microchip.com/downloads/jp/devicedoc/30292a_jp.pdf 

16F84A        mpasm only :: morikita 

16F648          mpasm only :: morikita
 
12F1822       c without mcc :: japanese datasheet!
       cf https://ww1.microchip.com/downloads/jp/DeviceDoc/41413C_JP.pdf

12LF1840    これも一緒でサイト情報豊富


16F1827x2     c without mcc ::  mosinyaga 

16F1938       c without mcc :: japanse datasheet わりとサイト情報豊富
    cf https://ww1.microchip.com/downloads/jp/DeviceDoc/41574A_JP.pdf


16F1778       c with mcc :: 逆引き

16F18857     c with mcc :: 大全

16F18313      c with mcc :: 8ピン



---------------------------------

// https://wak-tech.com/archives/292 tmr1 pic16f1938 using original aqm1602 lib 

      // CONFIG1
                #pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
                #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
                #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
                #pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
                #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
                #pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
                #pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
                #pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
                #pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
                #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

                // CONFIG2
                #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
                #pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable (All VCAP pin functionality is disabled)
                #pragma config PLLEN = ON// PLL Enable (4x PLL disabled)
                #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
                #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
                #pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

                // #pragma config statements should precede project file includes.
                // Use project enums instead of #define for ON and OFF.

                #include <xc.h>
                #include <pic16f1938.h>
                #include <math.h>
                #include <stdio.h>
                #define _XTAL_FREQ 32000000
                #define LCD_ADD 0x7C
                #define sound RA1
                int timer = 0;


                void I2C_Master_Init(const unsigned long c)
                {
                  SSPCON1 = 0b00101000;
                  SSPCON2 = 0;
                  SSPADD = (_XTAL_FREQ/(4*c))-1;
                  SSPSTAT = 0b00000000 ;    // 標準速度モードに設定する(100kHz)
                }

                void I2C_Master_Wait()
                {
                  while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
                }

                void I2C_Master_Start()
                {
                  I2C_Master_Wait();
                  SEN = 1;
                }

                void I2C_Master_RepeatedStart()
                {
                  I2C_Master_Wait();
                  RSEN = 1;
                }

                void I2C_Master_Stop()
                {
                  I2C_Master_Wait();
                  PEN = 1;
                }

                void I2C_Master_Write(unsigned d)
                {
                  I2C_Master_Wait();
                  SSPBUF = d;
                }
                void writeData(char t_data){
                    I2C_Master_Start();
                    I2C_Master_Write(LCD_ADD);
                    I2C_Master_Write(0x40);
                    I2C_Master_Write(t_data);
                    I2C_Master_Stop();
                    __delay_ms(10);
                }
                void writeCommand(char t_command){
                    I2C_Master_Start();
                    I2C_Master_Write(LCD_ADD);
                    I2C_Master_Write(0x00);
                    I2C_Master_Write(t_command);
                    I2C_Master_Stop();
                    __delay_ms(10);
                }
                void PICinit(){
                  OSCCON = 0b01110000;
                  ANSELA = 0b00000000;
                  ANSELB = 0b00000000;
                  TRISA  = 0b00000000;
                  TRISB  = 0b00000000;
                  TRISC  = 0b00011000;
                  PORTA  = 0b00000000;    //2進数で書いた場合
                  PORTB  = 0x00;          //16進数で書いた場合
                }
                void LCD_Init(){            //LCDの初期化
                  I2C_Master_Init(100000);
                  __delay_ms(400);
                  writeCommand(0x38);
                  __delay_ms(20);
                  writeCommand(0x39);
                  __delay_ms(20);
                  writeCommand(0x14);
                  __delay_ms(20);
                  writeCommand(0x73);
                  __delay_ms(20);
                  writeCommand(0x52);
                  __delay_ms(20);
                  writeCommand(0x6C);
                  __delay_ms(250);
                  writeCommand(0x38);
                  __delay_ms(20);
                  writeCommand(0x01);
                  __delay_ms(20);
                  writeCommand(0x0C);
                  __delay_ms(20);
                }

                void LCD_str(char *c) {     //LCDに配列の文字を表示
                  unsigned char i,wk;
                  for (i=0 ; ; i++) {
                    wk = c[i];
                    if  (wk == 0x00) {break;}
                    writeData(wk);
                  }
                }
            void __interrupt() isr(){               //割り込み関数
                volatile static int intr_counter;
                GIE = 0;
                if(TMR1IF == 1){
                  TMR1H = (55536 >>8);            //タイマー1の初期化(65536-10000=55536);
                  TMR1L = (55536 & 0x00ff);

                  intr_counter++;
                  if(intr_counter == 100){        //1sec周期でtimerに1を足す
                    timer++;
                    intr_counter = 0;
                  }
                  TMR1IF = 0;                     //割り込みフラグを落とす
                }
                GIE = 1;
            }
            void intrInit(){                    //割り込みの初期設定
                T1CON = 0b00110001;             //内部クロックの4分の1で,プリスケーラ1:8でカウント
                TMR1H = (55536 >>8);            //タイマー1の初期化(65536-10000=55536);
                TMR1L = (55536 & 0x00ff);
                TMR1IF = 0;                     //タイマー1割り込みフラグを0にする
                TMR1IE = 1;                     //タイマー1割り込みを許可する
                INTCONbits.GIE = 1;              //グローバル割り込みを許可
                INTCONbits.PEIE = 1;             //割り込みを許可

            }
                int main(void){
                  PICinit();      //PICを初期化
                  LCD_Init();
                  intrInit();
                  char time[16];

                  while(1){
                      sprintf(time,"Timer:%d",timer);
                      writeCommand(0x02);
                      LCD_str(time);
                  }
                }
    

0 件のコメント:

コメントを投稿