1 /* 
  2  * File:   main.c
  3  * Author: test
  4  *
  5  * Created on 2017/08/07, 19:16
  6  */
  7 
  8 /*
  9  * Config 設定上の注意
 10  * ICD3によるデバッグを行う場合、WDTは無効にする必要がある。
 11  * ICD3によるデバッグを行う場合、LVPは無効にする必要がある。
 12  */
 13 
 14 // PIC16F1827 Configuration Bit Settings
 15 // 'C' source line config statements
 16 // CONFIG1
 17 #pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
 18 #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
 19 #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
 20 #pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
 21 #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
 22 #pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
 23 #pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
 24 #pragma config CLKOUTEN = ON    // Clock Out Enable (CLKOUT function is enabled on the CLKOUT pin)
 25 #pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
 26 #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
 27 
 28 // CONFIG2
 29 #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
 30 #pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
 31 #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
 32 #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
 33 #pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
 34 
 35 // #pragma config statements should precede project file includes.
 36 // Use project enums instead of #define for ON and OFF.
 37 
 38 #include <xc.h>
 39 
 40 #include <stdio.h>
 41 #include <stdlib.h>
 42 
 43 #define TESTPULSE(aportno) PORTA = 1 << aportno; PORTA = 0
 44 #define LOGMAX 9
 45 
 46 int a;
 47 char pir1[LOGMAX];
 48 char ssp1add[LOGMAX];
 49 char ssp1buf[LOGMAX];
 50 char ssp1con1[LOGMAX];
 51 char ssp1con2[LOGMAX];
 52 char ssp1con3[LOGMAX];
 53 char ssp1stat[LOGMAX];
 54 
 55 int sleep;
 56 
 57 char senddata[3] = {0xd0,0xd1,0xd2};
 58 int senddata_index = 0;
 59 
 60 void ra1onoff(int n){
 61     for (int i=0; i < n;i++){
 62         PORTAbits.RA1 = 1;
 63         PORTAbits.RA1 = 0;
 64     }
 65     for (int i=0; i < n;i++){
 66         PORTAbits.RA1 = 0;
 67         PORTAbits.RA1 = 0;
 68     }
 69 }
 70 
 71 void interrupt intservice(){
 72     TESTPULSE(0);         
 73 
 74     INTCONbits.GIE = 0;
 75     asm("MOVF PORTA,W");
 76 
 77     //registers log
 78     pir1[a] = PIR1;
 79     PIR1 = 0x00;
 80     ssp1add[a] = SSP1ADD;
 81     ssp1buf[a] = SSP1BUF;
 82     ssp1con1[a] = SSP1CON1;
 83     ssp1con2[a] = SSP1CON2;
 84     ssp1con3[a] = SSP1CON3;
 85     ssp1stat[a] = SSP1STAT;
 86     if (++a >= LOGMAX) {
 87         a = 0;
 88     }
 89 
 90     if (SSP1STATbits.P == 1 ) {
 91         sleep = 1;    //stop bit検出でsleepさせる
 92     } else {
 93 //        sleep = 0;  //stop bit 以外の時はsleepさせない
 94         sleep = 1;    //常にsleepさせる
 95     }
 96     
 97     TESTPULSE(1);
 98 
 99     //送信データシーケンス制御
100     if (SSP1STATbits.R_nW == 1) {
101         TESTPULSE(2); 
102         if (SSP1STATbits.D_nA == 0) {
103             senddata_index = 0;
104         } else {
105             senddata_index++;
106         }
107         SSP1BUF = senddata[senddata_index]; //次の送信データセット
108     }
109 
110     TESTPULSE(2);
111     
112     SSP1CON1bits.CKP = 1;   //clock enable
113     INTCONbits.GIE = 1;
114 }
115 
116 /*
117  * 
118  */
119 int main(int argc, char** argv) {
120     
121     INTCON = 0x00;
122     PIR1 = 0x00;
123     PIR2 = 0x00;
124     PIR3 = 0x00;
125     PIR4 = 0x00;
126     T1CON = 0x00;
127     T1GCON = 0x00;
128     T2CON = 0x00;
129     CPSCON0 = 0x00;
130     CPSCON1 = 0x00;
131     PIE1 = 0x00;
132     PIE2 = 0x00;
133     PIE3 = 0x00;
134     PIE4 = 0x00;
135     OPTION_REG = 0x7f;  //b7:WPUEN : pull-up 個別指定
136     WDTCON = 0x16;
137     OSCCON = 0x78;  //Internal Oscillator Frequency 16MHz
138     
139     TRISA = 0x20;
140     TRISB = 0xC0;
141     PORTA = 0x00;
142     PORTB = 0x00;
143     
144     WPUA = 0x20;
145     WPUB = 0xff;
146     
147     ANSELA = 0x00;
148     ANSELB = 0x00;
149     
150     FVRCON = 0x00;
151     ADCON0 = 0x00;
152     ADCON1 = 0x00;
153     
154     CM1CON0 = 0x00;
155     CM2CON0 = 0x00;
156     
157     DACCON0 = 0x00;
158     
159     a = 0;
160     sleep = 1;
161 
162 /*
163  * I2C用設定
164  * SDA1(RB1:7pin)とSCK1(PB4:10pin)を使用する 
165  */
166 
167     TRISB = 0x12;       //SDA1(RB1:7pin)とSCK1(PB4:10pin) 入力設定
168     SSP1ADD = 0xc0;     //I2C address 0x60
169     SSP1CON1 = 0x3e;    //SSPxEN CPK I2C Slave mode, 7-bit address with Start and Stop bit interrupts enabled
170 //    SSP1CON1 = 0x36;    //SSPxEN CPK I2C Slave mode, 7-bit address
171     INTCON = 0x00;
172     INTCONbits.GIE = 1;
173     INTCONbits.PEIE = 1;
174     INTCONbits.INTE = 0;
175     
176     PIE1bits.SSP1IE = 1;
177     
178     PIR1 = 0x00;
179     SSP1CON2bits.SEN = 1;   //クロックストレッチ実施指定
180     
181    ra1onoff(3);
182     while (1) {
183         TESTPULSE(3);
184         if (sleep == 1) {
185             SLEEP();
186         }
187         NOP();
188         TESTPULSE(4);
189     }
190     
191     return (EXIT_SUCCESS);
192 }