Guest User

Untitled

a guest
Jul 20th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.83 KB | None | 0 0
  1. /*
  2. * File: blink01.c
  3. * Author: ryota
  4. *
  5. * Created on 2018/05/10, 0:33
  6. */
  7. /*
  8. * サンプル この下から
  9. */
  10.  
  11. // PIC18F4553 Configuration Bit Settings
  12.  
  13. // 'C' source line config statements
  14.  
  15. // CONFIG1L
  16. #pragma config PLLDIV = 5 // PLL Prescaler Selection bits (Divide by 5 (20 MHz oscillator input))
  17. #pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
  18. #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)
  19.  
  20. // CONFIG1H
  21. #pragma config FOSC = HSPLL_HS // Oscillator Selection bits (HS oscillator, PLL enabled (HSPLL))
  22. #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  23. #pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
  24.  
  25. // CONFIG2L
  26. #pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
  27. #pragma config BOR = ON // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
  28. #pragma config BORV = 3 // Brown-out Reset Voltage bits (Minimum setting)
  29. #pragma config VREGEN = ON // USB Voltage Regulator Enable bit (USB voltage regulator enabled)
  30.  
  31. // CONFIG2H
  32. #pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
  33. #pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
  34.  
  35. // CONFIG3H
  36. #pragma config CCP2MX = OFF // CCP2 MUX bit (CCP2 input/output is multiplexed with RB3)
  37. #pragma config PBADEN = OFF // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
  38. #pragma config LPT1OSC = OFF // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
  39. #pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
  40.  
  41. // CONFIG4L
  42. #pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  43. #pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
  44. #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT disabled)
  45. #pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  46.  
  47. // CONFIG5L
  48. #pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
  49. #pragma config CP1 = OFF // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
  50. #pragma config CP2 = OFF // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
  51. #pragma config CP3 = OFF // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
  52.  
  53. // CONFIG5H
  54. #pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
  55. #pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
  56.  
  57. // CONFIG6L
  58. #pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
  59. #pragma config WRT1 = OFF // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
  60. #pragma config WRT2 = OFF // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
  61. #pragma config WRT3 = OFF // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
  62.  
  63. // CONFIG6H
  64. #pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
  65. #pragma config WRTB = OFF // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
  66. #pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
  67.  
  68. // CONFIG7L
  69. #pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
  70. #pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
  71. #pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
  72. #pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
  73.  
  74. // CONFIG7H
  75. #pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
  76.  
  77. // #pragma config statements should precede project file includes.
  78. // Use project enums instead of #define for ON and OFF.
  79. // * サンプル この上まで
  80.  
  81. #include <p18f4553.h>
  82. #include<string.h>
  83.  
  84. #define _XTAL_FREQ 48000000 //for __delay_ms,__delay_us
  85. #define SW1 PORTEbits.RE0 //not use
  86. #define SW2 PORTEbits.RE1 //not use
  87. #define LED PORTEbits.RE2
  88. #define MAT 8
  89.  
  90. void OUTB(int i)
  91. {
  92. if(i==0) LATB=0b10000010;
  93. else if (i==1) LATB=0b10110111;
  94. else if (i==2) LATB=0b11000001;
  95. else if (i==3) LATB=0b10010001;
  96. else if (i==4) LATB=0b10110100;
  97. else if (i==5) LATB=0b10011000;
  98. else if (i==6) LATB=0b10001000;
  99. else if (i==7) LATB=0b10110011;
  100. else if (i==8) LATB=0b10000000;
  101. else if (i==9) LATB=0b10010000;
  102. else LATB=0b11111111;
  103. }
  104.  
  105. void OUTD(int i)
  106. {
  107. if(i==0) LATD=0b00010100;
  108. else if (i==1) LATD=0b01110111;
  109. else if (i==2) LATD=0b00111000;
  110. else if (i==3) LATD=0b00110001;
  111. else if (i==4) LATD=0b01010011;
  112. else if (i==5) LATD=0b10010001;
  113. else if (i==6) LATD=0b10010000;
  114. else if (i==7) LATD=0b00110111;
  115. else if (i==8) LATD=0b00010000;
  116. else if (i==9) LATD=0b00010001;
  117. else LATD=0b11111111;
  118. }
  119.  
  120. void init(void)
  121. {
  122. ADCON1 = 0b00001110;
  123. ADCON0 = 0b00000000;
  124. ADCON2 = 0b00000100;
  125. ADCON0 = 0b00000001;
  126. TRISA = 0b00001001;
  127. TRISB = 0b00000000;
  128. TRISC = 0b00110000; //RC4=USBD- & RC5=USBD+ IN
  129. TRISD = 0b00000000;
  130. TRISE = 0b00000011; //RE0=SW1 & RE1=SW2 IN, RE2=LED OUT
  131. LATA = 0b00000000;
  132. LATB = 0b00000000;
  133. LATC = 0b00000000;
  134. LATD = 0b00000000;
  135. LATE = 0b00000000;
  136.  
  137. }
  138.  
  139. #define DeltaT 100
  140. #define AN0 PORTAbits.RA0
  141. #define PhotoOFF PORTAbits.RA1
  142. #define SerialOUT PORTAbits.RA2
  143. #define SerialIN PORTAbits.RA3
  144.  
  145. int Wait(int T){
  146. for(int delay=0;delay<T;delay++) __delay_ms(1);
  147. }
  148.  
  149. int ADIN0(void){
  150. ADCON0=0b00000001;
  151. Wait(1);
  152. ADCON0=0b00000011;
  153. while((ADCON0 & 2)==2);
  154. return(ADRESH);
  155. }
  156.  
  157. //PIC間の通信は未実装だが、アイデア次第で使用するかもなので残しておく
  158. int RECV(void){
  159. int x,xin,M,xdummy;
  160. while(SerialIN==1);
  161. Wait(DeltaT/2);
  162. xdummy=SerialIN;
  163. Wait(DeltaT);
  164. x=0; M=0;
  165. for(int Bit=0;Bit<8;Bit++){
  166. xin=SerialIN;
  167. x=x+M*xin;
  168. M=M*2;
  169. Wait(DeltaT);
  170. }
  171. xdummy=SerialIN;
  172. Wait(DeltaT);
  173. xdummy=SerialIN;
  174. Wait(DeltaT/2);
  175. return (x);
  176. }
  177.  
  178. void SEND(int x)
  179. {
  180. SerialOUT=0;
  181. Wait(DeltaT);
  182. for(int Bit=0;Bit<8;Bit++){
  183. if((x&1)!=0)SerialOUT=1;
  184. else SerialOUT=0;
  185. x=x/2;
  186. Wait(DeltaT);
  187. }
  188. SerialOUT=0;
  189. Wait(DeltaT);
  190. SerialOUT=1;
  191. Wait(DeltaT);
  192. }
  193.  
  194. //各アルファベットの配列に対して、受け取った配列と比較し0/1を返すよ もう少しまともな方法があるはず
  195. int Evaluater(int x[], int y[], int z[]){
  196. int A[] = {0,1,0,0,0,0,0,1};
  197. int B[] = {0,1,0,0,0,0,1,0};
  198. int C[] = {0,1,0,0,0,0,1,1};
  199. int D[] = {0,1,0,0,0,1,0,0};
  200. int E[] = {0,1,0,0,0,1,0,1};
  201. int F[] = {0,1,0,0,0,1,1,0};
  202. int G[] = {0,1,0,0,0,1,1,1};
  203. int H[] = {0,1,0,0,1,0,0,0};
  204. int I[] = {0,1,0,0,1,0,0,1};
  205. int J[] = {0,1,0,0,1,0,1,0};
  206. int K[] = {0,1,0,0,1,0,1,1};
  207. int L[] = {0,1,0,0,1,1,0,0};
  208. int M[] = {0,1,0,0,1,1,0,1};
  209. int N[] = {0,1,0,0,1,1,1,0};
  210. int O[] = {0,1,0,0,1,1,1,1};
  211. int P[] = {0,1,0,1,0,0,0,0};
  212. int Q[] = {0,1,0,1,0,0,0,1};
  213. int R[] = {0,1,0,1,0,0,1,0};
  214. int S[] = {0,1,0,1,0,0,1,1};
  215. int T[] = {0,1,0,1,0,1,0,0};
  216. int U[] = {0,1,0,1,0,1,0,1};
  217. int V[] = {0,1,0,1,0,1,1,0};
  218. int W[] = {0,1,0,1,0,1,1,1};
  219. int X[] = {0,1,0,1,1,0,0,0};
  220. int Y[] = {0,1,0,1,1,0,0,1};
  221. int Z[] = {0,1,0,1,1,0,1,0};
  222.  
  223. if(memcmp(x, K, sizeof(int) * MAT) == 0 && memcmp(y, I, sizeof(int) * MAT) == 0 && memcmp(z, K, sizeof(int) * MAT) == 0) return 1;
  224. else return 0;
  225. }
  226.  
  227. //測定した光センサの値を表示するよ
  228. void Threshold_mesure(){
  229. int ADval,ADdisp;
  230. ADval = ADIN0();
  231. Wait(300);
  232. ADdisp=(ADval*100)/256;
  233. OUTB(ADdisp/10);
  234. OUTD(ADdisp%10);
  235. }
  236.  
  237. //受け取ったセンサの値をバイナリで表示するよ
  238. int Binary(int data){
  239. int Threshold = 35;
  240. if(data > Threshold){
  241. OUTB(1);
  242. return 1;
  243. }else{
  244. OUTB(0);
  245. return 0;
  246. }
  247. }
  248.  
  249. //ロード中のくるくる回るやつ(右回り)。LATDだけ
  250. void Load_Effect_D(){
  251. LATD = 0b01111110;
  252. Wait(200);
  253. LATD = 0b11010111;
  254. Wait(200);
  255. LATD = 0b10111101;
  256. Wait(200);
  257. }
  258.  
  259. //センサの値を読んでバイナリを返すよ
  260. int data_sokutei(){
  261. int ADval,ADdisp, B_Data = 0;
  262. SerialOUT=1;
  263. PhotoOFF=0;
  264.  
  265. ADval = ADIN0();
  266. ADdisp=(ADval*100)/256;
  267. B_Data = Binary(ADdisp);
  268.  
  269. return B_Data;
  270. }
  271.  
  272.  
  273. void main(void) {
  274. int p=0, q=0;
  275. init();
  276. SerialOUT=1;
  277. PhotoOFF=0;
  278.  
  279. int data1[8] = {0,0,0,0,0,0,0,0};
  280. int data2[8] = {0,0,0,0,0,0,0,0};
  281. int data3[8] = {0,0,0,0,0,0,0,0};
  282.  
  283. LATD = 0b11111111;
  284. LATB = 0b11111111;
  285.  
  286. while(1){
  287. if(SW1 == 0) break;
  288. Wait(10);
  289. }
  290.  
  291. //START
  292. OUTD(3);
  293. Wait(700);
  294. OUTD(2);
  295. Wait(700);
  296. OUTD(1);
  297. Wait(700);
  298.  
  299. for(int i=0; i<24; ++i){
  300. Wait(1000);
  301.  
  302. //1st TRY: 0, 1, 2, 3, 4, 5, 6, 7 (8)
  303. if(i >= 0 && 7 >= i){
  304. OUTB(i+1);
  305. Wait(1000);
  306. data1[i] = data_sokutei();
  307. Load_Effect_D();
  308. }
  309. //2nd TRY: 8, 9, 10, 11, 12, 13, 14, 15 (8)
  310. if(i >= 8 && 15 >= i){
  311. p = i%8;
  312. OUTB(p+1);
  313. Wait(1000);
  314. data2[p] = data_sokutei();
  315. Load_Effect_D();
  316. }
  317. //3rd TRY: 16, 17, 18, 19, 20, 21, 22, 23 (8)
  318. if(i >= 16 && 23 >= i){
  319. q = i%16;
  320. OUTB(q+1);
  321. Wait(1000);
  322. data3[q] = data_sokutei();
  323. Load_Effect_D();
  324. }
  325. }
  326.  
  327. if(Evaluater(data1, data2, data3)){
  328. LED = 1;
  329. Wait(10000);
  330. }
  331. }
Add Comment
Please, Sign In to add comment