Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: blink01.c
- * Author: ryota
- *
- * Created on 2018/05/10, 0:33
- */
- /*
- * サンプル この下から
- */
- // PIC18F4553 Configuration Bit Settings
- // 'C' source line config statements
- // CONFIG1L
- #pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
- #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
- #pragma config USBDIV = 2 // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes from the 96 MHz PLL divided by 2)
- // CONFIG1H
- #pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
- #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
- #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
- // CONFIG2L
- #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
- #pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
- #pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)
- #pragma config VREGEN = ON // USB Voltage Regulator Enable bit (USB voltage regulator enabled)
- // CONFIG2H
- #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
- #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
- // CONFIG3H
- #pragma config CCP2MX = OFF // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
- #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
- #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
- #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
- // CONFIG4L
- #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
- #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
- #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
- #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
- // CONFIG5L
- #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
- #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
- #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
- #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
- // CONFIG5H
- #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
- #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
- // CONFIG6L
- #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
- #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
- #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
- #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
- // CONFIG6H
- #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
- #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
- #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
- // CONFIG7L
- #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
- #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
- #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
- #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
- // CONFIG7H
- #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
- // #pragma config statements should precede project file includes.
- // Use project enums instead of #define for ON and OFF.
- // * サンプル この上まで
- #include <p18f4553.h>
- #include<string.h>
- #define _XTAL_FREQ 48000000 //for __delay_ms,__delay_us
- #define SW1 PORTEbits.RE0 //not use
- #define SW2 PORTEbits.RE1 //not use
- #define LED PORTEbits.RE2
- #define MAT 8
- void OUTB(int i)
- {
- if(i==0) LATB=0b10000010;
- else if (i==1) LATB=0b10110111;
- else if (i==2) LATB=0b11000001;
- else if (i==3) LATB=0b10010001;
- else if (i==4) LATB=0b10110100;
- else if (i==5) LATB=0b10011000;
- else if (i==6) LATB=0b10001000;
- else if (i==7) LATB=0b10110011;
- else if (i==8) LATB=0b10000000;
- else if (i==9) LATB=0b10010000;
- else LATB=0b11111111;
- }
- void OUTD(int i)
- {
- if(i==0) LATD=0b00010100;
- else if (i==1) LATD=0b01110111;
- else if (i==2) LATD=0b00111000;
- else if (i==3) LATD=0b00110001;
- else if (i==4) LATD=0b01010011;
- else if (i==5) LATD=0b10010001;
- else if (i==6) LATD=0b10010000;
- else if (i==7) LATD=0b00110111;
- else if (i==8) LATD=0b00010000;
- else if (i==9) LATD=0b00010001;
- else LATD=0b11111111;
- }
- void init(void)
- {
- ADCON1 = 0b00001110;
- ADCON0 = 0b00000000;
- ADCON2 = 0b00000100;
- ADCON0 = 0b00000001;
- TRISA = 0b00001001;
- TRISB = 0b00000000;
- TRISC = 0b00110000; //RC4=USBD- & RC5=USBD+ IN
- TRISD = 0b00000000;
- TRISE = 0b00000011; //RE0=SW1 & RE1=SW2 IN, RE2=LED OUT
- LATA = 0b00000000;
- LATB = 0b00000000;
- LATC = 0b00000000;
- LATD = 0b00000000;
- LATE = 0b00000000;
- }
- #define DeltaT 100
- #define AN0 PORTAbits.RA0
- #define PhotoOFF PORTAbits.RA1
- #define SerialOUT PORTAbits.RA2
- #define SerialIN PORTAbits.RA3
- int Wait(int T){
- for(int delay=0;delay<T;delay++) __delay_ms(1);
- }
- int ADIN0(void){
- ADCON0=0b00000001;
- Wait(1);
- ADCON0=0b00000011;
- while((ADCON0 & 2)==2);
- return(ADRESH);
- }
- //PIC間の通信は未実装だが、アイデア次第で使用するかもなので残しておく
- int RECV(void){
- int x,xin,M,xdummy;
- while(SerialIN==1);
- Wait(DeltaT/2);
- xdummy=SerialIN;
- Wait(DeltaT);
- x=0; M=0;
- for(int Bit=0;Bit<8;Bit++){
- xin=SerialIN;
- x=x+M*xin;
- M=M*2;
- Wait(DeltaT);
- }
- xdummy=SerialIN;
- Wait(DeltaT);
- xdummy=SerialIN;
- Wait(DeltaT/2);
- return (x);
- }
- void SEND(int x)
- {
- SerialOUT=0;
- Wait(DeltaT);
- for(int Bit=0;Bit<8;Bit++){
- if((x&1)!=0)SerialOUT=1;
- else SerialOUT=0;
- x=x/2;
- Wait(DeltaT);
- }
- SerialOUT=0;
- Wait(DeltaT);
- SerialOUT=1;
- Wait(DeltaT);
- }
- //各アルファベットの配列に対して、受け取った配列と比較し0/1を返すよ もう少しまともな方法があるはず
- int Evaluater(int x[], int y[], int z[]){
- int A[] = {0,1,0,0,0,0,0,1};
- int B[] = {0,1,0,0,0,0,1,0};
- int C[] = {0,1,0,0,0,0,1,1};
- int D[] = {0,1,0,0,0,1,0,0};
- int E[] = {0,1,0,0,0,1,0,1};
- int F[] = {0,1,0,0,0,1,1,0};
- int G[] = {0,1,0,0,0,1,1,1};
- int H[] = {0,1,0,0,1,0,0,0};
- int I[] = {0,1,0,0,1,0,0,1};
- int J[] = {0,1,0,0,1,0,1,0};
- int K[] = {0,1,0,0,1,0,1,1};
- int L[] = {0,1,0,0,1,1,0,0};
- int M[] = {0,1,0,0,1,1,0,1};
- int N[] = {0,1,0,0,1,1,1,0};
- int O[] = {0,1,0,0,1,1,1,1};
- int P[] = {0,1,0,1,0,0,0,0};
- int Q[] = {0,1,0,1,0,0,0,1};
- int R[] = {0,1,0,1,0,0,1,0};
- int S[] = {0,1,0,1,0,0,1,1};
- int T[] = {0,1,0,1,0,1,0,0};
- int U[] = {0,1,0,1,0,1,0,1};
- int V[] = {0,1,0,1,0,1,1,0};
- int W[] = {0,1,0,1,0,1,1,1};
- int X[] = {0,1,0,1,1,0,0,0};
- int Y[] = {0,1,0,1,1,0,0,1};
- int Z[] = {0,1,0,1,1,0,1,0};
- if(memcmp(x, K, sizeof(int) * MAT) == 0 && memcmp(y, I, sizeof(int) * MAT) == 0 && memcmp(z, K, sizeof(int) * MAT) == 0) return 1;
- else return 0;
- }
- //測定した光センサの値を表示するよ
- void Threshold_mesure(){
- int ADval,ADdisp;
- ADval = ADIN0();
- Wait(300);
- ADdisp=(ADval*100)/256;
- OUTB(ADdisp/10);
- OUTD(ADdisp%10);
- }
- //受け取ったセンサの値をバイナリで表示するよ
- int Binary(int data){
- int Threshold = 35;
- if(data > Threshold){
- OUTB(1);
- return 1;
- }else{
- OUTB(0);
- return 0;
- }
- }
- //ロード中のくるくる回るやつ(右回り)。LATDだけ
- void Load_Effect_D(){
- LATD = 0b01111110;
- Wait(200);
- LATD = 0b11010111;
- Wait(200);
- LATD = 0b10111101;
- Wait(200);
- }
- //センサの値を読んでバイナリを返すよ
- int data_sokutei(){
- int ADval,ADdisp, B_Data = 0;
- SerialOUT=1;
- PhotoOFF=0;
- ADval = ADIN0();
- ADdisp=(ADval*100)/256;
- B_Data = Binary(ADdisp);
- return B_Data;
- }
- void main(void) {
- int p=0, q=0;
- init();
- SerialOUT=1;
- PhotoOFF=0;
- int data1[8] = {0,0,0,0,0,0,0,0};
- int data2[8] = {0,0,0,0,0,0,0,0};
- int data3[8] = {0,0,0,0,0,0,0,0};
- LATD = 0b11111111;
- LATB = 0b11111111;
- while(1){
- if(SW1 == 0) break;
- Wait(10);
- }
- //START
- OUTD(3);
- Wait(700);
- OUTD(2);
- Wait(700);
- OUTD(1);
- Wait(700);
- for(int i=0; i<24; ++i){
- Wait(1000);
- //1st TRY: 0, 1, 2, 3, 4, 5, 6, 7 (8)
- if(i >= 0 && 7 >= i){
- OUTB(i+1);
- Wait(1000);
- data1[i] = data_sokutei();
- Load_Effect_D();
- }
- //2nd TRY: 8, 9, 10, 11, 12, 13, 14, 15 (8)
- if(i >= 8 && 15 >= i){
- p = i%8;
- OUTB(p+1);
- Wait(1000);
- data2[p] = data_sokutei();
- Load_Effect_D();
- }
- //3rd TRY: 16, 17, 18, 19, 20, 21, 22, 23 (8)
- if(i >= 16 && 23 >= i){
- q = i%16;
- OUTB(q+1);
- Wait(1000);
- data3[q] = data_sokutei();
- Load_Effect_D();
- }
- }
- if(Evaluater(data1, data2, data3)){
- LED = 1;
- Wait(10000);
- }
- }
Add Comment
Please, Sign In to add comment